Skip to main content

gpui_table_runtime/
load.rs

1use gpui_kit::component::table::{TableDelegate, TableState};
2use gpui_kit::{App, Context, Window};
3
4/// Internal trait implemented by `#[gpui_table_impl]` to provide loading behavior.
5///
6/// This trait bridges user-defined `TableLoader` logic to the generated
7/// `TableDelegate` implementation.
8pub trait LoadMoreDelegate: TableDelegate {
9    /// Check if there is more data to load.
10    fn has_more(&self, app: &App) -> bool;
11
12    /// Threshold of rows from bottom to trigger load_more.
13    fn load_more_threshold(&self) -> usize {
14        10
15    }
16
17    /// Load more data into the table.
18    fn load_more(&mut self, window: &mut Window, cx: &mut Context<TableState<Self>>);
19}
20
21/// Trait for table delegates that support loading data.
22pub trait TableDataLoader: TableDelegate {
23    /// Load data into the table.
24    fn load_data(&mut self, window: &mut Window, cx: &mut Context<TableState<Self>>);
25}
26
27/// Trait for defining table loading behavior.
28pub trait TableLoader: TableDelegate {
29    /// Number of rows from the bottom at which to trigger loading more data.
30    const THRESHOLD: usize = 10;
31
32    /// Load more data into the table.
33    fn load_more(&mut self, window: &mut Window, cx: &mut Context<TableState<Self>>);
34}
35
36#[cfg(test)]
37mod tests {
38    use super::{LoadMoreDelegate, TableLoader};
39    use gpui_kit::component::table::{Column, TableDelegate, TableState};
40    use gpui_kit::{App, Context, IntoElement, Window, div};
41
42    struct Delegate;
43
44    impl TableDelegate for Delegate {
45        fn columns_count(&self, _cx: &App) -> usize {
46            0
47        }
48
49        fn rows_count(&self, _cx: &App) -> usize {
50            0
51        }
52
53        fn column(&self, _col_ix: usize, _cx: &App) -> Column {
54            unreachable!("the threshold contract does not inspect columns")
55        }
56
57        fn render_td(
58            &mut self,
59            _row_ix: usize,
60            _col_ix: usize,
61            _window: &mut Window,
62            _cx: &mut Context<TableState<Self>>,
63        ) -> impl IntoElement {
64            div()
65        }
66    }
67
68    impl LoadMoreDelegate for Delegate {
69        fn has_more(&self, _app: &App) -> bool {
70            true
71        }
72
73        fn load_more(&mut self, _window: &mut Window, _cx: &mut Context<TableState<Self>>) {}
74    }
75
76    impl TableLoader for Delegate {
77        fn load_more(&mut self, _window: &mut Window, _cx: &mut Context<TableState<Self>>) {}
78    }
79
80    #[test]
81    fn loading_traits_share_the_documented_default_threshold() {
82        assert_eq!(
83            <Delegate as LoadMoreDelegate>::load_more_threshold(&Delegate),
84            10
85        );
86        assert_eq!(<Delegate as TableLoader>::THRESHOLD, 10);
87    }
88}