makeover_immediate/table.rs
1//! Columns, narrowing, cell parts and the sort caret, over `egui_extras`.
2//!
3//! `makeover-webview`'s `list` module and `makeover-tui`'s `table` in the shape
4//! immediate mode allows. It owns the same four things: which columns exist, how
5//! wide they are, which ones survive a narrow viewport, and what each part of a
6//! cell is. It does not own what goes in a cell, which here is not a policy but
7//! a fact of the mode: a cell's contents are drawn by the app's own closure, the
8//! way [`group`](crate::group) already takes one per field.
9//!
10//! # Why `egui_extras` and not egui
11//!
12//! egui itself has no table. [`egui::Grid`] gives no per-column sizing, no
13//! sticky header and no scroll sync, which is why audiofiles reached for
14//! `egui_extras::TableBuilder` rather than building on `Grid`. Writing a third
15//! answer here would be reimplementing that crate worse, so this is a mapping
16//! layer over it.
17//!
18//! It is the first dependency this crate has taken beyond egui itself, and it
19//! moves in lockstep with egui's own version, which is the cost worth naming.
20//!
21//! # What immediate mode costs the narrowing
22//!
23//! The terminal renderer measures a [`Width::Content`] column from its cells,
24//! because it holds every cell before it draws any. Here the cells do not exist
25//! until the app's closure runs, so nothing can be measured before the layout is
26//! decided.
27//!
28//! That splits the answer in two, and both halves are honest:
29//!
30//! - **Sizing** hands a content column to
31//! [`egui_extras::Column::auto`], which measures it and holds the result
32//! between frames. This is better than the terminal gets, not worse.
33//! - **Narrowing** cannot wait for that, so it budgets a content column at the
34//! floor the app declared in [`Sizing`]. A column that turns out wider than
35//! its floor is still drawn; it is the *decision to drop* that uses the
36//! declared number, and a floor is what the app already has to supply for its
37//! fill columns.
38//!
39//! # Why positions are the bug
40//!
41//! Carried from the other two renderers, because the mistake is not a CSS
42//! mistake and not a terminal one. goingson hides its mobile columns with
43//! `nth-child(n+5)` against a seven-column table; insert a column left of the
44//! cut and the wrong one disappears, silently. A renderer narrows by raising a
45//! cutoff and never by counting.
46
47use crate::Palette;
48use egui::{Response, RichText, Sense, Ui};
49use egui_extras::{Column as Track, TableBuilder};
50use makeover_layout::{CellPart, Column, Priority, Sort, Width};
51
52/// The cutoffs, weakest first.
53///
54/// [`Priority`] is `#[non_exhaustive]` and a tier added upstream has to be added
55/// here in its place in the sequence, or a table will never narrow to it. Grep
56/// this when adopting a new `makeover-layout`; `makeover-tui` carries the same
57/// list for the same reason, and the two have to agree or a description narrows
58/// differently in a window than in a terminal.
59const CUTOFFS: [Priority; 3] = [Priority::Optional, Priority::Secondary, Priority::Essential];
60
61/// The lengths the description deferred, in points.
62///
63/// [`Width`] says `Content`, `Fixed` or `Fill` and carries no magnitude, because
64/// a magnitude is an answer for one renderer and the description is read by
65/// three. The other two renderers hold this same type over CSS lengths and over
66/// terminal cells.
67#[derive(Debug, Clone, Copy, Default)]
68pub struct Sizing<'a> {
69 /// `(column name, points)`. The track for a [`Width::Fixed`] column, the
70 /// floor for a [`Width::Fill`] one, and the narrowing budget for a
71 /// [`Width::Content`] one.
72 pub lengths: &'a [(&'a str, f32)],
73 /// Used for a column with no entry above.
74 pub fallback: f32,
75}
76
77impl Sizing<'_> {
78 /// The length for a named column.
79 fn length_for(&self, name: &str) -> f32 {
80 self.lengths
81 .iter()
82 .find(|(column, _)| *column == name)
83 .map_or(self.fallback, |(_, length)| *length)
84 }
85}
86
87/// The tones and metrics a table draws with.
88///
89/// Metrics only, and the tones come from [`Palette`]. That is the division this
90/// crate already draws: [`FieldStyle`](crate::FieldStyle) carries gaps and a
91/// marker while the colours stay in the palette, and a table's colours are the
92/// palette's `content`, `content_muted` and `action` rather than six new ones.
93/// `makeover-tui` splits it the other way round because its palette carries no
94/// text tones at all.
95#[derive(Debug, Clone, Copy, PartialEq)]
96pub struct TableStyle {
97 /// The height of the heading row.
98 pub header_height: f32,
99 /// The height of a body row.
100 pub row_height: f32,
101 /// The caret drawn after the heading of an ascending column.
102 ///
103 /// Defaults to [`Sort::glyph`], which is where the spelling lives now:
104 /// three renderers holding the same literal agreed by coincidence. Bare,
105 /// with no leading space -- the gap is [`heading`]'s, written once for all
106 /// three states rather than baked into two strings and forgotten in the
107 /// third.
108 pub ascending: &'static str,
109 /// Drawn after the heading of a descending column.
110 pub descending: &'static str,
111 /// Whether alternate rows take a different background.
112 ///
113 /// egui_extras' own striping, off by default: the description has no word
114 /// for it, and a renderer that turned it on would be adding a claim the
115 /// other two cannot make.
116 ///
117 /// Not every setting egui_extras has becomes a field here. A sticky heading
118 /// is what `TableBuilder::header` does and there is no version that does
119 /// not, so the knob 0.12.0 briefly carried for it offered a choice this
120 /// renderer cannot make. This one and [`resizable`](Self::resizable) are the
121 /// two that pass that test.
122 pub striped: bool,
123 /// Whether the user can drag the divider between two columns.
124 ///
125 /// The second knob that is not a metric, and it passes the same test
126 /// `sticky_header` failed: egui_extras offers both settings and a renderer
127 /// can honestly make either choice. Off by default for `striped`'s reason:
128 /// the description has no word for it, so a default that turned it on would
129 /// be this renderer adding a claim the other two cannot make.
130 ///
131 /// It does not fight the narrowing. A drag moves a track for the frames it
132 /// is held; [`cutoff_for`] still decides which columns exist, off the widths
133 /// the app declared in [`Sizing`], so a resize can never drop a column.
134 pub resizable: bool,
135}
136
137impl Default for TableStyle {
138 fn default() -> Self {
139 Self {
140 header_height: 20.0,
141 row_height: 18.0,
142 ascending: Sort::Ascending.glyph(),
143 descending: Sort::Descending.glyph(),
144 striped: false,
145 resizable: false,
146 }
147 }
148}
149
150/// The body's own facts for this frame: how many rows, which are selected, and
151/// which one to bring into view.
152///
153/// Held apart from [`TableStyle`] because none of it is style and none of it
154/// survives the frame: a row count changes when a folder does, a selection when
155/// the user clicks, and a scroll request exists for exactly one frame. Held
156/// apart from the [`Column`] slice because none of it is description either.
157/// The description says what a table *is*, and this says what it holds right
158/// now.
159///
160/// Both of the optional fields are here rather than left to the app because
161/// egui_extras answers them on a handle the app never sees: `set_selected` is a
162/// method on the row, and `scroll_to_row` a method on the builder, and this
163/// crate owns both. That is the same reason [`cell`] exists.
164#[derive(Default)]
165pub struct Body<'a> {
166 /// How many rows to draw.
167 pub rows: usize,
168 /// Whether a row is selected, by index.
169 ///
170 /// A predicate rather than a set, so an app whose selection is a range, a
171 /// bitmap or a single index does not have to build a collection to be asked.
172 /// `None` is a table no row of which is selected, which is not the same
173 /// claim as a predicate that always answers false and costs nothing to make.
174 pub selected: Option<&'a dyn Fn(usize) -> bool>,
175 /// A row to bring into view this frame.
176 ///
177 /// Set it from a request the app then clears, the way a keyboard cursor
178 /// moving off-screen raises one: held rather than taken, it would fight
179 /// every scroll the user makes with the mouse.
180 pub scroll_to: Option<usize>,
181}
182
183impl std::fmt::Debug for Body<'_> {
184 // Hand-written because `selected` is a closure and `#[derive(Debug)]` will
185 // not have it. What is worth printing is whether one was supplied.
186 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187 f.debug_struct("Body")
188 .field("rows", &self.rows)
189 .field("selected", &self.selected.is_some())
190 .field("scroll_to", &self.scroll_to)
191 .finish()
192 }
193}
194
195/// The colour a cell of this part takes.
196///
197/// [`CellPart`] is `#[non_exhaustive]`, and a member added upstream lands on
198/// `content`: a part this renderer has not learned draws as text, which is a
199/// cell rendering plainly rather than a build that stops. Grep this when
200/// adopting a new `makeover-layout`.
201#[must_use]
202pub const fn part_color(part: Option<CellPart>, palette: &Palette) -> egui::Color32 {
203 match part {
204 // A token paints its own background and carries its own tone. What is
205 // set here is what shows between them, not what paints them.
206 Some(CellPart::Tokens) => palette.content_muted,
207 // The drift `CellPart` exists to end: a control in a cell inheriting the
208 // cell's text colour. Both of these take the action intent instead.
209 Some(CellPart::Actions | CellPart::Link) => palette.action,
210 _ => palette.content,
211 }
212}
213
214/// Draw a cell's contents with the tone its part takes.
215///
216/// The app calls this inside its own cell closure, wrapping whatever it draws.
217/// A scoping function rather than a parameter on [`table`], for the reason
218/// [`frame`](crate::frame) is one: the part is a property of the cell, the cell
219/// does not exist until the closure runs, and immediate mode has no cascade to
220/// carry the answer down on its own. This is the cascade, for one scope.
221///
222/// ```no_run
223/// # use makeover_layout::CellPart;
224/// # let palette: makeover_immediate::Palette = unimplemented!();
225/// # let ui: &mut egui::Ui = unimplemented!();
226/// makeover_immediate::table::cell(ui, Some(CellPart::Link), &palette, |ui| {
227/// ui.label("opens the item");
228/// });
229/// ```
230pub fn cell<R>(
231 ui: &mut Ui,
232 part: Option<CellPart>,
233 palette: &Palette,
234 add_contents: impl FnOnce(&mut Ui) -> R,
235) -> R {
236 let restore = ui.visuals().override_text_color;
237 ui.visuals_mut().override_text_color = Some(part_color(part, palette));
238 let out = add_contents(ui);
239 ui.visuals_mut().override_text_color = restore;
240 out
241}
242
243/// The heading, with the caret if the table is ordered by this column.
244///
245/// A column [`sorted`](Column::sorted) but not [`sortable`](Column::sortable)
246/// still gets its caret. Both combinations mean something, which is why the
247/// description holds the two fields apart: a list ordered by a key the user
248/// cannot change is a real thing, and the caret is how it says so.
249#[must_use]
250pub fn heading(column: &Column<'_>, style: &TableStyle) -> String {
251 let caret = match column.sorted {
252 Some(Sort::Ascending) => style.ascending,
253 Some(Sort::Descending) => style.descending,
254 // Sortable and not sorted draws the idle mark, in the ascending
255 // spelling because that is the direction a first press takes. What
256 // separates it from the column in force is the tone, which is
257 // [`press`]'s to pick.
258 None if column.sortable => style.ascending,
259 None => return column.name.to_owned(),
260 };
261 format!("{} {caret}", column.name)
262}
263
264/// How wide a column asks to be at its narrowest, in points.
265fn min_width(column: &Column<'_>, sizing: &Sizing<'_>) -> f32 {
266 // Every arm is the declared length, including `Content`: nothing can be
267 // measured before the app's closure has drawn it. See the module header on
268 // what immediate mode costs the narrowing.
269 sizing.length_for(column.name)
270}
271
272/// Whether the columns kept at `cutoff` fit in `width`.
273fn fits(columns: &[Column<'_>], sizing: &Sizing<'_>, cutoff: Priority, width: f32) -> bool {
274 columns
275 .iter()
276 .filter(|c| c.kept_at(cutoff))
277 .map(|c| min_width(c, sizing))
278 .sum::<f32>()
279 <= width
280}
281
282/// The weakest cutoff whose columns fit in `width`.
283///
284/// Raised until the layout fits, and never past [`Priority::Essential`]: the
285/// essential columns are what makes a row identify itself, so a window too
286/// narrow for them gets them squeezed rather than dropped. Nothing here counts
287/// positions, so which column drops is a property of the column.
288#[must_use]
289pub fn cutoff_for(columns: &[Column<'_>], sizing: &Sizing<'_>, width: f32) -> Priority {
290 for cutoff in CUTOFFS {
291 if fits(columns, sizing, cutoff, width) {
292 return cutoff;
293 }
294 }
295 Priority::Essential
296}
297
298/// The track for one column.
299fn track(column: &Column<'_>, sizing: &Sizing<'_>) -> Track {
300 match column.width {
301 // The one place immediate mode beats the terminal: egui_extras measures
302 // this and remembers it between frames, where `makeover-tui` has to walk
303 // the cells itself.
304 Width::Content => Track::auto(),
305 Width::Fixed => Track::exact(sizing.length_for(column.name)),
306 // Includes a width added to the description since this renderer was
307 // built. Taking the slack above a floor is the behaviour that makes no
308 // claim, which is the same fallback the webview renderer's `auto` track
309 // is chosen to be.
310 _ => Track::remainder().at_least(sizing.length_for(column.name)),
311 }
312}
313
314/// A described table, narrowed for the width available.
315///
316/// `draw` is called once per cell of each kept column, in column order, for each
317/// of [`Body::rows`] rows. Taking a closure rather than a slice of contents is
318/// what keeps the app's own data borrowed one cell at a time, which is
319/// [`group`](crate::group)'s reasoning and immediate mode's habit.
320///
321/// `body` is borrowed immutably and `draw` is `FnMut`, which is the split a
322/// caller has to plan for: a selection read by [`Body::selected`] cannot be the
323/// same value `draw` mutates. Snapshot it before the call. That is not this
324/// crate imposing anything. It is the borrow the app already takes when it
325/// clones its row list to hand egui a closure.
326///
327/// Returns the sortable column whose heading was pressed this frame, if any. The
328/// app owns the ordering, so this reports the press and changes nothing: what a
329/// press *calls* is an address, and the description names none. That is
330/// [`Column::sortable`]'s own documented split.
331///
332/// A heading is only pressable when its column says
333/// [`sortable`](Column::sortable). A column sorted by a key the user cannot
334/// change still draws its caret and does not answer.
335pub fn table<'a>(
336 ui: &mut Ui,
337 columns: &'a [Column<'a>],
338 body: &Body<'_>,
339 sizing: &Sizing<'_>,
340 palette: &Palette,
341 style: &TableStyle,
342 mut draw: impl FnMut(&mut Ui, &'a Column<'a>, usize),
343) -> Option<&'a Column<'a>> {
344 let cutoff = cutoff_for(columns, sizing, ui.available_width());
345 let kept: Vec<&'a Column<'a>> = columns.iter().filter(|c| c.kept_at(cutoff)).collect();
346
347 // egui_extras panics on a table with no tracks, and a description whose
348 // every column dropped is reachable: `kept_at` keeps the essential ones, and
349 // a table described with none at all has nothing to keep.
350 if kept.is_empty() {
351 return None;
352 }
353
354 let mut builder = TableBuilder::new(ui)
355 .striped(style.striped)
356 .resizable(style.resizable)
357 // Not a knob, because there is no second honest answer: a cell's
358 // contents sit on the row's centre line. CSS says `vertical-align:
359 // middle` and a terminal row is one line tall, so a field offering the
360 // choice would be offering one only this renderer could take. egui's own
361 // default is top-aligned, which is why it has to be said at all.
362 .cell_layout(egui::Layout::left_to_right(egui::Align::Center));
363 for column in &kept {
364 builder = builder.column(track(column, sizing));
365 }
366 if let Some(row) = body.scroll_to {
367 builder = builder.scroll_to_row(row, None);
368 }
369
370 // Written through a Cell rather than returned, because egui_extras hands the
371 // header and the body their own closures and neither can return a value past
372 // the other.
373 let pressed = std::cell::Cell::new(None::<&'a Column<'a>>);
374
375 builder
376 .header(style.header_height, |mut header| {
377 for column in &kept {
378 header.col(|ui| {
379 if press(ui, column, palette, style) {
380 pressed.set(Some(column));
381 }
382 });
383 }
384 })
385 .body(|table_body| {
386 table_body.rows(style.row_height, body.rows, |mut row| {
387 let index = row.index();
388 if let Some(selected) = body.selected {
389 // Before the cells, and on the row rather than on any of
390 // them: a selection marks the whole row, and a renderer that
391 // tinted each cell would leave the gaps between them
392 // unpainted.
393 row.set_selected(selected(index));
394 }
395 for column in &kept {
396 row.col(|ui| draw(ui, column, index));
397 }
398 });
399 });
400
401 pressed.get()
402}
403
404/// What one heading is drawn in.
405///
406/// Three states, three tones (wiki `three-tone-convention`). The column in force
407/// is the emphasised thing; a column offering to reorder is inactive but usable,
408/// because it answers a press; a column that is not a control at all is inert.
409///
410/// The middle one used to take `content_muted`, which is what
411/// [`State::Disabled`](makeover_layout::State::Disabled) resolves to, so a
412/// heading the user could press claimed it would not answer. The same lie the
413/// unchosen option in a choice field was telling before 0.26.0.
414fn heading_color(column: &Column<'_>, palette: &Palette) -> egui::Color32 {
415 match (column.sorted, column.sortable) {
416 (Some(_), _) => palette.content,
417 (None, true) => palette.content_secondary,
418 (None, false) => palette.content_muted,
419 }
420}
421
422/// One heading, and whether it was pressed.
423fn press(ui: &mut Ui, column: &Column<'_>, palette: &Palette, style: &TableStyle) -> bool {
424 let text = RichText::new(heading(column, style)).color(heading_color(column, palette));
425 if !column.sortable {
426 // Not sensed. A heading a user cannot press must not look like one they
427 // can, which is the affordance `Column::sortable` exists to carry, and
428 // the tone above is half of saying so.
429 ui.label(text.strong());
430 return false;
431 }
432 let response: Response = ui
433 .add(egui::Label::new(text.strong()).sense(Sense::click()))
434 .on_hover_cursor(egui::CursorIcon::PointingHand);
435 response.clicked()
436}
437
438#[cfg(test)]
439mod tests {
440 use super::*;
441 use egui::Color32;
442
443 fn palette() -> Palette {
444 Palette {
445 page: Color32::from_rgb(1, 1, 1),
446 raised: Color32::from_rgb(2, 2, 2),
447 overlay: Color32::from_rgb(3, 3, 3),
448 well: Color32::from_rgb(4, 4, 4),
449 sunken: Color32::from_rgb(5, 5, 5),
450 bevel_light: Color32::WHITE,
451 bevel_dark: Color32::BLACK,
452 elevation: Color32::from_black_alpha(46),
453 content: Color32::from_rgb(6, 6, 6),
454 content_secondary: Color32::from_rgb(56, 56, 56),
455 content_muted: Color32::from_rgb(7, 7, 7),
456 action: Color32::from_rgb(8, 8, 8),
457 danger: Color32::from_rgb(9, 9, 9),
458 success: Color32::from_rgb(10, 10, 10),
459 warning: Color32::from_rgb(11, 11, 11),
460 info: Color32::from_rgb(12, 12, 12),
461 }
462 }
463
464 fn columns() -> Vec<Column<'static>> {
465 vec![
466 Column {
467 name: "name",
468 width: Width::Fill,
469 priority: Priority::Essential,
470 sortable: true,
471 sorted: Some(Sort::Ascending),
472 },
473 Column {
474 name: "size",
475 width: Width::Fixed,
476 priority: Priority::Secondary,
477 sortable: true,
478 sorted: None,
479 },
480 Column {
481 name: "note",
482 width: Width::Content,
483 priority: Priority::Optional,
484 sortable: false,
485 sorted: None,
486 },
487 ]
488 }
489
490 fn sizing() -> Sizing<'static> {
491 Sizing {
492 lengths: &[("name", 120.0), ("size", 60.0), ("note", 80.0)],
493 fallback: 40.0,
494 }
495 }
496
497 #[test]
498 fn narrowing_drops_the_optional_column_first_and_the_essential_one_never() {
499 let (cols, sz) = (columns(), sizing());
500 assert_eq!(cutoff_for(&cols, &sz, 300.0), Priority::Optional);
501 assert_eq!(cutoff_for(&cols, &sz, 200.0), Priority::Secondary);
502 assert_eq!(cutoff_for(&cols, &sz, 150.0), Priority::Essential);
503 // Narrower than the essential column, which stays anyway.
504 assert_eq!(cutoff_for(&cols, &sz, 10.0), Priority::Essential);
505 }
506
507 #[test]
508 fn a_column_inserted_left_of_the_cut_does_not_change_what_drops() {
509 // The goingson bug, as a test. `nth-child(n+5)` against a seven-column
510 // table hides whatever lands at position five, so inserting a column
511 // moves the cut onto a different column with nothing edited.
512 //
513 // Asserted at a fixed cutoff, because that is where the two ways of
514 // addressing a column disagree. A narrower budget SHOULD drop more; what
515 // must not change is which ones, for a given cutoff.
516 let dropped = |cols: &[Column<'_>], cutoff| -> Vec<String> {
517 cols.iter()
518 .filter(|c| !c.kept_at(cutoff))
519 .map(|c| c.name.to_owned())
520 .collect()
521 };
522 let before = columns();
523 let mut after = vec![Column {
524 name: "mark",
525 width: Width::Fixed,
526 priority: Priority::Essential,
527 sortable: false,
528 sorted: None,
529 }];
530 after.extend(columns());
531
532 for cutoff in CUTOFFS {
533 assert_eq!(dropped(&before, cutoff), dropped(&after, cutoff));
534 }
535 assert_eq!(dropped(&before, Priority::Secondary), vec!["note"]);
536 }
537
538 #[test]
539 fn the_two_renderers_narrow_a_description_the_same_way() {
540 // The cutoff ladder is duplicated in `makeover-tui` because neither
541 // crate depends on the other, and duplication is what drifts. This is
542 // the assertion that would catch it: the ladder is the description's
543 // order, weakest first, and a tier added upstream belongs in both.
544 assert_eq!(CUTOFFS.len(), 3);
545 assert!(CUTOFFS.windows(2).all(|pair| pair[0] < pair[1]));
546 assert_eq!(CUTOFFS[0], Priority::Optional);
547 assert_eq!(CUTOFFS[2], Priority::Essential);
548 }
549
550 #[test]
551 fn a_content_column_is_measured_by_egui_and_budgeted_by_its_floor() {
552 // The split the module header names. The track defers to egui_extras,
553 // which can measure; the narrowing cannot wait for that and uses the
554 // declared floor. Both readings of the same column, and both honest.
555 let cols = columns();
556 let sz = sizing();
557 let note = &cols[2];
558 assert!(matches!(note.width, Width::Content));
559 assert!((min_width(note, &sz) - 80.0).abs() < f32::EPSILON);
560 // 120 + 60 + 80 is 260, so 300 fits and 250 does not.
561 assert!(fits(&cols, &sz, Priority::Optional, 300.0));
562 assert!(!fits(&cols, &sz, Priority::Optional, 250.0));
563 }
564
565 #[test]
566 fn a_column_with_no_length_of_its_own_takes_the_fallback() {
567 let column = Column {
568 name: "unlisted",
569 width: Width::Fixed,
570 priority: Priority::Essential,
571 sortable: false,
572 sorted: None,
573 };
574 assert!((min_width(&column, &sizing()) - 40.0).abs() < f32::EPSILON);
575 }
576
577 #[test]
578 fn the_parts_a_cell_can_be_are_coloured_apart() {
579 // The drift `CellPart` exists to end: one colour for a whole cell paints
580 // a control as though it were text.
581 let p = palette();
582 assert_eq!(part_color(Some(CellPart::Value), &p), p.content);
583 assert_eq!(part_color(Some(CellPart::Tokens), &p), p.content_muted);
584 assert_eq!(part_color(Some(CellPart::Actions), &p), p.action);
585 assert_eq!(part_color(Some(CellPart::Link), &p), p.action);
586 assert_ne!(part_color(Some(CellPart::Link), &p), p.content);
587 // A cell mixing parts says nothing, and takes the text colour.
588 assert_eq!(part_color(None, &p), p.content);
589 }
590
591 #[test]
592 fn a_heading_carries_a_caret_when_it_is_ordered_by_or_offers_to_be() {
593 let style = TableStyle::default();
594 let cols = columns();
595 assert_eq!(heading(&cols[0], &style), "name \u{25B2}");
596 // Sortable and idle. It draws the mark a first press would give, which
597 // is what stops the press from widening the column and shifting the
598 // ones after it.
599 assert_eq!(heading(&cols[1], &style), "size \u{25B2}");
600 // Not a control. Nothing to mark.
601 assert_eq!(heading(&cols[2], &style), "note");
602 }
603
604 #[test]
605 fn the_three_states_of_a_heading_are_three_tones() {
606 // wiki `three-tone-convention`. The middle state used to take
607 // content_muted, which is what `State::Disabled` resolves to, so a
608 // heading the user could press claimed it would not answer. The arm
609 // that keeps muted is the one where it is true.
610 let p = palette();
611 let cols = columns();
612 assert_eq!(heading_color(&cols[0], &p), p.content);
613 assert_eq!(heading_color(&cols[1], &p), p.content_secondary);
614 assert_eq!(heading_color(&cols[2], &p), p.content_muted);
615 }
616
617 #[test]
618 fn a_column_sorted_without_being_sortable_still_draws_its_caret() {
619 // A list ordered by a key the user cannot change is a real thing to
620 // describe, which is why the description holds the two fields apart.
621 let column = Column {
622 name: "rank",
623 width: Width::Content,
624 priority: Priority::Essential,
625 sortable: false,
626 sorted: Some(Sort::Descending),
627 };
628 assert_eq!(heading(&column, &TableStyle::default()), "rank \u{25BC}");
629 }
630
631 #[test]
632 fn the_carets_match_the_terminal_renderers() {
633 // Two crates, one glyph pair, and no dependency between them to enforce
634 // it. A description sorted ascending must not point up in a window and
635 // down in a terminal.
636 // Composition rather than agreement since makeover-layout 0.27.5: both
637 // read `Sort::glyph`, so a fourth spelling cannot appear in one crate.
638 let style = TableStyle::default();
639 assert_eq!(style.ascending, Sort::Ascending.glyph());
640 assert_eq!(style.descending, Sort::Descending.glyph());
641 // Bare. The gap is `heading`'s, so a consumer swapping the glyph for an
642 // ASCII one does not have to remember to bring a space with it.
643 assert_eq!(style.ascending.trim(), style.ascending);
644 }
645
646 #[test]
647 fn striping_is_off_because_the_description_has_no_word_for_it() {
648 // egui_extras offers it and the other two renderers cannot say it. A
649 // default that turned it on would be this renderer adding a claim.
650 assert!(!TableStyle::default().striped);
651 // Same test, same answer, and the reason `sticky_header` failed it: that
652 // one had no second setting to offer.
653 assert!(!TableStyle::default().resizable);
654 }
655
656 #[test]
657 fn a_body_claims_nothing_until_it_is_asked_to() {
658 // The default is a table of no rows, no selection and no scroll
659 // request. All three absences are the honest reading of an app that has
660 // not said otherwise, which is why they are `Option` and not a
661 // predicate that always answers false.
662 let body = Body::default();
663 assert_eq!(body.rows, 0);
664 assert!(body.selected.is_none());
665 assert!(body.scroll_to.is_none());
666 }
667
668 #[test]
669 fn a_selection_is_asked_per_row_and_not_collected() {
670 // A predicate, so an app whose selection is a range or a single index
671 // does not build a set to be asked. Exercised the way `table` asks it:
672 // once per row index, in order.
673 let selected = |index: usize| index.is_multiple_of(2);
674 let body = Body {
675 rows: 4,
676 selected: Some(&selected),
677 scroll_to: None,
678 };
679 let f = body.selected.expect("a predicate was supplied");
680 assert_eq!(
681 (0..body.rows).map(f).collect::<Vec<_>>(),
682 vec![true, false, true, false]
683 );
684 }
685
686 #[test]
687 fn narrowing_reads_the_declared_widths_and_not_a_dragged_track() {
688 // `resizable` lets the user move a divider, and `cutoff_for` must not
689 // hear about it: a drag that could drop a column would make the
690 // narrowing a thing the user does by accident rather than a property of
691 // the description. That `cutoff_for` takes no `TableStyle` at all is the
692 // structural half of the guarantee; this is the behavioural half, and it
693 // is what would fail if a measured width were ever threaded in beside
694 // the declared one.
695 let (cols, sz) = (columns(), sizing());
696 assert_eq!(cutoff_for(&cols, &sz, 300.0), Priority::Optional);
697 assert_eq!(cutoff_for(&cols, &sz, 200.0), Priority::Secondary);
698 }
699}