rdom-tui 0.3.10

Terminal rendering layer for rdom-core — flexbox layout, TUI styles, key/mouse events. Use rdom-core directly for headless DOM manipulation.
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
//! Crossterm-input → `rdom_core` event-detail translation.
//!
//! Single canonical seam between the terminal input layer and
//! the substrate's typed event detail. M4a step 8 will replace
//! every `TuiEvent.key` / `TuiEvent.mouse` sibling access with
//! reads through `event.detail.as_keyboard()` / `.as_mouse()`;
//! the helpers here are what those reads ultimately resolve to.
//!
//! ## Web fidelity
//!
//! `key_code_to_string` returns DOM `KeyboardEvent.key` values
//! per UI Events §named-key-attribute-values
//! (<https://www.w3.org/TR/uievents-key/#named-key-attribute-values>).
//! Crossterm taxonomy is wider in some places (left/right modifier
//! variants, BackTab) and narrower in others (no `.code` /
//! physical-key info, no AltGr alongside modifier flags).
//! Documented divergences:
//!
//! - `KeyCode::BackTab` collapses to `"Tab"`. DOM `key` is `"Tab"`
//!   regardless of Shift; browsers signal Shift+Tab via the
//!   `shiftKey` modifier. Apps that need to distinguish Tab from
//!   Shift+Tab read `modifiers.shift`.
//! - `Left*` / `Right*` modifier variants collapse to their
//!   unsided web names (`"Shift"`, `"Control"`, …). The left/right
//!   distinction belongs on `KeyboardEvent.code`, which rdom doesn't
//!   ship (terminals don't reliably expose physical-key info).
//! - `MediaKeyCode::Reverse` and `MediaKeyCode::Rewind` both map to
//!   `"MediaRewind"`. UI Events doesn't define a reverse-playback
//!   key; "Rewind" is the closest semantic neighbor. Information
//!   loss is bounded (TUI apps very rarely distinguish these).
//! - `IsoLevel3Shift` / `IsoLevel5Shift` collapse to `"AltGraph"`.
//! - Modifier bitflags `SUPER` and `META` both contribute to
//!   `KeyboardModifiers.meta`. macOS terminals usually report
//!   Command as `SUPER`; we keep that pulled together with `META`
//!   so apps don't have to special-case the platform.

use crossterm::event::{
    KeyCode, KeyEvent, KeyEventKind, KeyModifiers as CtKeyModifiers, MediaKeyCode, ModifierKeyCode,
    MouseButton as CtMouseButton, MouseEvent, MouseEventKind,
};

use rdom_core::{KeyboardDetail, KeyboardModifiers, MouseButton, MouseDetail};

/// Translate a crossterm `KeyEvent` into a substrate
/// [`KeyboardDetail`]. The `key` field is the DOM-faithful
/// `KeyboardEvent.key` value; modifiers flatten crossterm's
/// bitflags into the four-bool DOM shape; `repeat` is set for
/// `KeyEventKind::Repeat` only.
pub fn translate_key_event(ev: KeyEvent) -> KeyboardDetail {
    KeyboardDetail {
        key: key_code_to_string(ev.code),
        modifiers: translate_modifiers(ev.modifiers),
        repeat: matches!(ev.kind, KeyEventKind::Repeat),
    }
}

/// Map a crossterm [`KeyCode`] to its DOM `KeyboardEvent.key`
/// string. **Exhaustive** — adding a variant to crossterm fails
/// the build until a mapping is chosen here.
pub fn key_code_to_string(code: KeyCode) -> String {
    match code {
        KeyCode::Backspace => "Backspace".into(),
        KeyCode::Enter => "Enter".into(),
        KeyCode::Left => "ArrowLeft".into(),
        KeyCode::Right => "ArrowRight".into(),
        KeyCode::Up => "ArrowUp".into(),
        KeyCode::Down => "ArrowDown".into(),
        KeyCode::Home => "Home".into(),
        KeyCode::End => "End".into(),
        KeyCode::PageUp => "PageUp".into(),
        KeyCode::PageDown => "PageDown".into(),
        KeyCode::Tab => "Tab".into(),
        KeyCode::BackTab => "Tab".into(),
        KeyCode::Delete => "Delete".into(),
        KeyCode::Insert => "Insert".into(),
        KeyCode::F(n) => format!("F{n}"),
        KeyCode::Char(c) => c.to_string(),
        KeyCode::Null => "Unidentified".into(),
        KeyCode::Esc => "Escape".into(),
        KeyCode::CapsLock => "CapsLock".into(),
        KeyCode::ScrollLock => "ScrollLock".into(),
        KeyCode::NumLock => "NumLock".into(),
        KeyCode::PrintScreen => "PrintScreen".into(),
        KeyCode::Pause => "Pause".into(),
        KeyCode::Menu => "ContextMenu".into(),
        KeyCode::KeypadBegin => "Clear".into(),
        KeyCode::Media(m) => media_key_to_string(m).into(),
        KeyCode::Modifier(m) => modifier_key_to_string(m).into(),
    }
}

fn media_key_to_string(m: MediaKeyCode) -> &'static str {
    match m {
        MediaKeyCode::Play => "MediaPlay",
        MediaKeyCode::Pause => "MediaPause",
        MediaKeyCode::PlayPause => "MediaPlayPause",
        MediaKeyCode::Reverse => "MediaRewind",
        MediaKeyCode::Stop => "MediaStop",
        MediaKeyCode::FastForward => "MediaFastForward",
        MediaKeyCode::Rewind => "MediaRewind",
        MediaKeyCode::TrackNext => "MediaTrackNext",
        MediaKeyCode::TrackPrevious => "MediaTrackPrevious",
        MediaKeyCode::Record => "MediaRecord",
        MediaKeyCode::LowerVolume => "AudioVolumeDown",
        MediaKeyCode::RaiseVolume => "AudioVolumeUp",
        MediaKeyCode::MuteVolume => "AudioVolumeMute",
    }
}

fn modifier_key_to_string(m: ModifierKeyCode) -> &'static str {
    match m {
        ModifierKeyCode::LeftShift | ModifierKeyCode::RightShift => "Shift",
        ModifierKeyCode::LeftControl | ModifierKeyCode::RightControl => "Control",
        ModifierKeyCode::LeftAlt | ModifierKeyCode::RightAlt => "Alt",
        ModifierKeyCode::LeftSuper | ModifierKeyCode::RightSuper => "Super",
        ModifierKeyCode::LeftHyper | ModifierKeyCode::RightHyper => "Hyper",
        ModifierKeyCode::LeftMeta | ModifierKeyCode::RightMeta => "Meta",
        ModifierKeyCode::IsoLevel3Shift | ModifierKeyCode::IsoLevel5Shift => "AltGraph",
    }
}

/// Flatten crossterm's modifier bitflags into the four-boolean
/// DOM shape (`ctrlKey` / `shiftKey` / `altKey` / `metaKey`).
/// Crossterm's `SUPER` and `META` flags both feed `meta` —
/// macOS terminals report Command as SUPER on most terminals,
/// and the platform distinction isn't useful at the substrate
/// level.
pub fn translate_modifiers(m: CtKeyModifiers) -> KeyboardModifiers {
    KeyboardModifiers {
        ctrl: m.contains(CtKeyModifiers::CONTROL),
        shift: m.contains(CtKeyModifiers::SHIFT),
        alt: m.contains(CtKeyModifiers::ALT),
        meta: m.contains(CtKeyModifiers::SUPER) || m.contains(CtKeyModifiers::META),
    }
}

/// Translate a crossterm `MouseEvent` into a substrate
/// [`MouseDetail`]. Cell-grained coordinates flow through to
/// `client_x` / `client_y` (DOM `MouseEvent.clientX/Y`); the
/// button-that-transitioned + held-buttons bitmask come from
/// [`mouse_kind_to_button`].
pub fn translate_mouse_event(ev: MouseEvent) -> MouseDetail {
    let (button, buttons) = mouse_kind_to_button(ev.kind);
    let (delta_x, delta_y) = wheel_delta(ev.kind);
    MouseDetail {
        button,
        buttons,
        client_x: ev.column as i32,
        client_y: ev.row as i32,
        delta_x,
        delta_y,
        modifiers: translate_modifiers(ev.modifiers),
    }
}

/// Wheel `(delta_x, delta_y)` in cell units — `0` for non-wheel
/// events, `±1` per scroll tick (DOM `WheelEvent` uses pixels;
/// terminals are cell-grained so we report ticks as units).
fn wheel_delta(kind: MouseEventKind) -> (i32, i32) {
    match kind {
        MouseEventKind::ScrollUp => (0, -1),
        MouseEventKind::ScrollDown => (0, 1),
        MouseEventKind::ScrollLeft => (-1, 0),
        MouseEventKind::ScrollRight => (1, 0),
        MouseEventKind::Down(_)
        | MouseEventKind::Up(_)
        | MouseEventKind::Drag(_)
        | MouseEventKind::Moved => (0, 0),
    }
}

/// Map a crossterm [`MouseEventKind`] to `(button, buttons-bitmask)`.
/// `button` is the button that transitioned (for press / release /
/// drag); for move and scroll events it defaults to
/// [`MouseButton::Left`] — the DOM "main button" sentinel for `0`.
/// `buttons` reflects what's currently held (bit 0 = Left, bit 1 =
/// Right, bit 2 = Middle) — crossterm doesn't track multi-button
/// state, so `Up` reports `0` (released) and other events report
/// just the bit for the transitioning button.
fn mouse_kind_to_button(kind: MouseEventKind) -> (MouseButton, u8) {
    match kind {
        MouseEventKind::Down(b) => (translate_mouse_button(b), button_bit(b)),
        MouseEventKind::Up(b) => (translate_mouse_button(b), 0),
        MouseEventKind::Drag(b) => (translate_mouse_button(b), button_bit(b)),
        MouseEventKind::Moved
        | MouseEventKind::ScrollUp
        | MouseEventKind::ScrollDown
        | MouseEventKind::ScrollLeft
        | MouseEventKind::ScrollRight => (MouseButton::Left, 0),
    }
}

fn translate_mouse_button(b: CtMouseButton) -> MouseButton {
    match b {
        CtMouseButton::Left => MouseButton::Left,
        CtMouseButton::Middle => MouseButton::Middle,
        CtMouseButton::Right => MouseButton::Right,
    }
}

/// DOM `MouseEvent.buttons` bitmask: bit 0 = Left, bit 1 = Right,
/// bit 2 = Middle.
fn button_bit(b: CtMouseButton) -> u8 {
    match b {
        CtMouseButton::Left => 1,   // bit 0
        CtMouseButton::Right => 2,  // bit 1
        CtMouseButton::Middle => 4, // bit 2
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyEventKind, KeyEventState};

    fn key(code: KeyCode, modifiers: CtKeyModifiers) -> KeyEvent {
        KeyEvent {
            code,
            modifiers,
            kind: KeyEventKind::Press,
            state: KeyEventState::empty(),
        }
    }

    // ── Named key strings ─────────────────────────────────────────────

    #[test]
    fn navigation_keys() {
        assert_eq!(key_code_to_string(KeyCode::Enter), "Enter");
        assert_eq!(key_code_to_string(KeyCode::Esc), "Escape");
        assert_eq!(key_code_to_string(KeyCode::Tab), "Tab");
        assert_eq!(key_code_to_string(KeyCode::BackTab), "Tab");
        assert_eq!(key_code_to_string(KeyCode::Backspace), "Backspace");
        assert_eq!(key_code_to_string(KeyCode::Delete), "Delete");
        assert_eq!(key_code_to_string(KeyCode::Insert), "Insert");
    }

    #[test]
    fn arrow_keys() {
        assert_eq!(key_code_to_string(KeyCode::Up), "ArrowUp");
        assert_eq!(key_code_to_string(KeyCode::Down), "ArrowDown");
        assert_eq!(key_code_to_string(KeyCode::Left), "ArrowLeft");
        assert_eq!(key_code_to_string(KeyCode::Right), "ArrowRight");
    }

    #[test]
    fn paging_keys() {
        assert_eq!(key_code_to_string(KeyCode::Home), "Home");
        assert_eq!(key_code_to_string(KeyCode::End), "End");
        assert_eq!(key_code_to_string(KeyCode::PageUp), "PageUp");
        assert_eq!(key_code_to_string(KeyCode::PageDown), "PageDown");
    }

    #[test]
    fn lock_keys() {
        assert_eq!(key_code_to_string(KeyCode::CapsLock), "CapsLock");
        assert_eq!(key_code_to_string(KeyCode::NumLock), "NumLock");
        assert_eq!(key_code_to_string(KeyCode::ScrollLock), "ScrollLock");
    }

    #[test]
    fn screen_and_pause_keys() {
        assert_eq!(key_code_to_string(KeyCode::PrintScreen), "PrintScreen");
        assert_eq!(key_code_to_string(KeyCode::Pause), "Pause");
        assert_eq!(key_code_to_string(KeyCode::Menu), "ContextMenu");
        assert_eq!(key_code_to_string(KeyCode::KeypadBegin), "Clear");
    }

    #[test]
    fn null_is_unidentified() {
        assert_eq!(key_code_to_string(KeyCode::Null), "Unidentified");
    }

    #[test]
    fn f_keys_one_through_twelve() {
        for n in 1..=12u8 {
            assert_eq!(key_code_to_string(KeyCode::F(n)), format!("F{n}"));
        }
    }

    #[test]
    fn f_keys_high_numbers() {
        // F13–F24 also valid in DOM; crossterm reports them too.
        assert_eq!(key_code_to_string(KeyCode::F(24)), "F24");
    }

    #[test]
    fn printable_chars() {
        assert_eq!(key_code_to_string(KeyCode::Char('a')), "a");
        assert_eq!(key_code_to_string(KeyCode::Char('A')), "A");
        assert_eq!(key_code_to_string(KeyCode::Char('1')), "1");
        assert_eq!(key_code_to_string(KeyCode::Char('!')), "!");
        assert_eq!(key_code_to_string(KeyCode::Char(' ')), " ");
        assert_eq!(key_code_to_string(KeyCode::Char('é')), "é");
    }

    // ── Media keys ────────────────────────────────────────────────────

    #[test]
    fn media_keys() {
        use MediaKeyCode::*;
        assert_eq!(key_code_to_string(KeyCode::Media(Play)), "MediaPlay");
        assert_eq!(key_code_to_string(KeyCode::Media(Pause)), "MediaPause");
        assert_eq!(
            key_code_to_string(KeyCode::Media(PlayPause)),
            "MediaPlayPause"
        );
        assert_eq!(key_code_to_string(KeyCode::Media(Stop)), "MediaStop");
        assert_eq!(
            key_code_to_string(KeyCode::Media(FastForward)),
            "MediaFastForward"
        );
        assert_eq!(key_code_to_string(KeyCode::Media(Rewind)), "MediaRewind");
        // Reverse collapses to Rewind — documented divergence.
        assert_eq!(key_code_to_string(KeyCode::Media(Reverse)), "MediaRewind");
        assert_eq!(
            key_code_to_string(KeyCode::Media(TrackNext)),
            "MediaTrackNext"
        );
        assert_eq!(
            key_code_to_string(KeyCode::Media(TrackPrevious)),
            "MediaTrackPrevious"
        );
        assert_eq!(key_code_to_string(KeyCode::Media(Record)), "MediaRecord");
        assert_eq!(
            key_code_to_string(KeyCode::Media(LowerVolume)),
            "AudioVolumeDown"
        );
        assert_eq!(
            key_code_to_string(KeyCode::Media(RaiseVolume)),
            "AudioVolumeUp"
        );
        assert_eq!(
            key_code_to_string(KeyCode::Media(MuteVolume)),
            "AudioVolumeMute"
        );
    }

    // ── Modifier-only keys ────────────────────────────────────────────

    #[test]
    fn modifier_only_keys_collapse_left_and_right() {
        use ModifierKeyCode::*;
        assert_eq!(key_code_to_string(KeyCode::Modifier(LeftShift)), "Shift");
        assert_eq!(key_code_to_string(KeyCode::Modifier(RightShift)), "Shift");
        assert_eq!(
            key_code_to_string(KeyCode::Modifier(LeftControl)),
            "Control"
        );
        assert_eq!(
            key_code_to_string(KeyCode::Modifier(RightControl)),
            "Control"
        );
        assert_eq!(key_code_to_string(KeyCode::Modifier(LeftAlt)), "Alt");
        assert_eq!(key_code_to_string(KeyCode::Modifier(RightAlt)), "Alt");
        assert_eq!(key_code_to_string(KeyCode::Modifier(LeftSuper)), "Super");
        assert_eq!(key_code_to_string(KeyCode::Modifier(RightSuper)), "Super");
        assert_eq!(key_code_to_string(KeyCode::Modifier(LeftHyper)), "Hyper");
        assert_eq!(key_code_to_string(KeyCode::Modifier(RightHyper)), "Hyper");
        assert_eq!(key_code_to_string(KeyCode::Modifier(LeftMeta)), "Meta");
        assert_eq!(key_code_to_string(KeyCode::Modifier(RightMeta)), "Meta");
        assert_eq!(
            key_code_to_string(KeyCode::Modifier(IsoLevel3Shift)),
            "AltGraph"
        );
        assert_eq!(
            key_code_to_string(KeyCode::Modifier(IsoLevel5Shift)),
            "AltGraph"
        );
    }

    // ── Modifier bitflag translation ──────────────────────────────────

    #[test]
    fn modifiers_default_when_no_flags() {
        let m = translate_modifiers(CtKeyModifiers::empty());
        assert!(!m.ctrl && !m.shift && !m.alt && !m.meta);
    }

    #[test]
    fn modifiers_each_flag() {
        let m = translate_modifiers(CtKeyModifiers::CONTROL);
        assert!(m.ctrl && !m.shift && !m.alt && !m.meta);
        let m = translate_modifiers(CtKeyModifiers::SHIFT);
        assert!(!m.ctrl && m.shift && !m.alt && !m.meta);
        let m = translate_modifiers(CtKeyModifiers::ALT);
        assert!(!m.ctrl && !m.shift && m.alt && !m.meta);
    }

    #[test]
    fn modifiers_super_and_meta_both_feed_meta() {
        // macOS Command is reported as SUPER on most terminals.
        let from_super = translate_modifiers(CtKeyModifiers::SUPER);
        assert!(from_super.meta);
        let from_meta = translate_modifiers(CtKeyModifiers::META);
        assert!(from_meta.meta);
        let both = translate_modifiers(CtKeyModifiers::SUPER | CtKeyModifiers::META);
        assert!(both.meta);
    }

    #[test]
    fn modifiers_combined() {
        let m = translate_modifiers(CtKeyModifiers::CONTROL | CtKeyModifiers::SHIFT);
        assert!(m.ctrl && m.shift && !m.alt && !m.meta);
    }

    // ── Repeat flag ──────────────────────────────────────────────────

    #[test]
    fn repeat_unset_when_press_kind() {
        let d = translate_key_event(key(KeyCode::Char('a'), CtKeyModifiers::empty()));
        assert!(!d.repeat);
    }

    #[test]
    fn repeat_set_when_repeat_kind() {
        let mut k = key(KeyCode::Char('a'), CtKeyModifiers::empty());
        k.kind = KeyEventKind::Repeat;
        let d = translate_key_event(k);
        assert!(d.repeat);
    }

    #[test]
    fn repeat_unset_when_release_kind() {
        let mut k = key(KeyCode::Char('a'), CtKeyModifiers::empty());
        k.kind = KeyEventKind::Release;
        let d = translate_key_event(k);
        assert!(!d.repeat, "Release isn't a repeat");
    }

    // ── End-to-end key translation ────────────────────────────────────

    #[test]
    fn translate_enter_with_ctrl() {
        let d = translate_key_event(key(KeyCode::Enter, CtKeyModifiers::CONTROL));
        assert_eq!(d.key, "Enter");
        assert!(d.modifiers.ctrl);
        assert!(!d.modifiers.shift);
    }

    #[test]
    fn translate_backtab_reports_shifted_tab() {
        // BackTab is the wire-level encoding of Shift+Tab. Browsers
        // report this as `key = "Tab"` + `shiftKey = true`. Our
        // translation gives `key = "Tab"` but doesn't synthesize
        // the shift modifier — the wire format carries Shift in
        // the modifiers bits already.
        let d = translate_key_event(key(KeyCode::BackTab, CtKeyModifiers::SHIFT));
        assert_eq!(d.key, "Tab");
        assert!(d.modifiers.shift);
    }

    // ── MouseEvent translation ────────────────────────────────────────

    fn mouse(kind: MouseEventKind, column: u16, row: u16, m: CtKeyModifiers) -> MouseEvent {
        MouseEvent {
            kind,
            column,
            row,
            modifiers: m,
        }
    }

    #[test]
    fn mouse_down_left_sets_bit_zero() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Down(CtMouseButton::Left),
            5,
            3,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.button, MouseButton::Left);
        assert_eq!(d.buttons, 1);
        assert_eq!(d.client_x, 5);
        assert_eq!(d.client_y, 3);
        assert!(!d.modifiers.ctrl);
    }

    #[test]
    fn mouse_down_right_sets_bit_one() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Down(CtMouseButton::Right),
            0,
            0,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.button, MouseButton::Right);
        assert_eq!(d.buttons, 2);
    }

    #[test]
    fn mouse_down_middle_sets_bit_two() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Down(CtMouseButton::Middle),
            0,
            0,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.button, MouseButton::Middle);
        assert_eq!(d.buttons, 4);
    }

    #[test]
    fn mouse_up_clears_buttons() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Up(CtMouseButton::Left),
            0,
            0,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.button, MouseButton::Left);
        assert_eq!(d.buttons, 0);
    }

    #[test]
    fn mouse_drag_carries_button_bit() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Drag(CtMouseButton::Left),
            7,
            2,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.button, MouseButton::Left);
        assert_eq!(d.buttons, 1);
    }

    #[test]
    fn mouse_moved_defaults_to_left_no_bits() {
        let d = translate_mouse_event(mouse(MouseEventKind::Moved, 10, 4, CtKeyModifiers::empty()));
        assert_eq!(d.button, MouseButton::Left);
        assert_eq!(d.buttons, 0);
    }

    #[test]
    fn mouse_scroll_directions_default_to_left() {
        for kind in [
            MouseEventKind::ScrollUp,
            MouseEventKind::ScrollDown,
            MouseEventKind::ScrollLeft,
            MouseEventKind::ScrollRight,
        ] {
            let d = translate_mouse_event(mouse(kind, 0, 0, CtKeyModifiers::empty()));
            assert_eq!(d.button, MouseButton::Left);
            assert_eq!(d.buttons, 0);
        }
    }

    #[test]
    fn mouse_scroll_deltas_match_direction() {
        let cases = [
            (MouseEventKind::ScrollUp, (0, -1)),
            (MouseEventKind::ScrollDown, (0, 1)),
            (MouseEventKind::ScrollLeft, (-1, 0)),
            (MouseEventKind::ScrollRight, (1, 0)),
        ];
        for (kind, (dx, dy)) in cases {
            let d = translate_mouse_event(mouse(kind, 0, 0, CtKeyModifiers::empty()));
            assert_eq!(d.delta_x, dx);
            assert_eq!(d.delta_y, dy);
        }
    }

    #[test]
    fn non_wheel_events_have_zero_deltas() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Down(CtMouseButton::Left),
            0,
            0,
            CtKeyModifiers::empty(),
        ));
        assert_eq!(d.delta_x, 0);
        assert_eq!(d.delta_y, 0);
    }

    #[test]
    fn mouse_modifiers_propagate() {
        let d = translate_mouse_event(mouse(
            MouseEventKind::Down(CtMouseButton::Left),
            0,
            0,
            CtKeyModifiers::CONTROL | CtKeyModifiers::SHIFT,
        ));
        assert!(d.modifiers.ctrl);
        assert!(d.modifiers.shift);
        assert!(!d.modifiers.alt);
    }
}