Skip to main content

glory_render/
menu.rs

1//! Finite popup-menu commands and physical pointer boundary observations.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum MenuCommand {
4    First,
5    Last,
6    Open,
7    Close,
8    Exit { backwards: bool },
9}
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum PointerPhase {
12    Enter,
13    Leave,
14    /// Actual mouse/pen movement, distinct from layout-induced boundary entry.
15    Move,
16}
17
18impl crate::KeyboardInput<'_> {
19    pub fn menu_check_activation(self) -> bool {
20        self.key == "Enter"
21            && !self.repeat
22            && !self.composing
23            && !self.editable
24            && !self.control
25            && !self.meta
26            && !self.alt
27            && !self.shift
28    }
29    pub fn menu(self, direction: crate::Direction, trigger: bool) -> Option<MenuCommand> {
30        if self.composing || self.editable || self.control || self.meta || self.alt {
31            return None;
32        }
33        if self.key == "Tab" && !trigger && !self.repeat {
34            return Some(MenuCommand::Exit {
35                backwards: self.shift,
36            });
37        }
38        if self.shift {
39            return None;
40        }
41        match (trigger, direction, self.key) {
42            (true, _, "ArrowDown") => Some(MenuCommand::First),
43            (true, _, "ArrowUp") => Some(MenuCommand::Last),
44            (false, crate::Direction::Ltr, "ArrowRight")
45            | (false, crate::Direction::Rtl, "ArrowLeft") => Some(MenuCommand::Open),
46            (false, crate::Direction::Ltr, "ArrowLeft")
47            | (false, crate::Direction::Rtl, "ArrowRight") => Some(MenuCommand::Close),
48            _ => None,
49        }
50    }
51}