Skip to content

Commit

Permalink
Added dyn container example and fixed up example (#225)
Browse files Browse the repository at this point in the history
* Added dyn container example and fixed up example

* fmt
  • Loading branch information
dominikwilkowski authored Dec 21, 2023
1 parent bec3657 commit 5ed6a95
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 32 deletions.
8 changes: 8 additions & 0 deletions examples/dyn-container/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "dyn-container"
version = "0.1.0"
edition = "2021"

[dependencies]
im = "15.1.0"
floem = { path = "../.." }
64 changes: 64 additions & 0 deletions examples/dyn-container/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use floem::{
reactive::{create_rw_signal, RwSignal},
view::View,
views::{dyn_container, h_stack, label, v_stack, Decorators},
widgets::button,
};

#[derive(Clone, PartialEq)]
enum ViewSwitcher {
One,
Two,
}

fn view_one() -> impl View {
label(|| "A view")
}

fn view_two(view: RwSignal<ViewSwitcher>) -> impl View {
v_stack((
label(|| "Another view"),
button(|| "Switch back").on_click_stop(move |_| {
view.set(ViewSwitcher::One);
}),
))
.style(|s| s.gap(0.0, 10.0))
}

fn app_view() -> impl View {
let view = create_rw_signal(ViewSwitcher::One);

v_stack((
h_stack((
label(|| "Swap views:").style(|s| s.padding(5)),
button(|| "Switch views")
.on_click_stop(move |_| {
if view.get() == ViewSwitcher::One {
view.set(ViewSwitcher::Two);
} else {
view.set(ViewSwitcher::One);
}
})
.style(|s| s.margin_bottom(20)),
)),
dyn_container(
move || view.get(),
move |value| match value {
ViewSwitcher::One => Box::new(view_one()),
ViewSwitcher::Two => Box::new(view_two(view)),
},
)
.style(|s| s.padding(10).border(1)),
))
.style(|s| {
s.width_full()
.height_full()
.items_center()
.justify_center()
.gap(10, 0)
})
}

fn main() {
floem::launch(app_view);
}
63 changes: 31 additions & 32 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,51 @@ pub struct DynamicContainer<T: 'static> {
///
/// ## Example
/// ```ignore
/// #[derive(Debug, Clone)]
/// use floem::{
/// reactive::create_rw_signal,
/// view::View,
/// views::{dyn_container, label, v_stack, Decorators},
/// widgets::toggle_button,
/// };
///
/// #[derive(Clone)]
/// enum ViewSwitcher {
/// One,
/// Two,
/// }
///
/// fn app() -> impl View {
///
/// fn app_view() -> impl View {
/// let view = create_rw_signal(ViewSwitcher::One);
///
/// let button = || {
/// // imaginary toggle button and state
/// toggle_button(
/// // on toggle function
/// move |state| match state {
/// State::On => view.update(|val| *val = Views::One),
/// State::Off => view.update(|val| *val = Views::Two),
/// },
/// )
/// };
///
/// stack(|| (
/// button(),
/// v_stack((
/// toggle_button(|| true)
/// .on_toggle(move |is_on| {
/// if is_on {
/// view.update(|val| *val = ViewSwitcher::One);
/// } else {
/// view.update(|val| *val = ViewSwitcher::Two);
/// }
/// })
/// .style(|s| s.margin_bottom(20)),
/// dyn_container(
/// move || view.get(),
/// move |val: ViewSwitcher| match val {
/// ViewSwitcher::One => Box::new(label(|| "one".into())),
/// ViewSwitcher::Two => {
/// Box::new(
/// stack(|| (
/// label(|| "stacked".into()),
/// label(|| "two".into())
/// ))
/// ),
/// }
/// move |value| match value {
/// ViewSwitcher::One => Box::new(label(|| "One")),
/// ViewSwitcher::Two => Box::new(v_stack((label(|| "Stacked"), label(|| "Two")))),
/// },
/// )
/// ),
/// ))
/// .style(|| {
/// Style::new()
/// .size(100.pct(), 100.pct())
/// .style(|s| {
/// s.width_full()
/// .height_full()
/// .items_center()
/// .justify_center()
/// .gap(points(10.))
/// .gap(10, 0)
/// })
/// }
///
/// fn main() {
/// floem::launch(app_view);
/// }
/// ```
///
/// See [container_box](crate::views::container_box()) for more documentation on a general container
Expand Down

0 comments on commit 5ed6a95

Please sign in to comment.