Skip to main content

oxide_gui_core/
event.rs

1//! Input event types.
2//!
3//! Both the Linux and OxideOS backends translate their native input into these
4//! common types so application code never sees platform-specific key codes.
5
6/// A keyboard key.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Key {
9    // Printable ASCII
10    Char(char),
11
12    // Control keys
13    Enter,
14    Backspace,
15    Delete,
16    Escape,
17    Tab,
18    Space,
19
20    // Navigation
21    Up,
22    Down,
23    Left,
24    Right,
25    Home,
26    End,
27    PageUp,
28    PageDown,
29
30    // Modifiers (sent as independent events)
31    LeftShift,
32    RightShift,
33    LeftCtrl,
34    RightCtrl,
35    LeftAlt,
36    RightAlt,
37
38    // Function keys
39    F(u8),
40
41    /// A key the backend doesn't map to any of the above variants.
42    Unknown,
43}
44
45/// A mouse button identifier.
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47pub enum MouseButton {
48    Left,
49    Right,
50    Middle,
51}
52
53/// A GUI input event.
54#[derive(Clone, Copy, Debug)]
55pub enum Event {
56    /// A key was pressed.
57    KeyDown(Key),
58    /// A key was released.
59    KeyUp(Key),
60
61    /// The pointer moved to `(x, y)` in window-content coordinates.
62    MouseMove { x: i32, y: i32 },
63
64    /// A mouse button was pressed or released.
65    MouseButton {
66        x:       i32,
67        y:       i32,
68        button:  MouseButton,
69        pressed: bool,
70    },
71
72    /// The mouse wheel scrolled.  `delta` is positive = up, negative = down.
73    Scroll { delta: i32 },
74
75    /// The window was asked to close (e.g., the user clicked ✕).
76    Close,
77
78    /// The window was resized.
79    Resize { width: u32, height: u32 },
80}
81
82impl Event {
83    pub fn is_close(&self) -> bool { matches!(self, Event::Close) }
84
85    /// Returns the character if this is `KeyDown(Char(_))`.
86    pub fn as_char(&self) -> Option<char> {
87        if let Event::KeyDown(Key::Char(c)) = self { Some(*c) } else { None }
88    }
89
90    /// Returns the key if this is a `KeyDown` event.
91    pub fn as_key_down(&self) -> Option<Key> {
92        if let Event::KeyDown(k) = self { Some(*k) } else { None }
93    }
94
95    /// Returns `(x, y, button, pressed)` if this is a `MouseButton` event.
96    pub fn as_mouse_btn(&self) -> Option<(i32, i32, MouseButton, bool)> {
97        if let Event::MouseButton { x, y, button, pressed } = self {
98            Some((*x, *y, *button, *pressed))
99        } else { None }
100    }
101
102    /// Returns `(x, y)` if this is a `MouseMove` event.
103    pub fn as_mouse_move(&self) -> Option<(i32, i32)> {
104        if let Event::MouseMove { x, y } = self { Some((*x, *y)) } else { None }
105    }
106}