use gpui_kit::component::table::{Column, TableDelegate, TableSelection, TableState};
use gpui_kit::{AnyElement, App, Context, Window};
use gpui_table_core::sort::{ResolvedSortColumns, SortError, SortOrder, SortableRow};
pub trait OrderedTableDelegate: TableDelegate {
fn row_selection(&self) -> Option<&crate::selection::RowSelectionState> {
None
}
fn row_selection_mut(&mut self) -> Option<&mut crate::selection::RowSelectionState> {
None
}
type RowId: Clone + Eq;
fn ordering(&self) -> &SortOrder;
fn set_ordering(&mut self, ordering: SortOrder) -> Result<(), SortError>;
fn visible_row_id(&self, row_ix: usize) -> Option<Self::RowId>;
fn visible_row_position(&self, id: &Self::RowId) -> Option<usize>;
}
pub trait ResolvedOrderedTableDelegate<R: SortableRow>: OrderedTableDelegate {
fn resolved_sort_columns(&self) -> &ResolvedSortColumns<R>;
fn set_resolved_sort_columns(
&mut self,
columns: ResolvedSortColumns<R>,
) -> Result<(), SortError>;
}
type ResolvedCell<R> = dyn Fn(&R, &mut Window, &mut App) -> AnyElement;
pub struct ResolvedTableColumn<R> {
column: Column,
render: std::rc::Rc<ResolvedCell<R>>,
}
impl<R> Clone for ResolvedTableColumn<R> {
fn clone(&self) -> Self {
Self {
column: self.column.clone(),
render: self.render.clone(),
}
}
}
impl<R> ResolvedTableColumn<R> {
pub fn new(
column: Column,
render: impl Fn(&R, &mut Window, &mut App) -> AnyElement + 'static,
) -> Result<Self, SortError> {
SortOrder::new(vec![gpui_table_core::sort::SortClause::new(
column.key.to_string(),
gpui_table_core::sort::SortDirection::Ascending,
)])?;
Ok(Self {
column,
render: std::rc::Rc::new(render),
})
}
pub fn column(&self) -> &Column {
&self.column
}
pub fn render(&self, row: &R, window: &mut Window, cx: &mut App) -> AnyElement {
(self.render)(row, window, cx)
}
}
pub trait ResolvedTableDelegate<R: SortableRow>: ResolvedOrderedTableDelegate<R> {
fn resolved_table_columns(&self) -> &[ResolvedTableColumn<R>];
fn set_resolved_table_columns(
&mut self,
columns: Vec<ResolvedTableColumn<R>>,
) -> Result<(), SortError>;
}
pub fn set_table_resolved_columns<R: SortableRow, D: ResolvedTableDelegate<R>>(
table: &mut TableState<D>,
columns: Vec<ResolvedTableColumn<R>>,
cx: &mut Context<TableState<D>>,
) -> Result<(), SortError> {
let mut selected = TableRowSelection::capture(table);
let column_key = match selected.selection {
TableSelection::Cell(_, column) | TableSelection::Column(column)
if column < table.delegate().columns_count(cx) =>
{
Some(table.delegate().column(column, cx).key)
},
_ => None,
};
table.delegate_mut().set_resolved_table_columns(columns)?;
let column = column_key.and_then(|key| {
(0..table.delegate().columns_count(cx))
.find(|column| table.delegate().column(*column, cx).key == key)
});
selected.selection = match selected.selection {
TableSelection::Cell(row, _) => column.map_or(TableSelection::Row(row), |column| {
TableSelection::Cell(row, column)
}),
TableSelection::Column(_) => column.map_or(TableSelection::None, TableSelection::Column),
selection => selection,
};
table.refresh(cx);
selected.restore(table, cx);
cx.notify();
Ok(())
}
#[derive(Clone)]
pub struct TableRowSelection<Id> {
row: Option<Id>,
rows: Vec<Id>,
cursor: Option<Id>,
anchor: Option<Id>,
revision: u64,
selection: TableSelection,
}
impl<Id: Clone + Eq> TableRowSelection<Id> {
pub fn row_id(&self) -> Option<&Id> {
self.row.as_ref()
}
pub fn row_ids(&self) -> &[Id] {
&self.rows
}
pub fn selection_revision(&self) -> u64 {
self.revision
}
pub fn all_rows_visible<D: OrderedTableDelegate<RowId = Id>>(&self, delegate: &D) -> bool {
self.rows
.iter()
.all(|id| delegate.visible_row_position(id).is_some())
}
pub fn preserve_unavailable<D: OrderedTableDelegate<RowId = Id>>(
&mut self,
old: &Self,
delegate: &D,
) {
for id in &old.rows {
if delegate.visible_row_position(id).is_none() && !self.rows.contains(id) {
self.rows.push(id.clone());
}
}
if !self.rows.is_empty() && self.selection == TableSelection::None {
self.selection = TableSelection::Row(0);
}
}
pub fn remove_row_id(&mut self, id: &Id) {
self.rows.retain(|row| row != id);
if self.row.as_ref() == Some(id) {
self.row = self.rows.first().cloned();
}
{
if self.cursor.as_ref() == Some(id) {
self.cursor = None;
}
if self.anchor.as_ref() == Some(id) {
self.anchor = None;
}
}
if self.rows.is_empty()
&& matches!(
self.selection,
TableSelection::Row(_) | TableSelection::Cell(_, _)
)
{
self.selection = TableSelection::None;
}
}
pub fn capture<D: OrderedTableDelegate<RowId = Id>>(table: &TableState<D>) -> Self {
let selection = table.selection();
let row_ix = match selection {
TableSelection::Row(ix) | TableSelection::Cell(ix, _) => Some(ix),
_ => None,
};
let row = row_ix.and_then(|ix| table.delegate().visible_row_id(ix));
let rows = if matches!(selection, TableSelection::Cell(_, _)) {
row.iter().cloned().collect()
} else {
crate::selection::selected_rows(table)
.into_iter()
.filter_map(|ix| table.delegate().visible_row_id(ix))
.collect()
};
Self {
row,
rows,
cursor: crate::selection::row_selection_cursor(table)
.and_then(|ix| table.delegate().visible_row_id(ix)),
anchor: crate::selection::row_selection_anchor(table)
.and_then(|ix| table.delegate().visible_row_id(ix)),
revision: crate::selection::selection_revision(table),
selection,
}
}
pub fn restore<D: OrderedTableDelegate<RowId = Id>>(
self,
table: &mut TableState<D>,
cx: &mut Context<TableState<D>>,
) {
if matches!(self.selection, TableSelection::Row(_)) {
let rows = self
.rows
.iter()
.filter_map(|id| table.delegate().visible_row_position(id))
.collect();
let cursor = self
.cursor
.and_then(|id| table.delegate().visible_row_position(&id));
let anchor = self
.anchor
.and_then(|id| table.delegate().visible_row_position(&id));
crate::selection::restore_rows(table, rows, cursor, anchor, cx);
return;
}
let selection = match (self.row, self.selection) {
(Some(id), TableSelection::Row(_)) => table
.delegate()
.visible_row_position(&id)
.map(TableSelection::Row)
.unwrap_or_default(),
(Some(id), TableSelection::Cell(_, col)) => table
.delegate()
.visible_row_position(&id)
.map(|ix| TableSelection::Cell(ix, col))
.unwrap_or_default(),
(None, TableSelection::Row(_) | TableSelection::Cell(_, _)) => TableSelection::None,
(_, selection) => selection,
};
let columns = table.delegate().columns_count(cx);
let selection = match selection {
TableSelection::Cell(row, column) if column >= columns => TableSelection::Row(row),
TableSelection::Column(column) if column >= columns => TableSelection::None,
selection => selection,
};
table.set_selection(selection, cx);
}
}
pub fn set_table_ordering<D: OrderedTableDelegate>(
table: &mut TableState<D>,
ordering: SortOrder,
cx: &mut Context<TableState<D>>,
) -> Result<(), SortError> {
let selected = TableRowSelection::capture(table);
table.delegate_mut().set_ordering(ordering)?;
table.refresh(cx);
selected.restore(table, cx);
cx.notify();
Ok(())
}
pub fn set_table_resolved_sort_columns<R: SortableRow, D: ResolvedOrderedTableDelegate<R>>(
table: &mut TableState<D>,
columns: ResolvedSortColumns<R>,
cx: &mut Context<TableState<D>>,
) -> Result<(), SortError> {
let selected = TableRowSelection::capture(table);
table.delegate_mut().set_resolved_sort_columns(columns)?;
table.refresh(cx);
selected.restore(table, cx);
cx.notify();
Ok(())
}