From 5ed6a9553f9d095d24f4811803358568c85221cd Mon Sep 17 00:00:00 2001 From: Dominik Wilkowski Date: Thu, 21 Dec 2023 17:47:30 +1000 Subject: [PATCH] Added dyn container example and fixed up example (#225) * Added dyn container example and fixed up example * fmt --- examples/dyn-container/Cargo.toml | 8 ++++ examples/dyn-container/src/main.rs | 64 ++++++++++++++++++++++++++++++ src/views/dyn_container.rs | 63 +++++++++++++++-------------- 3 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 examples/dyn-container/Cargo.toml create mode 100644 examples/dyn-container/src/main.rs diff --git a/examples/dyn-container/Cargo.toml b/examples/dyn-container/Cargo.toml new file mode 100644 index 00000000..36458e04 --- /dev/null +++ b/examples/dyn-container/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dyn-container" +version = "0.1.0" +edition = "2021" + +[dependencies] +im = "15.1.0" +floem = { path = "../.." } diff --git a/examples/dyn-container/src/main.rs b/examples/dyn-container/src/main.rs new file mode 100644 index 00000000..1fc32d26 --- /dev/null +++ b/examples/dyn-container/src/main.rs @@ -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) -> 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); +} diff --git a/src/views/dyn_container.rs b/src/views/dyn_container.rs index 9610d3e2..b2aa4f1a 100644 --- a/src/views/dyn_container.rs +++ b/src/views/dyn_container.rs @@ -19,52 +19,51 @@ pub struct DynamicContainer { /// /// ## 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