use gpui_kit::component::{menu::PopupMenu, table::Column};
use gpui_kit::{AnyElement, App, Div, InteractiveElement as _, IntoElement, Stateful, Window, div};
use gpui_table_schema::{filter::FilterConfig, registry::TableId};
use crate::TableCell;
pub trait TableRowMeta {
const TABLE_ID: &'static str;
const TABLE_TITLE: &'static str;
fn table_title() -> String {
Self::TABLE_TITLE.to_string()
}
fn table_id() -> TableId<'static> {
TableId::new(Self::TABLE_ID)
}
fn table_columns() -> Vec<Column>;
fn cell_value(&self, col_ix: usize) -> Box<dyn TableCell + '_>;
fn table_filters() -> Vec<FilterConfig> {
Vec::new()
}
}
pub trait TableRowStyle: TableRowMeta {
type ColumnId: Into<usize> + From<usize>;
fn render_table_cell(
&self,
col: Self::ColumnId,
window: &mut Window,
cx: &mut App,
) -> AnyElement;
fn render_table_row(&self, row_ix: usize, window: &mut Window, cx: &mut App) -> Stateful<Div> {
default_render_row(row_ix, window, cx)
}
}
pub trait TableRowContextMenu: TableRowMeta {
fn render_table_context_menu(
&self,
_row_ix: usize,
menu: PopupMenu,
_window: &mut Window,
_cx: &mut App,
) -> PopupMenu {
menu
}
}
pub trait TableRowGeneratedContextMenu: TableRowMeta {
fn render_generated_table_context_menu(
&self,
_row_ix: usize,
menu: PopupMenu,
_window: &mut Window,
_cx: &mut App,
) -> PopupMenu {
menu
}
}
pub fn default_render_cell<R: TableRowMeta + ?Sized>(
row: &R,
col_ix: usize,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
row.cell_value(col_ix).draw(window, cx)
}
pub fn default_render_row(row_ix: usize, _window: &mut Window, _cx: &mut App) -> Stateful<Div> {
div().id(row_ix)
}
#[cfg(test)]
mod tests {
use super::TableRowMeta;
use crate::TableCell;
use gpui_kit::component::table::Column;
struct Row;
impl TableRowMeta for Row {
const TABLE_ID: &'static str = "row";
const TABLE_TITLE: &'static str = "Rows";
fn table_columns() -> Vec<Column> {
Vec::new()
}
fn cell_value(&self, _col_ix: usize) -> Box<dyn TableCell + '_> {
Box::new("cell")
}
}
#[test]
fn row_metadata_defaults_expose_ids_titles_and_no_filters() {
assert_eq!(Row::table_title(), "Rows");
assert_eq!(Row::table_id().as_str(), "row");
assert!(Row::table_columns().is_empty());
assert!(Row::table_filters().is_empty());
let _ = Row.cell_value(0);
}
}