quvyta-framework 0.1.35

A Rust framework for building terminal applications
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Tables: rows of cells under a header, virtualised, sortable and selectable.
//!
//! The columns, cells and rows an application builds are in `model`; `layout` decides column
//! widths and which columns show; `paint` draws the header and the rows.

mod layout;
mod model;
mod paint;
#[cfg(test)]
mod tests;

use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;

use crate::env::Env;
use crate::event::{Event, MouseButton, MouseKind};
use crate::geometry::{Rect, Size, clamp_u16};
use crate::keymap::{Key, KeyChord, Modifiers};
use crate::widget::{EventCx, MeasureCx, PaintCx, Widget, WidgetKey};

use super::click::Click;
use super::row::LEAD;
use super::row_menu::{self, RowAnchor, RowMenuItems};
use super::row_pointer::{self, PickedRows, Picking, RowDrop, Spot};
use super::rows::{self, RowScroll, Step};
use super::select_box;
use super::{ContextItem, IndexMessage};
use layout::Placed;
pub use model::{Column, ColumnWidth, SortDirection, TableCell, TableRow};
use paint::RowPaint;

/// Cells between two columns. Columns are told apart by space, never by a drawn line.
const COLUMN_GAP: u16 = 2;

/// Cells taken by the check mark of a multi-select table.
const MARK: u16 = 2;

/// Builds a message from a column and a direction.
type SortMessage<Msg> = Box<dyn Fn(usize, SortDirection) -> Msg>;

/// Rows of cells under a header row. Only the rows on screen are drawn, so a table stays fast
/// with any number of rows; pass the rows as an `Arc<[TableRow]>` kept in your state to avoid
/// copying them every frame.
///
/// The header sits on a raised surface with faint titles. Columns are separated by space; their
/// widths follow [`ColumnWidth`] and [`Column::min`], and when the minimums do not fit, the
/// columns scroll sideways: arrows at the ends of the header show which side hides columns. The
/// application owns the selection, the checked rows and the sort order and is told about changes
/// through messages. A hovered or selected row raises its surface and shows the pillar; only its
/// first cell slides one cell right. The check mark of a multi-select table never moves.
///
/// Keys while focused: ↑/↓ or k/j, PgUp/PgDn, Home/End move (↑/↓ round the ends with
/// [`wrap`](Self::wrap)); Enter activates; Space toggles in multi-select tables and activates
/// otherwise, or with [`space_activates(false)`](Self::space_activates) goes on to the
/// application; ←/→ scroll columns that overflow; with [`Table::on_sort`], `s` sorts by the next
/// sortable column and `shift+s` reverses the order.
/// These are the keys a [`HelpLayer`](super::HelpLayer) lists for a table with the focus, so a
/// screen needs no hint for them.
///
/// The mouse does the same: a click on a row selects and activates it, a click on its check mark
/// (or the cell after it) only toggles, a click on a sortable title sorts by it and a second
/// click reverses it, and a click on a header arrow scrolls the columns one step.
///
/// Four capabilities make the rows work the way a file explorer's do, each off until asked for:
///
/// - [`activate_on(Click::Double)`](Self::activate_on): a click only selects a row and a double
///   click activates it, so a click can start a drag or a selection without opening anything.
/// - [`multi_select`](Self::multi_select): several rows are selected at once with Ctrl+click,
///   Shift+click, Shift+arrows, Ctrl+A and Space; they share the selection tone while only the cursor's row carries the
///   pillar and slides.
/// - [`box_select`](Self::box_select): a drag from the free space below the rows draws a box, and
///   the rows it covers become the selection.
/// - [`droppable`](Self::droppable): the selection is dragged onto a row that takes it, such as a
///   folder, which takes the accent tone while the drag is over it.
///
/// Style keys: rows use `list-item` (`hover`, `selected`, `focus`, `pressed`) and
/// `list-item.faint` like [`List`](super::List); `table-header` (`bg`, `fg`) with `hover` over a
/// sortable title and `selected` on the sorted one; `table-sort` for the sort arrow;
/// `table-scroll` (`fg`, `bg`) with `hover` for the header arrows; `list-header` for the empty
/// text; `tree-drop` for the row a drag would drop on; `text-selection` (`bg`) for the selection
/// box; `scrollbar`.
pub struct Table<Msg> {
    columns: Vec<Column>,
    rows: Rows,
    selected: Option<usize>,
    checked: Option<Vec<bool>>,
    sort: Option<(usize, SortDirection)>,
    empty: String,
    on_select: Option<IndexMessage<Msg>>,
    on_activate: Option<IndexMessage<Msg>>,
    on_toggle: Option<IndexMessage<Msg>>,
    on_sort: Option<SortMessage<Msg>>,
    menu: Option<Box<RowMenuItems<'static, Msg>>>,
    menu_on_activate: bool,
    picking: Picking<Msg>,
    wrap: bool,
    space_activates: bool,
}

/// The rows a table shows: built in full, or asked for one at a time as they come on screen.
enum Rows {
    Built(Arc<[TableRow]>),
    /// [`Table::lazy`]: how many there are and how to build row `index`.
    Lazy {
        count: usize,
        row: Rc<dyn Fn(usize) -> TableRow>,
    },
}

impl Rows {
    fn len(&self) -> usize {
        match self {
            Self::Built(rows) => rows.len(),
            Self::Lazy { count, .. } => *count,
        }
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Row `index`, built now for a lazy table.
    fn get(&self, index: usize) -> Cow<'_, TableRow> {
        match self {
            Self::Built(rows) => Cow::Borrowed(&rows[index]),
            Self::Lazy { row, .. } => Cow::Owned(row(index)),
        }
    }
}

#[derive(Debug, Default)]
struct TableMemory {
    /// Widest cell of every column, for the rows it was measured on.
    fit: Option<(Arc<[TableRow]>, Vec<u16>)>,
    /// First visible column when the columns overflow.
    column_offset: usize,
    /// The largest useful `column_offset` in the last frame; zero when nothing overflows.
    max_column_offset: usize,
    /// Whether columns were hidden on the right in the last frame.
    more: bool,
    placed: Vec<Placed>,
}

impl<Msg: 'static> Table<Msg> {
    /// A table with `columns` showing `rows`.
    #[must_use]
    pub fn new(columns: impl IntoIterator<Item = Column>, rows: impl Into<Arc<[TableRow]>>) -> Self {
        Self {
            columns: columns.into_iter().collect(),
            rows: Rows::Built(rows.into()),
            selected: None,
            checked: None,
            sort: None,
            empty: String::new(),
            on_select: None,
            on_activate: None,
            on_toggle: None,
            on_sort: None,
            menu: None,
            menu_on_activate: false,
            picking: Picking::default(),
            wrap: false,
            space_activates: true,
        }
    }

    /// A table with `columns` and `count` rows that are built only when they are drawn: `row`
    /// gives row `index` and is called for the rows on screen in each frame, so a frame costs as
    /// much as the rows it shows, whatever the table holds. For a folder of a hundred thousand
    /// entries, a log or a query's results, where building every row for every frame is what makes
    /// a key slow.
    ///
    /// The rows are not all known, so a [`ColumnWidth::Fit`] column fits its title and its
    /// [`min`](Column::min) instead of its widest cell; give a column whose cells are wider a
    /// [`ColumnWidth::Fixed`] width. Everything else works as with [`new`](Self::new).
    #[must_use]
    pub fn lazy(
        columns: impl IntoIterator<Item = Column>,
        count: usize,
        row: impl Fn(usize) -> TableRow + 'static,
    ) -> Self {
        let mut table = Self::new(columns, Vec::new());
        table.rows = Rows::Lazy { count, row: Rc::new(row) };
        table
    }

    /// Lets ↓ (or j) on the last row go on to the first and ↑ (or k) on the first go to the
    /// last, as a menu does. PgUp/PgDn and Home/End still stop at the ends, and Shift with an
    /// arrow never wraps a range. Off by default: the keys stop at either end.
    #[must_use]
    pub fn wrap(mut self, wrap: bool) -> Self {
        self.wrap = wrap;
        self
    }

    /// Says whether Space acts on the cursor's row: `true`, the default, opens it, or checks it in
    /// a table that checks rows. With `false` Space does nothing here and is not used at all, so it
    /// travels on to the application's keymap actions exactly as an unhandled key does.
    ///
    /// For the table that holds the focus almost all the time, such as a music player's songs: in a
    /// music player Space is play and pause everywhere else, and one table must not take it away.
    /// A table that checks rows loses the key with it — the marks stay, a click still checks a row,
    /// and a person who wants Space to check rows leaves the option on. Enter and the mouse are the
    /// same either way.
    #[must_use]
    pub fn space_activates(mut self, on: bool) -> Self {
        self.space_activates = on;
        self
    }

    /// The selected row index.
    #[must_use]
    pub fn selected(mut self, index: Option<usize>) -> Self {
        self.selected = index;
        self
    }

    /// Turns on multiple selection; `checked[i]` tells whether row `i` is checked.
    #[must_use]
    pub fn checked(mut self, checked: Vec<bool>) -> Self {
        self.checked = Some(checked);
        self
    }

    /// Shows the sort arrow on `column` pointing in `direction`. The rows must already be in
    /// that order; the table does not reorder them.
    #[must_use]
    pub fn sort(mut self, column: usize, direction: SortDirection) -> Self {
        self.sort = Some((column, direction));
        self
    }

    /// Text shown under the header when there are no rows.
    #[must_use]
    pub fn empty_text(mut self, text: impl Into<String>) -> Self {
        self.empty = text.into();
        self
    }

    /// Message for moving the selection to a row.
    #[must_use]
    pub fn on_select(mut self, message: impl Fn(usize) -> Msg + 'static) -> Self {
        self.on_select = Some(Box::new(message));
        self
    }

    /// Message for opening a row (Enter, click).
    #[must_use]
    pub fn on_activate(mut self, message: impl Fn(usize) -> Msg + 'static) -> Self {
        self.on_activate = Some(Box::new(message));
        self
    }

    /// Message for checking or unchecking a row of a multi-select table (Space while
    /// [`space_activates`](Self::space_activates) is on, click on the mark).
    #[must_use]
    pub fn on_toggle(mut self, message: impl Fn(usize) -> Msg + 'static) -> Self {
        self.on_toggle = Some(Box::new(message));
        self
    }

    /// Message asking to sort by a column in a direction; turns on sorting by clicking titles of
    /// [`Column::sortable`] columns and with `s` / `shift+s`.
    #[must_use]
    pub fn on_sort(mut self, message: impl Fn(usize, SortDirection) -> Msg + 'static) -> Self {
        self.on_sort = Some(Box::new(message));
        self
    }

    /// Gives every row a context menu: `items(index)` builds the entries for the row of that
    /// index, and the menu acts on the row it was opened on rather than on the selected one.
    ///
    /// A right press on a row opens the menu at the pointer; the menu key or Shift+F10 opens the
    /// menu of the selected row below it, scrolling it into view first. The row the menu belongs
    /// to stays raised while it is open, so it is clear what the entries act on. A right press on
    /// a row that is not checked makes it the selection first, so a menu never acts on rows the
    /// person did not mean.
    #[must_use]
    pub fn context_menu(mut self, items: impl Fn(usize) -> Vec<ContextItem<Msg>> + 'static) -> Self {
        self.menu = Some(Box::new(items));
        self
    }

    /// Makes a row's [context menu](Self::context_menu) its action: Enter opens the menu of the
    /// selected row below it and a click opens the menu of the clicked row where it was clicked,
    /// instead of sending [`on_activate`](Self::on_activate).
    ///
    /// For a table whose rows are acted on only through a few choices: a right click is not what
    /// most people try in a terminal and many keyboards have no menu key, so the menu is also
    /// reached the way any row is opened. A row whose menu has no entries opens nothing. Off by
    /// default; without a context menu it does nothing.
    #[must_use]
    pub fn menu_on_activate(mut self, on: bool) -> Self {
        self.menu_on_activate = on;
        self
    }

    /// How many clicks activate a row: [`Click::Single`], the default, selects and activates at
    /// once; [`Click::Double`] only selects on a click and activates on a second press on the same
    /// row within [`Click::INTERVAL`]. Enter activates either way.
    #[must_use]
    pub fn activate_on(mut self, click: Click) -> Self {
        self.picking.activate_on = click;
        self
    }

    /// Lets several rows be selected at once: `selected` holds their indexes and `message(rows)`
    /// asks the application to make `rows` the whole new selection.
    ///
    /// The row given to [`selected`](Self::selected) stays the cursor: the row the keys move from
    /// and the only one with the pillar, while every selected row takes the selection tone.
    /// Ctrl+click adds a row or takes it out, Shift+click selects the rows from the last plain or
    /// Ctrl click to this one; Shift with ↑/↓, PgUp/PgDn or Home/End extends that range, Ctrl+A
    /// selects every row, Space adds or takes out the cursor's row while
    /// [`space_activates`](Self::space_activates) is on, Esc reduces several selected rows to the
    /// cursor's, and a plain click or arrow selects that one row. A right click on a selected row
    /// keeps the selection for its menu; on another row it makes that row the selection first.
    #[must_use]
    pub fn multi_select(mut self, selected: &[usize], message: impl Fn(Vec<usize>) -> Msg + 'static) -> Self {
        self.picking.chosen = selected.to_vec();
        self.picking.on_choose = Some(Box::new(message));
        self
    }

    /// Lets a drag from the free space below the rows draw a box: the rows it covers become the
    /// selection while it is drawn, or join it when Ctrl was held at the press, and a click there
    /// without a drag clears the selection. The box is a tone laid over the cells it covers, never
    /// a frame. It needs [`multi_select`](Self::multi_select) and does nothing without it.
    #[must_use]
    pub fn box_select(mut self, on: bool) -> Self {
        self.picking.box_select = on;
        self
    }

    /// Lets rows be dragged onto other rows, such as files onto a folder: `accepts(index)` tells
    /// whether a row takes drops and `message(RowDrop)` asks the application to move the rows.
    ///
    /// A drag carries the pressed row, or the whole [selection](Self::multi_select) when it is
    /// pressed on one of its rows; a click on a selected row without a drag makes it the one
    /// selected row on release. The row under the pointer takes the accent tone while it can take
    /// the drag. A release anywhere else, or on one of the dragged rows, does nothing. With
    /// [`Click::Single`] a row activates on release rather than on press, so pressing a row to
    /// drag it does not activate it.
    #[must_use]
    pub fn droppable(
        mut self,
        message: impl Fn(RowDrop) -> Msg + 'static,
        accepts: impl Fn(usize) -> bool + 'static,
    ) -> Self {
        self.picking.dropping = Some((Box::new(message), Box::new(accepts)));
        self
    }

    /// A drop released with Ctrl held asks for a copy with `message` instead of the move of
    /// [`droppable`](Self::droppable), the way a file explorer copies. A terminal that does not
    /// report Ctrl with the pointer always moves. It does nothing without `droppable`.
    #[must_use]
    pub fn on_copy_drop(mut self, message: impl Fn(RowDrop) -> Msg + 'static) -> Self {
        self.picking.copy_drop = Some(Box::new(message));
        self
    }

    /// The cells the rows have to themselves: the scrollbar column is not part of a row.
    fn rows_width(area: Rect, overflows: bool) -> u16 {
        area.width.saturating_sub(u16::from(overflows))
    }

    /// Offers `event` to the row menu. A right press picks the row under the pointer and makes it
    /// the selection unless it is checked, because a menu on a checked row acts on the checked
    /// rows, which the application knows about.
    fn menu_event(&self, cx: &mut EventCx<'_, Msg>, event: &Event) -> bool {
        let area = cx.area();
        let body = Rect::new(area.x, area.y + 1, area.width, area.height.saturating_sub(1));
        let total = self.rows.len();
        let visible = usize::from(body.height);
        let overflows = total > visible;
        row_menu::event(
            cx,
            event,
            self.menu.as_deref(),
            total,
            |cx, x, y| {
                if y < body.y || x >= area.x + i32::from(Self::rows_width(area, overflows)) {
                    return None;
                }
                let offset = cx.memory::<RowScroll>().offset;
                let row = usize::try_from(y - body.y).ok().map(|row| offset + row).filter(|row| *row < total)?;
                let checked = self.checked.as_ref().is_some_and(|checked| checked.get(row).copied().unwrap_or(false));
                if self.picking.is_multi() && !self.picking.is_chosen(row) {
                    self.picking.select_one(cx, self, row);
                } else if !checked && !self.picking.is_multi() {
                    self.select(cx, row);
                }
                Some(RowAnchor { row, at: Rect::new(x, y, 1, 1), keyboard: false })
            },
            |cx| self.selected_anchor(cx),
        )
    }

    /// Where the menu of the selected row unfolds from for the keyboard: below the whole row,
    /// scrolled into view first.
    fn selected_anchor(&self, cx: &mut EventCx<'_, Msg>) -> Option<RowAnchor> {
        let area = cx.area();
        let body_y = area.y + 1;
        let total = self.rows.len();
        let visible = usize::from(area.height.saturating_sub(1));
        let overflows = total > visible;
        let row = self.selected.filter(|row| *row < total)?;
        let memory = cx.memory::<RowScroll>();
        if row < memory.offset {
            memory.offset = row;
        } else if visible > 0 && row >= memory.offset + visible {
            memory.offset = row + 1 - visible;
        }
        let y = body_y + i32::try_from(row - memory.offset).unwrap_or(0);
        let at = Rect::new(area.x, y, Self::rows_width(area, overflows), 1);
        Some(RowAnchor { row, at, keyboard: true })
    }

    /// Whether Enter and a click open the row's menu rather than the row.
    fn activation_is_menu(&self) -> bool {
        self.menu_on_activate && self.menu.is_some()
    }

    /// Whether Enter opens something: a row of the application's or, with
    /// [`menu_on_activate`](Self::menu_on_activate), the row's own menu.
    fn activates(&self) -> bool {
        self.on_activate.is_some() || self.activation_is_menu()
    }

    /// Whether Space checks a row instead of opening it: a multi-select table takes the cursor's
    /// row in and out of the selection, and check marks are toggled when the table has both.
    fn checks(&self) -> bool {
        self.picking.is_multi() || (self.checked.is_some() && self.on_toggle.is_some())
    }

    fn lead(&self) -> u16 {
        LEAD + if self.checked.is_some() { MARK } else { 0 }
    }

    fn select(&self, cx: &mut EventCx<'_, Msg>, index: usize) {
        if Some(index) != self.selected
            && let Some(message) = &self.on_select
        {
            cx.emit(message(index));
        }
    }

    fn activate(&self, cx: &mut EventCx<'_, Msg>, index: usize) -> bool {
        let Some(message) = &self.on_activate else {
            return false;
        };
        cx.memory::<RowScroll>().flashed = Some(index);
        cx.flash();
        cx.emit(message(index));
        true
    }

    fn toggle(&self, cx: &mut EventCx<'_, Msg>, index: usize) -> bool {
        match (&self.checked, &self.on_toggle) {
            (Some(_), Some(message)) => {
                cx.emit(message(index));
                true
            }
            _ => false,
        }
    }

    fn request_sort(&self, cx: &mut EventCx<'_, Msg>, column: usize, direction: SortDirection) -> bool {
        match &self.on_sort {
            Some(message) if self.columns.get(column).is_some_and(|c| c.sortable) => {
                cx.emit(message(column, direction));
                true
            }
            _ => false,
        }
    }

    /// Sorting after a click on `column`'s title: the other direction when it is already sorted.
    fn click_sort(&self, column: usize) -> SortDirection {
        match self.sort {
            Some((sorted, direction)) if sorted == column => direction.reversed(),
            _ => SortDirection::Ascending,
        }
    }

    /// Scrolls overflowing columns one column forward or back. Returns whether the columns
    /// overflow at all, so the key or press is used even at an end.
    fn scroll_columns(cx: &mut EventCx<'_, Msg>, forward: bool) -> bool {
        let memory = cx.memory::<TableMemory>();
        if memory.max_column_offset == 0 {
            return false;
        }
        memory.column_offset = if forward {
            (memory.column_offset + 1).min(memory.max_column_offset)
        } else {
            memory.column_offset.saturating_sub(1)
        };
        true
    }

    /// Which way the header's scroll arrow at column `x` scrolls, when one is drawn there: the
    /// back arrow in the first cell while columns are hidden on the left, the forward arrow in
    /// the last cell while columns are hidden on the right.
    fn scroll_arrow_at(cx: &mut EventCx<'_, Msg>, area: Rect, x: i32) -> Option<bool> {
        let memory = cx.memory::<TableMemory>();
        if x == area.x && memory.column_offset > 0 {
            Some(false)
        } else if x == area.right() - 1 && memory.more {
            Some(true)
        } else {
            None
        }
    }
}

impl<Msg: 'static> Widget<Msg> for Table<Msg> {
    fn measure(&self, _cx: &mut MeasureCx<'_>, available: Size) -> Size {
        let rows = self.rows.len().max(1) + 1;
        let widths =
            self.columns.iter().fold(0u16, |sum, c| sum.saturating_add(c.title_width()).saturating_add(COLUMN_GAP));
        Size::new(widths.saturating_add(self.lead() + 1), clamp_u16(i32::try_from(rows).unwrap_or(i32::MAX)))
            .min(available)
    }

    fn paint(&self, cx: &mut PaintCx<'_>, area: Rect) {
        if area.is_empty() {
            return;
        }
        cx.register_hit(area);
        let body = Rect::new(area.x, area.y + 1, area.width, area.height.saturating_sub(1));
        let total = self.rows.len();
        let visible = usize::from(body.height);
        let overflows = total > visible;
        let lead = self.lead();
        let room = area.width.saturating_sub(lead + u16::from(overflows));

        let (placed, column_offset, more) = {
            let memory = cx.memory::<TableMemory>();
            let widest = if self.columns.iter().any(|c| c.width == ColumnWidth::Fit) {
                self.widest_cells(memory)
            } else {
                vec![0; self.columns.len()]
            };
            let (widths, overflow) = self.widths(&widest, room);
            // Columns that scroll sideways keep the forward arrow's cell and one cell of air before
            // it free, like the back arrow and the lead on the left, so the arrow never covers or
            // touches a title or a value. A scrollbar column already gives the arrow its cell.
            let arrow = if overflow { 2 - u16::from(overflows) } else { 0 };
            let room = room.saturating_sub(arrow);
            let max_offset = if overflow { Self::max_offset(&widths, room) } else { 0 };
            memory.max_column_offset = max_offset;
            memory.column_offset = memory.column_offset.min(max_offset);
            let placed = Self::place(&widths, memory.column_offset, area.x + i32::from(lead), room);
            let more =
                placed.last().is_some_and(|last| last.column + 1 < widths.len() || last.width < widths[last.column]);
            memory.placed.clone_from(&placed);
            memory.more = more;
            (placed, memory.column_offset, more)
        };
        self.paint_header(cx, area, &placed, column_offset, more);

        if total == 0 {
            let faint = cx.style("list-header", None, &[]).text();
            let budget = area.width.saturating_sub(LEAD);
            cx.text(area.x + i32::from(LEAD), body.y, &self.empty, faint, budget);
            return;
        }
        let focused = cx.is_focused();
        let pressed = cx.is_pressed();
        // An open row menu takes the pointer: only the row it acts on stays raised, so the menu
        // and the row it belongs to are read together.
        let menu_row = row_menu::open_row(cx, self.menu.as_deref());
        if menu_row.is_some() {
            cx.request_overlay(area);
        }
        let offset = cx.memory::<RowScroll>().follow(self.selected, total, visible);
        let row_width = Self::rows_width(area, overflows);
        let rows_rect = Rect::new(area.x, body.y, row_width, body.height);
        // The row a drag is over takes the accent tone when it can take what is dragged.
        let target = row_pointer::dragged(cx).and_then(|((x, y), carried)| {
            let index = offset + usize::try_from(y - body.y).ok()?;
            (rows_rect.contains(x, y) && index < total && self.picking.takes_drop(&carried, index)).then_some(index)
        });
        for (row, index) in (offset..total).take(visible).enumerate() {
            let rect = Rect::new(area.x, body.y + i32::try_from(row).unwrap_or(0), row_width, 1);
            self.paint_row(cx, rect, index, &placed, RowPaint { focused, pressed, menu_row, target });
        }
        if let Some(drawn) = row_pointer::drawn_box(cx) {
            select_box::paint(cx, drawn, rows_rect);
        }
        rows::paint_scrollbar(cx, body, total, offset, None);
    }

    fn paint_overlay(&self, cx: &mut PaintCx<'_>, anchor: Rect) {
        row_menu::paint(cx, self.menu.as_deref(), anchor);
    }

    fn event(&self, cx: &mut EventCx<'_, Msg>, event: &Event) -> bool {
        if self.menu_event(cx, event) {
            return true;
        }
        let area = cx.area();
        let body = Rect::new(area.x, area.y + 1, area.width, area.height.saturating_sub(1));
        let total = self.rows.len();
        match event {
            Event::Key(key) => {
                let page = usize::from(body.height);
                let extend = |_: &mut EventCx<'_, Msg>, plain: &crate::event::KeyEvent| {
                    rows::SHIFT_STEPS
                        .contains(&plain.chord.key)
                        .then(|| Step::from_key(plain).and_then(|step| step.apply(self.selected, total, page)))
                };
                if self.picking.selection_key(cx, key, self, total, extend) {
                    return true;
                }
                if let Some(step) = Step::from_key(key) {
                    let Some(target) = step.apply_wrapping(self.selected, total, page, self.wrap) else {
                        return false;
                    };
                    if self.picking.is_multi() {
                        self.picking.select_one(cx, self, target);
                    } else {
                        self.select(cx, target);
                    }
                    return true;
                }
                if key.is_plain(Key::Left) || key.is_plain(Key::Right) {
                    return Self::scroll_columns(cx, key.is_plain(Key::Right));
                }
                if key.is_plain(Key::Enter) {
                    if self.activation_is_menu() {
                        return self
                            .selected_anchor(cx)
                            .is_some_and(|anchor| row_menu::open_as_action(cx, self.menu.as_deref(), &anchor));
                    }
                    return self.selected.is_some_and(|index| self.activate(cx, index));
                }
                // A table that leaves Space to the application falls through here, so the key goes
                // on to the keymap actions as an unhandled key does.
                if self.space_activates && key.is_plain(Key::Space) {
                    let Some(index) = self.selected else { return false };
                    return self.picking.toggle(cx, self, index) || self.toggle(cx, index) || self.activate(cx, index);
                }
                let shift_s = KeyChord { key: Key::Char('s'), mods: Modifiers { shift: true, ..Modifiers::default() } };
                if self.on_sort.is_some() && (key.is_plain(Key::Char('s')) || key.chord == shift_s) {
                    return match (key.chord == shift_s, self.sort) {
                        (true, Some((column, direction))) => self.request_sort(cx, column, direction.reversed()),
                        (true, None) => false,
                        (false, current) => {
                            let start = current.map_or(0, |(column, _)| column + 1);
                            let count = self.columns.len();
                            let next = (0..count)
                                .map(|step| (start + step) % count.max(1))
                                .find(|i| self.columns[*i].sortable);
                            next.is_some_and(|column| self.request_sort(cx, column, SortDirection::Ascending))
                        }
                    };
                }
                false
            }
            Event::Mouse(mouse) => {
                if rows::scroll_mouse(cx, mouse, body, total) {
                    return true;
                }
                if mouse.kind == MouseKind::Down(MouseButton::Left) {
                    if mouse.y == area.y {
                        if let Some(forward) = Self::scroll_arrow_at(cx, area, mouse.x) {
                            return Self::scroll_columns(cx, forward);
                        }
                        let placed = cx.memory::<TableMemory>().placed.clone();
                        let Some(place) = placed.iter().find(|place| Self::spans(place, mouse.x)) else {
                            return false;
                        };
                        return self.request_sort(cx, place.column, self.click_sort(place.column));
                    }
                    if self.checked.is_some()
                        && mouse.x < area.x + i32::from(LEAD + MARK)
                        && let Spot::Row(index) = self.spot(cx, mouse.x, mouse.y)
                        && self.toggle(cx, index)
                    {
                        return true;
                    }
                }
                self.picking.mouse(cx, mouse, self).unwrap_or(false)
            }
            _ => false,
        }
    }

    fn focusable(&self) -> bool {
        !self.rows.is_empty()
    }

    fn keys(&self, env: &Env) -> Vec<WidgetKey> {
        let i18n = env.i18n();
        let mut keys = rows::declared_keys(env);
        if self.activates() {
            keys.push(WidgetKey::new("enter", i18n.translate("quvyta.widget.open", &[])));
        }
        // Space is Enter's twin unless it checks a row, which only a table that shows check marks
        // or takes a whole selection does; a table that leaves Space to the application lists
        // nothing for it, since the key is not the table's.
        if self.space_activates && self.checks() {
            keys.push(WidgetKey::new("space", i18n.translate("quvyta.widget.check", &[])));
        }
        if self.on_sort.is_some() {
            keys.push(WidgetKey::new("s", i18n.translate("quvyta.widget.sort", &[])));
        }
        keys
    }
}

impl<Msg: 'static> PickedRows<Msg> for Table<Msg> {
    fn spot(&self, cx: &mut EventCx<'_, Msg>, x: i32, y: i32) -> Spot {
        let area = cx.area();
        let body = Rect::new(area.x, area.y + 1, area.width, area.height.saturating_sub(1));
        let total = self.rows.len();
        let overflows = total > usize::from(body.height);
        let rows = Rect::new(area.x, body.y, Self::rows_width(area, overflows), body.height);
        if !rows.contains(x, y) {
            return Spot::Outside;
        }
        let index = cx.memory::<RowScroll>().offset + usize::try_from(y - body.y).unwrap_or(0);
        if index < total { Spot::Row(index) } else { Spot::Free }
    }

    fn covered(&self, cx: &mut EventCx<'_, Msg>, rect: Rect) -> Vec<usize> {
        let area = cx.area();
        let body = Rect::new(area.x, area.y + 1, area.width, area.height.saturating_sub(1));
        let offset = cx.memory::<RowScroll>().offset;
        let (top, bottom) = (rect.y.max(body.y), rect.bottom().min(body.bottom()));
        (top..bottom)
            .filter_map(|y| usize::try_from(y - body.y).ok())
            .map(|row| offset + row)
            .filter(|index| *index < self.rows.len())
            .collect()
    }

    fn cursor(&self) -> Option<usize> {
        self.selected
    }

    fn select(&self, cx: &mut EventCx<'_, Msg>, index: usize) {
        Table::select(self, cx, index);
    }

    fn open(&self, cx: &mut EventCx<'_, Msg>, index: usize, (x, y): (i32, i32)) {
        if self.activation_is_menu() {
            let anchor = RowAnchor { row: index, at: Rect::new(x, y, 1, 1), keyboard: false };
            row_menu::open_as_action(cx, self.menu.as_deref(), &anchor);
        } else {
            self.activate(cx, index);
        }
    }
}