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
//! Smoke test for [`DataTable`]: a Kubernetes-style pod list.
//!
//! Keys:
//! - `j` / `↓` — select next row
//! - `k` / `↑` — select previous row
//! - `s` — cycle sort column (Name → Status → Age → Restarts → none)
//! - `d` — toggle sort direction
//! - `f` — toggle the pinned footer/summary row
//! - `H` / `←`, `L` / `→` — scroll horizontally by 5 units
//! - `PageDown` / `PageUp` — page down/up a full screen (keyboard
//!   equivalent of clicking the scrollbar track, #828)
//! - `q` / `Esc` — quit
//!
//! Horizontal scrolling only has anywhere to go when the terminal is
//! narrower than the table's `min_total_width` (80 cells) — run this at
//! ~50 columns to exercise it. Clicks stay routed to the column painted
//! under the cursor at any `h_scroll`: `DataTableLayout::hit_test`
//! undoes the same offset the rasteriser applies.

use quadraui::primitives::scrollbar::fit_thumb;
use quadraui::{
    AppLogic, Backend, Color, Column, ColumnAlign, ColumnWidth, DataRow, DataTable, DataTableHit,
    DataTableLayout, InteractionState, Key, NamedKey, Reaction, Rect, SortDirection, StatusBar,
    StatusBarSegment, StyledText, UiEvent, WidgetId,
};

pub struct DataTableApp {
    columns: Vec<Column>,
    selected: Option<usize>,
    scroll_offset: usize,
    sort_col: Option<usize>,
    sort_asc: bool,
    resize_col: Option<usize>,
    /// The table layout as it was at the *start* of the in-progress
    /// divider drag (captured on `HeaderDivider` mouse-down), held fixed
    /// for every `MouseMoved` in that same drag rather than re-fetched
    /// from `table_layout` each move. `DataTableLayout::drag_divider`
    /// computes the last column's new width as `pair - target` where
    /// `pair` comes from `self.columns[col].width + self.columns[last]
    /// .width` — that's only the *true* pre-drag pair the first time it's
    /// called. Re-deriving `self` from the drag's own prior output (this
    /// gesture's evolving `column_overrides`) would feed back in the
    /// *already-dragged* value once the last column has bottomed out and
    /// overflowed, permanently losing how far past the floor it went —
    /// the divider would then fail to retrace its steps on the way back.
    /// Anchoring `self` to the gesture's start avoids that: `col`'s own
    /// `x` and both `col` and the last column's pre-drag widths never
    /// change mid-gesture (only `col` and the last column move), so the
    /// snapshot stays valid for the whole drag and every intermediate
    /// column is still frozen via `column_overrides` exactly as before.
    resize_base: Option<DataTableLayout>,
    /// Per-column width overrides from divider drags, layered on top of
    /// `columns`' declared strategy — mirrors how a real consumer app
    /// drives `DataTable`. Kept separate from `columns[i].width`
    /// deliberately: overriding a column must never rewrite its original
    /// declared strategy, only lay a resolved width on top of it — that
    /// distinction is exactly what `primitives::data_table::resolve_columns`'s
    /// pass 2 has to respect for a `Flex`-declared column.
    column_overrides: Vec<Option<f32>>,
    /// Vertical scrollbar thumb drag: (track_start_y, track_height, thumb_length, grab_offset)
    sb_drag: Option<(f32, f32, f32, f32)>,
    /// Horizontal scrollbar thumb drag: (track_start_x, track_width, thumb_length, grab_offset)
    h_sb_drag: Option<(f32, f32, f32, f32)>,
    h_scroll: f32,
    hovered_idx: Option<usize>,
    /// Toggle for the pinned footer/summary row demo.
    show_footer: bool,
}

impl DataTableApp {
    pub fn new() -> Self {
        Self {
            columns: Self::default_columns(),
            selected: Some(0),
            scroll_offset: 0,
            sort_col: Some(0),
            sort_asc: true,
            resize_col: None,
            resize_base: None,
            column_overrides: Vec::new(),
            sb_drag: None,
            h_sb_drag: None,
            h_scroll: 0.0,
            hovered_idx: None,
            show_footer: true,
        }
    }

    fn default_columns() -> Vec<Column> {
        vec![
            Column {
                title: "Name".into(),
                width: ColumnWidth::Flex(3.0),
                align: ColumnAlign::Left,
            },
            Column {
                title: "Status".into(),
                width: ColumnWidth::Flex(1.5),
                align: ColumnAlign::Left,
            },
            // Flex (not Fixed) so the divider immediately before the last column
            // (Restarts) sits between two Flex-declared columns — the exact shape
            // needed to exercise resize behavior: the *dragged* column (Age) must
            // itself be Flex-declared for `resolve_columns`'s flex-redistribution
            // pass to have a chance of affecting its override.
            Column {
                title: "Age".into(),
                width: ColumnWidth::Flex(0.5),
                align: ColumnAlign::Right,
            },
            Column {
                title: "Restarts".into(),
                width: ColumnWidth::Fixed(10.0),
                align: ColumnAlign::Right,
            },
        ]
    }

    fn rows() -> Vec<DataRow> {
        let pods = [
            ("nginx-7d9b8c66b-x2j4k", "Running", "3d", "0"),
            ("redis-master-0", "Running", "5d", "1"),
            ("api-gateway-5f6c8d-9mn2q", "Running", "1d", "0"),
            ("postgres-0", "Running", "5d", "0"),
            ("worker-batch-7b9c4-kl3m8", "Pending", "2m", "0"),
            ("cert-manager-cainjector-6d4", "Running", "12d", "3"),
            ("coredns-5d78c9869d-abc12", "Running", "12d", "0"),
            ("etcd-controlplane", "Running", "12d", "0"),
            ("kube-apiserver-cp", "Running", "12d", "2"),
            ("kube-scheduler-cp", "Running", "12d", "0"),
            ("ingress-nginx-controller-xyz", "Running", "7d", "1"),
            ("metrics-server-6d94bc", "Running", "7d", "0"),
            ("fluentd-daemonset-abc", "Running", "7d", "0"),
            ("prometheus-server-0", "Running", "3d", "0"),
            // Status is the *middle* column (not last) and this value is far wider
            // than its resolved Flex(1.5) share — exercises clipping behavior when
            // a column contains text wider than its resolved width.
            (
                "grafana-5f4c8d-mn2q7",
                "CrashLoopBackOff: ImagePullBackOff waiting for registry retry backoff window",
                "1h",
                "14",
            ),
            ("loki-0", "Running", "3d", "0"),
            ("argocd-server-6b8c9d-k3m8", "Running", "10d", "0"),
            ("vault-0", "Pending", "5m", "0"),
            ("consul-server-0", "Running", "10d", "1"),
            ("traefik-7d9b8c66b-x2j4k", "Running", "7d", "0"),
        ];
        pods.iter()
            .map(|(name, status, age, restarts)| DataRow {
                cells: vec![
                    StyledText::plain(*name),
                    if status.starts_with("Running") {
                        StyledText::colored(*status, Color::rgb(80, 200, 80))
                    } else if status.starts_with("CrashLoopBackOff") {
                        StyledText::colored(*status, Color::rgb(220, 60, 60))
                    } else {
                        StyledText::colored(*status, Color::rgb(220, 180, 50))
                    },
                    StyledText::plain(*age),
                    StyledText::plain(*restarts),
                ],
                decoration: Default::default(),
            })
            .collect()
    }

    /// Column-aligned totals row: pod count under Name, total restarts
    fn footer_row(&self) -> DataRow {
        let rows = Self::rows();
        let total_restarts: u32 = rows
            .iter()
            .filter_map(|r| r.cells.get(3))
            .filter_map(|c| {
                let text: String = c.spans.iter().map(|s| s.text.as_str()).collect();
                text.parse::<u32>().ok()
            })
            .sum();
        DataRow {
            cells: vec![
                StyledText::plain(format!("{} pods", rows.len())),
                StyledText::plain(""),
                StyledText::plain(""),
                StyledText::colored(total_restarts.to_string(), Color::rgb(220, 180, 50)),
            ],
            decoration: Default::default(),
        }
    }

    fn build_table(&self) -> DataTable {
        let mut rows = Self::rows();
        if let Some(col) = self.sort_col {
            rows.sort_by(|a, b| {
                let a_text: String = a
                    .cells
                    .get(col)
                    .map(|c| c.spans.iter().map(|s| s.text.as_str()).collect())
                    .unwrap_or_default();
                let b_text: String = b
                    .cells
                    .get(col)
                    .map(|c| c.spans.iter().map(|s| s.text.as_str()).collect())
                    .unwrap_or_default();
                let cmp = a_text.cmp(&b_text);
                if self.sort_asc {
                    cmp
                } else {
                    cmp.reverse()
                }
            });
        }
        DataTable {
            id: WidgetId::new("pods"),
            columns: self.columns.clone(),
            rows,
            selected_idx: self.selected,
            scroll_offset: self.scroll_offset,
            sort: self.sort_col.map(|c| {
                (
                    c,
                    if self.sort_asc {
                        SortDirection::Ascending
                    } else {
                        SortDirection::Descending
                    },
                )
            }),
            has_focus: true,
            show_scrollbar: true,
            min_total_width: None,
            h_scroll: self.h_scroll,
            column_overrides: self.column_overrides.clone(),
            footer: if self.show_footer {
                Some(self.footer_row())
            } else {
                None
            },
        }
    }

    /// Resolved column widths for the current viewport — exercises the
    /// same `DataTable::layout` (and thus the shared
    /// `primitives::data_table::resolve_columns`) that any real backend
    /// paints through.
    pub fn resolved_column_widths(&self, backend: &dyn Backend) -> Vec<f32> {
        self.table_layout(backend)
            .columns
            .iter()
            .map(|c| c.width)
            .collect()
    }

    fn status_bar(&self) -> StatusBar {
        let sort_text = match self.sort_col {
            Some(c) => {
                let dir = if self.sort_asc { "asc" } else { "desc" };
                let col_name = self.columns[c].title.clone();
                format!(" sort: {col_name} {dir} ")
            }
            None => " sort: none ".into(),
        };
        let sel_text = match self.selected {
            Some(i) => format!(" row {} / {} ", i + 1, Self::rows().len()),
            None => " no selection ".into(),
        };
        let fg = Color::rgb(220, 220, 220);
        let bg = Color::rgb(40, 40, 60);
        StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![StatusBarSegment {
                text: " k8s pods — DataTable smoke test ".into(),
                fg: Color::rgb(255, 255, 255),
                bg: Color::rgb(40, 80, 120),
                bold: true,
                action_id: None,
            }],
            right_segments: vec![
                StatusBarSegment {
                    text: sort_text,
                    fg,
                    bg,
                    bold: false,
                    action_id: None,
                },
                StatusBarSegment {
                    text: sel_text,
                    fg,
                    bg,
                    bold: false,
                    action_id: None,
                },
            ],
        }
    }

    fn visible_rows(&self, backend: &dyn Backend) -> usize {
        // Delegate to the real layout (rather than re-deriving header/
        // footer math here) so the footer row's reserved height is
        // accounted for exactly once, in `DataTable::layout`.
        self.table_layout(backend).visible_rows
    }

    /// Public so driver tests can locate real hit-test targets (e.g. a
    /// column divider) instead of hardcoding per-backend coordinates.
    pub fn table_layout(&self, backend: &dyn Backend) -> DataTableLayout {
        let vp = backend.viewport();
        let lh = backend.line_height();
        let cw = backend.char_width();
        let bar_h = if lh > 1.5 { lh * 1.5 } else { lh };
        let table_rect = Rect::new(0.0, 0.0, vp.width, vp.height - bar_h);
        let mut table = self.build_table();
        table.min_total_width = Some(80.0 * cw);
        backend.data_table_layout(table_rect, &table)
    }

    fn scrollbar_geometry(&self, backend: &dyn Backend) -> Option<(f32, f32, f32, f32, f32)> {
        let total = Self::rows().len();
        let layout = self.table_layout(backend);
        if !self.build_table().show_scrollbar
            || total <= layout.visible_rows
            || layout.scrollbar_width <= 0.0
        {
            return None;
        }
        let vp = backend.viewport();
        let lh = backend.line_height();
        let sb_x = vp.width - layout.scrollbar_width;
        let track_y = layout.header_height;
        let track_h =
            (vp.height - lh.max(1.0) * 1.5 - layout.header_height - layout.footer_height).max(1.0);
        let (thumb_start, thumb_len) = fit_thumb(
            self.scroll_offset as f32,
            total as f32,
            layout.visible_rows as f32,
            track_h,
            if lh > 1.5 { lh } else { 1.0 },
        );
        Some((sb_x, track_y, track_h, thumb_start, thumb_len))
    }

    fn ensure_visible(&mut self, backend: &dyn Backend) {
        let vis = self.visible_rows(backend);
        if let Some(sel) = self.selected {
            if sel < self.scroll_offset {
                self.scroll_offset = sel;
            } else if vis > 0 && sel >= self.scroll_offset + vis {
                self.scroll_offset = sel + 1 - vis;
            }
        }
    }
}

impl Default for DataTableApp {
    fn default() -> Self {
        Self::new()
    }
}

impl AppLogic for DataTableApp {
    type AreaId = ();

    fn render(&self, backend: &mut dyn Backend, _area: ()) {
        let vp = backend.viewport();
        let lh = backend.line_height();
        let cw = backend.char_width();
        let bar_h = if lh > 1.5 { lh * 1.5 } else { lh };
        let table_rect = Rect::new(0.0, 0.0, vp.width, vp.height - bar_h);
        let mut table = self.build_table();
        table.min_total_width = Some(80.0 * cw);
        let _layout = backend.draw_data_table(table_rect, &table, self.hovered_idx);

        let bar_rect = Rect::new(0.0, vp.height - bar_h, vp.width, bar_h);
        let _ = backend.draw_status_bar_interactive(
            bar_rect,
            &self.status_bar(),
            &InteractionState::new(),
        );
    }

    fn handle(&mut self, event: UiEvent, backend: &mut dyn Backend) -> Reaction {
        let total = Self::rows().len();
        match event {
            UiEvent::KeyPressed { key, .. } => {
                match key {
                    Key::Char('q') | Key::Named(NamedKey::Escape) => return Reaction::Exit,
                    Key::Char('j') | Key::Named(NamedKey::Down) => {
                        let cur = self.selected.unwrap_or(0);
                        if cur + 1 < total {
                            self.selected = Some(cur + 1);
                        }
                    }
                    Key::Char('k') | Key::Named(NamedKey::Up) => {
                        let cur = self.selected.unwrap_or(0);
                        self.selected = Some(cur.saturating_sub(1));
                    }
                    Key::Char('s') => {
                        self.sort_col = match self.sort_col {
                            None => Some(0),
                            Some(c) if c + 1 < self.columns.len() => Some(c + 1),
                            Some(_) => None,
                        };
                    }
                    Key::Char('d') => {
                        self.sort_asc = !self.sort_asc;
                    }
                    Key::Char('f') => {
                        self.show_footer = !self.show_footer;
                    }
                    Key::Named(NamedKey::Home) => {
                        self.selected = Some(0);
                    }
                    Key::Named(NamedKey::End) => {
                        self.selected = Some(total.saturating_sub(1));
                    }
                    // Keyboard equivalent of clicking above/below the scrollbar thumb
                    // (a "page" jump).
                    Key::Named(NamedKey::PageDown) => {
                        let vis = self.visible_rows(backend).max(1);
                        let cur = self.selected.unwrap_or(0);
                        self.selected = Some((cur + vis).min(total.saturating_sub(1)));
                    }
                    Key::Named(NamedKey::PageUp) => {
                        let vis = self.visible_rows(backend).max(1);
                        let cur = self.selected.unwrap_or(0);
                        self.selected = Some(cur.saturating_sub(vis));
                    }
                    Key::Char('H') | Key::Named(NamedKey::Left) => {
                        self.h_scroll = (self.h_scroll - 5.0).max(0.0);
                    }
                    Key::Char('L') | Key::Named(NamedKey::Right) => {
                        let layout = self.table_layout(backend);
                        let max_h = (layout.content_width - layout.viewport_width
                            + layout.scrollbar_width)
                            .max(0.0);
                        self.h_scroll = (self.h_scroll + 5.0).min(max_h);
                    }
                    _ => return Reaction::Continue,
                }
                self.ensure_visible(backend);
                Reaction::Redraw
            }
            UiEvent::MouseDown { position, .. } => {
                // Check h-scrollbar first.
                let layout = self.table_layout(backend);
                if layout.h_scrollbar_height > 0.0 && layout.content_width > 0.0 {
                    let vp = backend.viewport();
                    let lh = backend.line_height();
                    let bar_h = if lh > 1.5 { lh * 1.5 } else { lh };
                    let table_h = vp.height - bar_h;
                    let hsb_y = table_h - layout.footer_height - layout.h_scrollbar_height;
                    if position.y >= hsb_y && position.y < table_h {
                        let track_w = (vp.width - layout.scrollbar_width).max(1.0);
                        let visible_w = track_w;
                        let max_h_scroll = (layout.content_width - visible_w).max(0.0);
                        let (thumb_start, thumb_len) = fit_thumb(
                            self.h_scroll,
                            layout.content_width,
                            visible_w,
                            track_w,
                            if lh > 1.5 { lh } else { 1.0 },
                        );
                        let local_x = position.x;
                        if local_x >= thumb_start && local_x < thumb_start + thumb_len {
                            self.h_sb_drag = Some((0.0, track_w, thumb_len, local_x - thumb_start));
                            return Reaction::Continue;
                        }
                        // Track click — page left/right.
                        let page = visible_w;
                        if local_x < thumb_start {
                            self.h_scroll = (self.h_scroll - page).max(0.0);
                        } else {
                            self.h_scroll = (self.h_scroll + page).min(max_h_scroll);
                        }
                        return Reaction::Redraw;
                    }
                }
                // Check v-scrollbar.
                if let Some((sb_x, track_y, track_h, thumb_start, thumb_len)) =
                    self.scrollbar_geometry(backend)
                {
                    let vis = self.visible_rows(backend);
                    let max_scroll = total.saturating_sub(vis);
                    if position.x >= sb_x {
                        let local_y = position.y - track_y;
                        if local_y >= thumb_start && local_y < thumb_start + thumb_len {
                            self.sb_drag =
                                Some((track_y, track_h, thumb_len, local_y - thumb_start));
                            return Reaction::Continue;
                        }
                        // Track click — page up/down.
                        if local_y < thumb_start {
                            self.scroll_offset = self.scroll_offset.saturating_sub(vis);
                        } else {
                            self.scroll_offset = (self.scroll_offset + vis).min(max_scroll);
                        }
                        return Reaction::Redraw;
                    }
                }
                let layout = self.table_layout(backend);
                match layout.hit_test(position.x, position.y, self.scroll_offset, total) {
                    DataTableHit::Header { col } => {
                        if self.sort_col == Some(col) {
                            self.sort_asc = !self.sort_asc;
                        } else {
                            self.sort_col = Some(col);
                            self.sort_asc = true;
                        }
                        Reaction::Redraw
                    }
                    DataTableHit::HeaderDivider { col } => {
                        // Remember which divider is being dragged *and*
                        // snapshot the layout as it stands right now
                        // — `MouseMoved` below reuses this same
                        // snapshot for every move in the drag instead of
                        // re-fetching `table_layout` each time, which
                        // `resize_base`'s doc comment explains is required
                        // for the last-absorbs rule to stay reversible
                        // once it's overflowed. A drag that never moves
                        // still leaves `column_overrides` untouched.
                        self.resize_col = Some(col);
                        self.resize_base = Some(layout.clone());
                        Reaction::Continue
                    }
                    DataTableHit::Row { idx } => {
                        self.selected = Some(idx);
                        Reaction::Redraw
                    }
                    // Pinned footer isn't selectable — same as empty space.
                    DataTableHit::Footer | DataTableHit::Empty => Reaction::Continue,
                }
            }
            UiEvent::MouseMoved { position, .. } => {
                if let Some((track_x, track_w, thumb_len, grab_off)) = self.h_sb_drag {
                    let lay = self.table_layout(backend);
                    let visible_w = (lay.viewport_width - lay.scrollbar_width).max(1.0);
                    let max_h_scroll = (lay.content_width - visible_w).max(0.0);
                    let effective = (track_w - thumb_len).max(1.0);
                    let rel = ((position.x - track_x - grab_off) / effective).clamp(0.0, 1.0);
                    self.h_scroll = (rel * max_h_scroll).round().min(max_h_scroll);
                    return Reaction::Redraw;
                }
                if let Some((track_y, track_h, thumb_len, grab_off)) = self.sb_drag {
                    let vis = self.visible_rows(backend);
                    let max_scroll = total.saturating_sub(vis);
                    let effective = (track_h - thumb_len).max(1.0);
                    let rel = ((position.y - track_y - grab_off) / effective).clamp(0.0, 1.0);
                    self.scroll_offset = (rel * max_scroll as f32).round() as usize;
                    return Reaction::Redraw;
                }
                if let Some(col) = self.resize_col {
                    // Last-absorbs resize: a divider drag takes its slack from the *last*
                    // column (`Restarts`), not `col + 1`, and every column strictly between
                    // the two is frozen at its currently-resolved width. Once `Restarts`
                    // bottoms out at the floor below, the table overflows and
                    // `DataTableLayout` grows its own horizontal scrollbar instead of
                    // refusing the drag. Layered on top of `columns` via `column_overrides`
                    // — the real API surface a consumer drags through — rather than
                    // rewriting the columns' declared strategies directly.
                    //
                    // Uses a small absolute floor (not per-column minimum): this table's
                    // own `Restarts` column is declared `Fixed(10.0)` — the same literal
                    // value in both cell (TUI) and pixel (GTK) units.
                    //
                    // Uses `resize_base` (captured at mouse-down), not a freshly
                    // re-fetched `table_layout` — see its doc comment for why re-deriving
                    // the layout from this same drag's own prior output would corrupt the
                    // rightmost column's floor bookkeeping once overflowed.
                    if let Some(base) = &self.resize_base {
                        self.column_overrides =
                            base.drag_divider(&self.column_overrides, col, position.x, 4.0);
                    }
                    return Reaction::Redraw;
                }
                let layout = self.table_layout(backend);
                let total = Self::rows().len();
                let old = self.hovered_idx;
                match layout.hit_test(position.x, position.y, self.scroll_offset, total) {
                    DataTableHit::Row { idx } => self.hovered_idx = Some(idx),
                    _ => self.hovered_idx = None,
                }
                if self.hovered_idx != old {
                    return Reaction::Redraw;
                }
                Reaction::Continue
            }
            UiEvent::MouseUp { .. } => {
                self.resize_base = None;
                let had_drag = self.resize_col.take().is_some()
                    || self.sb_drag.take().is_some()
                    || self.h_sb_drag.take().is_some();
                if had_drag {
                    return Reaction::Redraw;
                }
                Reaction::Continue
            }
            UiEvent::Scroll { delta, .. } => {
                if delta.x.abs() > 0.01 {
                    let layout = self.table_layout(backend);
                    let max_h = (layout.content_width - layout.viewport_width
                        + layout.scrollbar_width)
                        .max(0.0);
                    self.h_scroll = (self.h_scroll - delta.x * 10.0).clamp(0.0, max_h);
                }
                let vis = self.visible_rows(backend);
                if delta.y < 0.0 {
                    self.scroll_offset = self
                        .scroll_offset
                        .saturating_add(3)
                        .min(total.saturating_sub(vis));
                } else if delta.y > 0.0 {
                    self.scroll_offset = self.scroll_offset.saturating_sub(3);
                }
                Reaction::Redraw
            }
            UiEvent::WindowResized { .. } => {
                self.ensure_visible(backend);
                Reaction::Redraw
            }
            _ => Reaction::Continue,
        }
    }
}