gpui_table_runtime/
row.rs1use 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
7pub trait TableRowMeta {
9 const TABLE_ID: &'static str;
11
12 const TABLE_TITLE: &'static str;
14
15 fn table_title() -> String {
18 Self::TABLE_TITLE.to_string()
19 }
20
21 fn table_id() -> TableId<'static> {
23 TableId::new(Self::TABLE_ID)
24 }
25
26 fn table_columns() -> Vec<Column>;
28
29 fn cell_value(&self, col_ix: usize) -> Box<dyn TableCell + '_>;
31
32 fn table_filters() -> Vec<FilterConfig> {
34 Vec::new()
35 }
36}
37
38pub trait TableRowStyle: TableRowMeta {
40 type ColumnId: Into<usize> + From<usize>;
42
43 fn render_table_cell(
45 &self,
46 col: Self::ColumnId,
47 window: &mut Window,
48 cx: &mut App,
49 ) -> AnyElement;
50
51 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
57pub trait TableRowContextMenu: TableRowMeta {
59 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
71pub trait TableRowGeneratedContextMenu: TableRowMeta {
73 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
85pub 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
95pub 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}