oxiui-core 0.1.0

Core traits and types for OxiUI — Pure Rust UI layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Input event types: mouse buttons, keyboard keys, and modifier state.
//!
//! These types back the richer [`UiEvent`](crate::UiEvent) variants
//! (`MouseDown`, `KeyDown`, …) added on top of the original immediate-mode
//! event set. The original variants (`Resize`, `Mouse`, `KeyPress`, IME …)
//! are retained for backward compatibility with existing adapters.

/// A mouse button.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MouseButton {
    /// Primary (usually left) button.
    Left,
    /// Secondary (usually right) button.
    Right,
    /// Middle / wheel button.
    Middle,
    /// Any other button, identified by platform index.
    Other(u16),
}

/// Keyboard modifier-key state.
///
/// `meta` is the Command key on macOS and the Super/Windows key elsewhere.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Modifiers {
    /// Control key held.
    pub ctrl: bool,
    /// Alt / Option key held.
    pub alt: bool,
    /// Shift key held.
    pub shift: bool,
    /// Meta (Command / Super / Windows) key held.
    pub meta: bool,
}

impl Modifiers {
    /// No modifiers held.
    pub const NONE: Modifiers = Modifiers {
        ctrl: false,
        alt: false,
        shift: false,
        meta: false,
    };

    /// Returns `true` if no modifier keys are held.
    pub fn is_empty(self) -> bool {
        !self.ctrl && !self.alt && !self.shift && !self.meta
    }

    /// Returns `true` if the platform "command" modifier is held: `meta` on
    /// macOS-style platforms, `ctrl` elsewhere. Callers that don't track the
    /// platform can treat either as the accelerator modifier.
    pub fn command(self) -> bool {
        self.ctrl || self.meta
    }
}

/// A logical key, abstracted away from physical scan codes.
///
/// `Character` carries the produced text for printable keys; named variants
/// cover the common non-printable keys needed for navigation and editing.
///
/// The enum is `#[non_exhaustive]`: new variants added in future releases will
/// deserialise as a serde unknown-field error rather than silently mismatching.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Key {
    /// A printable character (already mapped through the active layout/IME).
    Character(String),
    /// Enter / Return.
    Enter,
    /// Tab.
    Tab,
    /// Space bar.
    Space,
    /// Backspace.
    Backspace,
    /// Delete (forward delete).
    Delete,
    /// Escape.
    Escape,
    /// Left arrow.
    ArrowLeft,
    /// Right arrow.
    ArrowRight,
    /// Up arrow.
    ArrowUp,
    /// Down arrow.
    ArrowDown,
    /// Home.
    Home,
    /// End.
    End,
    /// Page Up.
    PageUp,
    /// Page Down.
    PageDown,
    /// A function key `F1`–`F24` (the `u8` is the number, 1-based).
    Function(u8),
    /// Any other named key, identified by string (forward-compatible escape).
    Named(String),
}

impl Key {
    /// If this key produces text, return it; otherwise `None`.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Key::Character(s) => Some(s.as_str()),
            Key::Space => Some(" "),
            _ => None,
        }
    }
}

/// Mouse scroll-wheel delta.
///
/// Positive `y` scrolls content up (wheel away from the user) by convention.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ScrollDelta {
    /// Discrete line-based scrolling (mouse wheel notches).
    Lines {
        /// Horizontal lines.
        x: f32,
        /// Vertical lines.
        y: f32,
    },
    /// Smooth pixel-precise scrolling (trackpads).
    Pixels {
        /// Horizontal pixels.
        x: f32,
        /// Vertical pixels.
        y: f32,
    },
}

// ── Rich pointer / keyboard / touch events (dispatch layer) ─────────────────
//
// The variants below feed the [`EventDispatcher`](crate::dispatch::EventDispatcher)
// capture/bubble pipeline. They are richer than the immediate-mode
// [`UiEvent`](crate::UiEvent) set (which adapters emit) and carry geometry in
// [`Point`](crate::geometry::Point) form for hit testing.

use crate::geometry::Point;

/// A pointer (mouse / trackpad) event in tree-local coordinates.
#[derive(Clone, Debug, PartialEq)]
pub enum MouseEvent {
    /// A button was pressed.
    Down {
        /// Position in tree-local logical pixels.
        pos: Point,
        /// Which button.
        button: MouseButton,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// A button was released.
    Up {
        /// Position in tree-local logical pixels.
        pos: Point,
        /// Which button.
        button: MouseButton,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// The pointer moved without a press/release.
    Move {
        /// New position.
        pos: Point,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// The pointer entered a widget's bounds.
    Enter {
        /// Position at entry.
        pos: Point,
    },
    /// The pointer left a widget's bounds.
    Leave {
        /// Position at exit (last known).
        pos: Point,
    },
    /// A double click (two `Down`s within the platform double-click window).
    DoubleClick {
        /// Position.
        pos: Point,
        /// Which button.
        button: MouseButton,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// A triple click (three rapid `Down`s; selects a paragraph by convention).
    TripleClick {
        /// Position.
        pos: Point,
        /// Which button.
        button: MouseButton,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// A scroll-wheel / trackpad scroll, discrete or smooth (see [`ScrollDelta`]).
    Scroll {
        /// Position of the pointer when the scroll occurred.
        pos: Point,
        /// Scroll delta.
        delta: ScrollDelta,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// A drag gesture began (button held and moved past the start threshold).
    DragStart {
        /// Position where the drag started.
        pos: Point,
        /// Which button initiated the drag.
        button: MouseButton,
    },
    /// The pointer moved while dragging.
    DragMove {
        /// Current position.
        pos: Point,
        /// Movement since the previous `DragMove`/`DragStart`.
        delta: Point,
    },
    /// A drag gesture ended (button released).
    DragEnd {
        /// Position where the drag ended.
        pos: Point,
        /// Which button was released.
        button: MouseButton,
    },
}

impl MouseEvent {
    /// The pointer position carried by this event.
    pub fn position(&self) -> Point {
        match self {
            MouseEvent::Down { pos, .. }
            | MouseEvent::Up { pos, .. }
            | MouseEvent::Move { pos, .. }
            | MouseEvent::Enter { pos }
            | MouseEvent::Leave { pos }
            | MouseEvent::DoubleClick { pos, .. }
            | MouseEvent::TripleClick { pos, .. }
            | MouseEvent::Scroll { pos, .. }
            | MouseEvent::DragStart { pos, .. }
            | MouseEvent::DragMove { pos, .. }
            | MouseEvent::DragEnd { pos, .. } => *pos,
        }
    }
}

/// A physical key location, independent of the active keyboard layout.
///
/// Identified by a USB-HID-style code name (e.g. `"KeyA"`, `"Digit1"`). This
/// distinguishes the *position* pressed from the *character produced* (which
/// lives in the logical [`Key`]); a French AZERTY `Q` key and a US QWERTY `A`
/// key share the same physical code `"KeyA"`.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PhysicalKey(pub String);

impl PhysicalKey {
    /// Construct from a code name.
    pub fn new(code: impl Into<String>) -> Self {
        Self(code.into())
    }

    /// Borrow the code name.
    pub fn code(&self) -> &str {
        &self.0
    }
}

/// A keyboard event carrying both logical and physical key information.
#[derive(Clone, Debug, PartialEq)]
pub enum KeyboardEvent {
    /// A key was pressed (or auto-repeated; see `repeat`).
    Down {
        /// The logical key (layout/IME mapped).
        key: Key,
        /// The physical key location, if known.
        physical: Option<PhysicalKey>,
        /// Modifier state.
        modifiers: Modifiers,
        /// `true` if this is an OS auto-repeat of a held key.
        repeat: bool,
    },
    /// A key was released.
    Up {
        /// The logical key.
        key: Key,
        /// The physical key location, if known.
        physical: Option<PhysicalKey>,
        /// Modifier state.
        modifiers: Modifiers,
    },
    /// Committed text input (post-IME). Distinct from `Down` so a text field can
    /// ignore navigation keys while still receiving typed characters.
    CharInput {
        /// The committed text (may be multiple code points).
        text: String,
    },
}

impl KeyboardEvent {
    /// Returns `true` if this is an auto-repeat `Down` event.
    pub fn is_repeat(&self) -> bool {
        matches!(self, KeyboardEvent::Down { repeat: true, .. })
    }
}

/// A recognised multi-touch gesture.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GestureKind {
    /// A two-finger pinch; `scale` is the relative scale factor (`1.0` = none).
    Pinch {
        /// Relative scale since gesture start (`> 1` zoom in, `< 1` zoom out).
        scale: f32,
    },
    /// A two-finger rotation; `radians` is the signed angle delta.
    Rotate {
        /// Rotation delta in radians (positive = counter-clockwise).
        radians: f32,
    },
    /// A swipe; `delta` is the translation vector of the swipe.
    Swipe {
        /// Swipe translation in logical pixels.
        delta: Point,
    },
}

/// A touch event from a single contact point, identified by a stable touch id.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TouchEvent {
    /// A finger touched down.
    Start {
        /// Stable identifier for this contact for its lifetime.
        id: u64,
        /// Contact position.
        pos: Point,
    },
    /// A touched finger moved.
    Move {
        /// Contact identifier.
        id: u64,
        /// New position.
        pos: Point,
    },
    /// A finger lifted off.
    End {
        /// Contact identifier.
        id: u64,
        /// Position at release.
        pos: Point,
    },
    /// The contact was cancelled by the system (e.g. palm rejection).
    Cancel {
        /// Contact identifier.
        id: u64,
    },
    /// A recognised gesture spanning one or more contacts.
    Gesture {
        /// The recognised gesture.
        kind: GestureKind,
        /// The gesture centroid.
        center: Point,
    },
}

impl TouchEvent {
    /// The stable touch id, or `None` for centroid-only gesture events.
    pub fn touch_id(&self) -> Option<u64> {
        match self {
            TouchEvent::Start { id, .. }
            | TouchEvent::Move { id, .. }
            | TouchEvent::End { id, .. }
            | TouchEvent::Cancel { id } => Some(*id),
            TouchEvent::Gesture { .. } => None,
        }
    }
}

/// Propagation control flags an event handler can set to influence dispatch.
///
/// Defaults are `false`/`false`: the event continues through the
/// capture/bubble phases and the default action is allowed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Propagation {
    /// Stop the event travelling to further nodes in the current and
    /// subsequent phases (capture/bubble).
    pub stop_propagation: bool,
    /// Suppress the framework's default action for this event.
    pub prevent_default: bool,
}

impl Propagation {
    /// No control set — continue propagation, allow default.
    pub const CONTINUE: Propagation = Propagation {
        stop_propagation: false,
        prevent_default: false,
    };

    /// A [`Propagation`] that stops further propagation.
    pub const fn stop() -> Self {
        Self {
            stop_propagation: true,
            prevent_default: false,
        }
    }

    /// A [`Propagation`] that prevents the default action.
    pub const fn prevent() -> Self {
        Self {
            stop_propagation: false,
            prevent_default: true,
        }
    }

    /// Merge two propagation results, OR-ing each flag (sticky once set).
    pub fn merge(self, other: Propagation) -> Propagation {
        Propagation {
            stop_propagation: self.stop_propagation || other.stop_propagation,
            prevent_default: self.prevent_default || other.prevent_default,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn modifiers_command() {
        assert!(Modifiers::NONE.is_empty());
        let ctrl = Modifiers {
            ctrl: true,
            ..Modifiers::NONE
        };
        assert!(ctrl.command());
        let meta = Modifiers {
            meta: true,
            ..Modifiers::NONE
        };
        assert!(meta.command());
        assert!(!Modifiers::NONE.command());
    }

    #[test]
    fn key_as_text() {
        assert_eq!(Key::Character("ä".into()).as_text(), Some("ä"));
        assert_eq!(Key::Space.as_text(), Some(" "));
        assert_eq!(Key::Enter.as_text(), None);
        assert_eq!(Key::Function(5), Key::Function(5));
    }

    #[test]
    fn mouse_button_eq() {
        assert_eq!(MouseButton::Left, MouseButton::Left);
        assert_ne!(MouseButton::Left, MouseButton::Right);
        assert_eq!(MouseButton::Other(7), MouseButton::Other(7));
    }

    #[test]
    fn mouse_event_position() {
        let e = MouseEvent::Down {
            pos: Point::new(3.0, 4.0),
            button: MouseButton::Left,
            modifiers: Modifiers::NONE,
        };
        assert_eq!(e.position(), Point::new(3.0, 4.0));
        let drag = MouseEvent::DragMove {
            pos: Point::new(9.0, 9.0),
            delta: Point::new(1.0, 1.0),
        };
        assert_eq!(drag.position(), Point::new(9.0, 9.0));
    }

    #[test]
    fn keyboard_event_repeat_and_char() {
        let down = KeyboardEvent::Down {
            key: Key::Character("a".into()),
            physical: Some(PhysicalKey::new("KeyA")),
            modifiers: Modifiers::NONE,
            repeat: true,
        };
        assert!(down.is_repeat());
        let ci = KeyboardEvent::CharInput { text: "x".into() };
        assert!(!ci.is_repeat());
        if let KeyboardEvent::Down {
            physical: Some(p), ..
        } = &down
        {
            assert_eq!(p.code(), "KeyA");
        } else {
            panic!("expected Down with physical key");
        }
    }

    #[test]
    fn touch_event_id_and_gesture() {
        assert_eq!(
            TouchEvent::Start {
                id: 7,
                pos: Point::ZERO
            }
            .touch_id(),
            Some(7)
        );
        let g = TouchEvent::Gesture {
            kind: GestureKind::Pinch { scale: 1.5 },
            center: Point::new(10.0, 10.0),
        };
        assert_eq!(g.touch_id(), None);
        assert_eq!(g, g);
    }

    #[test]
    fn propagation_merge_is_sticky() {
        let a = Propagation::stop();
        let b = Propagation::prevent();
        let m = a.merge(b);
        assert!(m.stop_propagation);
        assert!(m.prevent_default);
        // CONTINUE merged with stop still stops.
        assert!(
            Propagation::CONTINUE
                .merge(Propagation::stop())
                .stop_propagation
        );
        assert_eq!(Propagation::default(), Propagation::CONTINUE);
    }
}