1use gpui_base::LogicalStyledExt as _;
2use std::rc::Rc;
3
4use gpui::{
5 AnyElement, App, Entity, InteractiveElement as _, IntoElement, ListAlignment, ListOffset,
6 ListState, ParentElement as _, SharedString, StyleRefinement, Styled, Window, div, list,
7 prelude::FluentBuilder as _, px,
8};
9use rust_i18n::t;
10
11use crate::{
12 ActiveTheme, Icon, IconName, Sizable, StyledExt,
13 button::{Button, ButtonVariants},
14 h_flex,
15 label::Label,
16 scroll::ScrollableElement,
17 setting::{RenderOptions, SettingGroup, settings::SettingsState},
18 v_flex,
19};
20
21#[derive(Clone)]
23pub struct SettingPage {
24 pub(super) icon: Option<Icon>,
25 resettable: bool,
26 pub(super) default_open: bool,
27 pub(super) title: SharedString,
28 pub(super) title_suffix: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>>,
29 pub(super) description: Option<SharedString>,
30 pub(super) groups: Vec<SettingGroup>,
31 pub(super) header_style: StyleRefinement,
32}
33
34impl SettingPage {
35 pub fn new(title: impl Into<SharedString>) -> Self {
36 Self {
37 icon: None,
38 resettable: true,
39 default_open: false,
40 title: title.into(),
41 title_suffix: None,
42 description: None,
43 groups: Vec::new(),
44 header_style: StyleRefinement::default(),
45 }
46 }
47
48 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
50 self.title = title.into();
51 self
52 }
53
54 pub fn title_suffix<F, E>(mut self, suffix: F) -> Self
58 where
59 E: IntoElement,
60 F: Fn(&mut Window, &mut App) -> E + 'static,
61 {
62 self.title_suffix = Some(Rc::new(move |window, cx| {
63 suffix(window, cx).into_any_element()
64 }));
65 self
66 }
67
68 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
70 self.icon = Some(icon.into());
71 self
72 }
73
74 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
76 self.description = Some(description.into());
77 self
78 }
79
80 pub fn default_open(mut self, default_open: bool) -> Self {
82 self.default_open = default_open;
83 self
84 }
85
86 pub fn resettable(mut self, resettable: bool) -> Self {
90 self.resettable = resettable;
91 self
92 }
93
94 pub fn group(mut self, group: SettingGroup) -> Self {
96 self.groups.push(group);
97 self
98 }
99
100 pub fn groups(mut self, groups: impl IntoIterator<Item = SettingGroup>) -> Self {
102 self.groups.extend(groups);
103 self
104 }
105
106 pub fn header_style(mut self, style: &StyleRefinement) -> Self {
108 self.header_style = style.clone();
109 self
110 }
111
112 fn is_resettable(&self, query: &str, cx: &App) -> bool {
113 self.resettable
114 && self
115 .groups
116 .iter()
117 .any(|group| group.is_resettable(query, cx))
118 }
119
120 fn reset_all(&self, query: &str, window: &mut Window, cx: &mut App) {
121 for group in &self.groups {
122 group.reset(query, window, cx);
123 }
124 }
125
126 fn group_at_scroll_top(
132 &self,
133 group_indices: &[usize],
134 selected_group_ix: Option<usize>,
135 scroll_top: ListOffset,
136 is_scrolled_to_end: bool,
137 ) -> Option<usize> {
138 let selected_pos =
139 selected_group_ix.and_then(|ix| group_indices.iter().position(|&g| g == ix));
140 if is_scrolled_to_end && selected_pos.is_some_and(|pos| pos > scroll_top.item_ix) {
141 return selected_group_ix;
142 }
143
144 let top_pos = if is_scrolled_to_end {
145 group_indices.len().saturating_sub(1)
146 } else {
147 scroll_top.item_ix
148 };
149 group_indices
150 .iter()
151 .take(top_pos + 1)
152 .rev()
153 .copied()
154 .find(|&ix| self.groups[ix].title.is_some())
155 }
156
157 pub(super) fn render(
158 &self,
159 ix: usize,
160 group_indices: &[usize],
161 state: &Entity<SettingsState>,
162 options: &RenderOptions,
163 window: &mut Window,
164 cx: &mut App,
165 ) -> impl IntoElement {
166 let search_input = state.read(cx).search_input.clone();
167 let query = search_input.read(cx).value();
168 let groups = group_indices
169 .iter()
170 .map(|&ix| (ix, self.groups[ix].clone()))
171 .collect::<Vec<_>>();
172 let groups_count = groups.len();
173
174 let page_state = window.use_keyed_state(
175 SharedString::from(format!("list-state:{}", ix)),
176 cx,
177 |_, _| PageState {
178 list: ListState::new(groups_count, ListAlignment::Top, px(100.)),
179 query: query.clone(),
180 groups: group_indices.to_vec(),
181 scroll_top: ListOffset::default(),
182 scrolled_programmatically: false,
183 },
184 );
185 let changed =
186 page_state.read(cx).query != query || page_state.read(cx).groups != group_indices;
187 let list_state = page_state.read(cx).list.clone();
188 if changed {
189 page_state.update(cx, |state, _| {
190 state.list.reset(groups_count);
191 state.query = query.clone();
192 state.groups = group_indices.to_vec();
193 });
194 }
195
196 let deferred_scroll_group_ix = state.read(cx).deferred_scroll_group_ix;
197 let scroll_group_ix = deferred_scroll_group_ix.or_else(|| {
198 changed
199 .then_some(state.read(cx).selected_index.group_ix)
200 .flatten()
201 });
202 if deferred_scroll_group_ix.is_some() {
203 state.update(cx, |state, _| state.deferred_scroll_group_ix = None);
204 }
205 if let Some(group_ix) = scroll_group_ix
206 && let Some(visible_ix) = group_indices.iter().position(|&ix| ix == group_ix)
207 {
208 list_state.scroll_to(ListOffset {
211 item_ix: visible_ix,
212 offset_in_item: px(0.),
213 });
214 }
215
216 let scroll_top = list_state.logical_scroll_top();
218 let programmatic = changed || scroll_group_ix.is_some();
219 let scrolled = !programmatic
220 && !page_state.read(cx).scrolled_programmatically
221 && page_state.read(cx).scroll_top != scroll_top;
222 page_state.update(cx, |state, _| {
223 state.scroll_top = scroll_top;
224 state.scrolled_programmatically = programmatic;
225 });
226 if scrolled {
227 let selected_group_ix = state.read(cx).selected_index.group_ix;
228 let group_ix = self.group_at_scroll_top(
229 group_indices,
230 selected_group_ix,
231 scroll_top,
232 list_state.is_scrolled_to_end() == Some(true),
233 );
234 if group_ix != selected_group_ix {
235 state.update(cx, |state, _| state.selected_index.group_ix = group_ix);
236 window.refresh();
238 }
239 }
240
241 v_flex()
242 .id(ix)
243 .size_full()
244 .child(
245 v_flex()
246 .p_4()
247 .gap_3()
248 .border_b_1()
249 .border_color(cx.theme().border)
250 .refine_style(&self.header_style)
251 .child(
252 h_flex()
253 .flex_row_dir(cx)
254 .justify_between()
255 .child(
256 h_flex()
257 .flex_row_dir(cx)
258 .gap_1()
259 .child(self.title.clone())
260 .when_some(self.title_suffix.clone(), |this, suffix| {
261 this.child(suffix(window, cx))
262 }),
263 )
264 .when(self.is_resettable(&query, cx), |this| {
265 this.child(
266 Button::new("reset")
267 .icon(IconName::Undo2)
268 .ghost()
269 .small()
270 .tooltip(t!("Settings.Reset All"))
271 .on_click({
272 let page = self.clone();
273 let query = query.clone();
274 move |_, window, cx| {
275 page.reset_all(&query, window, cx);
276 }
277 }),
278 )
279 }),
280 )
281 .when_some(self.description.clone(), |this, description| {
282 this.child(
283 Label::new(description)
284 .text_sm()
285 .text_color(cx.theme().muted_foreground),
286 )
287 }),
288 )
289 .child(
290 div()
291 .relative()
292 .flex_1()
293 .w_full()
294 .child(gpui_base::ScrollBounce::new(
295 "bounce",
296 &list_state,
297 list(list_state.clone(), {
298 let query = query.clone();
299 let options = *options;
300 move |visible_ix, window, cx| {
301 let (group_ix, group) = groups[visible_ix].clone();
302 div()
305 .w_full()
306 .px_4()
307 .child(group.py_4().render(
308 &query,
309 &options.with_page_ix(ix).with_group_ix(group_ix),
310 window,
311 cx,
312 ))
313 .into_any_element()
314 }
315 })
316 .size_full(),
317 ))
318 .vertical_scrollbar(&list_state),
319 )
320 }
321}
322
323struct PageState {
324 list: ListState,
325 query: SharedString,
326 groups: Vec<usize>,
327 scroll_top: ListOffset,
329 scrolled_programmatically: bool,
332}