Skip to main content

gpui_table_runtime/
row.rs

1use gpui_kit::component::{menu::PopupMenu, table::Column};
2use gpui_kit::{AnyElement, App, Div, InteractiveElement as _, IntoElement, Stateful, Window, div};
3use gpui_table_schema::{filter::FilterConfig, registry::TableId};
4
5use crate::TableCell;
6
7/// Metadata for a table row type.
8pub trait TableRowMeta {
9    /// Unique identifier for this row type.
10    const TABLE_ID: &'static str;
11
12    /// Human-readable title for the table.
13    const TABLE_TITLE: &'static str;
14
15    /// Returns the table title. This can be overridden to provide dynamic titles,
16    /// for example from localization libraries.
17    fn table_title() -> String {
18        Self::TABLE_TITLE.to_string()
19    }
20
21    /// Returns the typed table identifier.
22    fn table_id() -> TableId<'static> {
23        TableId::new(Self::TABLE_ID)
24    }
25
26    /// Returns the column definitions for this row type.
27    fn table_columns() -> Vec<Column>;
28
29    /// Returns the value for a specific column index.
30    fn cell_value(&self, col_ix: usize) -> Box<dyn TableCell + '_>;
31
32    /// Returns the filter configuration for the table.
33    fn table_filters() -> Vec<FilterConfig> {
34        Vec::new()
35    }
36}
37
38/// Rendering contract for a table row.
39pub trait TableRowStyle: TableRowMeta {
40    /// The type representing the columns of the table.
41    type ColumnId: Into<usize> + From<usize>;
42
43    /// Renders a single cell.
44    fn render_table_cell(
45        &self,
46        col: Self::ColumnId,
47        window: &mut Window,
48        cx: &mut App,
49    ) -> AnyElement;
50
51    /// Renders the row container.
52    fn render_table_row(&self, row_ix: usize, window: &mut Window, cx: &mut App) -> Stateful<Div> {
53        default_render_row(row_ix, window, cx)
54    }
55}
56
57/// Context-menu hooks for a table row.
58pub trait TableRowContextMenu: TableRowMeta {
59    /// Builds the context menu for this row.
60    fn render_table_context_menu(
61        &self,
62        _row_ix: usize,
63        menu: PopupMenu,
64        _window: &mut Window,
65        _cx: &mut App,
66    ) -> PopupMenu {
67        menu
68    }
69}
70
71/// Generated/default context-menu composition hooks for a table row.
72pub trait TableRowGeneratedContextMenu: TableRowMeta {
73    /// Builds the derive-generated/default portion of the row context menu.
74    fn render_generated_table_context_menu(
75        &self,
76        _row_ix: usize,
77        menu: PopupMenu,
78        _window: &mut Window,
79        _cx: &mut App,
80    ) -> PopupMenu {
81        menu
82    }
83}
84
85/// Default implementation for rendering a cell.
86pub fn default_render_cell<R: TableRowMeta + ?Sized>(
87    row: &R,
88    col_ix: usize,
89    window: &mut Window,
90    cx: &mut App,
91) -> impl IntoElement {
92    row.cell_value(col_ix).draw(window, cx)
93}
94
95/// Default implementation for rendering a row.
96pub fn default_render_row(row_ix: usize, _window: &mut Window, _cx: &mut App) -> Stateful<Div> {
97    div().id(("row", row_ix))
98}
99
100#[cfg(test)]
101mod tests {
102    use super::TableRowMeta;
103    use crate::TableCell;
104    use gpui_kit::component::table::Column;
105
106    struct Row;
107
108    impl TableRowMeta for Row {
109        const TABLE_ID: &'static str = "row";
110        const TABLE_TITLE: &'static str = "Rows";
111
112        fn table_columns() -> Vec<Column> {
113            Vec::new()
114        }
115
116        fn cell_value(&self, _col_ix: usize) -> Box<dyn TableCell + '_> {
117            Box::new("cell")
118        }
119    }
120
121    #[test]
122    fn row_metadata_defaults_expose_ids_titles_and_no_filters() {
123        assert_eq!(Row::table_title(), "Rows");
124        assert_eq!(Row::table_id().as_str(), "row");
125        assert!(Row::table_columns().is_empty());
126        assert!(Row::table_filters().is_empty());
127        let _ = Row.cell_value(0);
128    }
129}