gpui-kit-grid 2.0.0

A live data grid for GPUI applications built with gpui-kit: real-time rows by key from any thread, change highlight, and sorting, filtering, grouping and trees in the background.
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
//! Tooltips: what the tooltip hooks of the delegate know and return, the
//! hovered body cell with its tooltip slot, and the tooltips of header
//! cells.
//!
//! The body paints without an element per cell, so a cell tooltip hangs on
//! one transparent div over the hovered cell, planned with the frame. Its
//! id is the hovered row's key and column, so a row moving away from under
//! the pointer is a new element: GPUI drops the old tooltip, and a tooltip
//! never shows another row's data. While the id stays, the frame writes the
//! hook's fresh result into the slot the shown tooltip reads, so a ticking
//! price updates in place (Design Guides, "Motion"). A pinned row's key is
//! of its band's own key space, so its slot hashes the edge with the key: a
//! band row and a data row with the same key are different elements. A
//! group caption's text and aggregate cells hash its group path
//! (`group_hash`), a group footer's cells its path (`footer_hash`), the
//! totals row's cells its band (`totals_hash`).

use std::{
    cell::{Cell, RefCell},
    fmt,
    rc::Rc,
};

use gpui_kit::component::tooltip::Tooltip;
use gpui_kit::{
    AnyElement, App, Bounds, Context, ElementId, Empty, InteractiveElement as _, IntoElement,
    Pixels, Point, SharedString, StatefulInteractiveElement as _, Styled as _, Window, div, px,
};

use rust_i18n::t;

use super::{
    CELL_PADDING, ColumnOf, HeaderLabel, LiveGridDelegate, LiveGridState,
    edit::EditState,
    header::{HEADER_SORT_LANE, SORT_GAP, SORT_ICON, header_icon_lane, header_label_style},
    pinned::BodyLine,
};
use crate::model::{CellValue, ColumnIx, GridColumns, GridRow};

/// Tooltip content of a cell or a header cell: text, or an element built
/// each time the tooltip draws.
#[derive(Clone)]
pub struct CellTooltip {
    content: TooltipContent,
}

#[derive(Clone)]
enum TooltipContent {
    Text(SharedString),
    Element(Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>),
}

impl CellTooltip {
    /// A text tooltip.
    pub fn text(text: impl Into<SharedString>) -> Self {
        Self {
            content: TooltipContent::Text(text.into()),
        }
    }

    /// A tooltip with custom content, built each time the tooltip draws.
    pub fn element(build: impl Fn(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
        Self {
            content: TooltipContent::Element(Rc::new(build)),
        }
    }

    /// The text of a text tooltip; `None` for custom content.
    pub fn as_text(&self) -> Option<&SharedString> {
        match &self.content {
            TooltipContent::Text(text) => Some(text),
            TooltipContent::Element(_) => None,
        }
    }

    /// The content as an element.
    fn render(&self, window: &mut Window, cx: &mut App) -> AnyElement {
        match &self.content {
            TooltipContent::Text(text) => text.clone().into_any_element(),
            TooltipContent::Element(build) => build(window, cx),
        }
    }

    /// The tooltip view of gpui-kit with this content.
    pub(super) fn into_view(self, window: &mut Window, cx: &mut App) -> gpui_kit::AnyView {
        Tooltip::element(move |window, cx| self.render(window, cx)).build(window, cx)
    }
}

impl fmt::Debug for CellTooltip {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.content {
            TooltipContent::Text(text) => f.debug_tuple("CellTooltip::Text").field(text).finish(),
            TooltipContent::Element(_) => f.write_str("CellTooltip::Element"),
        }
    }
}

/// What [`LiveGridDelegate::tooltip`] knows about the hovered cell.
pub struct CellTooltipContext<'a, R: GridRow> {
    pub(super) row: &'a R,
    pub(super) column: R::Column,
    pub(super) value: CellValue<'a>,
    pub(super) text: &'a str,
    pub(super) truncated: bool,
    pub(super) edit_state: Option<&'a EditState>,
}

impl<'a, R: GridRow> CellTooltipContext<'a, R> {
    /// The row.
    pub fn row(&self) -> &'a R {
        self.row
    }

    /// The column.
    pub fn column(&self) -> R::Column {
        self.column
    }

    /// The value shown in the cell.
    pub fn value(&self) -> CellValue<'a> {
        self.value
    }

    /// The full text of the cell, as painted before truncation.
    pub fn text(&self) -> &str {
        self.text
    }

    /// Returns true if the painted text was cut with "…".
    pub fn is_truncated(&self) -> bool {
        self.truncated
    }

    /// The cell's edits: a commit that waits for an answer or for the
    /// store, or a refused one; `None` for a cell with no edits.
    pub fn edit_state(&self) -> Option<&'a EditState> {
        self.edit_state
    }

    /// The default tooltip: "Saving…" while a commit waits for an answer,
    /// "Not saved: " and the reason after a refused one; otherwise the full
    /// text of a truncated cell, and none for a cell that shows all its
    /// text.
    pub fn default_tooltip(&self) -> Option<CellTooltip> {
        match self.edit_state {
            Some(EditState::Pending) => {
                return Some(CellTooltip::text(SharedString::from(
                    t!("live_grid.edit.pending").into_owned(),
                )));
            }
            Some(EditState::Failed(error)) => {
                return Some(CellTooltip::text(SharedString::from(
                    t!("live_grid.edit.failed", message = error.message()).into_owned(),
                )));
            }
            _ => {}
        }
        self.truncated
            .then(|| CellTooltip::text(SharedString::from(self.text.to_owned())))
    }
}

/// What [`LiveGridDelegate::header_tooltip`] knows about the hovered header
/// cell.
pub struct HeaderTooltipContext<'a, C> {
    pub(super) column: C,
    pub(super) title: &'a SharedString,
    pub(super) truncated: bool,
    // The title, an icon or both.
    pub(super) label: &'a HeaderLabel,
}

impl<'a, C: GridColumns> HeaderTooltipContext<'a, C> {
    /// The column.
    pub fn column(&self) -> C {
        self.column
    }

    /// The column title.
    pub fn title(&self) -> &'a SharedString {
        self.title
    }

    /// Returns true if the title is cut with "…"; false for a header that
    /// shows its icon alone.
    pub fn is_truncated(&self) -> bool {
        self.truncated
    }

    /// What the header shows: the title, an icon and the title, or the icon
    /// alone.
    pub fn header_label(&self) -> &'a HeaderLabel {
        self.label
    }

    /// The full title when it is truncated, and always for a header that
    /// shows its icon alone, whose meaning would otherwise rest on the icon;
    /// otherwise none.
    pub fn default_tooltip(&self) -> Option<CellTooltip> {
        (self.truncated || matches!(self.label, HeaderLabel::Icon(_)))
            .then(|| CellTooltip::text(self.title.clone()))
    }
}

/// The tooltip of the hovered cell, shared by the frame, which writes the
/// hook's result every frame, and the shown tooltip view, which draws it.
#[derive(Default)]
pub(crate) struct TooltipSlot {
    content: RefCell<Option<CellTooltip>>,
    /// GPUI built the tooltip view for this slot: it is shown, or was until
    /// a press (GPUI does not report hiding it).
    shown: Cell<bool>,
}

impl TooltipSlot {
    fn render(&self, window: &mut Window, cx: &mut App) -> AnyElement {
        // Cloned out first: the content may read anything but this slot.
        let content = self.content.borrow().clone();
        match content {
            Some(content) => content.render(window, cx),
            None => Empty.into_any_element(),
        }
    }
}

/// The hovered body cell of one frame: the transparent div the body lays
/// out, prepaints and paints over the cell (as `uniform_list` does its
/// items), carrying the GPUI tooltip.
pub(crate) struct CellHover {
    id: ElementId,
    bounds: Bounds<Pixels>,
    slot: Rc<TooltipSlot>,
}

impl CellHover {
    /// The cell within the mask of its zone, in window coordinates.
    pub(crate) fn bounds(&self) -> Bounds<Pixels> {
        self.bounds
    }

    /// The div over the cell.
    pub(crate) fn into_element(self) -> AnyElement {
        let slot = self.slot;
        div()
            .id(self.id)
            .w(self.bounds.size.width)
            .h(self.bounds.size.height)
            .tooltip(move |window, cx| {
                slot.shown.set(true);
                let slot = slot.clone();
                Tooltip::element(move |window, cx| slot.render(window, cx)).build(window, cx)
            })
            .into_any_element()
    }
}

/// The hovered body cell as the frame found it.
pub(super) struct HoveredCell {
    pub(super) key_hash: u64,
    pub(super) column: ColumnIx,
    pub(super) id: &'static str,
    pub(super) bounds: Bounds<Pixels>,
    pub(super) tooltip: Option<CellTooltip>,
}

/// The slot of the hovered cell, by the identity its div's id is made of.
struct HoverSlot {
    key_hash: u64,
    column: ColumnIx,
    slot: Rc<TooltipSlot>,
}

/// The pointer over the body and the header, for tooltips.
#[derive(Default)]
pub(super) struct TooltipState {
    /// The last pointer position over the body.
    pub(super) position: Option<Point<Pixels>>,
    /// The body line and column under `position` when it was set: a move
    /// within one cell does not repaint.
    cell: Option<(BodyLine, Option<ColumnIx>)>,
    slot: Option<HoverSlot>,
    /// The header cell under the pointer.
    header: Option<ColumnIx>,
}

impl TooltipState {
    /// A press hides GPUI's tooltip.
    pub(super) fn on_press(&mut self) {
        if let Some(hover) = &self.slot {
            hover.slot.shown.set(false);
        }
    }

    /// Returns true while the tooltip of the hovered cell is shown.
    pub(super) fn is_shown(&self) -> bool {
        self.slot
            .as_ref()
            .is_some_and(|hover| hover.slot.shown.get())
    }

    /// The header cell under the pointer.
    pub(super) fn header(&self) -> Option<ColumnIx> {
        self.header
    }
}

impl<D: LiveGridDelegate> LiveGridState<D> {
    /// The screen line of the data rows under a window position, from the
    /// top of the data rows; `None` over a pinned band or outside the body.
    pub(super) fn line_at(&self, position: Point<Pixels>) -> Option<usize> {
        let rows = self.rows_bounds();
        let y = position.y - rows.origin.y + self.scroll_offset;
        (position.y >= rows.origin.y && position.y < rows.bottom())
            .then(|| (y / self.row_height).floor() as usize)
    }

    /// A button went down anywhere in the window, in the capture phase:
    /// GPUI hides the shown tooltip, and an open editor the press is
    /// outside of ends first.
    pub(crate) fn on_window_mouse_down(
        &mut self,
        position: Point<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.tooltip.on_press();
        self.commit_edit_on_press(position, window, cx);
    }

    /// The pointer moved over the body (`Some`) or off it (`None`). Repaints
    /// only when the cell under it changes: O(1) for the line, O(log
    /// columns) for the column.
    pub(crate) fn set_hover_position(
        &mut self,
        position: Option<Point<Pixels>>,
        cx: &mut Context<Self>,
    ) {
        let cell = position.and_then(|position| {
            Some((
                self.body_line_at(position)?,
                self.column_at_position(position),
            ))
        });
        self.tooltip.position = position;
        self.update_link_hover(position, cx);
        if self.tooltip.cell != cell {
            self.tooltip.cell = cell;
            cx.notify();
        }
    }

    /// The line and column of the cell under the pointer this frame, if a
    /// tooltip may show: not while a press, a column gesture or the menu is
    /// in progress. Called after the frame has its bounds and scroll
    /// position, so a row moved under a resting pointer is the new row.
    pub(super) fn hover_target(&self) -> Option<(BodyLine, ColumnIx)> {
        if self.press.is_some() || self.is_column_gesture_active() || self.is_menu_open() {
            return None;
        }
        let position = self.tooltip.position?;
        Some((
            self.body_line_at(position)?,
            self.column_at_position(position)?,
        ))
    }

    /// The body line and the pointer position this frame, if a tooltip may
    /// show (as [`Self::hover_target`]): a group caption's text is not a
    /// column's cell.
    pub(super) fn hover_point(&self) -> Option<(BodyLine, Point<Pixels>)> {
        if self.press.is_some() || self.is_column_gesture_active() || self.is_menu_open() {
            return None;
        }
        let position = self.tooltip.position?;
        Some((self.body_line_at(position)?, position))
    }

    /// Keeps the slot of the hovered cell while its identity stays, fills
    /// it with the hook's fresh result, and plans the div. Without a
    /// tooltip there is no div.
    pub(super) fn finish_hover(&mut self, hovered: Option<HoveredCell>) -> Option<CellHover> {
        let Some(HoveredCell {
            key_hash,
            column,
            id,
            bounds,
            tooltip: Some(tooltip),
        }) = hovered.filter(|hovered| {
            hovered.bounds.size.width > px(0.) && hovered.bounds.size.height > px(0.)
        })
        else {
            self.tooltip.slot = None;
            return None;
        };
        let slot = match &self.tooltip.slot {
            Some(hover) if hover.key_hash == key_hash && hover.column == column => {
                hover.slot.clone()
            }
            _ => {
                let slot = Rc::new(TooltipSlot::default());
                self.tooltip.slot = Some(HoverSlot {
                    key_hash,
                    column,
                    slot: slot.clone(),
                });
                slot
            }
        };
        slot.content.replace(Some(tooltip));
        Some(CellHover {
            id: ElementId::NamedInteger(SharedString::new_static(id), key_hash),
            bounds,
            slot,
        })
    }

    /// Returns true while a cell tooltip is shown: GPUI built it for the
    /// hovered cell, which has not changed since, and no button was pressed
    /// (what `ReorderContext::is_tooltip_shown` tells the reorder policy).
    #[cfg(any(test, feature = "test-support"))]
    pub fn is_tooltip_shown(&self) -> bool {
        self.tooltip.is_shown()
    }

    /// The text of the tooltip the last frame planned for the body cell
    /// under the pointer, shown or not yet; `None` without one, or for
    /// custom content.
    #[cfg(any(test, feature = "test-support"))]
    pub fn hovered_tooltip_text(&self) -> Option<SharedString> {
        let hover = self.tooltip.slot.as_ref()?;
        let content = hover.slot.content.borrow();
        content.as_ref()?.as_text().cloned()
    }

    /// The pointer entered or left a header cell.
    pub(super) fn on_header_cell_hover(
        &mut self,
        column: ColumnIx,
        hovered: bool,
        cx: &mut Context<Self>,
    ) {
        let header = if hovered {
            Some(column)
        } else if self.tooltip.header == Some(column) {
            None
        } else {
            return;
        };
        if self.tooltip.header != header {
            self.tooltip.header = header;
            cx.notify();
        }
    }

    /// The tooltip of the hovered header cell, from the delegate. Called in
    /// `render`, once, for that cell only. The title is truncated when its
    /// width (shaped once and cached, as autofit measures it) and the sort
    /// indicator do not fit between the cell's paddings.
    pub(super) fn header_tooltip(
        &mut self,
        window: &Window,
        cx: &App,
    ) -> Option<(ColumnIx, CellTooltip)> {
        let ix = self.tooltip.header()?;
        let column = self
            .columns
            .get(ix)
            .filter(|_| self.columns.is_visible(ix))?;
        let title = column.title.clone();
        let label = column.header_label.clone();
        let column: ColumnOf<D> = column.column;
        let width = self.columns.width(ix);
        let sort = self.view.sort();
        let indicator = match sort.iter().position(|(sorted, _)| sorted.ix() == ix) {
            None => px(0.),
            Some(_) if sort.len() >= 2 => HEADER_SORT_LANE,
            Some(_) => SORT_GAP + SORT_ICON,
        };
        let (font, font_size) = header_label_style(window);
        // An icon before the title takes room from it; a header that shows
        // the icon alone truncates no title.
        let truncated = match label {
            HeaderLabel::Icon(_) => false,
            HeaderLabel::Title | HeaderLabel::IconAndTitle(_) => {
                let title_width = self
                    .header
                    .labels
                    .width(ix, &title, &font, font_size, window);
                let icon = header_icon_lane(&label, self.row_height);
                title_width > width - CELL_PADDING * 2. - indicator - icon
            }
        };
        let context = HeaderTooltipContext {
            column,
            title: &title,
            truncated,
            label: &label,
        };
        let tooltip = self.delegate.header_tooltip(&context, cx)?;
        Some((ix, tooltip))
    }
}

#[cfg(test)]
mod tests {
    use std::hash::{Hash as _, Hasher as _};

    use gpui_kit::SharedString;
    use rustc_hash::FxHasher;

    use super::{CellTooltip, CellTooltipContext, HeaderLabel, HeaderTooltipContext};
    use crate::{
        model::{CellValue, ColumnIx, GridColumns, GridRow},
        state::key_hash,
    };

    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    struct Note;

    impl GridColumns for Note {
        fn all() -> &'static [Self] {
            &[Note]
        }

        fn id(self) -> &'static str {
            "note"
        }

        fn ix(self) -> ColumnIx {
            ColumnIx::new(0)
        }
    }

    struct Row(String);

    impl GridRow for Row {
        type Key = String;
        type Column = Note;

        fn key(&self) -> &String {
            &self.0
        }

        fn value(&self, _: Note) -> CellValue<'_> {
            CellValue::Text(&self.0)
        }
    }

    #[test]
    fn test_default_tooltips() {
        let row = Row("A note too long for its column".into());
        let cell = |truncated| CellTooltipContext {
            row: &row,
            column: Note,
            value: row.value(Note),
            text: &row.0,
            truncated,
            edit_state: None,
        };
        // The full text, only when truncated.
        let tooltip = cell(true).default_tooltip().expect("truncated");
        assert_eq!(tooltip.as_text().map(|text| &**text), Some(row.0.as_str()));
        assert!(cell(false).default_tooltip().is_none());
        assert_eq!(cell(true).text(), row.0);
        assert_eq!(cell(true).value(), CellValue::Text(&row.0));
        assert!(cell(true).is_truncated());

        let title = SharedString::from("Notes for the trader");
        let header = |truncated| HeaderTooltipContext {
            column: Note,
            title: &title,
            truncated,
            label: &HeaderLabel::Title,
        };
        let tooltip = header(true).default_tooltip().expect("truncated");
        assert_eq!(tooltip.as_text(), Some(&title));
        assert!(header(false).default_tooltip().is_none());
        assert_eq!(header(false).column(), Note);
        // A header that shows its icon alone names it.
        let icon = HeaderLabel::Icon("icons/check.svg".into());
        let icon_only = HeaderTooltipContext {
            column: Note,
            title: &title,
            truncated: false,
            label: &icon,
        };
        assert_eq!(icon_only.header_label(), &icon);
        let tooltip = icon_only.default_tooltip().expect("an icon alone");
        assert_eq!(tooltip.as_text(), Some(&title));

        let custom = CellTooltip::element(|_, _| unreachable!("never drawn here"));
        assert!(custom.as_text().is_none());
        assert_eq!(format!("{custom:?}"), "CellTooltip::Element");
    }

    #[test]
    fn test_key_hash() {
        // `FxHasher` over the key, as `state/a11y.rs` hashes row keys.
        let mut hasher = FxHasher::default();
        42u64.hash(&mut hasher);
        assert_eq!(key_hash(&42u64), hasher.finish());
        assert_eq!(key_hash("key"), key_hash(&String::from("key")));
        assert_ne!(key_hash(&1u32), key_hash(&2u32));
    }
}