Add split hotkeys to panes example

This commit is contained in:
Héctor Ramón Jiménez 2020-03-04 22:31:59 +01:00
parent 58adfcd514
commit cc310f71cc
2 changed files with 86 additions and 10 deletions

View File

@ -7,5 +7,6 @@ publish = false
[dependencies]
iced = { path = "../..", features = ["async-std"] }
iced_native = { path = "../../native" }
clock = { path = "../clock" }
stopwatch = { path = "../stopwatch" }

View File

@ -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)),
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)),
}
}))
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()
}
}
}