diff options
-rw-r--r-- | examples/panes/Cargo.toml | 1 | ||||
-rw-r--r-- | examples/panes/src/main.rs | 97 |
2 files changed, 87 insertions, 11 deletions
diff --git a/examples/panes/Cargo.toml b/examples/panes/Cargo.toml index 174d2cde..dc94cc2c 100644 --- a/examples/panes/Cargo.toml +++ b/examples/panes/Cargo.toml @@ -7,5 +7,6 @@ publish = false [dependencies] iced = { path = "../..", features = ["async-std"] } +iced_native = { path = "../../native" } clock = { path = "../clock" } stopwatch = { path = "../stopwatch" } diff --git a/examples/panes/src/main.rs b/examples/panes/src/main.rs index 65db2b40..50b21fc5 100644 --- a/examples/panes/src/main.rs +++ b/examples/panes/src/main.rs @@ -1,6 +1,7 @@ use iced::{ panes, Application, Command, Element, Panes, Settings, Subscription, }; +use iced_native::input::keyboard; use clock::{self, Clock}; use stopwatch::{self, Stopwatch}; @@ -27,6 +28,7 @@ enum Example { enum Message { Clock(panes::Pane, clock::Message), Stopwatch(panes::Pane, stopwatch::Message), + Split(panes::Split), } impl Application for Launcher { @@ -58,6 +60,21 @@ impl Application for Launcher { 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); + } + } } dbg!(self); @@ -66,17 +83,26 @@ impl Application for Launcher { } 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)), - } - })) + let panes_subscriptions = + 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)), + } + })); + + 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)), + panes_subscriptions, + ]) } fn view(&mut self) -> Element<Message> { @@ -94,3 +120,52 @@ impl Application for Launcher { .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<Hasher, Event> for KeyReleased { + type Output = (); + + fn hash(&self, state: &mut Hasher) { + use std::hash::Hash; + + std::any::TypeId::of::<Self>().hash(state); + self.key_code.hash(state); + } + + fn stream( + self: Box<Self>, + 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() + } + } +} |