Skip to main content

gpui_component/list/
delegate.rs

1use gpui::ColorExt as _;
2use gpui::{AnyElement, App, Context, IntoElement, ParentElement as _, Styled as _, Task, Window};
3
4use crate::{
5    ActiveTheme as _, Icon, IconName, IndexPath, Selectable, h_flex,
6    list::{ListState, loading::Loading},
7};
8
9/// A delegate for the List.
10#[allow(unused)]
11pub trait ListDelegate: Sized + 'static {
12    type Item: Selectable + IntoElement;
13
14    /// When Query Input change, this method will be called.
15    /// You can perform search here.
16    fn perform_search(
17        &mut self,
18        query: &str,
19        window: &mut Window,
20        cx: &mut Context<ListState<Self>>,
21    ) -> Task<()> {
22        Task::ready(())
23    }
24
25    /// Return the number of sections in the list, default is 1.
26    ///
27    /// Min value is 1.
28    fn sections_count(&self, cx: &App) -> usize {
29        1
30    }
31
32    /// Return the number of items in the section at the given index.
33    ///
34    /// NOTE: Only the sections with items_count > 0 will be rendered. If the section has 0 items,
35    /// the section header and footer will also be skipped.
36    fn items_count(&self, section: usize, cx: &App) -> usize;
37
38    /// Render the item at the given index.
39    ///
40    /// Return None will skip the item.
41    ///
42    /// NOTE: Every item should have same height.
43    fn render_item(
44        &mut self,
45        ix: IndexPath,
46        window: &mut Window,
47        cx: &mut Context<ListState<Self>>,
48    ) -> Option<Self::Item>;
49
50    /// Render the section header at the given index, default is None.
51    ///
52    /// NOTE: Every header should have same height.
53    fn render_section_header(
54        &mut self,
55        section: usize,
56        window: &mut Window,
57        cx: &mut Context<ListState<Self>>,
58    ) -> Option<impl IntoElement> {
59        None::<AnyElement>
60    }
61
62    /// Render the section footer at the given index, default is None.
63    ///
64    /// NOTE: Every footer should have same height.
65    fn render_section_footer(
66        &mut self,
67        section: usize,
68        window: &mut Window,
69        cx: &mut Context<ListState<Self>>,
70    ) -> Option<impl IntoElement> {
71        None::<AnyElement>
72    }
73
74    /// Return a Element to show when list is empty.
75    fn render_empty(
76        &mut self,
77        window: &mut Window,
78        cx: &mut Context<ListState<Self>>,
79    ) -> impl IntoElement {
80        h_flex()
81            .size_full()
82            .justify_center()
83            .text_color(cx.theme().muted_foreground.opacity(0.6))
84            .child(Icon::new(IconName::Inbox).size_12())
85            .into_any_element()
86    }
87
88    /// Returns Some(AnyElement) to render the initial state of the list.
89    ///
90    /// This can be used to show a view for the list before the user has
91    /// interacted with it.
92    ///
93    /// For example: The last search results, or the last selected item.
94    ///
95    /// Default is None, that means no initial state.
96    fn render_initial(
97        &mut self,
98        window: &mut Window,
99        cx: &mut Context<ListState<Self>>,
100    ) -> Option<AnyElement> {
101        None
102    }
103
104    /// Returns the loading state to show the loading view.
105    fn loading(&self, cx: &App) -> bool {
106        false
107    }
108
109    /// Returns a Element to show when loading, default is built-in Skeleton
110    /// loading view.
111    fn render_loading(
112        &mut self,
113        window: &mut Window,
114        cx: &mut Context<ListState<Self>>,
115    ) -> impl IntoElement {
116        Loading
117    }
118
119    /// Set the selected index, just store the ix, don't confirm.
120    fn set_selected_index(
121        &mut self,
122        ix: Option<IndexPath>,
123        window: &mut Window,
124        cx: &mut Context<ListState<Self>>,
125    );
126
127    /// Set the index of the item that has been right clicked.
128    fn set_right_clicked_index(
129        &mut self,
130        ix: Option<IndexPath>,
131        window: &mut Window,
132        cx: &mut Context<ListState<Self>>,
133    ) {
134    }
135
136    /// Set the confirm and give the selected index,
137    /// this is means user have clicked the item or pressed Enter.
138    ///
139    /// This will always to `set_selected_index` before confirm.
140    fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<ListState<Self>>) {
141    }
142
143    /// Cancel the selection, e.g.: Pressed ESC.
144    fn cancel(&mut self, window: &mut Window, cx: &mut Context<ListState<Self>>) {}
145
146    /// Return true to enable load more data when scrolling to the bottom.
147    ///
148    /// Default: false
149    fn has_more(&self, cx: &App) -> bool {
150        false
151    }
152
153    /// Returns a threshold value (n entities), of course,
154    /// when scrolling to the bottom, the remaining number of rows
155    /// triggers `load_more`.
156    ///
157    /// This should smaller than the total number of first load rows.
158    ///
159    /// Default: 20 entities (section header, footer and row)
160    fn load_more_threshold(&self) -> usize {
161        20
162    }
163
164    /// Load more data when the table is scrolled to the bottom.
165    ///
166    /// This will performed in a background task.
167    ///
168    /// This is always called when the table is near the bottom,
169    /// so you must check if there is more data to load or lock
170    /// the loading state.
171    fn load_more(&mut self, window: &mut Window, cx: &mut Context<ListState<Self>>) {}
172}