cosmic/
keyboard_nav.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Subscribe to common application keyboard shortcuts.
5
6use iced::{Event, Subscription, event, keyboard};
7use iced_core::keyboard::key::Named;
8use iced_futures::event::listen_raw;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub enum Action {
12    Escape,
13    FocusNext,
14    FocusPrevious,
15    Fullscreen,
16    Search,
17}
18
19#[cold]
20pub fn subscription() -> Subscription<Action> {
21    listen_raw(|event, status, _| {
22        if event::Status::Ignored != status {
23            return None;
24        }
25
26        match event {
27            Event::Keyboard(keyboard::Event::KeyPressed {
28                key: keyboard::Key::Named(key),
29                modifiers,
30                ..
31            }) => match key {
32                Named::Tab if !modifiers.control() => {
33                    return Some(if modifiers.shift() {
34                        Action::FocusPrevious
35                    } else {
36                        Action::FocusNext
37                    });
38                }
39
40                Named::Escape => {
41                    return Some(Action::Escape);
42                }
43
44                Named::F11 => {
45                    return Some(Action::Fullscreen);
46                }
47
48                _ => (),
49            },
50            Event::Keyboard(keyboard::Event::KeyPressed {
51                key: keyboard::Key::Character(c),
52                modifiers,
53                ..
54            }) if c == "f" && modifiers.control() => {
55                return Some(Action::Search);
56            }
57
58            _ => (),
59        }
60
61        None
62    })
63}