iced_core/keyboard/
modifiers.rs

1use bitflags::bitflags;
2
3bitflags! {
4    /// The current state of the keyboard modifiers.
5    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
6    pub struct Modifiers: u32{
7        /// The "shift" key.
8        const SHIFT = 0b100;
9        // const LSHIFT = 0b010 << 0;
10        // const RSHIFT = 0b001 << 0;
11        //
12        /// The "control" key.
13        const CTRL = 0b100 << 3;
14        // const LCTRL = 0b010 << 3;
15        // const RCTRL = 0b001 << 3;
16        //
17        /// The "alt" key.
18        const ALT = 0b100 << 6;
19        // const LALT = 0b010 << 6;
20        // const RALT = 0b001 << 6;
21        //
22        /// The "windows" key on Windows, "command" key on Mac, and
23        /// "super" key on Linux.
24        const LOGO = 0b100 << 9;
25        // const LLOGO = 0b010 << 9;
26        // const RLOGO = 0b001 << 9;
27    }
28}
29
30impl Modifiers {
31    /// The "command" key.
32    ///
33    /// This is normally the main modifier to be used for hotkeys.
34    ///
35    /// On macOS, this is equivalent to `Self::LOGO`.
36    /// Otherwise, this is equivalent to `Self::CTRL`.
37    pub const COMMAND: Self = if cfg!(target_os = "macos") {
38        Self::LOGO
39    } else {
40        Self::CTRL
41    };
42
43    /// Returns true if the [`SHIFT`] key is pressed in the [`Modifiers`].
44    ///
45    /// [`SHIFT`]: Self::SHIFT
46    pub fn shift(self) -> bool {
47        self.contains(Self::SHIFT)
48    }
49
50    /// Returns true if the [`CTRL`] key is pressed in the [`Modifiers`].
51    ///
52    /// [`CTRL`]: Self::CTRL
53    pub fn control(self) -> bool {
54        self.contains(Self::CTRL)
55    }
56
57    /// Returns true if the [`ALT`] key is pressed in the [`Modifiers`].
58    ///
59    /// [`ALT`]: Self::ALT
60    pub fn alt(self) -> bool {
61        self.contains(Self::ALT)
62    }
63
64    /// Returns true if the [`LOGO`] key is pressed in the [`Modifiers`].
65    ///
66    /// [`LOGO`]: Self::LOGO
67    pub fn logo(self) -> bool {
68        self.contains(Self::LOGO)
69    }
70
71    /// Returns true if a "command key" is pressed in the [`Modifiers`].
72    ///
73    /// The "command key" is the main modifier key used to issue commands in the
74    /// current platform. Specifically:
75    ///
76    /// - It is the `logo` or command key (⌘) on macOS
77    /// - It is the `control` key on other platforms
78    pub fn command(self) -> bool {
79        #[cfg(target_os = "macos")]
80        let is_pressed = self.logo();
81
82        #[cfg(not(target_os = "macos"))]
83        let is_pressed = self.control();
84
85        is_pressed
86    }
87
88    /// Returns true if the "jump key" is pressed in the [`Modifiers`].
89    ///
90    /// The "jump key" is the modifier key used to widen text motions. It is the `Alt`
91    /// key in macOS and the `Ctrl` key in other platforms.
92    pub fn jump(self) -> bool {
93        if cfg!(target_os = "macos") {
94            self.alt()
95        } else {
96            self.control()
97        }
98    }
99
100    /// Returns true if the "command key" is pressed on a macOS device.
101    ///
102    /// This is relevant for macOS-specific actions (e.g. `⌘ + ArrowLeft` moves the cursor
103    /// to the beginning of the line).
104    pub fn macos_command(self) -> bool {
105        if cfg!(target_os = "macos") {
106            self.logo()
107        } else {
108            false
109        }
110    }
111}