2023-08-02 11:54:07 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-08-15 10:51:59 +02:00
|
|
|
//! Subscribe to common application keyboard shortcuts.
|
|
|
|
|
|
2025-03-21 03:17:59 +01:00
|
|
|
use iced::{Event, Subscription, event, keyboard};
|
2024-09-16 19:11:29 +02:00
|
|
|
use iced_core::keyboard::key::Named;
|
2023-11-30 14:01:42 -05:00
|
|
|
use iced_futures::event::listen_raw;
|
2023-01-10 13:58:13 +01:00
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
2025-03-14 11:56:21 -04:00
|
|
|
pub enum Action {
|
2023-01-25 04:47:27 +01:00
|
|
|
Escape,
|
2023-01-10 13:58:13 +01:00
|
|
|
FocusNext,
|
|
|
|
|
FocusPrevious,
|
2023-08-02 11:54:07 +02:00
|
|
|
Fullscreen,
|
2023-01-25 04:47:27 +01:00
|
|
|
Search,
|
2023-01-10 13:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-21 03:17:59 +01:00
|
|
|
#[cold]
|
2025-03-14 11:56:21 -04:00
|
|
|
pub fn subscription() -> Subscription<Action> {
|
2024-10-16 20:36:46 -04:00
|
|
|
listen_raw(|event, status, _| {
|
2023-08-02 11:54:07 +02:00
|
|
|
if event::Status::Ignored != status {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match event {
|
2023-01-25 04:47:27 +01:00
|
|
|
Event::Keyboard(keyboard::Event::KeyPressed {
|
2024-01-30 22:14:00 -05:00
|
|
|
key: keyboard::Key::Named(key),
|
2023-01-25 04:47:27 +01:00
|
|
|
modifiers,
|
2024-01-30 22:14:00 -05:00
|
|
|
..
|
|
|
|
|
}) => match key {
|
2025-03-04 15:47:57 -05:00
|
|
|
Named::Tab if !modifiers.control() => {
|
2023-08-02 11:54:07 +02:00
|
|
|
return Some(if modifiers.shift() {
|
2025-03-14 11:56:21 -04:00
|
|
|
Action::FocusPrevious
|
2023-08-02 11:54:07 +02:00
|
|
|
} else {
|
2025-03-14 11:56:21 -04:00
|
|
|
Action::FocusNext
|
2023-08-02 11:54:07 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
Named::Escape => {
|
2025-03-14 11:56:21 -04:00
|
|
|
return Some(Action::Escape);
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 22:14:00 -05:00
|
|
|
Named::F11 => {
|
2025-03-14 11:56:21 -04:00
|
|
|
return Some(Action::Fullscreen);
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
},
|
2024-01-30 22:14:00 -05:00
|
|
|
Event::Keyboard(keyboard::Event::KeyPressed {
|
|
|
|
|
key: keyboard::Key::Character(c),
|
|
|
|
|
modifiers,
|
|
|
|
|
..
|
|
|
|
|
}) if c == "f" && modifiers.control() => {
|
2025-03-14 11:56:21 -04:00
|
|
|
return Some(Action::Search);
|
2024-01-30 22:14:00 -05:00
|
|
|
}
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
_ => (),
|
2023-01-25 04:47:27 +01:00
|
|
|
}
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
None
|
2023-01-10 13:58:13 +01:00
|
|
|
})
|
|
|
|
|
}
|