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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Settings UI module.
//!
//! This module provides layout primitives for native settings screens. It does
//! not replace `Form`; instead it standardizes common desktop settings page
//! structure: pages, groups, explanatory copy, rows, controls, extra content,
//! and semantic item states.
use crate::{Text, Title};
use gpui::{
AnyElement, App, Component, Hsla, IntoElement, Pixels, RenderOnce, SharedString, Window, div,
prelude::*, px,
};
use liora_core::Config;
use liora_icons::Icon;
use liora_icons_lucide::IconName;
/// Semantic treatment for one settings row.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SettingsItemTone {
/// Neutral setting row.
#[default]
Neutral,
/// Destructive or security-sensitive setting row.
Danger,
/// Highlighted recommendation or active setting row.
Primary,
}
/// One row inside a [`SettingsGroup`].
pub struct SettingsItem {
label: SharedString,
description: Option<SharedString>,
icon: Option<IconName>,
control: Option<AnyElement>,
extra: Option<AnyElement>,
tone: SettingsItemTone,
disabled: bool,
compact: bool,
}
impl SettingsItem {
/// Creates a settings item with a primary label.
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
description: None,
icon: None,
control: None,
extra: None,
tone: SettingsItemTone::Neutral,
disabled: false,
compact: false,
}
}
/// Adds explanatory copy below the label.
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Adds a leading icon.
pub fn icon(mut self, icon: IconName) -> Self {
self.icon = Some(icon);
self
}
/// Adds the primary trailing control, for example Switch, Select, or Button.
pub fn control(mut self, control: impl IntoElement) -> Self {
self.control = Some(control.into_any_element());
self
}
/// Adds secondary content below the main row, for example validation text or inline fields.
pub fn extra(mut self, extra: impl IntoElement) -> Self {
self.extra = Some(extra.into_any_element());
self
}
/// Applies danger styling for destructive or sensitive settings.
pub fn danger(mut self) -> Self {
self.tone = SettingsItemTone::Danger;
self
}
/// Applies primary styling for recommended or active settings.
pub fn primary(mut self) -> Self {
self.tone = SettingsItemTone::Primary;
self
}
/// Toggles disabled visual state.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Uses denser vertical padding.
pub fn compact(mut self) -> Self {
self.compact = true;
self
}
fn text_color(&self, theme: &liora_theme::Theme) -> Hsla {
if self.disabled {
theme.neutral.text_disabled
} else {
match self.tone {
SettingsItemTone::Neutral => theme.neutral.text_1,
SettingsItemTone::Danger => theme.danger.base,
SettingsItemTone::Primary => theme.primary.base,
}
}
}
fn render(self, theme: &liora_theme::Theme) -> AnyElement {
let fg = self.text_color(theme);
let py = if self.compact { px(10.0) } else { px(14.0) };
div()
.flex()
.flex_col()
.gap_2()
.px_4()
.py(py)
.border_b_1()
.border_color(theme.neutral.border.opacity(0.72))
.when(self.disabled, |s| s.opacity(0.64))
.child(
div()
.flex()
.items_center()
.justify_between()
.gap_4()
.child(
div()
.flex()
.items_start()
.gap_3()
.min_w(px(0.0))
.when_some(self.icon, |s, icon| {
s.child(Icon::new(icon).size(px(16.0)).color(fg))
})
.child(
div()
.flex_1()
.min_w(px(0.0))
.flex()
.flex_col()
.gap_1()
.child(Text::new(self.label).sm().bold().text_color(fg).wrap())
.when_some(self.description, |s, description| {
s.child(
Text::new(description)
.xs()
.text_color(theme.neutral.text_3)
.wrap(),
)
}),
),
)
.when_some(self.control, |s, control| {
s.child(div().flex_none().child(control))
}),
)
.when_some(self.extra, |s, extra| {
s.child(div().pl(px(28.0)).child(extra))
})
.into_any_element()
}
}
/// A titled group of settings rows.
pub struct SettingsGroup {
title: SharedString,
description: Option<SharedString>,
items: Vec<SettingsItem>,
footer: Option<AnyElement>,
}
impl SettingsGroup {
/// Creates an empty group with a title.
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
description: None,
items: Vec::new(),
footer: None,
}
}
/// Adds group-level explanatory copy.
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Appends an item row.
pub fn item(mut self, item: SettingsItem) -> Self {
self.items.push(item);
self
}
/// Replaces all item rows.
pub fn items(mut self, items: Vec<SettingsItem>) -> Self {
self.items = items;
self
}
/// Adds footer content below rows.
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
/// Returns item count for tests and dynamic page summaries.
pub fn item_count(&self) -> usize {
self.items.len()
}
fn render(self, theme: &liora_theme::Theme) -> AnyElement {
let mut card = div()
.rounded_lg()
.border_1()
.border_color(theme.neutral.border)
.bg(theme.neutral.card)
.overflow_hidden()
.child(
div()
.p_4()
.border_b_1()
.border_color(theme.neutral.border)
.child(Title::new(self.title).h4())
.when_some(self.description, |s, description| {
s.child(
Text::new(description)
.sm()
.text_color(theme.neutral.text_3)
.wrap(),
)
}),
);
for item in self.items {
card = card.child(item.render(theme));
}
if let Some(footer) = self.footer {
card = card.child(
div()
.p_4()
.bg(theme.neutral.hover.opacity(0.5))
.child(footer),
);
}
card.into_any_element()
}
}
/// Full settings page composed from groups.
pub struct SettingsPage {
title: SharedString,
description: Option<SharedString>,
groups: Vec<SettingsGroup>,
max_width: Pixels,
}
impl SettingsPage {
/// Creates an empty settings page.
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
description: None,
groups: Vec::new(),
max_width: px(820.0),
}
}
/// Adds page-level explanatory copy.
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Appends a settings group.
pub fn group(mut self, group: SettingsGroup) -> Self {
self.groups.push(group);
self
}
/// Replaces all settings groups.
pub fn groups(mut self, groups: Vec<SettingsGroup>) -> Self {
self.groups = groups;
self
}
/// Sets maximum page width.
pub fn max_width(mut self, width: impl Into<Pixels>) -> Self {
self.max_width = width.into().max(px(360.0));
self
}
/// Returns number of groups for tests and page summaries.
pub fn group_count(&self) -> usize {
self.groups.len()
}
}
impl RenderOnce for SettingsPage {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
div().w_full().flex().justify_center().child(
div()
.w_full()
.max_w(self.max_width)
.flex()
.flex_col()
.gap_5()
.child(
div()
.flex()
.flex_col()
.gap_1()
.child(Title::new(self.title).h2())
.when_some(self.description, |s, description| {
s.child(
Text::new(description)
.text_color(theme.neutral.text_3)
.wrap(),
)
}),
)
.children(self.groups.into_iter().map(|group| group.render(&theme))),
)
}
}
impl IntoElement for SettingsPage {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn settings_page_tracks_groups_and_items() {
let group = SettingsGroup::new("Editor")
.item(SettingsItem::new("Format on save"))
.item(SettingsItem::new("Tab size"));
assert_eq!(group.item_count(), 2);
let page = SettingsPage::new("Settings").group(group);
assert_eq!(page.group_count(), 1);
}
#[test]
fn settings_item_tracks_tone_and_disabled_state() {
let item = SettingsItem::new("Delete cache")
.danger()
.disabled(true)
.compact();
assert_eq!(item.tone, SettingsItemTone::Danger);
assert!(item.disabled);
assert!(item.compact);
}
}