Unify keyboard subscriptions into listen
This commit is contained in:
parent
0df5765e2f
commit
7e5b6f6802
9 changed files with 77 additions and 96 deletions
|
|
@ -381,14 +381,15 @@ where
|
||||||
program.subscription(&self.state).map(Event::Program);
|
program.subscription(&self.state).map(Event::Program);
|
||||||
debug::subscriptions_tracked(subscription.units());
|
debug::subscriptions_tracked(subscription.units());
|
||||||
|
|
||||||
let hotkeys = futures::keyboard::on_key_press(
|
let hotkeys = futures::keyboard::listen()
|
||||||
|key, _physical_key, _modifiers| match key {
|
.filter_map(|event| match event {
|
||||||
keyboard::Key::Named(keyboard::key::Named::F12) => {
|
keyboard::Event::KeyPressed {
|
||||||
Some(Message::ToggleComet)
|
modified_key:
|
||||||
}
|
keyboard::Key::Named(keyboard::key::Named::F12),
|
||||||
|
..
|
||||||
|
} => Some(Message::ToggleComet),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
})
|
||||||
)
|
|
||||||
.map(Event::Message);
|
.map(Event::Message);
|
||||||
|
|
||||||
let commands = debug::commands().map(Event::Command);
|
let commands = debug::commands().map(Event::Command);
|
||||||
|
|
|
||||||
|
|
@ -58,12 +58,20 @@ impl Layout {
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
use keyboard::key;
|
use keyboard::key;
|
||||||
|
|
||||||
keyboard::on_key_release(|key, _physical_key, _modifiers| match key {
|
keyboard::listen().filter_map(|event| {
|
||||||
|
let keyboard::Event::KeyPressed { modified_key, .. } = event else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
match modified_key {
|
||||||
keyboard::Key::Named(key::Named::ArrowLeft) => {
|
keyboard::Key::Named(key::Named::ArrowLeft) => {
|
||||||
Some(Message::Previous)
|
Some(Message::Previous)
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::ArrowRight) => Some(Message::Next),
|
keyboard::Key::Named(key::Named::ArrowRight) => {
|
||||||
|
Some(Message::Next)
|
||||||
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,12 @@ impl Example {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
keyboard::on_key_press(|key, _physical_key, modifiers| {
|
keyboard::listen().filter_map(|event| {
|
||||||
|
let keyboard::Event::KeyPressed { key, modifiers, .. } = event
|
||||||
|
else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
if !modifiers.command() {
|
if !modifiers.command() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -227,8 +227,12 @@ impl Example {
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
use keyboard::key;
|
use keyboard::key;
|
||||||
|
|
||||||
keyboard::on_key_press(|key, _physical_key, _modifiers| {
|
keyboard::listen().filter_map(|event| {
|
||||||
if let keyboard::Key::Named(key::Named::F5) = key {
|
if let keyboard::Event::KeyPressed {
|
||||||
|
modified_key: keyboard::Key::Named(key::Named::F5),
|
||||||
|
..
|
||||||
|
} = event
|
||||||
|
{
|
||||||
Some(Message::Screenshot)
|
Some(Message::Screenshot)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
||||||
|
|
@ -65,14 +65,14 @@ impl Stopwatch {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fn handle_hotkey(
|
fn handle_hotkey(event: keyboard::Event) -> Option<Message> {
|
||||||
key: keyboard::Key,
|
|
||||||
_physical_key: keyboard::key::Physical,
|
|
||||||
_modifiers: keyboard::Modifiers,
|
|
||||||
) -> Option<Message> {
|
|
||||||
use keyboard::key;
|
use keyboard::key;
|
||||||
|
|
||||||
match key.as_ref() {
|
let keyboard::Event::KeyPressed { modified_key, .. } = event else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
match modified_key.as_ref() {
|
||||||
keyboard::Key::Named(key::Named::Space) => {
|
keyboard::Key::Named(key::Named::Space) => {
|
||||||
Some(Message::Toggle)
|
Some(Message::Toggle)
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +81,10 @@ impl Stopwatch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Subscription::batch(vec![tick, keyboard::on_key_press(handle_hotkey)])
|
Subscription::batch(vec![
|
||||||
|
tick,
|
||||||
|
keyboard::listen().filter_map(handle_hotkey),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<'_, Message> {
|
fn view(&self) -> Element<'_, Message> {
|
||||||
|
|
|
||||||
|
|
@ -190,18 +190,25 @@ impl Styling {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
keyboard::on_key_press(|key, _physical_key, _modifiers| match key {
|
keyboard::listen().filter_map(|event| {
|
||||||
keyboard::Key::Named(
|
let keyboard::Event::KeyPressed {
|
||||||
keyboard::key::Named::ArrowUp | keyboard::key::Named::ArrowLeft,
|
modified_key: keyboard::Key::Named(modified_key),
|
||||||
) => Some(Message::PreviousTheme),
|
..
|
||||||
keyboard::Key::Named(
|
} = event
|
||||||
keyboard::key::Named::ArrowDown
|
else {
|
||||||
| keyboard::key::Named::ArrowRight,
|
return None;
|
||||||
) => Some(Message::NextTheme),
|
};
|
||||||
keyboard::Key::Named(keyboard::key::Named::Space) => {
|
|
||||||
Some(Message::ClearTheme)
|
match modified_key {
|
||||||
|
keyboard::key::Named::ArrowUp
|
||||||
|
| keyboard::key::Named::ArrowLeft => {
|
||||||
|
Some(Message::PreviousTheme)
|
||||||
}
|
}
|
||||||
|
keyboard::key::Named::ArrowDown
|
||||||
|
| keyboard::key::Named::ArrowRight => Some(Message::NextTheme),
|
||||||
|
keyboard::key::Named::Space => Some(Message::ClearTheme),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -254,12 +254,12 @@ impl Todos {
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
use keyboard::key;
|
use keyboard::key;
|
||||||
|
|
||||||
keyboard::on_key_press(|key, _physical_key, modifiers| {
|
keyboard::listen().filter_map(|event| match event {
|
||||||
let keyboard::Key::Named(key) = key else {
|
keyboard::Event::KeyPressed {
|
||||||
return None;
|
key: keyboard::Key::Named(key),
|
||||||
};
|
modifiers,
|
||||||
|
..
|
||||||
match (key, modifiers) {
|
} => match (key, modifiers) {
|
||||||
(key::Named::Tab, _) => Some(Message::TabPressed {
|
(key::Named::Tab, _) => Some(Message::TabPressed {
|
||||||
shift: modifiers.shift(),
|
shift: modifiers.shift(),
|
||||||
}),
|
}),
|
||||||
|
|
@ -270,7 +270,8 @@ impl Todos {
|
||||||
Some(Message::ToggleFullscreen(window::Mode::Windowed))
|
Some(Message::ToggleFullscreen(window::Mode::Windowed))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
},
|
||||||
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,66 +1,18 @@
|
||||||
//! Listen to keyboard events.
|
//! Listen to keyboard events.
|
||||||
use crate::MaybeSend;
|
|
||||||
use crate::core;
|
use crate::core;
|
||||||
use crate::core::event;
|
use crate::core::keyboard::Event;
|
||||||
use crate::core::keyboard::{Event, Key, Modifiers, key};
|
|
||||||
use crate::subscription::{self, Subscription};
|
use crate::subscription::{self, Subscription};
|
||||||
|
|
||||||
/// Listens to keyboard key presses and calls the given function
|
/// Returns a [`Subscription`] that listens to ignored keyboard events.
|
||||||
/// to map them into actual messages.
|
pub fn listen() -> Subscription<Event> {
|
||||||
///
|
|
||||||
/// If the function returns `None`, the key press will be simply
|
|
||||||
/// ignored.
|
|
||||||
pub fn on_key_press<Message>(
|
|
||||||
f: fn(Key, key::Physical, Modifiers) -> Option<Message>,
|
|
||||||
) -> Subscription<Message>
|
|
||||||
where
|
|
||||||
Message: MaybeSend + 'static,
|
|
||||||
{
|
|
||||||
#[derive(Hash)]
|
#[derive(Hash)]
|
||||||
struct OnKeyPress;
|
struct Listen;
|
||||||
|
|
||||||
subscription::filter_map((OnKeyPress, f), move |event| match event {
|
subscription::filter_map(Listen, move |event| match event {
|
||||||
subscription::Event::Interaction {
|
subscription::Event::Interaction {
|
||||||
event:
|
event: core::Event::Keyboard(event),
|
||||||
core::Event::Keyboard(Event::KeyPressed {
|
|
||||||
key,
|
|
||||||
physical_key,
|
|
||||||
modifiers,
|
|
||||||
..
|
..
|
||||||
}),
|
} => Some(event),
|
||||||
status: event::Status::Ignored,
|
|
||||||
..
|
|
||||||
} => f(key, physical_key, modifiers),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Listens to keyboard key releases and calls the given function
|
|
||||||
/// to map them into actual messages.
|
|
||||||
///
|
|
||||||
/// If the function returns `None`, the key release will be simply
|
|
||||||
/// ignored.
|
|
||||||
pub fn on_key_release<Message>(
|
|
||||||
f: fn(Key, key::Physical, Modifiers) -> Option<Message>,
|
|
||||||
) -> Subscription<Message>
|
|
||||||
where
|
|
||||||
Message: MaybeSend + 'static,
|
|
||||||
{
|
|
||||||
#[derive(Hash)]
|
|
||||||
struct OnKeyRelease;
|
|
||||||
|
|
||||||
subscription::filter_map((OnKeyRelease, f), move |event| match event {
|
|
||||||
subscription::Event::Interaction {
|
|
||||||
event:
|
|
||||||
core::Event::Keyboard(Event::KeyReleased {
|
|
||||||
key,
|
|
||||||
physical_key,
|
|
||||||
modifiers,
|
|
||||||
..
|
|
||||||
}),
|
|
||||||
status: event::Status::Ignored,
|
|
||||||
..
|
|
||||||
} => f(key, physical_key, modifiers),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -590,7 +590,7 @@ pub mod keyboard {
|
||||||
//! Listen and react to keyboard events.
|
//! Listen and react to keyboard events.
|
||||||
pub use crate::core::keyboard::key;
|
pub use crate::core::keyboard::key;
|
||||||
pub use crate::core::keyboard::{Event, Key, Location, Modifiers};
|
pub use crate::core::keyboard::{Event, Key, Location, Modifiers};
|
||||||
pub use iced_futures::keyboard::{on_key_press, on_key_release};
|
pub use iced_futures::keyboard::listen;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod mouse {
|
pub mod mouse {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue