Skip to main content

gpui_kit_grid/
lib.rs

1//! gpui-kit-grid: a live-updating data grid for gpui-kit applications.
2//!
3//! The core: a background ingest that owns the rows, background projection
4//! rebuilds, a custom-painted body with a colour-free text cache, and change
5//! highlight on a global tick.
6//!
7//! ```text
8//! GridWriter (any thread) → ingest task → StoreSnapshot → LiveGridState (view)
9//!                                                       → Rebuild (background) → Projection
10//! ```
11
12// docs.rs (`--cfg docsrs`, nightly) marks each item of an optional feature.
13#![cfg_attr(docsrs, feature(doc_cfg))]
14
15// GPUI's `Action` derive spells its paths `gpui::`; the facade resolves them
16// through this alias (gpui-kit's `crates/kit/src/lib.rs`).
17extern crate gpui_kit as gpui;
18
19pub mod actions;
20mod element;
21mod engine;
22mod export;
23mod grid;
24mod highlight;
25mod model;
26mod persist;
27mod policy;
28mod quick_filter;
29mod state;
30mod store;
31mod text;
32
33pub use actions::init;
34pub use engine::{
35    Aggregate, AggregateFn, AggregateOrder, AggregateRow, Branch, DisplayRow, EveryGroupFooter,
36    ExpandDepth, Expansion, GroupBy, GroupFooter, GroupId, GroupInfo, GroupKey, GroupOrder,
37    GroupOrderContext, GroupPath, GroupTable, KeyOrder, NoGroupFooter, Projection,
38    ProjectionEngine, Rebuild, SortDirection, TreeFilter, TreeFilterContext, TreeFilterRule,
39    TreeSpec, TreeTotals, TreeTotalsContext, TreeTotalsRule, ViewSpec,
40};
41pub use export::{
42    CellExport, CellExporter, CsvEncoding, CsvFormat, DisplayedCells, ExportCell, ExportColumn,
43    ExportControl, ExportError, ExportFormat, ExportLine, ExportLines, ExportRow, ExportScope,
44    ExportTable, Exporter, LineFilter, NumberHint, NumberPrecision, RawCells, TextFile,
45    TsvExporter,
46};
47/// Derives [`GridColumns`](trait@GridColumns) for an enum of columns (feature
48/// `derive`).
49#[cfg(feature = "derive")]
50pub use gpui_kit_grid_macros::GridColumns;
51pub use grid::LiveGrid;
52pub use highlight::{ChangeDirection, HighlightPolicy, RowChange, RowStamps, Stamp};
53pub use model::{
54    CellValue, ColumnId, ColumnIx, ColumnSet, GridColumns, GridRow, GridValue, IconValue,
55    ImageValue, LinkValue, NumberFormat, OwnedValue,
56};
57pub use persist::{
58    AppendNewColumns, GridLayout, JsonLayoutCodec, LayoutCodec, LayoutColumn, LayoutError,
59    LayoutExpansion, LayoutGroup, LayoutLibrary, LayoutMerge, LayoutMergeContext, LayoutReport,
60    LayoutSort,
61};
62pub use policy::{
63    AfterCommit, AutofitContext, AutofitPolicy, BackgroundRunner, BandedMove, CellMove,
64    ColumnMoveContext, ColumnMovePolicy, ColumnOrder, ContextMenuGesture,
65    ContextMenuGestureContext, DraftAction, DropBetweenRows, DropLine, DropOnRows, EditCommitKey,
66    EditEnd, EditGesture, EditGestureContext, EditPolicy, EditPolicyContext, ExpandArea,
67    ExpandEdit, ExpandGesture, ExpandGestureContext, FocusBranches, FocusCaptions, FollowLiveValue,
68    FreeMove, GestureInput, GridArea, HoldReorder, InlineRunner, LineScopeContext, LiveReorder,
69    ManualReorder, NoRowDrag, NoRowDrop, ProjectionScope, ReorderContext, ReorderDecision,
70    ReorderPolicy, RowDragGesture, RowDragPressContext, RowDropContext, RowDropPolicy,
71    SelectableLine, SelectedRowsDrag, SelectionEdit, SelectionGesture, SelectionGestureContext,
72    SelectionPolicy, SelectionScope, SelectionScopeContext, SortCycle, SortCycleContext,
73    StandardContextMenuGesture, StandardEditGesture, StandardEditPolicy, StandardExpandGesture,
74    StandardSelectionGesture, StandardSortCycle, StoreScope, TaskRunner, VisibleRowsAutofit,
75};
76pub use quick_filter::{
77    CellTexts, ContainsAllTerms, ContainsAllTermsQuery, QuickFilter, QuickFilterMatcher,
78    QuickFilterQuery, QuickFilterRule,
79};
80pub use state::{
81    AggregateLine, AggregateStyleContext, AssetImageSource, BandInfo, CaptionTag, CellChange,
82    CellContent, CellEditHandle, CellEditor, CellElementContext, CellImage, CellImageSource,
83    CellImages, CellStyle, CellStyleContext, CellTooltip, CellTooltipContext, ChangePaint,
84    ChangePainter, ChangePalette, CheckboxEditor, ColumnAlign, ColumnBand, ColumnChooserContext,
85    ColumnChooserFactory, ColumnChooserPanel, ColumnInfo, ColumnZone, DateEditor,
86    DirectionalChanges, DragPayload, DropPlace, DropSource, DropTarget, EditCancelReason,
87    EditError, EditId, EditInput, EditMessageContext, EditOrigin, EditState, EditValidateContext,
88    EditableContext, EditorCell, EditorField, EditorMark, EditorOpen, EditorStart, EmptyExport,
89    ExpandTarget, ExportHeader, ExportJob, ExportOutcome, ExportProgress, ExportRequest,
90    FlushOutcome, FocusedCell, GridColumn, GridHandleEvent, GridMenuContext, GridSchema,
91    GridSelection, GridStats, GridTarget, GridTuning, GroupCaptionContext, GroupOrigin,
92    HeaderLabel, HeaderTooltipContext, ImageFit, LayoutOrigin, LayoutPart, LayoutParts, LinkOpener,
93    LiveGridDelegate, LiveGridEvent, LiveGridHandle, LiveGridState, MIN_COLUMN_WIDTH, NumberEditor,
94    NumberKind, OpenInBrowser, PopoverColumnChooser, ProgressContext, RowDrag, RowDragContext,
95    ScopedSelection, SelectEditor, SelectOption, SelectionMode, SelectionOrigin, SortOrigin,
96    TextEditor, UniformChanges, ViewColumnIx, WeakLiveGridHandle,
97};
98pub use store::{
99    ChangeSet, Clock, GridProgress, GridStore, GridStoreBuilder, GridWriter, Ingest, Op, PAGE_SIZE,
100    PinnedEdge, Reload, ReloadPolicy, ReplayLive, RowId, StoreOptions, StoreSnapshot, StoreStats,
101    StoreSubscription, WriteBatch, WriteCoalescing,
102};
103pub use text::TextStats;
104
105rust_i18n::i18n!("locales", fallback = "en");
106
107#[cfg(test)]
108mod tests {
109    use rust_i18n::t;
110
111    /// Every key of `locales/live_grid.yml` with its English text.
112    const CATALOG: &[(&str, &str)] = &[
113        ("live_grid.menu.copy", "Copy"),
114        ("live_grid.menu.copy_all", "Copy all"),
115        ("live_grid.menu.sort", "Sort"),
116        ("live_grid.menu.sort_ascending", "Sort ascending"),
117        ("live_grid.menu.sort_descending", "Sort descending"),
118        ("live_grid.menu.clear_sort", "Clear sort"),
119        ("live_grid.menu.clear_all_sorting", "Clear all sorting"),
120        ("live_grid.menu.column", "Column"),
121        ("live_grid.menu.autofit_column", "Autofit column"),
122        ("live_grid.menu.autofit_columns", "Autofit all columns"),
123        ("live_grid.menu.hide_column", "Hide column"),
124        ("live_grid.menu.show_all_columns", "Show all columns"),
125        ("live_grid.menu.choose_columns", "Choose columns…"),
126        ("live_grid.menu.freeze_left", "Freeze left"),
127        ("live_grid.menu.freeze_right", "Freeze right"),
128        ("live_grid.menu.unfreeze", "Unfreeze"),
129        ("live_grid.menu.reset_columns", "Reset columns"),
130        ("live_grid.sort.ascending", "Sorted ascending"),
131        ("live_grid.sort.descending", "Sorted descending"),
132        (
133            "live_grid.sort.ascending_priority",
134            "Sorted ascending, priority %{priority}",
135        ),
136        (
137            "live_grid.sort.descending_priority",
138            "Sorted descending, priority %{priority}",
139        ),
140        ("live_grid.progress.loading", "Loading…"),
141        ("live_grid.menu.group_by", "Group by this column"),
142        ("live_grid.menu.ungroup", "Ungroup"),
143        ("live_grid.menu.clear_grouping", "Ungroup all"),
144        ("live_grid.menu.move_left", "Move left"),
145        ("live_grid.menu.move_right", "Move right"),
146        ("live_grid.group_panel.label", "Grouping"),
147        (
148            "live_grid.group_panel.hint",
149            "Drag a column header here to group by it",
150        ),
151        (
152            "live_grid.group_panel.tree",
153            "Grouping is off in the tree view",
154        ),
155        ("live_grid.group_panel.chip", "%{title}, %{direction}"),
156        (
157            "live_grid.group_panel.remove",
158            "Remove grouping by %{title}",
159        ),
160        // Group captions, their menu and aggregates.
161        ("live_grid.group.caption", "%{title}: %{value} (%{count})"),
162        ("live_grid.group.blank", "(Blank)"),
163        ("live_grid.group.true", "Yes"),
164        ("live_grid.group.false", "No"),
165        ("live_grid.menu.expand", "Expand"),
166        ("live_grid.menu.collapse", "Collapse"),
167        ("live_grid.menu.expand_all", "Expand all"),
168        ("live_grid.menu.collapse_all", "Collapse all"),
169        ("live_grid.menu.captions_only", "Captions only"),
170        ("live_grid.aggregate.sum", "Sum"),
171        ("live_grid.aggregate.avg", "Average"),
172        ("live_grid.aggregate.min", "Min"),
173        ("live_grid.aggregate.max", "Max"),
174        ("live_grid.aggregate.count", "Count"),
175        ("live_grid.aggregate.tooltip", "%{name}: %{value}"),
176        // The totals row.
177        ("live_grid.totals.label", "Total"),
178        // Accessibility texts.
179        ("live_grid.a11y.up", "rising"),
180        ("live_grid.a11y.down", "falling"),
181        ("live_grid.a11y.expanded", "Expanded"),
182        ("live_grid.a11y.collapsed", "Collapsed"),
183        // The column chooser.
184        ("live_grid.chooser.label", "Columns"),
185        ("live_grid.chooser.search", "Search columns"),
186        ("live_grid.chooser.frozen_left", "Frozen left"),
187        ("live_grid.chooser.scrolling", "Scrollable"),
188        ("live_grid.chooser.frozen_right", "Frozen right"),
189        ("live_grid.chooser.band_shown", "%{shown} of %{count}"),
190        ("live_grid.chooser.no_match", "No columns match"),
191        ("live_grid.chooser.show_all", "Show all"),
192        ("live_grid.chooser.move", "Move %{title}"),
193        // The messages of the built-in editors.
194        ("live_grid.edit.not_a_number", "Enter a number"),
195        ("live_grid.edit.not_a_whole_number", "Enter a whole number"),
196        (
197            "live_grid.edit.too_many_decimals",
198            "Use at most %{scale} decimals",
199        ),
200        ("live_grid.edit.below_min", "Enter %{min} or more"),
201        ("live_grid.edit.above_max", "Enter %{max} or less"),
202        ("live_grid.edit.too_long", "Use at most %{count} characters"),
203        // The list of a select editor.
204        ("live_grid.edit.search", "Search"),
205        ("live_grid.edit.no_matches", "No matches"),
206        // The ghost of a row drag.
207        ("live_grid.drag.row", "1 row"),
208        ("live_grid.drag.rows", "%{count} rows"),
209        // Editing under live updates.
210        (
211            "live_grid.edit.conflict",
212            "Changed to %{value} while you edited",
213        ),
214        ("live_grid.edit.pending", "Saving…"),
215        ("live_grid.edit.failed", "Not saved: %{message}"),
216        ("live_grid.a11y.saving", "saving"),
217        ("live_grid.a11y.not_saved", "not saved"),
218        ("live_grid.a11y.link", "link"),
219    ];
220
221    #[test]
222    fn test_locale_catalog() {
223        // `ru` is added only if the product turns out to be Russian-speaking.
224        assert_eq!(rust_i18n::available_locales!(), ["en", "zh-CN", "zh-HK"]);
225        for &(key, english) in CATALOG {
226            assert_eq!(t!(key, locale = "en"), english);
227            for locale in ["zh-CN", "zh-HK"] {
228                // A missing translation falls back to English.
229                let text = t!(key, locale = locale);
230                assert_ne!(text, english, "{key} has no {locale} text");
231                for placeholder in [
232                    "%{priority}",
233                    "%{title}",
234                    "%{direction}",
235                    "%{value}",
236                    "%{count}",
237                    "%{name}",
238                    "%{scale}",
239                    "%{min}",
240                    "%{max}",
241                    "%{message}",
242                    "%{shown}",
243                ] {
244                    assert_eq!(
245                        text.contains(placeholder),
246                        english.contains(placeholder),
247                        "{key} in {locale}: {placeholder}"
248                    );
249                }
250            }
251        }
252        for locale in ["en", "zh-CN", "zh-HK"] {
253            let text = t!(
254                "live_grid.sort.descending_priority",
255                locale = locale,
256                priority = 2
257            );
258            assert!(text.ends_with(" 2"), "{locale}: {text}");
259            assert!(!text.contains("..."), "{locale}: {text}");
260        }
261        assert!(!t!("live_grid.progress.loading", locale = "en").contains("..."));
262    }
263}