diff options
author | 2020-03-04 04:10:26 +0100 | |
---|---|---|
committer | 2020-03-04 04:10:26 +0100 | |
commit | 012b4adec7a87331b2d75f6bc5d2a0189dcd7ec5 (patch) | |
tree | ce668596d734fe9fe04e1fa40cb932cd3e1c1789 /examples/panes | |
parent | 02091267bf38d4da5efab57e9b63504804b3a468 (diff) | |
download | iced-012b4adec7a87331b2d75f6bc5d2a0189dcd7ec5.tar.gz iced-012b4adec7a87331b2d75f6bc5d2a0189dcd7ec5.tar.bz2 iced-012b4adec7a87331b2d75f6bc5d2a0189dcd7ec5.zip |
Draft `Panes` widget and `panes` example
Diffstat (limited to 'examples/panes')
-rw-r--r-- | examples/panes/Cargo.toml | 11 | ||||
-rw-r--r-- | examples/panes/README.md | 18 | ||||
-rw-r--r-- | examples/panes/src/main.rs | 95 |
3 files changed, 124 insertions, 0 deletions
diff --git a/examples/panes/Cargo.toml b/examples/panes/Cargo.toml new file mode 100644 index 00000000..174d2cde --- /dev/null +++ b/examples/panes/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "panes" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../..", features = ["async-std"] } +clock = { path = "../clock" } +stopwatch = { path = "../stopwatch" } diff --git a/examples/panes/README.md b/examples/panes/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/panes/README.md @@ -0,0 +1,18 @@ +## Counter + +The classic counter example explained in the [`README`](../../README.md). + +The __[`main`]__ file contains all the code of the example. + +<div align="center"> + <a href="https://gfycat.com/fairdeadcatbird"> + <img src="https://thumbs.gfycat.com/FairDeadCatbird-small.gif"> + </a> +</div> + +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/panes/src/main.rs b/examples/panes/src/main.rs new file mode 100644 index 00000000..c1bf991a --- /dev/null +++ b/examples/panes/src/main.rs @@ -0,0 +1,95 @@ +use iced::{ + panes, Application, Command, Element, Panes, Settings, Subscription, +}; + +use clock::{self, Clock}; +use stopwatch::{self, Stopwatch}; + +pub fn main() { + Launcher::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +struct Launcher { + panes: panes::State<Example>, +} + +#[derive(Debug)] +enum Example { + Clock(Clock), + Stopwatch(Stopwatch), +} + +#[derive(Debug, Clone)] +enum Message { + Clock(panes::Pane, clock::Message), + Stopwatch(panes::Pane, stopwatch::Message), +} + +impl Application for Launcher { + type Executor = iced::executor::Default; + type Message = Message; + + fn new() -> (Self, Command<Message>) { + let (clock, _) = Clock::new(); + let (panes, _) = panes::State::new(Example::Clock(clock)); + + dbg!(&panes); + + (Self { panes }, Command::none()) + } + + fn title(&self) -> String { + String::from("Panes - Iced") + } + + fn update(&mut self, message: Message) -> Command<Message> { + match message { + Message::Clock(pane, message) => { + if let Some(Example::Clock(clock)) = self.panes.get_mut(&pane) { + let _ = clock.update(message); + } + } + Message::Stopwatch(pane, message) => { + if let Some(Example::Stopwatch(stopwatch)) = + self.panes.get_mut(&pane) + { + let _ = stopwatch.update(message); + } + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription<Message> { + Subscription::batch(self.panes.iter().map(|(pane, example)| { + match example { + Example::Clock(clock) => clock + .subscription() + .map(move |message| Message::Clock(pane, message)), + + Example::Stopwatch(stopwatch) => stopwatch + .subscription() + .map(move |message| Message::Stopwatch(pane, message)), + } + })) + } + + fn view(&mut self) -> Element<Message> { + let Self { panes } = self; + + Panes::new(panes, |pane, example| match example { + Example::Clock(clock) => clock + .view() + .map(move |message| Message::Clock(pane, message)), + + Example::Stopwatch(stopwatch) => stopwatch + .view() + .map(move |message| Message::Stopwatch(pane, message)), + }) + .into() + } +} |