libcosmic/src/keyboard_nav.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

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.
use iced::{Event, Subscription, event, keyboard};
use iced_core::keyboard::key::Named;
2023-11-30 14:01:42 -05:00
use iced_futures::event::listen_raw;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Action {
Escape,
FocusNext,
FocusPrevious,
2023-08-02 11:54:07 +02:00
Fullscreen,
Search,
}
#[cold]
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 {
Event::Keyboard(keyboard::Event::KeyPressed {
2024-01-30 22:14:00 -05:00
key: keyboard::Key::Named(key),
modifiers,
2024-01-30 22:14:00 -05:00
..
}) => match key {
Named::Tab if !modifiers.control() => {
2023-08-02 11:54:07 +02:00
return Some(if modifiers.shift() {
Action::FocusPrevious
2023-08-02 11:54:07 +02:00
} else {
Action::FocusNext
2023-08-02 11:54:07 +02:00
});
}
2024-01-30 22:14:00 -05:00
Named::Escape => {
return Some(Action::Escape);
2023-08-02 11:54:07 +02:00
}
2024-01-30 22:14:00 -05:00
Named::F11 => {
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() => {
return Some(Action::Search);
2024-01-30 22:14:00 -05:00
}
2023-08-02 11:54:07 +02:00
_ => (),
}
2023-08-02 11:54:07 +02:00
None
})
}