1use gpui::{
2 AnyElement, App, Axis, Div, InteractiveElement as _, IntoElement, ParentElement, SharedString,
3 Stateful, Styled, Window, div, prelude::FluentBuilder as _,
4};
5use gpui_base::LogicalStyledExt as _;
6use std::{any::TypeId, ops::Deref, rc::Rc};
7
8use crate::{
9 ActiveTheme as _, AxisExt, StyledExt as _,
10 label::Label,
11 setting::{
12 AnySettingField, ElementField, RenderOptions,
13 fields::{
14 BoolField, DropdownField, NumberField, ResetHandler, SettingFieldRender, StringField,
15 },
16 },
17 text::Text,
18 v_flex,
19};
20
21#[derive(Clone)]
23pub enum SettingItem {
24 Item {
26 title: SharedString,
27 description: Option<Text>,
28 keywords: Vec<SharedString>,
29 layout: Axis,
30 disabled: bool,
31 field: Rc<dyn AnySettingField>,
32 },
33 Element {
35 disabled: bool,
36 keywords: Vec<SharedString>,
37 reset_handler: Option<ResetHandler>,
41 render: Rc<dyn Fn(&RenderOptions, &mut Window, &mut App) -> AnyElement + 'static>,
42 },
43}
44
45impl SettingItem {
46 pub fn new<F>(title: impl Into<SharedString>, field: F) -> Self
48 where
49 F: AnySettingField + 'static,
50 {
51 SettingItem::Item {
52 title: title.into(),
53 description: None,
54 layout: Axis::Horizontal,
55 disabled: false,
56 keywords: Vec::new(),
57 field: Rc::new(field),
58 }
59 }
60
61 pub fn render<R, E>(render: R) -> Self
63 where
64 E: IntoElement,
65 R: Fn(&RenderOptions, &mut Window, &mut App) -> E + 'static,
66 {
67 SettingItem::Element {
68 disabled: false,
69 keywords: Vec::new(),
70 reset_handler: None,
71 render: Rc::new(move |options, window, cx| {
72 render(options, window, cx).into_any_element()
73 }),
74 }
75 }
76
77 pub fn on_reset<D, R>(mut self, is_dirty: D, reset: R) -> Self
86 where
87 D: Fn(&App) -> bool + 'static,
88 R: Fn(&mut Window, &mut App) + 'static,
89 {
90 match &mut self {
91 SettingItem::Element { reset_handler, .. } => {
92 *reset_handler = Some((Rc::new(is_dirty), Rc::new(reset)));
93 }
94 SettingItem::Item { .. } => {
97 debug_assert!(
98 false,
99 "SettingItem::on_reset only applies to SettingItem::Element; \
100 use SettingField::default_value or SettingField::on_reset for a normal item"
101 );
102 }
103 }
104 self
105 }
106
107 pub fn keywords<I, S>(mut self, keywords: I) -> Self
113 where
114 I: IntoIterator<Item = S>,
115 S: Into<SharedString>,
116 {
117 let keywords: Vec<SharedString> = keywords.into_iter().map(Into::into).collect();
118 match &mut self {
119 SettingItem::Item { keywords: k, .. } => *k = keywords,
120 SettingItem::Element { keywords: k, .. } => *k = keywords,
121 }
122 self
123 }
124
125 pub fn disabled(mut self, disabled: bool) -> Self {
133 match &mut self {
134 SettingItem::Item { disabled: d, .. } => *d = disabled,
135 SettingItem::Element { disabled: d, .. } => *d = disabled,
136 }
137 self
138 }
139
140 pub fn description(mut self, description: impl Into<Text>) -> Self {
144 match &mut self {
145 SettingItem::Item { description: d, .. } => {
146 *d = Some(description.into());
147 }
148 SettingItem::Element { .. } => {}
149 }
150 self
151 }
152
153 pub fn layout(mut self, layout: Axis) -> Self {
157 match &mut self {
158 SettingItem::Item { layout: l, .. } => {
159 *l = layout;
160 }
161 SettingItem::Element { .. } => {}
162 }
163 self
164 }
165
166 pub(crate) fn is_match(&self, query: &str, cx: &App) -> bool {
167 match self {
168 SettingItem::Item {
169 title,
170 description,
171 keywords,
172 ..
173 } => {
174 let q = &query.to_lowercase();
175 title.to_lowercase().contains(q)
176 || description
177 .as_ref()
178 .map_or(false, |d| d.get_text(cx).to_lowercase().contains(q))
179 || keywords.iter().any(|s| s.to_lowercase().contains(q))
180 }
181 SettingItem::Element { keywords, .. } => {
183 let q = &query.to_lowercase();
184 query.is_empty() || keywords.iter().any(|s| s.to_lowercase().contains(q))
185 }
186 }
187 }
188
189 pub(crate) fn is_resettable(&self, cx: &App) -> bool {
190 match self {
191 SettingItem::Item { field, .. } => field.is_resettable(cx),
192 SettingItem::Element { reset_handler, .. } => reset_handler
193 .as_ref()
194 .is_some_and(|(is_dirty, _)| is_dirty(cx)),
195 }
196 }
197
198 pub(crate) fn reset(&self, window: &mut Window, cx: &mut App) {
199 match self {
200 SettingItem::Item { field, .. } => field.reset(window, cx),
201 SettingItem::Element { reset_handler, .. } => {
202 if let Some((_, reset)) = reset_handler.as_ref() {
203 reset(window, cx);
204 }
205 }
206 }
207 }
208
209 fn render_field(
210 title: SharedString,
211 field: Rc<dyn AnySettingField>,
212 options: RenderOptions,
213 window: &mut Window,
214 cx: &mut App,
215 ) -> impl IntoElement {
216 let field_type = field.field_type();
217 let style = field.style().clone();
218 let type_id = field.deref().type_id();
219 let renderer: Box<dyn SettingFieldRender> = match type_id {
220 t if t == std::any::TypeId::of::<bool>() => {
221 Box::new(BoolField::new(field_type.is_switch(), title))
222 }
223 t if t == TypeId::of::<f64>() && field_type.is_number_input() => {
224 Box::new(NumberField::new(field_type.number_input_options(), title))
225 }
226 t if t == TypeId::of::<SharedString>() && field_type.is_input() => {
227 Box::new(StringField::<SharedString>::new(title))
228 }
229 t if t == TypeId::of::<String>() && field_type.is_input() => {
230 Box::new(StringField::<String>::new(title))
231 }
232 t if t == TypeId::of::<SharedString>() && field_type.is_dropdown() => {
233 Box::new(DropdownField::<SharedString>::new(
234 field_type.dropdown_options(),
235 field_type.dropdown_scrollable(),
236 title,
237 ))
238 }
239 t if t == TypeId::of::<String>() && field_type.is_dropdown() => {
240 Box::new(DropdownField::<String>::new(
241 field_type.dropdown_options(),
242 field_type.dropdown_scrollable(),
243 title,
244 ))
245 }
246 _ if field_type.is_element() => Box::new(ElementField::new(field_type.element())),
247 _ => unimplemented!("Unsupported setting type: {}", field.deref().type_name()),
248 };
249
250 renderer.render(field, &options, &style, window, cx)
251 }
252
253 pub(super) fn render_item(
254 self,
255 options: &RenderOptions,
256 window: &mut Window,
257 cx: &mut App,
258 ) -> Stateful<Div> {
259 div()
260 .id(SharedString::from(format!("item-{}", options.item_ix())))
261 .w_full()
262 .child(match self {
263 SettingItem::Item {
264 title,
265 description,
266 layout,
267 disabled,
268 field,
269 ..
270 } => {
271 let layout = if options.layout().is_vertical() {
272 Axis::Vertical
273 } else {
274 layout
275 };
276
277 div()
278 .w_full()
279 .when(disabled, |this| this.opacity(0.5))
280 .map(|this| {
281 if layout.is_horizontal() {
282 this.h_flex()
283 .flex_row_dir(cx)
284 .justify_between()
285 .items_center()
286 } else {
287 this.v_flex()
288 }
289 })
290 .gap_3()
291 .child(
292 v_flex()
293 .map(|this| {
294 if layout.is_horizontal() {
295 this.flex_1().max_w_3_5()
296 } else {
297 this.w_full()
298 }
299 })
300 .child(Label::new(title.clone()).text_sm())
301 .when_some(description, |this, description| {
302 this.child(
303 div()
304 .size_full()
305 .text_sm()
306 .text_color(cx.theme().muted_foreground)
307 .child(description),
308 )
309 }),
310 )
311 .child(div().id("field").child(Self::render_field(
312 title,
313 field,
314 options.with_layout(layout).with_disabled(disabled),
315 window,
316 cx,
317 )))
318 .into_any_element()
319 }
320 SettingItem::Element {
321 disabled, render, ..
322 } => div()
323 .w_full()
324 .when(disabled, |this| this.opacity(0.5))
325 .child((render)(&options.with_disabled(disabled), window, cx))
326 .into_any_element(),
327 })
328 }
329}