1use gpui_kit::component::table::{Column, TableDelegate, TableSelection, TableState};
4use gpui_kit::{AnyElement, App, Context, Window};
5use gpui_table_core::sort::{ResolvedSortColumns, SortError, SortOrder, SortableRow};
6
7pub trait OrderedTableDelegate: TableDelegate {
9 fn row_selection(&self) -> Option<&crate::selection::RowSelectionState> {
10 None
11 }
12 fn row_selection_mut(&mut self) -> Option<&mut crate::selection::RowSelectionState> {
13 None
14 }
15 type RowId: Clone + Eq;
16 fn ordering(&self) -> &SortOrder;
17 fn set_ordering(&mut self, ordering: SortOrder) -> Result<(), SortError>;
18 fn visible_row_id(&self, row_ix: usize) -> Option<Self::RowId>;
19 fn visible_row_position(&self, id: &Self::RowId) -> Option<usize>;
20}
21
22pub trait ResolvedOrderedTableDelegate<R: SortableRow>: OrderedTableDelegate {
24 fn resolved_sort_columns(&self) -> &ResolvedSortColumns<R>;
26 fn set_resolved_sort_columns(
28 &mut self,
29 columns: ResolvedSortColumns<R>,
30 ) -> Result<(), SortError>;
31}
32
33type ResolvedCell<R> = dyn Fn(&R, &mut Window, &mut App) -> AnyElement;
34
35pub struct ResolvedTableColumn<R> {
38 column: Column,
39 render: std::rc::Rc<ResolvedCell<R>>,
40}
41impl<R> Clone for ResolvedTableColumn<R> {
42 fn clone(&self) -> Self {
43 Self {
44 column: self.column.clone(),
45 render: self.render.clone(),
46 }
47 }
48}
49impl<R> ResolvedTableColumn<R> {
50 pub fn new(
53 column: Column,
54 render: impl Fn(&R, &mut Window, &mut App) -> AnyElement + 'static,
55 ) -> Result<Self, SortError> {
56 SortOrder::new(vec![gpui_table_core::sort::SortClause::new(
57 column.key.to_string(),
58 gpui_table_core::sort::SortDirection::Ascending,
59 )])?;
60 Ok(Self {
61 column,
62 render: std::rc::Rc::new(render),
63 })
64 }
65 pub fn column(&self) -> &Column {
66 &self.column
67 }
68 pub fn render(&self, row: &R, window: &mut Window, cx: &mut App) -> AnyElement {
69 (self.render)(row, window, cx)
70 }
71}
72
73pub trait ResolvedTableDelegate<R: SortableRow>: ResolvedOrderedTableDelegate<R> {
75 fn resolved_table_columns(&self) -> &[ResolvedTableColumn<R>];
76 fn set_resolved_table_columns(
77 &mut self,
78 columns: Vec<ResolvedTableColumn<R>>,
79 ) -> Result<(), SortError>;
80}
81
82pub fn set_table_resolved_columns<R: SortableRow, D: ResolvedTableDelegate<R>>(
84 table: &mut TableState<D>,
85 columns: Vec<ResolvedTableColumn<R>>,
86 cx: &mut Context<TableState<D>>,
87) -> Result<(), SortError> {
88 let mut selected = TableRowSelection::capture(table);
89 let column_key = match selected.selection {
90 TableSelection::Cell(_, column) | TableSelection::Column(column)
91 if column < table.delegate().columns_count(cx) =>
92 {
93 Some(table.delegate().column(column, cx).key)
94 },
95 _ => None,
96 };
97 table.delegate_mut().set_resolved_table_columns(columns)?;
98 let column = column_key.and_then(|key| {
99 (0..table.delegate().columns_count(cx))
100 .find(|column| table.delegate().column(*column, cx).key == key)
101 });
102 selected.selection = match selected.selection {
103 TableSelection::Cell(row, _) => column.map_or(TableSelection::Row(row), |column| {
104 TableSelection::Cell(row, column)
105 }),
106 TableSelection::Column(_) => column.map_or(TableSelection::None, TableSelection::Column),
107 selection => selection,
108 };
109 table.refresh(cx);
110 selected.restore(table, cx);
111 cx.notify();
112 Ok(())
113}
114
115#[derive(Clone)]
120pub struct TableRowSelection<Id> {
121 row: Option<Id>,
122 rows: Vec<Id>,
123 cursor: Option<Id>,
124 anchor: Option<Id>,
125 revision: u64,
126 selection: TableSelection,
127}
128
129impl<Id: Clone + Eq> TableRowSelection<Id> {
130 pub fn row_id(&self) -> Option<&Id> {
132 self.row.as_ref()
133 }
134
135 pub fn row_ids(&self) -> &[Id] {
137 &self.rows
138 }
139
140 pub fn selection_revision(&self) -> u64 {
142 self.revision
143 }
144
145 pub fn all_rows_visible<D: OrderedTableDelegate<RowId = Id>>(&self, delegate: &D) -> bool {
146 self.rows
147 .iter()
148 .all(|id| delegate.visible_row_position(id).is_some())
149 }
150
151 pub fn preserve_unavailable<D: OrderedTableDelegate<RowId = Id>>(
153 &mut self,
154 old: &Self,
155 delegate: &D,
156 ) {
157 for id in &old.rows {
158 if delegate.visible_row_position(id).is_none() && !self.rows.contains(id) {
159 self.rows.push(id.clone());
160 }
161 }
162 if !self.rows.is_empty() && self.selection == TableSelection::None {
163 self.selection = TableSelection::Row(0);
164 }
165 }
166
167 pub fn remove_row_id(&mut self, id: &Id) {
169 self.rows.retain(|row| row != id);
170 if self.row.as_ref() == Some(id) {
171 self.row = self.rows.first().cloned();
172 }
173 {
174 if self.cursor.as_ref() == Some(id) {
175 self.cursor = None;
176 }
177 if self.anchor.as_ref() == Some(id) {
178 self.anchor = None;
179 }
180 }
181 if self.rows.is_empty()
182 && matches!(
183 self.selection,
184 TableSelection::Row(_) | TableSelection::Cell(_, _)
185 )
186 {
187 self.selection = TableSelection::None;
188 }
189 }
190
191 pub fn capture<D: OrderedTableDelegate<RowId = Id>>(table: &TableState<D>) -> Self {
192 let selection = table.selection();
193 let row_ix = match selection {
194 TableSelection::Row(ix) | TableSelection::Cell(ix, _) => Some(ix),
195 _ => None,
196 };
197 let row = row_ix.and_then(|ix| table.delegate().visible_row_id(ix));
198 let rows = if matches!(selection, TableSelection::Cell(_, _)) {
199 row.iter().cloned().collect()
200 } else {
201 crate::selection::selected_rows(table)
202 .into_iter()
203 .filter_map(|ix| table.delegate().visible_row_id(ix))
204 .collect()
205 };
206 Self {
207 row,
208 rows,
209 cursor: crate::selection::row_selection_cursor(table)
210 .and_then(|ix| table.delegate().visible_row_id(ix)),
211 anchor: crate::selection::row_selection_anchor(table)
212 .and_then(|ix| table.delegate().visible_row_id(ix)),
213 revision: crate::selection::selection_revision(table),
214 selection,
215 }
216 }
217
218 pub fn restore<D: OrderedTableDelegate<RowId = Id>>(
219 self,
220 table: &mut TableState<D>,
221 cx: &mut Context<TableState<D>>,
222 ) {
223 if matches!(self.selection, TableSelection::Row(_)) {
224 let rows = self
225 .rows
226 .iter()
227 .filter_map(|id| table.delegate().visible_row_position(id))
228 .collect();
229 let cursor = self
230 .cursor
231 .and_then(|id| table.delegate().visible_row_position(&id));
232 let anchor = self
233 .anchor
234 .and_then(|id| table.delegate().visible_row_position(&id));
235 crate::selection::restore_rows(table, rows, cursor, anchor, cx);
236 return;
237 }
238 let selection = match (self.row, self.selection) {
239 (Some(id), TableSelection::Row(_)) => table
240 .delegate()
241 .visible_row_position(&id)
242 .map(TableSelection::Row)
243 .unwrap_or_default(),
244 (Some(id), TableSelection::Cell(_, col)) => table
245 .delegate()
246 .visible_row_position(&id)
247 .map(|ix| TableSelection::Cell(ix, col))
248 .unwrap_or_default(),
249 (None, TableSelection::Row(_) | TableSelection::Cell(_, _)) => TableSelection::None,
250 (_, selection) => selection,
251 };
252 let columns = table.delegate().columns_count(cx);
253 let selection = match selection {
254 TableSelection::Cell(row, column) if column >= columns => TableSelection::Row(row),
255 TableSelection::Column(column) if column >= columns => TableSelection::None,
256 selection => selection,
257 };
258 table.set_selection(selection, cx);
259 }
260}
261
262pub fn set_table_ordering<D: OrderedTableDelegate>(
265 table: &mut TableState<D>,
266 ordering: SortOrder,
267 cx: &mut Context<TableState<D>>,
268) -> Result<(), SortError> {
269 let selected = TableRowSelection::capture(table);
270 table.delegate_mut().set_ordering(ordering)?;
271 table.refresh(cx);
272 selected.restore(table, cx);
273 cx.notify();
274 Ok(())
275}
276
277pub fn set_table_resolved_sort_columns<R: SortableRow, D: ResolvedOrderedTableDelegate<R>>(
279 table: &mut TableState<D>,
280 columns: ResolvedSortColumns<R>,
281 cx: &mut Context<TableState<D>>,
282) -> Result<(), SortError> {
283 let selected = TableRowSelection::capture(table);
284 table.delegate_mut().set_resolved_sort_columns(columns)?;
285 table.refresh(cx);
286 selected.restore(table, cx);
287 cx.notify();
288 Ok(())
289}