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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! A grid without its delegate type: what a toolbar, a status bar or a
//! column chooser needs to read and command any grid by column id
//! ([`LiveGridHandle`]), and what its events say without keys and columns
//! ([`GridHandleEvent`]).
//!
//! The handle holds the typed entity behind an object-safe trait
//! ([`GridOps`]): each method is one `read` or one `update` of the grid.
//! Like the entity, it must not be used from inside an update of the same
//! grid, a delegate hook included: the re-entrant borrow panics. Commands
//! are `Programmatic`, as the typed API's are.

use std::{fmt, hash::Hash, rc::Rc};

use gpui_kit::{
    App, Context, Entity, EntityId, FocusHandle, Focusable as _, Pixels, SharedString,
    Subscription, WeakEntity, Window,
};

#[cfg(not(target_family = "wasm"))]
use super::export::{ExportJob, ExportRequest};
use super::{
    ColumnOf, LiveGridDelegate, LiveGridState,
    chooser::ChooserCommand,
    columns::{ColumnZone, HeaderLabel},
    events::{GroupOrigin, LayoutOrigin, LayoutParts, LiveGridEvent, SelectionOrigin, SortOrigin},
};
#[cfg(not(target_family = "wasm"))]
use crate::export::{CsvFormat, ExportError};
use crate::{
    engine::{ExpandDepth, GroupBy, SortDirection},
    export::ExportScope,
    model::{ColumnId, ColumnIx, GridColumns as _},
    persist::{GridLayout, LayoutError, LayoutGroup, LayoutReport, LayoutSort},
    policy::ReorderPolicy,
};

/// A grid without its delegate type: columns, layout, sort, grouping, quick
/// filter, counts, the held order, copy and export, and its events, all by
/// column id. What a toolbar, a status bar or a column chooser needs.
///
/// Clones refer to the same grid and keep it alive, as an `Entity` does;
/// [`downgrade`](Self::downgrade) for a back reference. Each method is one
/// read or one update of the grid: never call it from inside an update of
/// the same grid, such as a delegate hook (the borrow panics). Edits are
/// not here: they need row keys, the application's own type.
///
/// ```
/// use gpui_kit::{App, IntoElement, ParentElement as _, div};
/// use gpui_kit_grid::LiveGridHandle;
///
/// /// "120 of 4000 rows", whatever the grid's rows are.
/// fn row_counts(grid: &LiveGridHandle, cx: &App) -> impl IntoElement {
///     div().child(format!(
///         "{} of {} rows",
///         grid.row_count(cx),
///         grid.total_row_count(cx)
///     ))
/// }
/// ```
#[derive(Clone)]
pub struct LiveGridHandle {
    ops: Rc<dyn GridOps>,
}

impl LiveGridHandle {
    /// A handle of `grid`.
    pub fn new<D: LiveGridDelegate>(grid: &Entity<LiveGridState<D>>) -> Self {
        Self {
            ops: Rc::new(Ops { grid: grid.clone() }),
        }
    }

    /// The grid entity's id.
    pub fn entity_id(&self) -> EntityId {
        self.ops.entity_id()
    }

    /// A handle that does not keep the grid alive.
    pub fn downgrade(&self) -> WeakLiveGridHandle {
        self.ops.downgrade()
    }

    /// The grid's focus handle: focus it to give the grid the keyboard.
    pub fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.ops.focus_handle(cx)
    }

    // Columns, by `ColumnId` text.

    /// Every schema column in display order (left, scrolling, right zone),
    /// hidden ones at their place. O(columns).
    pub fn columns(&self, cx: &App) -> Vec<ColumnInfo> {
        self.ops.columns(cx)
    }

    /// The schema column `id`; `None` if the schema has none.
    pub fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo> {
        self.ops.column(id, cx)
    }

    /// Shows or hides the column `id`, as
    /// [`LiveGridState::set_column_visible`] does. Returns false, changing
    /// nothing, if the column is unknown or is the last visible one.
    pub fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool {
        self.ops.set_column_visible(id, visible, cx)
    }

    /// Shows every column, with one `LayoutChange`.
    pub fn show_all_columns(&self, cx: &mut App) {
        self.ops.show_all_columns(cx);
    }

    /// Moves the column `id` before the column `before` (`None`: to the end
    /// of its zone), as [`LiveGridState::move_column`] does. Returns false,
    /// changing nothing, if a column is unknown, the zones differ or the
    /// move policy refuses.
    pub fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool {
        self.ops.move_column(id, before, cx)
    }

    /// Moves every column of `band` before the column `before` (`None`: to
    /// the end of the zone), as [`LiveGridState::move_band`] does. Returns
    /// false, changing nothing, if the band or the column is unknown, the
    /// zones differ or the move policy refuses.
    pub fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool {
        self.ops.move_band(band, before, cx)
    }

    /// Moves the column `id` to `zone`, as
    /// [`LiveGridState::set_column_zone`] does. Returns false if the column
    /// is unknown.
    pub fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool {
        self.ops.set_column_zone(id, zone, cx)
    }

    /// Sets the width of the column `id`, within its limits. Returns false
    /// if the column is unknown.
    pub fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool {
        self.ops.set_column_width(id, width, cx)
    }

    /// Fits every visible column to its header and painted rows, as
    /// [`LiveGridState::autofit_columns`] does.
    pub fn autofit_columns(&self, cx: &mut App) {
        self.ops.autofit_columns(cx);
    }

    // Layout, sort, grouping, quick filter.

    /// The view id of the grid's saved layouts, if it has one.
    pub fn view_id(&self, cx: &App) -> Option<SharedString> {
        self.ops.view_id(cx)
    }

    /// The current layout ([`LiveGridState::layout`]).
    pub fn layout(&self, cx: &App) -> GridLayout {
        self.ops.layout(cx)
    }

    /// Restores a saved layout ([`LiveGridState::set_layout`]).
    pub fn set_layout(
        &self,
        layout: &GridLayout,
        cx: &mut App,
    ) -> Result<LayoutReport, LayoutError> {
        self.ops.set_layout(layout, cx)
    }

    /// Restores the default layout ([`LiveGridState::reset_layout`]).
    pub fn reset_layout(&self, cx: &mut App) {
        self.ops.reset_layout(cx);
    }

    /// The sort by column id, most significant first; empty when unsorted.
    pub fn sort(&self, cx: &App) -> Vec<LayoutSort> {
        self.ops.sort(cx)
    }

    /// Replaces the sort, as [`LiveGridState::set_sort`] does. Returns
    /// false, changing nothing, if a column is unknown.
    pub fn set_sort(&self, sort: impl IntoIterator<Item = LayoutSort>, cx: &mut App) -> bool {
        self.ops.set_sort(&sort.into_iter().collect::<Vec<_>>(), cx)
    }

    /// The grouping levels by id, the top level first: a column level by its
    /// column's id, a `GroupKey` level by its own.
    pub fn group_by(&self, cx: &App) -> Vec<LayoutGroup> {
        self.ops.group_by(cx)
    }

    /// Replaces the grouping by column levels, as
    /// [`LiveGridState::set_group_by`] does; no levels turn grouping off.
    /// Returns false, changing nothing, if a column is unknown: a `GroupKey`
    /// level needs the typed state.
    pub fn set_group_by(
        &self,
        levels: impl IntoIterator<Item = LayoutGroup>,
        cx: &mut App,
    ) -> bool {
        self.ops
            .set_group_by(&levels.into_iter().collect::<Vec<_>>(), cx)
    }

    /// How deep groups and tree rows are expanded by default.
    pub fn expand_depth(&self, cx: &App) -> ExpandDepth {
        self.ops.expand_depth(cx)
    }

    /// Sets the default depth and clears every toggle
    /// ([`LiveGridState::set_expand_depth`]).
    pub fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App) {
        self.ops.set_expand_depth(depth, cx);
    }

    /// The quick filter text; empty when the filter is off.
    pub fn quick_filter(&self, cx: &App) -> SharedString {
        self.ops.quick_filter(cx)
    }

    /// Sets the quick filter text ([`LiveGridState::set_quick_filter`]).
    pub fn set_quick_filter(&self, text: impl Into<SharedString>, cx: &mut App) {
        self.ops.set_quick_filter(text.into(), cx);
    }

    // Counts and the order: O(1).

    /// The data rows of the shown order (`Projection::row_count`): rows of
    /// collapsed groups count, pinned rows do not.
    pub fn row_count(&self, cx: &App) -> usize {
        self.ops.row_count(cx)
    }

    /// The live rows of the shown snapshot: the "4000" of "120 of 4000".
    pub fn total_row_count(&self, cx: &App) -> usize {
        self.ops.total_row_count(cx)
    }

    /// The selected rows, those a filter hides included.
    pub fn selected_count(&self, cx: &App) -> usize {
        self.ops.selected_count(cx)
    }

    /// Returns true if a newer order is ready but held by the reorder
    /// policy.
    pub fn is_order_held(&self, cx: &App) -> bool {
        self.ops.is_order_held(cx)
    }

    /// Shows the held order now.
    pub fn apply_held_order(&self, cx: &mut App) {
        self.ops.apply_held_order(cx);
    }

    /// Replaces the reorder policy, as [`LiveGridState::set_reorder_policy`]
    /// does: a held order is offered to the new policy at once. A toolbar's
    /// Live / Hold / Manual switch, without the delegate type.
    pub fn set_reorder_policy(&self, policy: impl ReorderPolicy, cx: &mut App) {
        self.ops.set_reorder_policy(Box::new(policy), cx);
    }

    // Commands.

    /// Copies `scope` to the clipboard ([`LiveGridState::copy_scope`]).
    /// Returns false, leaving the clipboard alone, if there is nothing to
    /// copy.
    pub fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool {
        self.ops.copy_scope(scope, cx)
    }

    /// Returns true if an export of `scope` would have nothing to write
    /// ([`LiveGridState::is_export_empty`]).
    pub fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool {
        self.ops.is_export_empty(scope, cx)
    }

    /// Exports `scope` as CSV to a file at `path`, with a header line and
    /// the grid's cell export ([`LiveGridState::export_to_path`]). Other
    /// formats and requests go through the typed state. Not on the web.
    #[cfg(not(target_family = "wasm"))]
    pub fn export_to_path(
        &self,
        scope: ExportScope,
        format: CsvFormat,
        path: impl Into<std::path::PathBuf>,
        cx: &mut App,
    ) -> Result<ExportJob<std::path::PathBuf>, ExportError> {
        self.ops.export_to_path(scope, format, path.into(), cx)
    }

    // Observing.

    /// Calls `on_event` with what each event of the grid says without keys
    /// and columns ([`GridHandleEvent`]), until the subscription is
    /// dropped. Events with keys (`Click`, `Confirm`, `FocusChange`, edits)
    /// need the typed subscription.
    pub fn subscribe(
        &self,
        cx: &mut App,
        on_event: impl FnMut(&GridHandleEvent, &mut App) + 'static,
    ) -> Subscription {
        self.ops.subscribe(Box::new(on_event), cx)
    }

    /// Calls `on_notify` whenever the grid notifies, until the subscription
    /// is dropped: for a status bar that reads the counts again.
    pub fn observe(&self, cx: &mut App, on_notify: impl FnMut(&mut App) + 'static) -> Subscription {
        self.ops.observe(Box::new(on_notify), cx)
    }

    // The column chooser.

    /// Opens the grid's column chooser, as
    /// [`LiveGridState::show_column_chooser`] does.
    pub fn show_column_chooser(&self, window: &mut Window, cx: &mut App) {
        self.ops.show_column_chooser(window, cx);
    }

    /// Returns true while the grid shows its column chooser.
    pub fn is_column_chooser_open(&self, cx: &App) -> bool {
        self.ops.is_column_chooser_open(cx)
    }

    /// The grid behind the handle, for the column chooser.
    pub(super) fn ops(&self) -> &dyn GridOps {
        &*self.ops
    }
}

impl fmt::Debug for LiveGridHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("LiveGridHandle")
            .field(&self.entity_id())
            .finish()
    }
}

impl<D: LiveGridDelegate> From<&Entity<LiveGridState<D>>> for LiveGridHandle {
    fn from(grid: &Entity<LiveGridState<D>>) -> Self {
        Self::new(grid)
    }
}

/// A [`LiveGridHandle`] that does not keep the grid alive.
#[derive(Clone)]
pub struct WeakLiveGridHandle {
    entity_id: EntityId,
    upgrade: Rc<dyn Fn() -> Option<LiveGridHandle>>,
}

impl WeakLiveGridHandle {
    /// The handle, if the grid is still alive.
    pub fn upgrade(&self) -> Option<LiveGridHandle> {
        (self.upgrade)()
    }

    /// The grid entity's id.
    pub fn entity_id(&self) -> EntityId {
        self.entity_id
    }
}

impl fmt::Debug for WeakLiveGridHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("WeakLiveGridHandle")
            .field(&self.entity_id)
            .finish()
    }
}

/// What a `LiveGridEvent` says without keys and columns.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum GridHandleEvent {
    /// The column layout changed.
    LayoutChange {
        /// What changed.
        parts: LayoutParts,
        /// Who changed it.
        origin: LayoutOrigin,
    },
    /// The sort changed; [`LiveGridHandle::sort`] reads it.
    SortChange {
        /// Why it changed.
        origin: SortOrigin,
    },
    /// The grouping or the tree changed.
    GroupChange {
        /// Who changed it.
        origin: GroupOrigin,
    },
    /// Groups or tree rows were expanded or collapsed.
    ExpansionChange {
        /// Who changed it.
        origin: GroupOrigin,
    },
    /// A new display order is shown.
    ProjectionChange {
        /// Data rows in the shown order.
        row_count: usize,
        /// The version of the store snapshot the order was built from.
        store_version: u64,
    },
    /// The selection changed.
    SelectionChange {
        /// The selected rows, those a filter hides included.
        count: usize,
        /// Why it changed.
        origin: SelectionOrigin,
    },
    /// A newer order is held (`held`), or the held order was applied.
    OrderHeldChange {
        /// Whether a newer order is waiting.
        held: bool,
    },
}

impl GridHandleEvent {
    /// What `event` says without keys and columns; `None` for an event about
    /// rows or cells.
    fn from_grid_event<K: Eq + Hash, C>(event: &LiveGridEvent<K, C>) -> Option<Self> {
        Some(match event {
            LiveGridEvent::LayoutChange { parts, origin, .. } => Self::LayoutChange {
                parts: *parts,
                origin: *origin,
            },
            LiveGridEvent::SortChange { origin, .. } => Self::SortChange { origin: *origin },
            LiveGridEvent::GroupChange { origin } => Self::GroupChange { origin: *origin },
            LiveGridEvent::ExpansionChange { origin, .. } => {
                Self::ExpansionChange { origin: *origin }
            }
            LiveGridEvent::ProjectionChange {
                row_count,
                store_version,
            } => Self::ProjectionChange {
                row_count: *row_count,
                store_version: *store_version,
            },
            LiveGridEvent::SelectionChange { selection, origin } => Self::SelectionChange {
                count: selection.len(),
                origin: *origin,
            },
            LiveGridEvent::OrderHeldChange { held } => Self::OrderHeldChange { held: *held },
            _ => return None,
        })
    }
}

/// A schema column as the grid shows it now.
#[derive(Clone, Debug)]
pub struct ColumnInfo {
    id: ColumnId,
    title: SharedString,
    header_label: HeaderLabel,
    zone: ColumnZone,
    visible: bool,
    width: Pixels,
    bands: Box<[BandInfo]>,
    hideable: bool,
    internal: bool,
    movable: bool,
    resizable: bool,
    sortable: bool,
    groupable: bool,
    sort: Option<(SortDirection, usize)>,
}

impl ColumnInfo {
    /// The column's id.
    pub fn id(&self) -> &ColumnId {
        &self.id
    }

    /// The column's name: its header text, or the tooltip and accessible
    /// name of a header that shows an icon alone.
    pub fn title(&self) -> &SharedString {
        &self.title
    }

    /// The header's icon (its asset path), if it shows one.
    pub fn icon(&self) -> Option<&SharedString> {
        match &self.header_label {
            HeaderLabel::Title => None,
            HeaderLabel::IconAndTitle(icon) | HeaderLabel::Icon(icon) => Some(icon),
        }
    }

    /// What the header shows.
    pub fn header_label(&self) -> &HeaderLabel {
        &self.header_label
    }

    /// The zone the column is in.
    pub fn zone(&self) -> ColumnZone {
        self.zone
    }

    /// Returns true if the column is shown.
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// The column's width.
    pub fn width(&self) -> Pixels {
        self.width
    }

    /// The bands over the column, outermost first.
    pub fn bands(&self) -> &[BandInfo] {
        &self.bands
    }

    /// Returns true if the user may hide the column.
    pub fn is_hideable(&self) -> bool {
        self.hideable
    }

    /// Returns true if the column is internal (`GridColumn::with_internal`):
    /// never shown, and left out of a column chooser.
    pub fn is_internal(&self) -> bool {
        self.internal
    }

    /// Returns true if the user can drag the column's header.
    pub fn is_movable(&self) -> bool {
        self.movable
    }

    /// Returns true if the user can resize the column.
    pub fn is_resizable(&self) -> bool {
        self.resizable
    }

    /// Returns true if a click on the column's header sorts by it.
    pub fn is_sortable(&self) -> bool {
        self.sortable
    }

    /// Returns true if the user can group by the column.
    pub fn is_groupable(&self) -> bool {
        self.groupable
    }

    /// The column's sort direction, if the view sorts by it.
    pub fn sort(&self) -> Option<SortDirection> {
        self.sort.map(|(direction, _)| direction)
    }

    /// The column's place in a multi-column sort, from 0 for the most
    /// significant.
    pub fn sort_priority(&self) -> Option<usize> {
        self.sort.map(|(_, priority)| priority)
    }
}

/// A header band over a column.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BandInfo {
    id: &'static str,
    title: SharedString,
}

impl BandInfo {
    /// The band's id (`ColumnBand::new`).
    pub fn id(&self) -> &'static str {
        self.id
    }

    /// The band's header text.
    pub fn title(&self) -> &SharedString {
        &self.title
    }
}

impl<D: LiveGridDelegate> LiveGridState<D> {
    /// A handle of this grid without its delegate type: for a toolbar, a
    /// status bar or a column chooser.
    pub fn handle(&self, cx: &Context<Self>) -> LiveGridHandle {
        LiveGridHandle::new(&cx.entity())
    }

    /// The schema column whose id is `id`.
    pub(super) fn column_by_id(&self, id: &str) -> Option<ColumnOf<D>> {
        <ColumnOf<D> as crate::model::GridColumns>::all()
            .iter()
            .copied()
            .find(|column| column.id() == id)
            .filter(|column| self.columns.get(column.ix()).is_some())
    }

    /// The column `ix` as the grid shows it now; `None` outside the schema.
    fn column_info(&self, ix: ColumnIx) -> Option<ColumnInfo> {
        let column = self.columns.get(ix)?;
        let sort = self
            .view
            .sort()
            .iter()
            .enumerate()
            .find(|(_, (sorted, _))| sorted.ix() == ix)
            .map(|(priority, (_, direction))| (*direction, priority));
        Some(ColumnInfo {
            id: column.column().column_id(),
            title: column.title().clone(),
            header_label: column.header_label().clone(),
            zone: self.columns.zone_of(ix),
            visible: self.columns.is_visible(ix),
            width: self.columns.width(ix),
            bands: self
                .columns
                .bands_of(ix)
                .iter()
                .map(|&id| BandInfo {
                    id,
                    title: self
                        .columns
                        .band_title(id)
                        .cloned()
                        .unwrap_or_else(|| SharedString::new_static(id)),
                })
                .collect(),
            hideable: column.is_hideable(),
            internal: column.is_internal(),
            movable: column.is_movable(),
            resizable: column.is_resizable(),
            sortable: column.is_sortable(),
            groupable: column.is_groupable(),
            sort,
        })
    }

    /// The `'static` id of the band `band`, if a schema column is under it.
    pub(super) fn band_id(&self, band: &str) -> Option<&'static str> {
        self.columns
            .order()
            .iter()
            .flat_map(|ix| self.columns.bands_of(*ix).iter().copied())
            .find(|id| *id == band)
    }
}

/// The grid behind a handle: the typed entity, as an object-safe trait.
pub(super) trait GridOps {
    fn entity_id(&self) -> EntityId;
    fn downgrade(&self) -> WeakLiveGridHandle;
    fn focus_handle(&self, cx: &App) -> FocusHandle;
    fn columns(&self, cx: &App) -> Vec<ColumnInfo>;
    fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo>;
    fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool;
    fn show_all_columns(&self, cx: &mut App);
    fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool;
    fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool;
    fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool;
    fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool;
    fn autofit_columns(&self, cx: &mut App);
    fn view_id(&self, cx: &App) -> Option<SharedString>;
    fn layout(&self, cx: &App) -> GridLayout;
    fn set_layout(&self, layout: &GridLayout, cx: &mut App) -> Result<LayoutReport, LayoutError>;
    fn reset_layout(&self, cx: &mut App);
    fn sort(&self, cx: &App) -> Vec<LayoutSort>;
    fn set_sort(&self, sort: &[LayoutSort], cx: &mut App) -> bool;
    fn group_by(&self, cx: &App) -> Vec<LayoutGroup>;
    fn set_group_by(&self, levels: &[LayoutGroup], cx: &mut App) -> bool;
    fn expand_depth(&self, cx: &App) -> ExpandDepth;
    fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App);
    fn quick_filter(&self, cx: &App) -> SharedString;
    fn set_quick_filter(&self, text: SharedString, cx: &mut App);
    fn row_count(&self, cx: &App) -> usize;
    fn total_row_count(&self, cx: &App) -> usize;
    fn selected_count(&self, cx: &App) -> usize;
    fn is_order_held(&self, cx: &App) -> bool;
    fn apply_held_order(&self, cx: &mut App);
    fn set_reorder_policy(&self, policy: Box<dyn ReorderPolicy>, cx: &mut App);
    fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool;
    fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool;
    #[cfg(not(target_family = "wasm"))]
    fn export_to_path(
        &self,
        scope: ExportScope,
        format: CsvFormat,
        path: std::path::PathBuf,
        cx: &mut App,
    ) -> Result<ExportJob<std::path::PathBuf>, ExportError>;
    fn subscribe(
        &self,
        on_event: Box<dyn FnMut(&GridHandleEvent, &mut App)>,
        cx: &mut App,
    ) -> Subscription;
    fn observe(&self, on_notify: Box<dyn FnMut(&mut App)>, cx: &mut App) -> Subscription;
    // The column chooser.
    fn show_column_chooser(&self, window: &mut Window, cx: &mut App);
    fn is_column_chooser_open(&self, cx: &App) -> bool;
    fn chooser(
        &self,
        command: ChooserCommand<'_>,
        window: Option<&mut Window>,
        cx: &mut App,
    ) -> bool;
}

/// [`GridOps`] of a grid with the delegate `D`.
struct Ops<D: LiveGridDelegate> {
    grid: Entity<LiveGridState<D>>,
}

impl<D: LiveGridDelegate> Ops<D> {
    /// Runs `command` on the column `id`; false, changing nothing, if the
    /// column is unknown.
    fn on_column(
        &self,
        id: &str,
        cx: &mut App,
        command: impl FnOnce(&mut LiveGridState<D>, ColumnOf<D>, &mut Context<LiveGridState<D>>) -> bool,
    ) -> bool {
        self.grid
            .update(cx, |grid, cx| match grid.column_by_id(id) {
                Some(column) => command(grid, column, cx),
                None => false,
            })
    }
}

impl<D: LiveGridDelegate> GridOps for Ops<D> {
    fn entity_id(&self) -> EntityId {
        self.grid.entity_id()
    }

    fn downgrade(&self) -> WeakLiveGridHandle {
        let grid: WeakEntity<LiveGridState<D>> = self.grid.downgrade();
        WeakLiveGridHandle {
            entity_id: self.grid.entity_id(),
            upgrade: Rc::new(move || grid.upgrade().map(|grid| LiveGridHandle::new(&grid))),
        }
    }

    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.grid.read(cx).focus_handle(cx)
    }

    fn columns(&self, cx: &App) -> Vec<ColumnInfo> {
        let grid = self.grid.read(cx);
        grid.columns
            .order()
            .iter()
            .filter_map(|ix| grid.column_info(*ix))
            .collect()
    }

    fn column(&self, id: &str, cx: &App) -> Option<ColumnInfo> {
        let grid = self.grid.read(cx);
        grid.column_info(grid.column_by_id(id)?.ix())
    }

    fn set_column_visible(&self, id: &str, visible: bool, cx: &mut App) -> bool {
        self.on_column(id, cx, |grid, column, cx| {
            grid.set_column_visible(column, visible, cx)
        })
    }

    fn show_all_columns(&self, cx: &mut App) {
        self.grid.update(cx, LiveGridState::show_all_columns);
    }

    fn move_column(&self, id: &str, before: Option<&str>, cx: &mut App) -> bool {
        self.grid.update(cx, |grid, cx| {
            let Some(column) = grid.column_by_id(id) else {
                return false;
            };
            let before = match before {
                Some(before) => match grid.column_by_id(before) {
                    Some(before) => Some(before),
                    None => return false,
                },
                None => None,
            };
            grid.move_column(column, before, cx)
        })
    }

    fn move_band(&self, band: &str, before: Option<&str>, cx: &mut App) -> bool {
        self.grid.update(cx, |grid, cx| {
            let Some(band) = grid.band_id(band) else {
                return false;
            };
            let before = match before {
                Some(before) => match grid.column_by_id(before) {
                    Some(before) => Some(before),
                    None => return false,
                },
                None => None,
            };
            grid.move_band(band, before, cx)
        })
    }

    fn set_column_zone(&self, id: &str, zone: ColumnZone, cx: &mut App) -> bool {
        self.on_column(id, cx, |grid, column, cx| {
            grid.set_column_zone(column, zone, cx);
            true
        })
    }

    fn set_column_width(&self, id: &str, width: Pixels, cx: &mut App) -> bool {
        self.on_column(id, cx, |grid, column, cx| {
            grid.set_column_width(column, width, cx);
            true
        })
    }

    fn autofit_columns(&self, cx: &mut App) {
        self.grid.update(cx, LiveGridState::autofit_columns);
    }

    fn view_id(&self, cx: &App) -> Option<SharedString> {
        self.grid.read(cx).view_id().cloned()
    }

    fn layout(&self, cx: &App) -> GridLayout {
        self.grid.read(cx).layout()
    }

    fn set_layout(&self, layout: &GridLayout, cx: &mut App) -> Result<LayoutReport, LayoutError> {
        self.grid.update(cx, |grid, cx| grid.set_layout(layout, cx))
    }

    fn reset_layout(&self, cx: &mut App) {
        self.grid.update(cx, LiveGridState::reset_layout);
    }

    fn sort(&self, cx: &App) -> Vec<LayoutSort> {
        self.grid
            .read(cx)
            .sort()
            .iter()
            .map(|(column, direction)| LayoutSort::new(column.column_id(), *direction))
            .collect()
    }

    fn set_sort(&self, sort: &[LayoutSort], cx: &mut App) -> bool {
        self.grid.update(cx, |grid, cx| {
            let Some(sort) = sort
                .iter()
                .map(|level| {
                    Some((
                        grid.column_by_id(level.column().as_ref())?,
                        level.direction(),
                    ))
                })
                .collect::<Option<Vec<_>>>()
            else {
                return false;
            };
            grid.set_sort(sort, cx);
            true
        })
    }

    fn group_by(&self, cx: &App) -> Vec<LayoutGroup> {
        self.grid
            .read(cx)
            .group_by()
            .iter()
            .map(|level| LayoutGroup::new(level.id(), level.direction()))
            .collect()
    }

    fn set_group_by(&self, levels: &[LayoutGroup], cx: &mut App) -> bool {
        self.grid.update(cx, |grid, cx| {
            let Some(levels) = levels
                .iter()
                .map(|level| {
                    let column = grid.column_by_id(level.column().as_ref())?;
                    Some(GroupBy::column(column).with_direction(level.direction()))
                })
                .collect::<Option<Vec<_>>>()
            else {
                return false;
            };
            grid.set_group_by(levels, cx);
            true
        })
    }

    fn expand_depth(&self, cx: &App) -> ExpandDepth {
        self.grid.read(cx).expand_depth()
    }

    fn set_expand_depth(&self, depth: ExpandDepth, cx: &mut App) {
        self.grid
            .update(cx, |grid, cx| grid.set_expand_depth(depth, cx));
    }

    fn quick_filter(&self, cx: &App) -> SharedString {
        self.grid.read(cx).quick_filter().clone()
    }

    fn set_quick_filter(&self, text: SharedString, cx: &mut App) {
        self.grid
            .update(cx, |grid, cx| grid.set_quick_filter(text, cx));
    }

    fn row_count(&self, cx: &App) -> usize {
        self.grid.read(cx).projection.row_count()
    }

    fn total_row_count(&self, cx: &App) -> usize {
        self.grid.read(cx).snapshot.len()
    }

    fn selected_count(&self, cx: &App) -> usize {
        self.grid.read(cx).selection().len()
    }

    fn is_order_held(&self, cx: &App) -> bool {
        self.grid.read(cx).is_order_held()
    }

    fn apply_held_order(&self, cx: &mut App) {
        self.grid.update(cx, LiveGridState::apply_held_order);
    }

    fn set_reorder_policy(&self, policy: Box<dyn ReorderPolicy>, cx: &mut App) {
        self.grid.update(cx, |grid, cx| {
            // As `set_reorder_policy`, with the policy already boxed.
            grid.reorder = policy;
            grid.reconsider_held(cx);
        });
    }

    fn copy_scope(&self, scope: ExportScope, cx: &mut App) -> bool {
        self.grid.update(cx, |grid, cx| grid.copy_scope(scope, cx))
    }

    fn is_export_empty(&self, scope: ExportScope, cx: &App) -> bool {
        self.grid.read(cx).is_export_empty(scope)
    }

    #[cfg(not(target_family = "wasm"))]
    fn export_to_path(
        &self,
        scope: ExportScope,
        format: CsvFormat,
        path: std::path::PathBuf,
        cx: &mut App,
    ) -> Result<ExportJob<std::path::PathBuf>, ExportError> {
        self.grid.update(cx, |grid, cx| {
            grid.export_to_path(ExportRequest::new(scope), format, path, cx)
        })
    }

    fn subscribe(
        &self,
        mut on_event: Box<dyn FnMut(&GridHandleEvent, &mut App)>,
        cx: &mut App,
    ) -> Subscription {
        cx.subscribe(&self.grid, move |_, event: &LiveGridEvent<_, _>, cx| {
            if let Some(event) = GridHandleEvent::from_grid_event(event) {
                on_event(&event, cx);
            }
        })
    }

    fn observe(&self, mut on_notify: Box<dyn FnMut(&mut App)>, cx: &mut App) -> Subscription {
        cx.observe(&self.grid, move |_, cx| on_notify(cx))
    }

    fn show_column_chooser(&self, window: &mut Window, cx: &mut App) {
        self.grid
            .update(cx, |grid, cx| grid.show_column_chooser(window, cx));
    }

    fn is_column_chooser_open(&self, cx: &App) -> bool {
        self.grid.read(cx).is_column_chooser_open()
    }

    fn chooser(
        &self,
        command: ChooserCommand<'_>,
        window: Option<&mut Window>,
        cx: &mut App,
    ) -> bool {
        self.grid
            .update(cx, |grid, cx| grid.run_chooser_command(command, window, cx))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::selection::GridSelection;

    #[test]
    fn test_handle_event_translation() {
        let event = |event: LiveGridEvent<u32, u16>| GridHandleEvent::from_grid_event(&event);
        assert_eq!(
            event(LiveGridEvent::OrderHeldChange { held: true }),
            Some(GridHandleEvent::OrderHeldChange { held: true })
        );
        assert_eq!(
            event(LiveGridEvent::ProjectionChange {
                row_count: 12,
                store_version: 3
            }),
            Some(GridHandleEvent::ProjectionChange {
                row_count: 12,
                store_version: 3
            })
        );
        assert_eq!(
            event(LiveGridEvent::SelectionChange {
                selection: GridSelection::default(),
                origin: SelectionOrigin::Programmatic,
            }),
            Some(GridHandleEvent::SelectionChange {
                count: 0,
                origin: SelectionOrigin::Programmatic
            })
        );
        // An event about rows or cells has no untyped form.
        assert_eq!(
            event(LiveGridEvent::Confirm {
                keys: [1].into(),
                secondary: false
            }),
            None
        );
    }
}