Skip to main content

ferroui_base/input/platform/
platform_hotkey_configuration.rs

1use crate::input::{Key, KeyGesture, KeyModifiers};
2
3/// The key gestures of the platform for common editing and navigation
4/// commands.
5#[derive(Clone, Debug, PartialEq)]
6pub struct PlatformHotkeyConfiguration {
7    pub command_modifiers: KeyModifiers,
8    pub whole_word_text_action_modifiers: KeyModifiers,
9    pub selection_modifiers: KeyModifiers,
10    pub copy: Vec<KeyGesture>,
11    pub cut: Vec<KeyGesture>,
12    pub paste: Vec<KeyGesture>,
13    pub undo: Vec<KeyGesture>,
14    pub redo: Vec<KeyGesture>,
15    pub select_all: Vec<KeyGesture>,
16    pub move_cursor_to_the_start_of_line: Vec<KeyGesture>,
17    pub move_cursor_to_the_end_of_line: Vec<KeyGesture>,
18    pub move_cursor_to_the_start_of_document: Vec<KeyGesture>,
19    pub move_cursor_to_the_end_of_document: Vec<KeyGesture>,
20    pub move_cursor_to_the_start_of_line_with_selection: Vec<KeyGesture>,
21    pub move_cursor_to_the_end_of_line_with_selection: Vec<KeyGesture>,
22    pub move_cursor_to_the_start_of_document_with_selection: Vec<KeyGesture>,
23    pub move_cursor_to_the_end_of_document_with_selection: Vec<KeyGesture>,
24    pub open_context_menu: Vec<KeyGesture>,
25    pub back: Vec<KeyGesture>,
26    pub page_up: Vec<KeyGesture>,
27    pub page_down: Vec<KeyGesture>,
28    pub page_right: Vec<KeyGesture>,
29    pub page_left: Vec<KeyGesture>,
30}
31
32impl Default for PlatformHotkeyConfiguration {
33    fn default() -> Self {
34        Self::new(KeyModifiers::CONTROL)
35    }
36}
37
38impl PlatformHotkeyConfiguration {
39    /// Creates a configuration for the given command modifiers, with Shift
40    /// as the selection modifier and Control as the whole-word modifier.
41    pub fn new(command_modifiers: KeyModifiers) -> Self {
42        Self::with_modifiers(command_modifiers, KeyModifiers::SHIFT, KeyModifiers::CONTROL)
43    }
44
45    /// Creates a configuration for the given modifiers.
46    pub fn with_modifiers(
47        command_modifiers: KeyModifiers,
48        selection_modifiers: KeyModifiers,
49        whole_word_text_action_modifiers: KeyModifiers,
50    ) -> Self {
51        let g = KeyGesture::new;
52        let k = KeyGesture::from_key;
53
54        Self {
55            command_modifiers,
56            selection_modifiers,
57            whole_word_text_action_modifiers,
58            copy: vec![g(Key::C, command_modifiers), g(Key::Insert, KeyModifiers::CONTROL)],
59            cut: vec![g(Key::X, command_modifiers)],
60            paste: vec![g(Key::V, command_modifiers), g(Key::Insert, KeyModifiers::SHIFT)],
61            undo: vec![g(Key::Z, command_modifiers)],
62            redo: vec![g(Key::Y, command_modifiers), g(Key::Z, command_modifiers | selection_modifiers)],
63            select_all: vec![g(Key::A, command_modifiers)],
64            move_cursor_to_the_start_of_line: vec![k(Key::Home)],
65            move_cursor_to_the_end_of_line: vec![k(Key::End)],
66            move_cursor_to_the_start_of_document: vec![g(Key::Home, command_modifiers)],
67            move_cursor_to_the_end_of_document: vec![g(Key::End, command_modifiers)],
68            move_cursor_to_the_start_of_line_with_selection: vec![g(Key::Home, selection_modifiers)],
69            move_cursor_to_the_end_of_line_with_selection: vec![g(Key::End, selection_modifiers)],
70            move_cursor_to_the_start_of_document_with_selection: vec![g(
71                Key::Home,
72                command_modifiers | selection_modifiers,
73            )],
74            move_cursor_to_the_end_of_document_with_selection: vec![g(
75                Key::End,
76                command_modifiers | selection_modifiers,
77            )],
78            open_context_menu: vec![k(Key::Apps)],
79            back: vec![g(Key::Left, KeyModifiers::ALT)],
80            page_left: vec![g(Key::PageUp, KeyModifiers::SHIFT)],
81            page_right: vec![g(Key::PageDown, KeyModifiers::SHIFT)],
82            page_up: vec![k(Key::PageUp)],
83            page_down: vec![k(Key::PageDown)],
84        }
85    }
86}