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
use gpui::{
App, Entity, InteractiveElement as _, IntoElement, ListAlignment, ListState,
ParentElement as _, SharedString, Styled, Window, div, list, prelude::FluentBuilder as _, px,
};
use rust_i18n::t;
use crate::{
ActiveTheme, IconName, Sizable,
button::{Button, ButtonVariants},
h_flex,
label::Label,
scroll::ScrollableElement,
setting::{RenderOptions, SettingGroup, settings::SettingsState},
v_flex,
};
/// A setting page that can contain multiple setting groups.
#[derive(Clone)]
pub struct SettingPage {
resettable: bool,
pub(super) default_open: bool,
pub(super) title: SharedString,
pub(super) description: Option<SharedString>,
pub(super) groups: Vec<SettingGroup>,
}
impl SettingPage {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
resettable: true,
default_open: false,
title: title.into(),
description: None,
groups: Vec::new(),
}
}
/// Set the title of the setting page.
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = title.into();
self
}
/// Set the description of the setting page, default is None.
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Set the default open state of the setting page, default is false.
pub fn default_open(mut self, default_open: bool) -> Self {
self.default_open = default_open;
self
}
/// Set whether the setting page is resettable, default is true.
///
/// If true and the items in this page has changed, the reset button will appear.
pub fn resettable(mut self, resettable: bool) -> Self {
self.resettable = resettable;
self
}
/// Add a setting group to the page.
pub fn group(mut self, group: SettingGroup) -> Self {
self.groups.push(group);
self
}
/// Add multiple setting groups to the page.
pub fn groups(mut self, groups: impl IntoIterator<Item = SettingGroup>) -> Self {
self.groups.extend(groups);
self
}
fn is_resettable(&self, cx: &App) -> bool {
self.resettable && self.groups.iter().any(|group| group.is_resettable(cx))
}
fn reset_all(&self, window: &mut Window, cx: &mut App) {
for group in &self.groups {
group.reset(window, cx);
}
}
pub(super) fn render(
&self,
ix: usize,
state: &Entity<SettingsState>,
options: &RenderOptions,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let search_input = state.read(cx).search_input.clone();
let query = search_input.read(cx).value();
let groups = self
.groups
.iter()
.filter(|group| group.is_match(&query))
.cloned()
.collect::<Vec<_>>();
let groups_count = groups.len();
let list_state = window
.use_keyed_state(
SharedString::from(format!("list-state:{}", ix)),
cx,
|_, _| ListState::new(groups_count, ListAlignment::Top, px(100.)),
)
.read(cx)
.clone();
if list_state.item_count() != groups_count {
list_state.reset(groups_count);
}
let deferred_scroll_group_ix = state.read(cx).deferred_scroll_group_ix;
if let Some(ix) = deferred_scroll_group_ix {
state.update(cx, |state, _| {
state.deferred_scroll_group_ix = None;
});
list_state.scroll_to_reveal_item(ix);
}
v_flex()
.id(ix)
.size_full()
.child(
v_flex()
.p_4()
.gap_3()
.border_b_1()
.border_color(cx.theme().border)
.child(h_flex().justify_between().child(self.title.clone()).when(
self.is_resettable(cx),
|this| {
this.child(
Button::new("reset")
.icon(IconName::Undo2)
.ghost()
.small()
.tooltip(t!("Settings.Reset All"))
.on_click({
let page = self.clone();
move |_, window, cx| {
page.reset_all(window, cx);
}
}),
)
},
))
.when_some(self.description.clone(), |this, description| {
this.child(
Label::new(description)
.text_sm()
.text_color(cx.theme().muted_foreground),
)
}),
)
.child(
div()
.px_4()
.relative()
.flex_1()
.w_full()
.child(
list(list_state.clone(), {
let query = query.clone();
let options = *options;
move |group_ix, window, cx| {
let group = groups[group_ix].clone();
group
.py_4()
.render(
&query,
&RenderOptions {
page_ix: ix,
group_ix,
..options
},
window,
cx,
)
.into_any_element()
}
})
.size_full(),
)
.vertical_scrollbar(&list_state),
)
}
}