From 6151c528241d0a6ece88e6e664df1b50f8174ecb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Mar 2020 02:57:13 +0100 Subject: Rename `Panes` widget to `PaneGrid` --- examples/pane_grid/Cargo.toml | 12 +++ examples/pane_grid/README.md | 18 +++++ examples/pane_grid/src/main.rs | 179 +++++++++++++++++++++++++++++++++++++++++ examples/panes/Cargo.toml | 12 --- examples/panes/README.md | 18 ----- examples/panes/src/main.rs | 179 ----------------------------------------- 6 files changed, 209 insertions(+), 209 deletions(-) create mode 100644 examples/pane_grid/Cargo.toml create mode 100644 examples/pane_grid/README.md create mode 100644 examples/pane_grid/src/main.rs delete mode 100644 examples/panes/Cargo.toml delete mode 100644 examples/panes/README.md delete mode 100644 examples/panes/src/main.rs (limited to 'examples') diff --git a/examples/pane_grid/Cargo.toml b/examples/pane_grid/Cargo.toml new file mode 100644 index 00000000..6d8573bd --- /dev/null +++ b/examples/pane_grid/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pane_grid" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../..", features = ["async-std"] } +iced_native = { path = "../../native" } +clock = { path = "../clock" } +stopwatch = { path = "../stopwatch" } diff --git a/examples/pane_grid/README.md b/examples/pane_grid/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/pane_grid/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. + +
+ + + +
+ +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs new file mode 100644 index 00000000..b103afc8 --- /dev/null +++ b/examples/pane_grid/src/main.rs @@ -0,0 +1,179 @@ +use iced::{ + pane_grid, Application, Command, Element, PaneGrid, Settings, Subscription, +}; +use iced_native::input::keyboard; + +use clock::{self, Clock}; +use stopwatch::{self, Stopwatch}; + +pub fn main() { + Launcher::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +#[derive(Debug)] +struct Launcher { + panes: pane_grid::State, +} + +#[derive(Debug)] +enum Example { + Clock(Clock), + Stopwatch(Stopwatch), +} + +#[derive(Debug, Clone)] +enum Message { + Clock(pane_grid::Pane, clock::Message), + Stopwatch(pane_grid::Pane, stopwatch::Message), + Split(pane_grid::Split), + Close, +} + +impl Application for Launcher { + type Executor = iced::executor::Default; + type Message = Message; + + fn new() -> (Self, Command) { + let (clock, _) = Clock::new(); + let (panes, _) = pane_grid::State::new(Example::Clock(clock)); + + (Self { panes }, Command::none()) + } + + fn title(&self) -> String { + String::from("Panes - Iced") + } + + fn update(&mut self, message: Message) -> Command { + 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); + } + } + Message::Split(kind) => { + if let Some(pane) = self.panes.focused_pane() { + let state = if pane.index() % 2 == 0 { + let (stopwatch, _) = Stopwatch::new(); + + Example::Stopwatch(stopwatch) + } else { + let (clock, _) = Clock::new(); + + Example::Clock(clock) + }; + + self.panes.split(kind, &pane, state); + } + } + Message::Close => { + if let Some(pane) = self.panes.focused_pane() { + self.panes.close(&pane); + } + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription { + let panes_subscriptions = + Subscription::batch(self.panes.iter().map(|(pane, example)| { + match example { + Example::Clock(clock) => clock + .subscription() + .with(pane) + .map(|(pane, message)| Message::Clock(pane, message)), + + Example::Stopwatch(stopwatch) => { + stopwatch.subscription().with(pane).map( + |(pane, message)| Message::Stopwatch(pane, message), + ) + } + } + })); + + Subscription::batch(vec![ + events::key_released(keyboard::KeyCode::H) + .map(|_| Message::Split(pane_grid::Split::Horizontal)), + events::key_released(keyboard::KeyCode::V) + .map(|_| Message::Split(pane_grid::Split::Vertical)), + events::key_released(keyboard::KeyCode::Q).map(|_| Message::Close), + panes_subscriptions, + ]) + } + + fn view(&mut self) -> Element { + let Self { panes } = self; + + PaneGrid::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() + } +} + +mod events { + use iced_native::{ + futures::{ + self, + stream::{BoxStream, StreamExt}, + }, + input::{keyboard, ButtonState}, + subscription, Event, Hasher, Subscription, + }; + + pub fn key_released(key_code: keyboard::KeyCode) -> Subscription<()> { + Subscription::from_recipe(KeyReleased { key_code }) + } + + struct KeyReleased { + key_code: keyboard::KeyCode, + } + + impl subscription::Recipe for KeyReleased { + type Output = (); + + fn hash(&self, state: &mut Hasher) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + self.key_code.hash(state); + } + + fn stream( + self: Box, + events: subscription::EventStream, + ) -> BoxStream<'static, Self::Output> { + events + .filter(move |event| match event { + Event::Keyboard(keyboard::Event::Input { + key_code, + state: ButtonState::Released, + .. + }) if *key_code == self.key_code => { + futures::future::ready(true) + } + _ => futures::future::ready(false), + }) + .map(|_| ()) + .boxed() + } + } +} diff --git a/examples/panes/Cargo.toml b/examples/panes/Cargo.toml deleted file mode 100644 index dc94cc2c..00000000 --- a/examples/panes/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "panes" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2018" -publish = false - -[dependencies] -iced = { path = "../..", features = ["async-std"] } -iced_native = { path = "../../native" } -clock = { path = "../clock" } -stopwatch = { path = "../stopwatch" } diff --git a/examples/panes/README.md b/examples/panes/README.md deleted file mode 100644 index 4d9fc5b9..00000000 --- a/examples/panes/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Counter - -The classic counter example explained in the [`README`](../../README.md). - -The __[`main`]__ file contains all the code of the example. - - - -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 deleted file mode 100644 index 34206a2c..00000000 --- a/examples/panes/src/main.rs +++ /dev/null @@ -1,179 +0,0 @@ -use iced::{ - panes, Application, Command, Element, Panes, Settings, Subscription, -}; -use iced_native::input::keyboard; - -use clock::{self, Clock}; -use stopwatch::{self, Stopwatch}; - -pub fn main() { - Launcher::run(Settings { - antialiasing: true, - ..Settings::default() - }) -} - -#[derive(Debug)] -struct Launcher { - panes: panes::State, -} - -#[derive(Debug)] -enum Example { - Clock(Clock), - Stopwatch(Stopwatch), -} - -#[derive(Debug, Clone)] -enum Message { - Clock(panes::Pane, clock::Message), - Stopwatch(panes::Pane, stopwatch::Message), - Split(panes::Split), - Close, -} - -impl Application for Launcher { - type Executor = iced::executor::Default; - type Message = Message; - - fn new() -> (Self, Command) { - let (clock, _) = Clock::new(); - let (panes, _) = panes::State::new(Example::Clock(clock)); - - (Self { panes }, Command::none()) - } - - fn title(&self) -> String { - String::from("Panes - Iced") - } - - fn update(&mut self, message: Message) -> Command { - 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); - } - } - Message::Split(kind) => { - if let Some(pane) = self.panes.focused_pane() { - let state = if pane.index() % 2 == 0 { - let (stopwatch, _) = Stopwatch::new(); - - Example::Stopwatch(stopwatch) - } else { - let (clock, _) = Clock::new(); - - Example::Clock(clock) - }; - - self.panes.split(kind, &pane, state); - } - } - Message::Close => { - if let Some(pane) = self.panes.focused_pane() { - self.panes.close(&pane); - } - } - } - - Command::none() - } - - fn subscription(&self) -> Subscription { - let panes_subscriptions = - Subscription::batch(self.panes.iter().map(|(pane, example)| { - match example { - Example::Clock(clock) => clock - .subscription() - .with(pane) - .map(|(pane, message)| Message::Clock(pane, message)), - - Example::Stopwatch(stopwatch) => { - stopwatch.subscription().with(pane).map( - |(pane, message)| Message::Stopwatch(pane, message), - ) - } - } - })); - - Subscription::batch(vec![ - events::key_released(keyboard::KeyCode::H) - .map(|_| Message::Split(panes::Split::Horizontal)), - events::key_released(keyboard::KeyCode::V) - .map(|_| Message::Split(panes::Split::Vertical)), - events::key_released(keyboard::KeyCode::Q).map(|_| Message::Close), - panes_subscriptions, - ]) - } - - fn view(&mut self) -> Element { - 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() - } -} - -mod events { - use iced_native::{ - futures::{ - self, - stream::{BoxStream, StreamExt}, - }, - input::{keyboard, ButtonState}, - subscription, Event, Hasher, Subscription, - }; - - pub fn key_released(key_code: keyboard::KeyCode) -> Subscription<()> { - Subscription::from_recipe(KeyReleased { key_code }) - } - - struct KeyReleased { - key_code: keyboard::KeyCode, - } - - impl subscription::Recipe for KeyReleased { - type Output = (); - - fn hash(&self, state: &mut Hasher) { - use std::hash::Hash; - - std::any::TypeId::of::().hash(state); - self.key_code.hash(state); - } - - fn stream( - self: Box, - events: subscription::EventStream, - ) -> BoxStream<'static, Self::Output> { - events - .filter(move |event| match event { - Event::Keyboard(keyboard::Event::Input { - key_code, - state: ButtonState::Released, - .. - }) if *key_code == self.key_code => { - futures::future::ready(true) - } - _ => futures::future::ready(false), - }) - .map(|_| ()) - .boxed() - } - } -} -- cgit