Skip to main content

agg_gui/
event.rs

1//! Event types for the widget system.
2//!
3//! All coordinates in events are **first-quadrant (Y-up)** by the time any
4//! widget code sees them. The single Y-down → Y-up conversion happens at the
5//! platform boundary inside [`crate::widget::App`].
6
7use crate::geometry::Point;
8
9/// Which mouse button triggered a `MouseDown` or `MouseUp` event.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum MouseButton {
12    Left,
13    Middle,
14    Right,
15    Other(u8),
16}
17
18/// Modifier keys held at the time of an event.
19///
20/// `meta` is the platform-specific "super" key: **Cmd** on macOS, **Super /
21/// Windows key** on Linux, **Windows key** on Windows. Widgets that want
22/// portable command shortcuts should use the runtime platform helpers rather
23/// than treating `ctrl || meta` as universally equivalent.
24#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
25pub struct Modifiers {
26    pub shift: bool,
27    pub ctrl: bool,
28    pub alt: bool,
29    pub meta: bool,
30}
31
32/// A logical keyboard key.
33#[derive(Clone, Debug, PartialEq)]
34pub enum Key {
35    /// A printable character, already translated through the keyboard layout.
36    Char(char),
37    Backspace,
38    Delete,
39    /// The `Insert` key.  Paired with `Shift`/`Ctrl` for classic Windows
40    /// clipboard shortcuts (`Shift+Ins` paste, `Ctrl+Ins` copy).
41    Insert,
42    ArrowLeft,
43    ArrowRight,
44    ArrowUp,
45    ArrowDown,
46    Home,
47    End,
48    Tab,
49    Enter,
50    Escape,
51    /// Any key not in the above set — not usually handled, included for
52    /// completeness.
53    Other(String),
54}
55
56/// A GUI event delivered to a widget.
57///
58/// Coordinate positions are in the **local** coordinate space of the widget
59/// receiving the event (bottom-left origin, Y-up). The framework translates
60/// positions as it descends the widget tree.
61#[derive(Clone, Debug)]
62pub enum Event {
63    /// The cursor moved to `pos` (may be outside widget bounds — used to
64    /// clear hover state).
65    MouseMove { pos: Point },
66    /// A mouse button was pressed at `pos`.
67    MouseDown {
68        pos: Point,
69        button: MouseButton,
70        modifiers: Modifiers,
71    },
72    /// A mouse button was released at `pos`.
73    MouseUp {
74        pos: Point,
75        button: MouseButton,
76        modifiers: Modifiers,
77    },
78    /// A key was pressed while this widget (or a descendant) had focus.
79    KeyDown { key: Key, modifiers: Modifiers },
80    /// A key was released.
81    KeyUp { key: Key, modifiers: Modifiers },
82    /// Sent by the framework when this widget gains keyboard focus.
83    FocusGained,
84    /// Sent by the framework when this widget loses keyboard focus.
85    FocusLost,
86    /// Mouse wheel scrolled.  `delta_y` is in logical pixels; positive =
87    /// scroll up (content moves up, typical "natural" scroll direction).
88    /// `delta_x` is horizontal wheel / trackpad input in the same units;
89    /// positive = content moves right.  `pos` is the cursor location at the
90    /// time of the scroll.
91    MouseWheel {
92        pos: Point,
93        delta_y: f64,
94        delta_x: f64,
95        modifiers: Modifiers,
96    },
97}
98
99/// What a widget returns from [`crate::widget::Widget::on_event`].
100#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101pub enum EventResult {
102    /// The widget handled the event; stop propagation.
103    Consumed,
104    /// The widget did not handle the event; continue bubbling up.
105    Ignored,
106}