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 "command" key: **Cmd** on macOS, **Super /
21/// Windows key** on Linux, **Windows key** on Windows.  Widgets that want
22/// portable clipboard / select-all / undo shortcuts should treat
23/// `ctrl || meta` as the "command" modifier so both Windows-style
24/// `Ctrl+C` and Mac-style `Cmd+C` work without branching on the host OS.
25#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
26pub struct Modifiers {
27    pub shift: bool,
28    pub ctrl: bool,
29    pub alt: bool,
30    pub meta: bool,
31}
32
33/// A logical keyboard key.
34#[derive(Clone, Debug, PartialEq)]
35pub enum Key {
36    /// A printable character, already translated through the keyboard layout.
37    Char(char),
38    Backspace,
39    Delete,
40    /// The `Insert` key.  Paired with `Shift`/`Ctrl` for classic Windows
41    /// clipboard shortcuts (`Shift+Ins` paste, `Ctrl+Ins` copy).
42    Insert,
43    ArrowLeft,
44    ArrowRight,
45    ArrowUp,
46    ArrowDown,
47    Home,
48    End,
49    Tab,
50    Enter,
51    Escape,
52    /// Any key not in the above set — not usually handled, included for
53    /// completeness.
54    Other(String),
55}
56
57/// A GUI event delivered to a widget.
58///
59/// Coordinate positions are in the **local** coordinate space of the widget
60/// receiving the event (bottom-left origin, Y-up). The framework translates
61/// positions as it descends the widget tree.
62#[derive(Clone, Debug)]
63pub enum Event {
64    /// The cursor moved to `pos` (may be outside widget bounds — used to
65    /// clear hover state).
66    MouseMove { pos: Point },
67    /// A mouse button was pressed at `pos`.
68    MouseDown { pos: Point, button: MouseButton, modifiers: Modifiers },
69    /// A mouse button was released at `pos`.
70    MouseUp   { pos: Point, button: MouseButton, modifiers: Modifiers },
71    /// A key was pressed while this widget (or a descendant) had focus.
72    KeyDown { key: Key, modifiers: Modifiers },
73    /// A key was released.
74    KeyUp { key: Key, modifiers: Modifiers },
75    /// Sent by the framework when this widget gains keyboard focus.
76    FocusGained,
77    /// Sent by the framework when this widget loses keyboard focus.
78    FocusLost,
79    /// Mouse wheel scrolled.  `delta_y` is in logical pixels; positive =
80    /// scroll up (content moves up, typical "natural" scroll direction).
81    /// `delta_x` is horizontal wheel / trackpad input in the same units;
82    /// positive = content moves right.  `pos` is the cursor location at the
83    /// time of the scroll.
84    MouseWheel { pos: Point, delta_y: f64, delta_x: f64 },
85}
86
87/// What a widget returns from [`crate::widget::Widget::on_event`].
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub enum EventResult {
90    /// The widget handled the event; stop propagation.
91    Consumed,
92    /// The widget did not handle the event; continue bubbling up.
93    Ignored,
94}