Add split hotkeys to panes example
This commit is contained in:
parent
58adfcd514
commit
cc310f71cc
2 changed files with 86 additions and 10 deletions
|
|
@ -7,5 +7,6 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = { path = "../..", features = ["async-std"] }
|
iced = { path = "../..", features = ["async-std"] }
|
||||||
|
iced_native = { path = "../../native" }
|
||||||
clock = { path = "../clock" }
|
clock = { path = "../clock" }
|
||||||
stopwatch = { path = "../stopwatch" }
|
stopwatch = { path = "../stopwatch" }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
panes, Application, Command, Element, Panes, Settings, Subscription,
|
panes, Application, Command, Element, Panes, Settings, Subscription,
|
||||||
};
|
};
|
||||||
|
use iced_native::input::keyboard;
|
||||||
|
|
||||||
use clock::{self, Clock};
|
use clock::{self, Clock};
|
||||||
use stopwatch::{self, Stopwatch};
|
use stopwatch::{self, Stopwatch};
|
||||||
|
|
@ -27,6 +28,7 @@ enum Example {
|
||||||
enum Message {
|
enum Message {
|
||||||
Clock(panes::Pane, clock::Message),
|
Clock(panes::Pane, clock::Message),
|
||||||
Stopwatch(panes::Pane, stopwatch::Message),
|
Stopwatch(panes::Pane, stopwatch::Message),
|
||||||
|
Split(panes::Split),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Launcher {
|
impl Application for Launcher {
|
||||||
|
|
@ -58,6 +60,21 @@ impl Application for Launcher {
|
||||||
let _ = stopwatch.update(message);
|
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);
|
dbg!(self);
|
||||||
|
|
@ -66,17 +83,26 @@ impl Application for Launcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
Subscription::batch(self.panes.iter().map(|(pane, example)| {
|
let panes_subscriptions =
|
||||||
match example {
|
Subscription::batch(self.panes.iter().map(|(pane, example)| {
|
||||||
Example::Clock(clock) => clock
|
match example {
|
||||||
.subscription()
|
Example::Clock(clock) => clock
|
||||||
.map(move |message| Message::Clock(pane, message)),
|
.subscription()
|
||||||
|
.map(move |message| Message::Clock(pane, message)),
|
||||||
|
|
||||||
Example::Stopwatch(stopwatch) => stopwatch
|
Example::Stopwatch(stopwatch) => stopwatch
|
||||||
.subscription()
|
.subscription()
|
||||||
.map(move |message| Message::Stopwatch(pane, message)),
|
.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> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
|
|
@ -94,3 +120,52 @@ impl Application for Launcher {
|
||||||
.into()
|
.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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue