1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use gpui::{
AnyElement, App, Axis, Div, InteractiveElement as _, IntoElement, ParentElement, SharedString,
Stateful, Styled, Window, div, prelude::FluentBuilder as _,
};
use std::{any::TypeId, ops::Deref, rc::Rc};
use crate::{
ActiveTheme as _, AxisExt, StyledExt as _,
label::Label,
setting::{
AnySettingField, ElementField, RenderOptions,
fields::{
BoolField, DropdownField, NumberField, ResetHandler, SettingFieldRender, StringField,
},
},
text::Text,
v_flex,
};
/// Setting item.
#[derive(Clone)]
pub enum SettingItem {
/// A normal setting item with a title, description, and field.
Item {
title: SharedString,
description: Option<Text>,
keywords: Vec<SharedString>,
layout: Axis,
disabled: bool,
field: Rc<dyn AnySettingField>,
},
/// A full custom element to render.
Element {
disabled: bool,
keywords: Vec<SharedString>,
/// Optional custom reset behavior. The first closure reports whether
/// the item is "dirty" (controls reset button visibility), the second
/// performs the reset.
reset_handler: Option<ResetHandler>,
render: Rc<dyn Fn(&RenderOptions, &mut Window, &mut App) -> AnyElement + 'static>,
},
}
impl SettingItem {
/// Create a new setting item.
pub fn new<F>(title: impl Into<SharedString>, field: F) -> Self
where
F: AnySettingField + 'static,
{
SettingItem::Item {
title: title.into(),
description: None,
layout: Axis::Horizontal,
disabled: false,
keywords: Vec::new(),
field: Rc::new(field),
}
}
/// Create a new custom element setting item with a render closure.
pub fn render<R, E>(render: R) -> Self
where
E: IntoElement,
R: Fn(&RenderOptions, &mut Window, &mut App) -> E + 'static,
{
SettingItem::Element {
disabled: false,
keywords: Vec::new(),
reset_handler: None,
render: Rc::new(move |options, window, cx| {
render(options, window, cx).into_any_element()
}),
}
}
/// Provide custom reset behavior for a custom element item.
///
/// Only applies to [`SettingItem::Element`] (created via
/// [`SettingItem::render`]). When set, the page-level reset button will
/// appear while `is_dirty` returns true, and clicking it invokes `reset`.
///
/// - `is_dirty` reports whether the item differs from its default state.
/// - `reset` performs the reset.
pub fn on_reset<D, R>(mut self, is_dirty: D, reset: R) -> Self
where
D: Fn(&App) -> bool + 'static,
R: Fn(&mut Window, &mut App) + 'static,
{
match &mut self {
SettingItem::Element { reset_handler, .. } => {
*reset_handler = Some((Rc::new(is_dirty), Rc::new(reset)));
}
// `on_reset` is meaningless for a value-bearing item: use the
// field's own `default_value` / `SettingField::on_reset` instead.
SettingItem::Item { .. } => {
debug_assert!(
false,
"SettingItem::on_reset only applies to SettingItem::Element; \
use SettingField::default_value or SettingField::on_reset for a normal item"
);
}
}
self
}
/// Set additional keywords used only for search matching (not rendered).
///
/// For example, an item titled "Enable Two-factor auth" can be made
/// searchable via "MFA". This is also useful for custom elements that
/// have no title/description but should still show up in search results.
pub fn keywords<I, S>(mut self, keywords: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<SharedString>,
{
let keywords: Vec<SharedString> = keywords.into_iter().map(Into::into).collect();
match &mut self {
SettingItem::Item { keywords: k, .. } => *k = keywords,
SettingItem::Element { keywords: k, .. } => *k = keywords,
}
self
}
/// Set whether the setting item is disabled, default is false.
///
/// A disabled item is rendered with reduced opacity. For
/// [`SettingItem::Item`] the underlying field is also rendered in a
/// non-interactive state. For [`SettingItem::Element`] the `disabled` flag
/// is forwarded via [`RenderOptions::disabled`] so the custom renderer can
/// disable its interactive controls.
pub fn disabled(mut self, disabled: bool) -> Self {
match &mut self {
SettingItem::Item { disabled: d, .. } => *d = disabled,
SettingItem::Element { disabled: d, .. } => *d = disabled,
}
self
}
/// Set the description of the setting item.
///
/// Only applies to [`SettingItem::Item`].
pub fn description(mut self, description: impl Into<Text>) -> Self {
match &mut self {
SettingItem::Item { description: d, .. } => {
*d = Some(description.into());
}
SettingItem::Element { .. } => {}
}
self
}
/// Set the layout of the setting item.
///
/// Only applies to [`SettingItem::Item`].
pub fn layout(mut self, layout: Axis) -> Self {
match &mut self {
SettingItem::Item { layout: l, .. } => {
*l = layout;
}
SettingItem::Element { .. } => {}
}
self
}
pub(crate) fn is_match(&self, query: &str, cx: &App) -> bool {
match self {
SettingItem::Item {
title,
description,
keywords,
..
} => {
let q = &query.to_lowercase();
title.to_lowercase().contains(q)
|| description
.as_ref()
.map_or(false, |d| d.get_text(cx).to_lowercase().contains(q))
|| keywords.iter().any(|s| s.to_lowercase().contains(q))
}
// We need to show all custom elements when not searching.
SettingItem::Element { keywords, .. } => {
let q = &query.to_lowercase();
query.is_empty() || keywords.iter().any(|s| s.to_lowercase().contains(q))
}
}
}
pub(crate) fn is_resettable(&self, cx: &App) -> bool {
match self {
SettingItem::Item { field, .. } => field.is_resettable(cx),
SettingItem::Element { reset_handler, .. } => reset_handler
.as_ref()
.is_some_and(|(is_dirty, _)| is_dirty(cx)),
}
}
pub(crate) fn reset(&self, window: &mut Window, cx: &mut App) {
match self {
SettingItem::Item { field, .. } => field.reset(window, cx),
SettingItem::Element { reset_handler, .. } => {
if let Some((_, reset)) = reset_handler.as_ref() {
reset(window, cx);
}
}
}
}
fn render_field(
field: Rc<dyn AnySettingField>,
options: RenderOptions,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let field_type = field.field_type();
let style = field.style().clone();
let type_id = field.deref().type_id();
let renderer: Box<dyn SettingFieldRender> = match type_id {
t if t == std::any::TypeId::of::<bool>() => {
Box::new(BoolField::new(field_type.is_switch()))
}
t if t == TypeId::of::<f64>() && field_type.is_number_input() => {
Box::new(NumberField::new(field_type.number_input_options()))
}
t if t == TypeId::of::<SharedString>() && field_type.is_input() => {
Box::new(StringField::<SharedString>::new())
}
t if t == TypeId::of::<String>() && field_type.is_input() => {
Box::new(StringField::<String>::new())
}
t if t == TypeId::of::<SharedString>() && field_type.is_dropdown() => {
Box::new(DropdownField::<SharedString>::new(
field_type.dropdown_options(),
field_type.dropdown_scrollable(),
))
}
t if t == TypeId::of::<String>() && field_type.is_dropdown() => {
Box::new(DropdownField::<String>::new(
field_type.dropdown_options(),
field_type.dropdown_scrollable(),
))
}
_ if field_type.is_element() => Box::new(ElementField::new(field_type.element())),
_ => unimplemented!("Unsupported setting type: {}", field.deref().type_name()),
};
renderer.render(field, &options, &style, window, cx)
}
pub(super) fn render_item(
self,
options: &RenderOptions,
window: &mut Window,
cx: &mut App,
) -> Stateful<Div> {
div()
.id(SharedString::from(format!("item-{}", options.item_ix())))
.w_full()
.child(match self {
SettingItem::Item {
title,
description,
layout,
disabled,
field,
..
} => {
let layout = if options.layout().is_vertical() {
Axis::Vertical
} else {
layout
};
div()
.w_full()
.when(disabled, |this| this.opacity(0.5))
.map(|this| {
if layout.is_horizontal() {
this.h_flex().justify_between().items_center()
} else {
this.v_flex()
}
})
.gap_3()
.child(
v_flex()
.map(|this| {
if layout.is_horizontal() {
this.flex_1().max_w_3_5()
} else {
this.w_full()
}
})
.child(Label::new(title).text_sm())
.when_some(description, |this, description| {
this.child(
div()
.size_full()
.text_sm()
.text_color(cx.theme().muted_foreground)
.child(description),
)
}),
)
.child(div().id("field").child(Self::render_field(
field,
options.with_layout(layout).with_disabled(disabled),
window,
cx,
)))
.into_any_element()
}
SettingItem::Element {
disabled, render, ..
} => div()
.w_full()
.when(disabled, |this| this.opacity(0.5))
.child((render)(&options.with_disabled(disabled), window, cx))
.into_any_element(),
})
}
}