quadraui 0.6.1

Cross-platform Rust UI toolkit: write one AppLogic and run it unmodified on TUI, GTK4 (Linux), macOS, and Windows backends
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
//! Direct2D / DirectWrite rasteriser for [`crate::DataTable`].
//!
//! Painting uses the shared [`crate::primitives::data_table::native_surface_paint::paint`].
//! See that function's documentation for the full per-backend divergence survey.
//! This module now uses the shared, translucent
//! [`crate::primitives::scrollbar::native_surface_paint::paint`] for scrollbars
//! and respects each footer span's foreground colour.
//! [`draw_data_table`] is a thin wrapper over the shared paint, using
//! [`crate::win::surface::D2dSurface`] as the `PaintSurface` adapter.
//!
//! [`win_data_table_layout`] is pure geometry generic over
//! [`crate::primitives::layout_metrics::TextMeasure`] — no Direct2D/
//! DirectWrite type in its signature — so it compiles and runs
//! everywhere. Only [`draw_data_table`] is `#[cfg(target_os = "windows")]`-gated.
//! See `win::status_bar`'s module doc for why colours come from `Theme::default()`
//! rather than a live `WinBackend` theme field.

#[cfg(target_os = "windows")]
use windows::Win32::Graphics::Direct2D::ID2D1RenderTarget;

use crate::event::Rect;
use crate::primitives::data_table::DataTable;
#[cfg(target_os = "windows")]
use crate::primitives::data_table::DataTableGlyphs;
use crate::primitives::layout_metrics::{pixel_data_table_layout, TextMeasure};
#[cfg(target_os = "windows")]
use crate::theme::Theme;
use crate::DataTableLayout;

/// Compute a [`DataTable`]'s layout without painting — the DirectWrite
/// twin of [`draw_data_table`]'s internal layout call. Pure geometry over
/// [`TextMeasure`] — `measure` may be a live `&DWrite` (when
/// painting) or [`super::backend`]'s nominal measurer (no surface yet).
/// Shares its column-measurement path with `gtk_data_table_layout` /
/// `mac_data_table_layout` via [`pixel_data_table_layout`].
pub fn win_data_table_layout(
    measure: &dyn TextMeasure,
    rect: Rect,
    table: &DataTable,
    line_height: f32,
) -> DataTableLayout {
    pixel_data_table_layout(table, rect.width, rect.height, line_height, measure)
}

/// Draw a [`DataTable`] into `rect` (DIPs) on `target`. Returns the
/// resolved [`DataTableLayout`] for host click dispatch.
///
/// `hovered_idx` tints the hovered body row (skipped when it's also
/// the selected row). See
/// [`crate::primitives::data_table::native_surface_paint::paint`]'s doc
/// for the full visual contract this now shares with
/// `macos::data_table::draw_data_table`.
#[cfg(target_os = "windows")]
pub fn draw_data_table(
    target: &ID2D1RenderTarget,
    dwrite: &super::text::DWrite,
    rect: Rect,
    table: &DataTable,
    line_height: f32,
    hovered_idx: Option<usize>,
) -> DataTableLayout {
    draw_data_table_with_glyphs(
        target,
        dwrite,
        rect,
        table,
        line_height,
        hovered_idx,
        None,
        false,
    )
}

/// Identical to [`draw_data_table`], plus an optional [`DataTableGlyphs`]
/// override for the sort-direction glyphs and the `nerd_fonts_enabled`
/// flag that selects each override [`crate::types::Icon`]'s `glyph` vs.
/// `fallback` form. `glyphs: None` renders byte-for-byte the same as
/// [`draw_data_table`].
#[cfg(target_os = "windows")]
#[allow(clippy::too_many_arguments)]
pub fn draw_data_table_with_glyphs(
    target: &ID2D1RenderTarget,
    dwrite: &super::text::DWrite,
    rect: Rect,
    table: &DataTable,
    line_height: f32,
    hovered_idx: Option<usize>,
    glyphs: Option<&DataTableGlyphs>,
    nerd_fonts_enabled: bool,
) -> DataTableLayout {
    let theme = Theme::default();
    let mut surface = super::surface::D2dSurface {
        target,
        dwrite: Some(dwrite),
    };
    crate::primitives::data_table::native_surface_paint::paint_with_glyphs(
        table,
        &mut surface,
        &theme,
        rect,
        line_height,
        hovered_idx,
        glyphs,
        nerd_fonts_enabled,
    )
}

// Tests paint through a real `DWrite`/`HeadlessSurface`.
#[cfg(all(test, target_os = "windows"))]
mod tests {
    use super::*;
    use crate::primitives::data_table::{Column, ColumnAlign, ColumnWidth, DataRow, DataTableHit};
    use crate::types::{Decoration, StyledText, WidgetId};
    use crate::win::testing::HeadlessSurface;
    use crate::win::text::DWrite;

    const W: f32 = 300.0;
    const H: f32 = 100.0;
    const LINE_HEIGHT: f32 = 16.0;

    fn table(rows: Vec<DataRow>) -> DataTable {
        DataTable {
            id: WidgetId::new("table"),
            columns: vec![
                Column {
                    title: "Name".into(),
                    width: ColumnWidth::Flex(1.0),
                    align: ColumnAlign::Left,
                },
                Column {
                    title: "Size".into(),
                    width: ColumnWidth::Flex(1.0),
                    align: ColumnAlign::Right,
                },
            ],
            rows,
            selected_idx: Some(0),
            scroll_offset: 0,
            sort: None,
            has_focus: true,
            show_scrollbar: false,
            min_total_width: None,
            h_scroll: 0.0,
            column_overrides: Vec::new(),
            footer: None,
        }
    }

    fn row(name: &str, size: &str) -> DataRow {
        DataRow {
            cells: vec![
                StyledText::plain(name.to_string()),
                StyledText::plain(size.to_string()),
            ],
            decoration: Decoration::Normal,
        }
    }

    /// Paint↔click round trip: the selected row's blended background
    /// must be painted at its own bounds, and clicking each row/header
    /// resolves to the expected `DataTableHit`.
    #[test]
    fn paint_and_hit_test_round_trip() {
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let table = table(vec![
            row("a.txt", "1kb"),
            row("b.txt", "2kb"),
            row("c.txt", "3kb"),
        ]);
        let rect = Rect::new(0.0, 0.0, W, H);

        let layout = surface
            .paint(|target| {
                draw_data_table(target, &dwrite, rect, &table, LINE_HEIGHT, None);
            })
            .map(|_| win_data_table_layout(&dwrite, rect, &table, LINE_HEIGHT))
            .expect("paint data table");

        // Header click.
        let hit = layout.hit_test(1.0, 1.0, table.scroll_offset, table.rows.len());
        assert_eq!(hit, DataTableHit::Header { col: 0 });

        // Row click (row 1, below the header).
        let row_y = layout.header_height + line_height_mid(LINE_HEIGHT);
        let hit = layout.hit_test(1.0, row_y, table.scroll_offset, table.rows.len());
        assert_eq!(hit, DataTableHit::Row { idx: 0 });

        // Selected row (idx 0) painted a blended background distinct
        // from the plain body background.
        let theme = Theme::default();
        let sample_y = (layout.header_height + 2.0) as u32;
        let px = surface.pixel_at(2, sample_y);
        assert_ne!(
            (px.r, px.g, px.b),
            (theme.background.r, theme.background.g, theme.background.b),
            "selected row should paint a tinted background distinct from the plain body bg"
        );
    }

    fn line_height_mid(h: f32) -> f32 {
        h / 2.0
    }

    /// Scroll-offset round trip: with `scroll_offset = 1`, a click on
    /// the first body row resolves to row index 1.
    #[test]
    fn scroll_offset_hit_test_agrees() {
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let mut t = table(vec![row("a", "1"), row("b", "2"), row("c", "3")]);
        t.scroll_offset = 1;
        let rect = Rect::new(0.0, 0.0, W, H);
        let layout = win_data_table_layout(&dwrite, rect, &t, LINE_HEIGHT);
        let row_y = layout.header_height + line_height_mid(LINE_HEIGHT);
        let hit = layout.hit_test(1.0, row_y, t.scroll_offset, t.rows.len());
        assert_eq!(hit, DataTableHit::Row { idx: 1 });
    }

    /// No-paint layout must agree byte-for-byte with what
    /// `draw_data_table` painted.
    #[test]
    fn no_paint_layout_matches_paint_layout() {
        let table = table(vec![row("a.txt", "1kb"), row("b.txt", "2kb")]);
        let rect = Rect::new(0.0, 0.0, W, H);
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");

        let painted = surface
            .paint(|target| {
                draw_data_table(target, &dwrite, rect, &table, LINE_HEIGHT, None);
            })
            .map(|_| win_data_table_layout(&dwrite, rect, &table, LINE_HEIGHT))
            .expect("paint");
        let no_paint = win_data_table_layout(&dwrite, rect, &table, LINE_HEIGHT);
        assert_eq!(painted, no_paint);
    }

    /// Footer spans' foreground colours are respected, not discarded
    /// in favour of the theme's default foreground.
    #[test]
    fn footer_span_colour_overrides_the_default_foreground() {
        use crate::types::{Color, StyledSpan};

        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let mut t = table(vec![row("a.txt", "1kb")]);
        t.footer = Some(DataRow {
            cells: vec![
                StyledText {
                    spans: vec![StyledSpan {
                        text: "Total".into(),
                        fg: Some(Color::rgb(255, 0, 0)),
                        bg: None,
                        bold: false,
                        italic: false,
                        underline: false,
                    }],
                },
                StyledText::plain("3"),
            ],
            decoration: Decoration::Normal,
        });
        let rect = Rect::new(0.0, 0.0, W, H);
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        surface
            .fill_rect(rect, Color::rgb(255, 255, 255))
            .expect("fill white bg");
        surface
            .paint(|target| {
                draw_data_table(target, &dwrite, rect, &t, LINE_HEIGHT, None);
            })
            .expect("paint data table");

        let layout = win_data_table_layout(&dwrite, rect, &t, LINE_HEIGHT);
        let footer_top = (H - layout.footer_height).max(0.0) as u32;

        let mut found_red = false;
        for y in footer_top..(H as u32) {
            for x in 0..(W as u32) {
                let px = surface.pixel_at(x, y);
                if px.r > 200 && px.g < 80 && px.b < 80 {
                    found_red = true;
                }
            }
        }
        assert!(
            found_red,
            "footer 'Total' span should paint in its own red fg, not \
             the pre-#1084 whole-cell `theme.foreground`"
        );
    }

    /// An app-supplied [`DataTableGlyphs`] override swaps the sort-direction
    /// glyph the Windows rasteriser paints; with `glyphs: None`,
    /// [`draw_data_table_with_glyphs`] paints the same codicon default as
    /// [`draw_data_table`].
    #[test]
    fn sort_glyph_override_paints_app_glyph_default_paints_codicon() {
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let mut t = table(vec![row("a.txt", "1kb")]);
        t.sort = Some((0, crate::primitives::data_table::SortDirection::Ascending));
        let rect = Rect::new(0.0, 0.0, W, H);

        let glyphs = DataTableGlyphs::new().with_sort_ascending_glyph("^");
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let previous = crate::testing::install_text_run_sink();
        surface
            .paint(|target| {
                draw_data_table_with_glyphs(
                    target,
                    &dwrite,
                    rect,
                    &t,
                    LINE_HEIGHT,
                    None,
                    Some(&glyphs),
                    true,
                );
            })
            .expect("paint with glyph override");
        let runs = crate::testing::take_text_run_sink(previous);
        assert!(
            runs.iter().any(|r| r.text.contains("Name ^")),
            "expected the overridden sort glyph among painted runs, got {:?}",
            runs.iter().map(|r| r.text.as_str()).collect::<Vec<_>>()
        );
        assert!(!runs
            .iter()
            .any(|r| r.text.contains(crate::codicon::SORT_ASCENDING)));

        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let previous = crate::testing::install_text_run_sink();
        surface
            .paint(|target| {
                draw_data_table_with_glyphs(
                    target,
                    &dwrite,
                    rect,
                    &t,
                    LINE_HEIGHT,
                    None,
                    None,
                    true,
                );
            })
            .expect("paint with no override");
        let runs = crate::testing::take_text_run_sink(previous);
        assert!(
            runs.iter()
                .any(|r| r.text.contains(crate::codicon::SORT_ASCENDING)),
            "expected the default codicon sort glyph among painted runs, got {:?}",
            runs.iter().map(|r| r.text.as_str()).collect::<Vec<_>>()
        );
    }

    /// Drives the override through [`crate::backend::Backend`]'s
    /// `draw_data_table_with_glyphs` method on [`crate::win::backend::WinBackend`]
    /// — the actual deliverable this module's free functions are wired
    /// behind — rather than calling [`draw_data_table_with_glyphs`] (the
    /// free fn) directly, the way the two tests above do.
    #[test]
    fn backend_trait_sort_glyph_override_paints_app_glyph_default_paints_codicon() {
        use crate::backend::Backend;
        use crate::win::backend::WinBackend;

        let mut t = table(vec![row("a.txt", "1kb")]);
        t.sort = Some((0, crate::primitives::data_table::SortDirection::Ascending));
        let rect = Rect::new(0.0, 0.0, W, H);

        let paint = |glyphs: Option<&DataTableGlyphs>| {
            let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
            let mut backend = WinBackend::new();
            backend
                .attach_headless(surface.target().clone(), W as u32, H as u32)
                .expect("attach headless surface");
            backend.set_painted_text_recording(true);
            backend.begin_frame(crate::event::Viewport::new(W, H, 1.0));
            let _ = backend.draw_data_table_with_glyphs(rect, &t, None, glyphs);
            backend.end_frame();
            backend.text_runs().to_vec()
        };

        let glyphs = DataTableGlyphs::new().with_sort_ascending_glyph("^");
        let overridden = paint(Some(&glyphs));
        assert!(
            overridden.iter().any(|r| r.text.contains("Name ^")),
            "expected the overridden sort glyph among painted runs, got {:?}",
            overridden
                .iter()
                .map(|r| r.text.as_str())
                .collect::<Vec<_>>()
        );
        assert!(!overridden
            .iter()
            .any(|r| r.text.contains(crate::codicon::SORT_ASCENDING)));

        let default = paint(None);
        assert!(
            default
                .iter()
                .any(|r| r.text.contains(crate::codicon::SORT_ASCENDING)),
            "expected the default codicon sort glyph among painted runs, got {:?}",
            default.iter().map(|r| r.text.as_str()).collect::<Vec<_>>()
        );
    }

    /// Same as [`sort_glyph_override_paints_app_glyph_default_paints_codicon`]
    /// for [`crate::primitives::data_table::SortDirection::Descending`] —
    /// `with_sort_ascending_glyph` and `with_sort_descending_glyph` are
    /// independent override slots, so the ascending-only test above never
    /// exercises this one.
    #[test]
    fn sort_descending_glyph_override_paints_app_glyph_default_paints_codicon() {
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let mut t = table(vec![row("a.txt", "1kb")]);
        t.sort = Some((0, crate::primitives::data_table::SortDirection::Descending));
        let rect = Rect::new(0.0, 0.0, W, H);

        let glyphs = DataTableGlyphs::new().with_sort_descending_glyph("v");
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let previous = crate::testing::install_text_run_sink();
        surface
            .paint(|target| {
                draw_data_table_with_glyphs(
                    target,
                    &dwrite,
                    rect,
                    &t,
                    LINE_HEIGHT,
                    None,
                    Some(&glyphs),
                    true,
                );
            })
            .expect("paint with glyph override");
        let runs = crate::testing::take_text_run_sink(previous);
        assert!(
            runs.iter().any(|r| r.text.contains("Name v")),
            "expected the overridden sort glyph among painted runs, got {:?}",
            runs.iter().map(|r| r.text.as_str()).collect::<Vec<_>>()
        );
        assert!(!runs
            .iter()
            .any(|r| r.text.contains(crate::codicon::SORT_DESCENDING)));

        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let previous = crate::testing::install_text_run_sink();
        surface
            .paint(|target| {
                draw_data_table_with_glyphs(
                    target,
                    &dwrite,
                    rect,
                    &t,
                    LINE_HEIGHT,
                    None,
                    None,
                    true,
                );
            })
            .expect("paint with no override");
        let runs = crate::testing::take_text_run_sink(previous);
        assert!(
            runs.iter()
                .any(|r| r.text.contains(crate::codicon::SORT_DESCENDING)),
            "expected the default codicon sort glyph among painted runs, got {:?}",
            runs.iter().map(|r| r.text.as_str()).collect::<Vec<_>>()
        );
    }
}

/// Multi-select click wiring over [`crate::win::testing::WinDriver`].
///
/// `DataTable` stays purely declarative (no selection-set field of its
/// own — see `crate::types::apply_selection_gesture`'s doc), so the app
/// owns the selected-index set and the gesture anchor, resolves each
/// `MouseDown` to a row via
/// [`crate::primitives::data_table::DataTableLayout::hit_test`], derives
/// the gesture from the event's `Modifiers`, and emits
/// `DataTableEvent::SelectionSet` with the resulting indices. Mirrors
/// `gtk::data_table`'s `multi_select_driver_tests`, except `WinDriver` has
/// no `set_double_click_folding` toggle, so consecutive clicks that must
/// stay independent (rather than fold into a `DoubleClick`) are spaced
/// more than `WIN_DOUBLE_CLICK_RADIUS` DIPs apart on the x axis — row hit
/// testing only depends on y, so this keeps each click landing on its
/// intended row without tripping the shared `DoubleClickDetector`.
#[cfg(all(test, target_os = "windows"))]
mod multi_select_driver_tests {
    use crate::primitives::data_table::ColumnMeasure;
    use crate::runner::{AppLogic, Reaction};
    use crate::win::testing::WinDriver;
    use crate::{
        apply_selection_gesture, Backend, Column, DataRow, DataTable, DataTableEvent, DataTableHit,
        Decoration, Modifiers, MouseButton, Point, Rect, SelectionGesture, StyledText, WidgetId,
    };

    const VIEWPORT_W: f32 = 40.0;
    const VIEWPORT_H: f32 = 10.0;
    const ROW_H: f32 = 1.0;
    const HEADER_H: f32 = 1.0;
    const SCROLLBAR_W: f32 = 1.0;

    fn row(label: &str) -> DataRow {
        DataRow {
            cells: vec![StyledText::plain(label.to_string())],
            decoration: Decoration::Normal,
        }
    }

    fn sample_table() -> DataTable {
        DataTable {
            id: WidgetId::new("dt"),
            columns: vec![Column {
                title: "Name".to_string(),
                width: Default::default(),
                align: Default::default(),
            }],
            rows: (0..5).map(|i| row(&format!("row {i}"))).collect(),
            selected_idx: None,
            scroll_offset: 0,
            sort: None,
            has_focus: true,
            show_scrollbar: false,
            min_total_width: None,
            h_scroll: 0.0,
            column_overrides: vec![],
            footer: None,
        }
    }

    struct TableApp {
        table: DataTable,
        selected: Vec<usize>,
        anchor: usize,
        events: Vec<DataTableEvent>,
    }

    impl TableApp {
        fn new() -> Self {
            Self {
                table: sample_table(),
                selected: Vec::new(),
                anchor: 0,
                events: Vec::new(),
            }
        }
    }

    impl AppLogic for TableApp {
        type AreaId = ();

        fn render(&self, backend: &mut dyn Backend, _area: ()) {
            backend.draw_data_table(
                Rect::new(0.0, 0.0, VIEWPORT_W, VIEWPORT_H),
                &self.table,
                None,
            );
        }

        fn handle(&mut self, event: crate::UiEvent, _backend: &mut dyn Backend) -> Reaction {
            let crate::UiEvent::MouseDown {
                position,
                modifiers,
                ..
            } = event
            else {
                return Reaction::Continue;
            };
            let layout =
                self.table
                    .layout(VIEWPORT_W, VIEWPORT_H, ROW_H, HEADER_H, SCROLLBAR_W, |_| {
                        ColumnMeasure::new(20.0)
                    });
            let hit = layout.hit_test(
                position.x,
                position.y,
                self.table.scroll_offset,
                self.table.rows.len(),
            );
            let idx = match hit {
                DataTableHit::Row { idx } => idx,
                _ => return Reaction::Continue,
            };
            let gesture = SelectionGesture::from_modifiers(modifiers);
            let indices = apply_selection_gesture(&self.selected, self.anchor, idx, gesture);
            self.selected = indices.clone();
            if !matches!(gesture, SelectionGesture::Range) {
                self.anchor = idx;
            }
            self.events.push(DataTableEvent::SelectionSet { indices });
            Reaction::Redraw
        }
    }

    fn mouse_down(x: f32, y: f32, modifiers: Modifiers) -> crate::UiEvent {
        crate::UiEvent::MouseDown {
            widget: None,
            button: MouseButton::Left,
            position: Point::new(x, y),
            modifiers,
        }
    }

    #[test]
    fn plain_click_replaces_selection() {
        let mut driver = WinDriver::new(TableApp::new(), 80, 40);
        driver.dispatch(mouse_down(
            2.0,
            HEADER_H + 2.0 * ROW_H,
            Modifiers::default(),
        ));
        assert_eq!(driver.app().selected, vec![2]);
    }

    #[test]
    fn ctrl_click_toggles_rows_into_a_multi_selection() {
        let mut driver = WinDriver::new(TableApp::new(), 80, 40);
        driver.dispatch(mouse_down(2.0, HEADER_H, Modifiers::default()));
        let ctrl = Modifiers {
            ctrl: true,
            ..Default::default()
        };
        // x is spaced > WIN_DOUBLE_CLICK_RADIUS DIPs from the previous
        // click so the shared `DoubleClickDetector` doesn't fold these
        // back-to-back single clicks into a `DoubleClick`; y alone picks
        // the row.
        driver.dispatch(mouse_down(10.0, HEADER_H + 2.0 * ROW_H, ctrl));
        assert_eq!(driver.app().selected, vec![0, 2]);
        // Ctrl-click the same row again toggles it back off.
        driver.dispatch(mouse_down(18.0, HEADER_H + 2.0 * ROW_H, ctrl));
        assert_eq!(driver.app().selected, vec![0]);
        assert_eq!(
            driver.app().events.last(),
            Some(&DataTableEvent::SelectionSet { indices: vec![0] })
        );
    }

    #[test]
    fn shift_click_selects_the_range_from_the_anchor() {
        let mut driver = WinDriver::new(TableApp::new(), 80, 40);
        driver.dispatch(mouse_down(2.0, HEADER_H, Modifiers::default()));
        let shift = Modifiers {
            shift: true,
            ..Default::default()
        };
        driver.dispatch(mouse_down(10.0, HEADER_H + 3.0 * ROW_H, shift));
        assert_eq!(driver.app().selected, vec![0, 1, 2, 3]);
        assert_eq!(
            driver.app().events.last(),
            Some(&DataTableEvent::SelectionSet {
                indices: vec![0, 1, 2, 3]
            })
        );
    }

    #[test]
    fn cmd_click_toggles_the_same_as_ctrl_click() {
        let mut driver = WinDriver::new(TableApp::new(), 80, 40);
        driver.dispatch(mouse_down(2.0, HEADER_H + ROW_H, Modifiers::default()));
        driver.dispatch(mouse_down(
            10.0,
            HEADER_H + 4.0 * ROW_H,
            Modifiers {
                cmd: true,
                ..Default::default()
            },
        ));
        assert_eq!(driver.app().selected, vec![1, 4]);
    }
}