Skip to main content

gpui_component/table/
delegate.rs

1use std::ops::Range;
2
3use gpui::ColorExt as _;
4use gpui::{
5    App, Context, Div, InteractiveElement as _, IntoElement, ParentElement as _, Pixels,
6    SharedString, Stateful, Styled as _, Window, div,
7};
8
9use crate::{
10    ActiveTheme as _, Icon, IconName, Size, h_flex,
11    menu::PopupMenu,
12    table::{Column, ColumnGroup, ColumnSort, TableState, loading::Loading},
13};
14
15/// A delegate trait for providing data and rendering for a table.
16#[allow(unused)]
17pub trait TableDelegate: Sized + 'static {
18    /// Return the number of columns in the table.
19    fn columns_count(&self, cx: &App) -> usize;
20
21    /// Return the number of rows in the table.
22    fn rows_count(&self, cx: &App) -> usize;
23
24    /// Returns the table column at the given index.
25    ///
26    /// This only call on Table prepare or refresh.
27    fn column(&self, col_ix: usize, cx: &App) -> Column;
28
29    /// Perform sort on the column at the given index.
30    fn perform_sort(
31        &mut self,
32        col_ix: usize,
33        sort: ColumnSort,
34        window: &mut Window,
35        cx: &mut Context<TableState<Self>>,
36    ) {
37    }
38
39    /// Render the table head row.
40    fn render_header(
41        &mut self,
42        window: &mut Window,
43        cx: &mut Context<TableState<Self>>,
44    ) -> Stateful<Div> {
45        div().id("header")
46    }
47
48    /// Return the group headers definitions (can be multi-level).
49    ///
50    /// By default, it returns None, meaning no group headers.
51    fn group_headers(&self, cx: &App) -> Option<Vec<Vec<ColumnGroup>>> {
52        None
53    }
54
55    /// Custom render for a group header cell.
56    /// Receives the group label, the logical col_span, and the pixel width.
57    fn render_group_th(
58        &mut self,
59        label: &SharedString,
60        _col_span: usize,
61        width: Pixels,
62        _window: &mut Window,
63        cx: &mut Context<TableState<Self>>,
64    ) -> impl IntoElement {
65        div()
66            .w(width)
67            .h_full()
68            .flex_shrink_0()
69            .flex()
70            .items_center()
71            .justify_center()
72            .border_r_1()
73            .border_color(cx.theme().border)
74            .child(label.clone())
75    }
76
77    /// Render the header cell at the given column index, default to the column name.
78    fn render_th(
79        &mut self,
80        col_ix: usize,
81        window: &mut Window,
82        cx: &mut Context<TableState<Self>>,
83    ) -> impl IntoElement {
84        div()
85            .size_full()
86            .child(self.column(col_ix, cx).name.clone())
87    }
88
89    /// Render the row at the given row and column.
90    ///
91    /// Not include the table head row.
92    fn render_tr(
93        &mut self,
94        row_ix: usize,
95        window: &mut Window,
96        cx: &mut Context<TableState<Self>>,
97    ) -> Stateful<Div> {
98        div().id(("row", row_ix))
99    }
100
101    /// Render the context menu for the row at the given row index.
102    fn context_menu(
103        &mut self,
104        row_ix: usize,
105        menu: PopupMenu,
106        window: &mut Window,
107        cx: &mut Context<TableState<Self>>,
108    ) -> PopupMenu {
109        menu
110    }
111
112    /// Render cell at the given row and column.
113    fn render_td(
114        &mut self,
115        row_ix: usize,
116        col_ix: usize,
117        window: &mut Window,
118        cx: &mut Context<TableState<Self>>,
119    ) -> impl IntoElement;
120
121    /// Move the column at the given `col_ix` so that it ends up at the index `to_ix`.
122    ///
123    /// e.g.: `let col = self.columns.remove(col_ix); self.columns.insert(to_ix, col);`
124    fn move_column(
125        &mut self,
126        col_ix: usize,
127        to_ix: usize,
128        window: &mut Window,
129        cx: &mut Context<TableState<Self>>,
130    ) {
131    }
132
133    /// Return a Element to show when table is empty.
134    fn render_empty(
135        &mut self,
136        window: &mut Window,
137        cx: &mut Context<TableState<Self>>,
138    ) -> impl IntoElement {
139        h_flex()
140            .size_full()
141            .justify_center()
142            .text_color(cx.theme().muted_foreground.opacity(0.6))
143            .child(Icon::new(IconName::Inbox).size_12())
144            .into_any_element()
145    }
146
147    /// Return true to show the loading view.
148    fn loading(&self, cx: &App) -> bool {
149        false
150    }
151
152    /// Return a Element to show when table is loading, default is built-in Skeleton loading view.
153    ///
154    /// The size is the size of the Table.
155    fn render_loading(
156        &mut self,
157        size: Size,
158        window: &mut Window,
159        cx: &mut Context<TableState<Self>>,
160    ) -> impl IntoElement {
161        Loading::new().size(size)
162    }
163
164    /// Return true to enable load more data when scrolling to the bottom.
165    ///
166    /// Default: false
167    fn has_more(&self, cx: &App) -> bool {
168        false
169    }
170
171    /// Returns a threshold value (n rows), of course, when scrolling to the bottom,
172    /// the remaining number of rows triggers `load_more`.
173    /// This should smaller than the total number of first load rows.
174    ///
175    /// Default: 20 rows
176    fn load_more_threshold(&self) -> usize {
177        20
178    }
179
180    /// Load more data when the table is scrolled to the bottom.
181    ///
182    /// This will performed in a background task.
183    ///
184    /// This is always called when the table is near the bottom,
185    /// so you must check if there is more data to load or lock the loading state.
186    fn load_more(&mut self, window: &mut Window, cx: &mut Context<TableState<Self>>) {}
187
188    /// Render the last empty column, default to empty.
189    fn render_last_empty_col(
190        &mut self,
191        window: &mut Window,
192        cx: &mut Context<TableState<Self>>,
193    ) -> impl IntoElement {
194        h_flex().w_3().h_full().flex_shrink_0()
195    }
196
197    /// Called when the visible range of the rows changed.
198    ///
199    /// NOTE: Make sure this method is fast, because it will be called frequently.
200    ///
201    /// This can used to handle some data update, to only update the visible rows.
202    /// Please ensure that the data is updated in the background task.
203    fn visible_rows_changed(
204        &mut self,
205        visible_range: Range<usize>,
206        window: &mut Window,
207        cx: &mut Context<TableState<Self>>,
208    ) {
209    }
210
211    /// Called when the visible range of the columns changed.
212    ///
213    /// NOTE: Make sure this method is fast, because it will be called frequently.
214    ///
215    /// This can used to handle some data update, to only update the visible rows.
216    /// Please ensure that the data is updated in the background task.
217    fn visible_columns_changed(
218        &mut self,
219        visible_range: Range<usize>,
220        window: &mut Window,
221        cx: &mut Context<TableState<Self>>,
222    ) {
223    }
224
225    /// Get the text representation of a cell for export purposes (e.g., CSV export).
226    ///
227    /// Returns an empty string by default. Implement this method to support export.
228    /// The text should be formatted as it should appear in the exported data.
229    fn cell_text(&self, row_ix: usize, col_ix: usize, cx: &App) -> String {
230        String::new()
231    }
232}