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
use std::ops::Range;
use gpui::{
App, Context, Div, InteractiveElement as _, IntoElement, ParentElement as _, Pixels,
SharedString, Stateful, Styled as _, Window, div,
};
use crate::{
ActiveTheme as _, Icon, IconName, Size, h_flex,
menu::PopupMenu,
table::{Column, ColumnGroup, ColumnSort, TableState, loading::Loading},
};
/// A delegate trait for providing data and rendering for a table.
#[allow(unused)]
pub trait TableDelegate: Sized + 'static {
/// Return the number of columns in the table.
fn columns_count(&self, cx: &App) -> usize;
/// Return the number of rows in the table.
fn rows_count(&self, cx: &App) -> usize;
/// Returns the table column at the given index.
///
/// This only call on Table prepare or refresh.
fn column(&self, col_ix: usize, cx: &App) -> Column;
/// Perform sort on the column at the given index.
fn perform_sort(
&mut self,
col_ix: usize,
sort: ColumnSort,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) {
}
/// Render the table head row.
fn render_header(
&mut self,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> Stateful<Div> {
div().id("header")
}
/// Return the group headers definitions (can be multi-level).
///
/// By default, it returns None, meaning no group headers.
fn group_headers(&self, cx: &App) -> Option<Vec<Vec<ColumnGroup>>> {
None
}
/// Custom render for a group header cell.
/// Receives the group label, the logical col_span, and the pixel width.
fn render_group_th(
&mut self,
label: &SharedString,
_col_span: usize,
width: Pixels,
_window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement {
div()
.w(width)
.h_full()
.flex_shrink_0()
.flex()
.items_center()
.justify_center()
.border_r_1()
.border_color(cx.theme().border)
.child(label.clone())
}
/// Render the header cell at the given column index, default to the column name.
fn render_th(
&mut self,
col_ix: usize,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement {
div()
.size_full()
.child(self.column(col_ix, cx).name.clone())
}
/// Render the row at the given row and column.
///
/// Not include the table head row.
fn render_tr(
&mut self,
row_ix: usize,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> Stateful<Div> {
div().id(("row", row_ix))
}
/// Render the context menu for the row at the given row index.
fn context_menu(
&mut self,
row_ix: usize,
menu: PopupMenu,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> PopupMenu {
menu
}
/// Render cell at the given row and column.
fn render_td(
&mut self,
row_ix: usize,
col_ix: usize,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement;
/// Move the column at the given `col_ix` so that it ends up at the index `to_ix`.
///
/// e.g.: `let col = self.columns.remove(col_ix); self.columns.insert(to_ix, col);`
fn move_column(
&mut self,
col_ix: usize,
to_ix: usize,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) {
}
/// Return a Element to show when table is empty.
fn render_empty(
&mut self,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement {
h_flex()
.size_full()
.justify_center()
.text_color(cx.theme().muted_foreground.opacity(0.6))
.child(Icon::new(IconName::Inbox).size_12())
.into_any_element()
}
/// Return true to show the loading view.
fn loading(&self, cx: &App) -> bool {
false
}
/// Return a Element to show when table is loading, default is built-in Skeleton loading view.
///
/// The size is the size of the Table.
fn render_loading(
&mut self,
size: Size,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement {
Loading::new().size(size)
}
/// Return true to enable load more data when scrolling to the bottom.
///
/// Default: false
fn has_more(&self, cx: &App) -> bool {
false
}
/// Returns a threshold value (n rows), of course, when scrolling to the bottom,
/// the remaining number of rows triggers `load_more`.
/// This should smaller than the total number of first load rows.
///
/// Default: 20 rows
fn load_more_threshold(&self) -> usize {
20
}
/// Load more data when the table is scrolled to the bottom.
///
/// This will performed in a background task.
///
/// This is always called when the table is near the bottom,
/// so you must check if there is more data to load or lock the loading state.
fn load_more(&mut self, window: &mut Window, cx: &mut Context<TableState<Self>>) {}
/// Render the last empty column, default to empty.
fn render_last_empty_col(
&mut self,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) -> impl IntoElement {
h_flex().w_3().h_full().flex_shrink_0()
}
/// Called when the visible range of the rows changed.
///
/// NOTE: Make sure this method is fast, because it will be called frequently.
///
/// This can used to handle some data update, to only update the visible rows.
/// Please ensure that the data is updated in the background task.
fn visible_rows_changed(
&mut self,
visible_range: Range<usize>,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) {
}
/// Called when the visible range of the columns changed.
///
/// NOTE: Make sure this method is fast, because it will be called frequently.
///
/// This can used to handle some data update, to only update the visible rows.
/// Please ensure that the data is updated in the background task.
fn visible_columns_changed(
&mut self,
visible_range: Range<usize>,
window: &mut Window,
cx: &mut Context<TableState<Self>>,
) {
}
/// Get the text representation of a cell for export purposes (e.g., CSV export).
///
/// Returns an empty string by default. Implement this method to support export.
/// The text should be formatted as it should appear in the exported data.
fn cell_text(&self, row_ix: usize, col_ix: usize, cx: &App) -> String {
String::new()
}
}