repose-core 0.30.17

Repose's core runtime, view model, signals, composition locals, and animation clock.
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use crate::Vec2;
use std::cell::Cell;
use std::rc::Rc;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PointerId(pub u64);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointerKind {
    Mouse,
    Touch,
    Pen,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PointerButton {
    Primary,   // Left mouse, touch
    Secondary, // Right mouse
    Tertiary,  // Middle mouse
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointerEventPass {
    /// Top-down pass: ancestor -> descendant. Allows ancestors to preview or
    /// intercept events before descendants see them.
    Initial,
    /// Bottom-up pass: descendant -> ancestor. The primary pass where gesture
    /// handlers react to and consume events. A child that consumes its event
    /// prevents the parent from reacting (Compose's requireUnconsumed).
    Main,
    /// Top-down pass: ancestor -> descendant. Allows descendants to learn
    /// about events consumed by ancestors during the Main pass.
    Final,
}

#[derive(Clone, Copy, Debug)]
pub enum PointerEventKind {
    Down(PointerButton),
    Up(PointerButton),
    Move,
    Cancel,
    Enter,
    Leave,
}

#[derive(Clone, Debug)]
pub struct PointerEvent {
    pub id: PointerId,
    pub kind: PointerKind,
    pub event: PointerEventKind,
    /// Position relative to `origin` (hit-region top-left), in physical px.
    pub position: Vec2,
    /// Top-left of the hit region this event is being delivered to (physical px).
    pub origin: Vec2,
    pub pressure: f32,
    pub modifiers: Modifiers,
    /// Shared consumed state -> every clone of this event points to the same
    /// Cell. Calling `consume()` on any clone marks it consumed for all clones.
    pub consumed: Rc<Cell<bool>>,
}

impl PointerEvent {
    pub fn new(
        id: PointerId,
        kind: PointerKind,
        event: PointerEventKind,
        position: Vec2,
        pressure: f32,
        modifiers: Modifiers,
    ) -> Self {
        Self {
            id,
            kind,
            event,
            position,
            origin: Vec2::ZERO,
            pressure,
            modifiers,
            consumed: Rc::new(Cell::new(false)),
        }
    }

    /// Absolute position in window/surface physical pixels.
    pub fn position_in_window(&self) -> Vec2 {
        self.position + self.origin
    }

    /// Mark this event as consumed. Once consumed, subsequent handlers in the
    /// same pass should skip processing it (equivalent to Compose's
    /// `PointerInputChange.consume()`).
    pub fn consume(&self) {
        self.consumed.set(true);
    }

    /// Returns `true` if `consume()` was called on this event or any clone of it.
    pub fn is_consumed(&self) -> bool {
        self.consumed.get()
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Modifiers {
    pub shift: bool,
    pub ctrl: bool,
    pub alt: bool,
    pub meta: bool,    // Cmd on Mac, Win key on Windows
    pub command: bool, // egui like (Cmd on macOS, Ctrl elsewhere)
}

/// Physical key position, layout-independent (W3C `KeyboardEvent.code`)
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PhysicalKey {
    KeyA,
    KeyB,
    KeyC,
    KeyD,
    KeyE,
    KeyF,
    KeyG,
    KeyH,
    KeyI,
    KeyJ,
    KeyK,
    KeyL,
    KeyM,
    KeyN,
    KeyO,
    KeyP,
    KeyQ,
    KeyR,
    KeyS,
    KeyT,
    KeyU,
    KeyV,
    KeyW,
    KeyX,
    KeyY,
    KeyZ,
    Digit0,
    Digit1,
    Digit2,
    Digit3,
    Digit4,
    Digit5,
    Digit6,
    Digit7,
    Digit8,
    Digit9,
    Minus,
    Equal,
    BracketLeft,
    BracketRight,
    Backslash,
    Semicolon,
    Quote,
    Backquote,
    Comma,
    Period,
    Slash,
    IntlBackslash,
    IntlRo,
    IntlYen,
    Escape,
    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    PrintScreen,
    ScrollLock,
    Pause,
    Insert,
    Home,
    PageUp,
    Delete,
    End,
    PageDown,
    ArrowRight,
    ArrowLeft,
    ArrowDown,
    ArrowUp,
    NumLock,
    NumpadDivide,
    NumpadMultiply,
    NumpadSubtract,
    NumpadAdd,
    NumpadEnter,
    NumpadDecimal,
    Numpad0,
    Numpad1,
    Numpad2,
    Numpad3,
    Numpad4,
    Numpad5,
    Numpad6,
    Numpad7,
    Numpad8,
    Numpad9,
    Backspace,
    Tab,
    Space,
    CapsLock,
    Enter,
    ShiftLeft,
    ShiftRight,
    ControlLeft,
    ControlRight,
    AltLeft,
    AltRight,
    SuperLeft,
    SuperRight,
    ContextMenu,
    Unidentified,
}

impl PhysicalKey {
    /// W3C `code` name (`KeyW`, `Digit1`, `Space`, ...).
    pub fn name(self) -> &'static str {
        match self {
            PhysicalKey::KeyA => "KeyA",
            PhysicalKey::KeyB => "KeyB",
            PhysicalKey::KeyC => "KeyC",
            PhysicalKey::KeyD => "KeyD",
            PhysicalKey::KeyE => "KeyE",
            PhysicalKey::KeyF => "KeyF",
            PhysicalKey::KeyG => "KeyG",
            PhysicalKey::KeyH => "KeyH",
            PhysicalKey::KeyI => "KeyI",
            PhysicalKey::KeyJ => "KeyJ",
            PhysicalKey::KeyK => "KeyK",
            PhysicalKey::KeyL => "KeyL",
            PhysicalKey::KeyM => "KeyM",
            PhysicalKey::KeyN => "KeyN",
            PhysicalKey::KeyO => "KeyO",
            PhysicalKey::KeyP => "KeyP",
            PhysicalKey::KeyQ => "KeyQ",
            PhysicalKey::KeyR => "KeyR",
            PhysicalKey::KeyS => "KeyS",
            PhysicalKey::KeyT => "KeyT",
            PhysicalKey::KeyU => "KeyU",
            PhysicalKey::KeyV => "KeyV",
            PhysicalKey::KeyW => "KeyW",
            PhysicalKey::KeyX => "KeyX",
            PhysicalKey::KeyY => "KeyY",
            PhysicalKey::KeyZ => "KeyZ",
            PhysicalKey::Digit0 => "Digit0",
            PhysicalKey::Digit1 => "Digit1",
            PhysicalKey::Digit2 => "Digit2",
            PhysicalKey::Digit3 => "Digit3",
            PhysicalKey::Digit4 => "Digit4",
            PhysicalKey::Digit5 => "Digit5",
            PhysicalKey::Digit6 => "Digit6",
            PhysicalKey::Digit7 => "Digit7",
            PhysicalKey::Digit8 => "Digit8",
            PhysicalKey::Digit9 => "Digit9",
            PhysicalKey::Minus => "Minus",
            PhysicalKey::Equal => "Equal",
            PhysicalKey::BracketLeft => "BracketLeft",
            PhysicalKey::BracketRight => "BracketRight",
            PhysicalKey::Backslash => "Backslash",
            PhysicalKey::Semicolon => "Semicolon",
            PhysicalKey::Quote => "Quote",
            PhysicalKey::Backquote => "Backquote",
            PhysicalKey::Comma => "Comma",
            PhysicalKey::Period => "Period",
            PhysicalKey::Slash => "Slash",
            PhysicalKey::IntlBackslash => "IntlBackslash",
            PhysicalKey::IntlRo => "IntlRo",
            PhysicalKey::IntlYen => "IntlYen",
            PhysicalKey::Escape => "Escape",
            PhysicalKey::F1 => "F1",
            PhysicalKey::F2 => "F2",
            PhysicalKey::F3 => "F3",
            PhysicalKey::F4 => "F4",
            PhysicalKey::F5 => "F5",
            PhysicalKey::F6 => "F6",
            PhysicalKey::F7 => "F7",
            PhysicalKey::F8 => "F8",
            PhysicalKey::F9 => "F9",
            PhysicalKey::F10 => "F10",
            PhysicalKey::F11 => "F11",
            PhysicalKey::F12 => "F12",
            PhysicalKey::PrintScreen => "PrintScreen",
            PhysicalKey::ScrollLock => "ScrollLock",
            PhysicalKey::Pause => "Pause",
            PhysicalKey::Insert => "Insert",
            PhysicalKey::Home => "Home",
            PhysicalKey::PageUp => "PageUp",
            PhysicalKey::Delete => "Delete",
            PhysicalKey::End => "End",
            PhysicalKey::PageDown => "PageDown",
            PhysicalKey::ArrowRight => "ArrowRight",
            PhysicalKey::ArrowLeft => "ArrowLeft",
            PhysicalKey::ArrowDown => "ArrowDown",
            PhysicalKey::ArrowUp => "ArrowUp",
            PhysicalKey::NumLock => "NumLock",
            PhysicalKey::NumpadDivide => "NumpadDivide",
            PhysicalKey::NumpadMultiply => "NumpadMultiply",
            PhysicalKey::NumpadSubtract => "NumpadSubtract",
            PhysicalKey::NumpadAdd => "NumpadAdd",
            PhysicalKey::NumpadEnter => "NumpadEnter",
            PhysicalKey::NumpadDecimal => "NumpadDecimal",
            PhysicalKey::Numpad0 => "Numpad0",
            PhysicalKey::Numpad1 => "Numpad1",
            PhysicalKey::Numpad2 => "Numpad2",
            PhysicalKey::Numpad3 => "Numpad3",
            PhysicalKey::Numpad4 => "Numpad4",
            PhysicalKey::Numpad5 => "Numpad5",
            PhysicalKey::Numpad6 => "Numpad6",
            PhysicalKey::Numpad7 => "Numpad7",
            PhysicalKey::Numpad8 => "Numpad8",
            PhysicalKey::Numpad9 => "Numpad9",
            PhysicalKey::Backspace => "Backspace",
            PhysicalKey::Tab => "Tab",
            PhysicalKey::Space => "Space",
            PhysicalKey::CapsLock => "CapsLock",
            PhysicalKey::Enter => "Enter",
            PhysicalKey::ShiftLeft => "ShiftLeft",
            PhysicalKey::ShiftRight => "ShiftRight",
            PhysicalKey::ControlLeft => "ControlLeft",
            PhysicalKey::ControlRight => "ControlRight",
            PhysicalKey::AltLeft => "AltLeft",
            PhysicalKey::AltRight => "AltRight",
            PhysicalKey::SuperLeft => "SuperLeft",
            PhysicalKey::SuperRight => "SuperRight",
            PhysicalKey::ContextMenu => "ContextMenu",
            PhysicalKey::Unidentified => "Unidentified",
        }
    }

    /// Parse a W3C `code` name. Unknown names map to `Unidentified`
    /// (never `None`: every physical press has a position, even when
    /// the platform cannot name it).
    pub fn from_name(name: &str) -> Self {
        match name {
            "KeyA" => PhysicalKey::KeyA,
            "KeyB" => PhysicalKey::KeyB,
            "KeyC" => PhysicalKey::KeyC,
            "KeyD" => PhysicalKey::KeyD,
            "KeyE" => PhysicalKey::KeyE,
            "KeyF" => PhysicalKey::KeyF,
            "KeyG" => PhysicalKey::KeyG,
            "KeyH" => PhysicalKey::KeyH,
            "KeyI" => PhysicalKey::KeyI,
            "KeyJ" => PhysicalKey::KeyJ,
            "KeyK" => PhysicalKey::KeyK,
            "KeyL" => PhysicalKey::KeyL,
            "KeyM" => PhysicalKey::KeyM,
            "KeyN" => PhysicalKey::KeyN,
            "KeyO" => PhysicalKey::KeyO,
            "KeyP" => PhysicalKey::KeyP,
            "KeyQ" => PhysicalKey::KeyQ,
            "KeyR" => PhysicalKey::KeyR,
            "KeyS" => PhysicalKey::KeyS,
            "KeyT" => PhysicalKey::KeyT,
            "KeyU" => PhysicalKey::KeyU,
            "KeyV" => PhysicalKey::KeyV,
            "KeyW" => PhysicalKey::KeyW,
            "KeyX" => PhysicalKey::KeyX,
            "KeyY" => PhysicalKey::KeyY,
            "KeyZ" => PhysicalKey::KeyZ,
            "Digit0" => PhysicalKey::Digit0,
            "Digit1" => PhysicalKey::Digit1,
            "Digit2" => PhysicalKey::Digit2,
            "Digit3" => PhysicalKey::Digit3,
            "Digit4" => PhysicalKey::Digit4,
            "Digit5" => PhysicalKey::Digit5,
            "Digit6" => PhysicalKey::Digit6,
            "Digit7" => PhysicalKey::Digit7,
            "Digit8" => PhysicalKey::Digit8,
            "Digit9" => PhysicalKey::Digit9,
            "Minus" => PhysicalKey::Minus,
            "Equal" => PhysicalKey::Equal,
            "BracketLeft" => PhysicalKey::BracketLeft,
            "BracketRight" => PhysicalKey::BracketRight,
            "Backslash" => PhysicalKey::Backslash,
            "Semicolon" => PhysicalKey::Semicolon,
            "Quote" => PhysicalKey::Quote,
            "Backquote" => PhysicalKey::Backquote,
            "Comma" => PhysicalKey::Comma,
            "Period" => PhysicalKey::Period,
            "Slash" => PhysicalKey::Slash,
            "IntlBackslash" => PhysicalKey::IntlBackslash,
            "IntlRo" => PhysicalKey::IntlRo,
            "IntlYen" => PhysicalKey::IntlYen,
            "Escape" => PhysicalKey::Escape,
            "F1" => PhysicalKey::F1,
            "F2" => PhysicalKey::F2,
            "F3" => PhysicalKey::F3,
            "F4" => PhysicalKey::F4,
            "F5" => PhysicalKey::F5,
            "F6" => PhysicalKey::F6,
            "F7" => PhysicalKey::F7,
            "F8" => PhysicalKey::F8,
            "F9" => PhysicalKey::F9,
            "F10" => PhysicalKey::F10,
            "F11" => PhysicalKey::F11,
            "F12" => PhysicalKey::F12,
            "PrintScreen" => PhysicalKey::PrintScreen,
            "ScrollLock" => PhysicalKey::ScrollLock,
            "Pause" => PhysicalKey::Pause,
            "Insert" => PhysicalKey::Insert,
            "Home" => PhysicalKey::Home,
            "PageUp" => PhysicalKey::PageUp,
            "Delete" => PhysicalKey::Delete,
            "End" => PhysicalKey::End,
            "PageDown" => PhysicalKey::PageDown,
            "ArrowRight" => PhysicalKey::ArrowRight,
            "ArrowLeft" => PhysicalKey::ArrowLeft,
            "ArrowDown" => PhysicalKey::ArrowDown,
            "ArrowUp" => PhysicalKey::ArrowUp,
            "NumLock" => PhysicalKey::NumLock,
            "NumpadDivide" => PhysicalKey::NumpadDivide,
            "NumpadMultiply" => PhysicalKey::NumpadMultiply,
            "NumpadSubtract" => PhysicalKey::NumpadSubtract,
            "NumpadAdd" => PhysicalKey::NumpadAdd,
            "NumpadEnter" => PhysicalKey::NumpadEnter,
            "NumpadDecimal" => PhysicalKey::NumpadDecimal,
            "Numpad0" => PhysicalKey::Numpad0,
            "Numpad1" => PhysicalKey::Numpad1,
            "Numpad2" => PhysicalKey::Numpad2,
            "Numpad3" => PhysicalKey::Numpad3,
            "Numpad4" => PhysicalKey::Numpad4,
            "Numpad5" => PhysicalKey::Numpad5,
            "Numpad6" => PhysicalKey::Numpad6,
            "Numpad7" => PhysicalKey::Numpad7,
            "Numpad8" => PhysicalKey::Numpad8,
            "Numpad9" => PhysicalKey::Numpad9,
            "Backspace" => PhysicalKey::Backspace,
            "Tab" => PhysicalKey::Tab,
            "Space" => PhysicalKey::Space,
            "CapsLock" => PhysicalKey::CapsLock,
            "Enter" => PhysicalKey::Enter,
            "ShiftLeft" => PhysicalKey::ShiftLeft,
            "ShiftRight" => PhysicalKey::ShiftRight,
            "ControlLeft" => PhysicalKey::ControlLeft,
            "ControlRight" => PhysicalKey::ControlRight,
            "AltLeft" => PhysicalKey::AltLeft,
            "AltRight" => PhysicalKey::AltRight,
            "SuperLeft" | "MetaLeft" => PhysicalKey::SuperLeft,
            "SuperRight" | "MetaRight" => PhysicalKey::SuperRight,
            "ContextMenu" => PhysicalKey::ContextMenu,
            _ => PhysicalKey::Unidentified,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Key {
    Character(char),
    Enter,
    Tab,
    Backspace,
    Delete,
    Insert,
    Escape,
    ArrowLeft,
    ArrowRight,
    ArrowUp,
    ArrowDown,
    Home,
    End,
    PageUp,
    PageDown,
    Space,
    ShiftLeft,
    ShiftRight,
    F(u8), // F1-F12
    Unknown,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyEventType {
    /// Key pressed down.
    Down,
    /// Key released.
    Up,
    /// Unknown or unsupported event type.
    Unknown,
}

#[derive(Clone, Debug)]
pub struct KeyEvent {
    pub key: Key,
    pub modifiers: Modifiers,
    pub is_repeat: bool,
    /// Whether this is a key-down or key-up event.
    pub event_type: KeyEventType,
    /// UTF-16 code point for character keys, or 0 for non-characters.
    /// Matches Compose's `utf16CodePoint`.
    pub utf16_code_point: u16,
    /// Physical key position, layout-independent. `None` for synthetic events.
    pub physical: Option<PhysicalKey>,
}

#[derive(Clone, Debug)]
pub struct TextInputEvent {
    pub text: String,
}

#[derive(Clone, Debug)]
pub enum ImeEvent {
    /// IME composition started
    Start,
    /// Composition text updated
    Update {
        text: String,
        cursor: Option<(usize, usize)>, // (start, end) of composition range
    },
    /// Composition committed (finalized)
    Commit(String),
    /// Composition cancelled
    Cancel,
}

#[derive(Clone, Debug)]
pub enum InputEvent {
    Pointer(PointerEvent),
    Key(KeyEvent),
    Text(TextInputEvent),
    Ime(ImeEvent),
    Gamepad(GamepadEvent),
}

/// Opaque gamepad handle. Backend-local index, stable for the connection
/// lifetime. Survives across frames; invalid after [`GamepadEvent::Disconnected`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct GamepadId(pub u32);

/// Standard-layout buttons (SDL gamecontroller mapping positions).
/// Backends translate hardware codes to these; unknown buttons are dropped.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GamepadButton {
    /// Bottom face button (A / Cross). UI default: activate.
    South,
    /// Right face button (B / Circle). UI default: back.
    East,
    /// Left face button (X / Square).
    West,
    /// Top face button (Y / Triangle).
    North,
    Start,
    Select,
    LeftShoulder,
    RightShoulder,
    LeftStick,
    RightStick,
    DPadUp,
    DPadDown,
    DPadLeft,
    DPadRight,
}

/// Analog axes, normalized to -1.0..=1.0. Triggers report 0.0..=1.0.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GamepadAxis {
    LeftStickX,
    LeftStickY,
    RightStickX,
    RightStickY,
    LeftTrigger,
    RightTrigger,
}

#[derive(Clone, Debug)]
pub enum GamepadEvent {
    Connected {
        id: GamepadId,
        name: String,
    },
    Disconnected {
        id: GamepadId,
    },
    Button {
        id: GamepadId,
        button: GamepadButton,
        pressed: bool,
    },
    Axis {
        id: GamepadId,
        axis: GamepadAxis,
        /// -1.0..=1.0 (sticks) or 0.0..=1.0 (triggers). Backends deadzone.
        value: f32,
    },
}

impl GamepadEvent {
    pub fn id(&self) -> GamepadId {
        match *self {
            GamepadEvent::Connected { id, .. } => id,
            GamepadEvent::Disconnected { id } => id,
            GamepadEvent::Button { id, .. } => id,
            GamepadEvent::Axis { id, .. } => id,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum InputMode {
    #[default]
    Touch,
    /// Keyboard, Tab, arrow/D-pad, or other non-pointer navigation.
    Keyboard,
}

thread_local! {
    static INPUT_MODE: Cell<InputMode> = const { Cell::new(InputMode::Touch) };
}

/// Current input mode (Compose `InputModeManager.inputMode`).
///
/// Composition-local override ([`crate::locals::with_input_mode`]) wins over
/// the thread default.
#[inline]
pub fn input_mode() -> InputMode {
    crate::locals::local_input_mode().unwrap_or_else(|| INPUT_MODE.get())
}

/// Force the global default input mode (no frame request). Prefer
/// [`request_input_mode`] from event handlers.
#[inline]
pub fn set_input_mode_default(mode: InputMode) {
    INPUT_MODE.set(mode);
}

/// Request a new input mode. Returns `true` if the mode changed.
///
/// On change, requests a frame so focus chrome can appear/disappear.
pub fn request_input_mode(mode: InputMode) -> bool {
    let prev = INPUT_MODE.get();
    if prev == mode {
        return false;
    }
    INPUT_MODE.set(mode);
    crate::frame_clock::request_frame();
    true
}

/// `true` when focus indication should paint (focused **and** keyboard mode).
#[inline]
pub fn is_focus_visible(focused: bool) -> bool {
    focused && input_mode() == InputMode::Keyboard
}

#[cfg(test)]
mod input_mode_tests {
    use super::*;
    use crate::frame_clock::take_frame_request;
    use crate::modifier::{Interaction, MutableInteractionSource};

    #[test]
    fn request_input_mode_changes_and_requests_frame() {
        set_input_mode_default(InputMode::Touch);
        let _ = take_frame_request();

        assert!(!request_input_mode(InputMode::Touch));
        assert!(!take_frame_request());

        assert!(request_input_mode(InputMode::Keyboard));
        assert_eq!(input_mode(), InputMode::Keyboard);
        assert!(take_frame_request());

        assert!(request_input_mode(InputMode::Touch));
        assert_eq!(input_mode(), InputMode::Touch);
        set_input_mode_default(InputMode::Touch);
    }

    #[test]
    fn focus_visible_requires_keyboard_mode() {
        set_input_mode_default(InputMode::Touch);
        let src = MutableInteractionSource::new();
        src.emit(Interaction::Focus);
        assert!(src.source().collect_is_focused());
        assert!(!src.source().collect_is_focus_visible());

        set_input_mode_default(InputMode::Keyboard);
        assert!(src.source().collect_is_focus_visible());

        set_input_mode_default(InputMode::Touch);
        src.emit(Interaction::Unfocus);
    }

    #[test]
    fn with_input_mode_overrides_global() {
        set_input_mode_default(InputMode::Touch);
        crate::locals::with_input_mode(InputMode::Keyboard, || {
            assert_eq!(input_mode(), InputMode::Keyboard);
        });
        assert_eq!(input_mode(), InputMode::Touch);
    }
}