Skip to main content

ferroui_base/input/
navigation_direction.rs

1use super::{Key, KeyModifiers};
2
3/// Describes how focus should be moved by directional or tab keys.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
5pub enum NavigationDirection {
6    /// Move the focus to the next control in the tab order.
7    Next,
8    /// Move the focus to the previous control in the tab order.
9    Previous,
10    /// Move the focus to the first control in the tab order.
11    First,
12    /// Move the focus to the last control in the tab order.
13    Last,
14    /// Move the focus to the left.
15    Left,
16    /// Move the focus to the right.
17    Right,
18    /// Move the focus up.
19    Up,
20    /// Move the focus down.
21    Down,
22    /// Move the focus up a page.
23    PageUp,
24    /// Move the focus down a page.
25    PageDown,
26}
27
28impl NavigationDirection {
29    /// Checks whether a navigation direction is a tab movement.
30    pub fn is_tab(self) -> bool {
31        self == NavigationDirection::Next || self == NavigationDirection::Previous
32    }
33
34    /// Checks whether a navigation direction is a directional movement.
35    pub fn is_directional(self) -> bool {
36        self > NavigationDirection::Previous && self <= NavigationDirection::PageDown
37    }
38}
39
40impl Key {
41    /// Converts a keypress into a navigation direction, if the key is a
42    /// navigation key.
43    pub fn to_navigation_direction(self, modifiers: KeyModifiers) -> Option<NavigationDirection> {
44        match self {
45            Key::Tab => Some(if !modifiers.contains(KeyModifiers::SHIFT) {
46                NavigationDirection::Next
47            } else {
48                NavigationDirection::Previous
49            }),
50            Key::Up => Some(NavigationDirection::Up),
51            Key::Down => Some(NavigationDirection::Down),
52            Key::Left => Some(NavigationDirection::Left),
53            Key::Right => Some(NavigationDirection::Right),
54            Key::Home => Some(NavigationDirection::First),
55            Key::End => Some(NavigationDirection::Last),
56            Key::PageUp => Some(NavigationDirection::PageUp),
57            Key::PageDown => Some(NavigationDirection::PageDown),
58            _ => None,
59        }
60    }
61}