Add hotkey support for stopwatch example
This commit is contained in:
parent
08a031cbe5
commit
d21f0698b5
2 changed files with 17 additions and 3 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use iced::alignment;
|
use iced::alignment;
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
|
use iced::keyboard;
|
||||||
use iced::theme::{self, Theme};
|
use iced::theme::{self, Theme};
|
||||||
use iced::time;
|
use iced::time;
|
||||||
use iced::widget::{button, column, container, row, text};
|
use iced::widget::{button, column, container, row, text};
|
||||||
|
|
@ -77,12 +78,25 @@ impl Application for Stopwatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
match self.state {
|
let tick = match self.state {
|
||||||
State::Idle => Subscription::none(),
|
State::Idle => Subscription::none(),
|
||||||
State::Ticking { .. } => {
|
State::Ticking { .. } => {
|
||||||
time::every(Duration::from_millis(10)).map(Message::Tick)
|
time::every(Duration::from_millis(10)).map(Message::Tick)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn handle_hotkey(
|
||||||
|
key_code: keyboard::KeyCode,
|
||||||
|
_modifiers: keyboard::Modifiers,
|
||||||
|
) -> Option<Message> {
|
||||||
|
match key_code {
|
||||||
|
keyboard::KeyCode::Space => Some(Message::Toggle),
|
||||||
|
keyboard::KeyCode::R => Some(Message::Reset),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Subscription::batch(vec![tick, keyboard::on_key_press(handle_hotkey)])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,9 @@ where
|
||||||
Message: MaybeSend + 'static,
|
Message: MaybeSend + 'static,
|
||||||
{
|
{
|
||||||
#[derive(Hash)]
|
#[derive(Hash)]
|
||||||
struct OnKeyPress;
|
struct OnKeyRelease;
|
||||||
|
|
||||||
subscription::filter_map((OnKeyPress, f), move |event, status| {
|
subscription::filter_map((OnKeyRelease, f), move |event, status| {
|
||||||
match (event, status) {
|
match (event, status) {
|
||||||
(
|
(
|
||||||
core::Event::Keyboard(Event::KeyReleased {
|
core::Event::Keyboard(Event::KeyReleased {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue