Skip to content

Commit 0e0f48d

Browse files
mikayla-makinathansoboAnthony-Eidprobably-neb
authored
Introduce SettingsField type to the settings UI (#38921)
Release Notes: - N/A --------- Co-authored-by: Nathan Sobo <[email protected]> Co-authored-by: Anthony Eid <[email protected]> Co-authored-by: Ben Kunkle <[email protected]>
1 parent 7980dbd commit 0e0f48d

File tree

3 files changed

+323
-143
lines changed

3 files changed

+323
-143
lines changed

crates/settings_ui/examples/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fn main() {
4949
app.run(move |cx| {
5050
<dyn fs::Fs>::set_global(fs.clone(), cx);
5151
settings::init(cx);
52+
settings_ui::init(cx);
5253
theme::init(theme::LoadThemes::JustBase, cx);
53-
5454
client::init_settings(cx);
5555
workspace::init_settings(cx);
5656
// production client because fake client requires gpui/test-support
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use editor::Editor;
2+
use gpui::div;
3+
use ui::{
4+
ActiveTheme as _, App, FluentBuilder as _, InteractiveElement as _, IntoElement,
5+
ParentElement as _, RenderOnce, Styled as _, Window,
6+
};
7+
8+
#[derive(IntoElement)]
9+
pub struct SettingsEditor {
10+
initial_text: Option<String>,
11+
placeholder: Option<&'static str>,
12+
confirm: Option<Box<dyn Fn(Option<String>, &mut App)>>,
13+
}
14+
15+
impl SettingsEditor {
16+
pub fn new() -> Self {
17+
Self {
18+
initial_text: None,
19+
placeholder: None,
20+
confirm: None,
21+
}
22+
}
23+
24+
pub fn with_initial_text(mut self, initial_text: String) -> Self {
25+
self.initial_text = Some(initial_text);
26+
self
27+
}
28+
29+
pub fn with_placeholder(mut self, placeholder: &'static str) -> Self {
30+
self.placeholder = Some(placeholder);
31+
self
32+
}
33+
34+
pub fn on_confirm(mut self, confirm: impl Fn(Option<String>, &mut App) + 'static) -> Self {
35+
self.confirm = Some(Box::new(confirm));
36+
self
37+
}
38+
}
39+
40+
impl RenderOnce for SettingsEditor {
41+
fn render(self, window: &mut Window, cx: &mut App) -> impl ui::IntoElement {
42+
let editor = window.use_state(cx, {
43+
move |window, cx| {
44+
let mut editor = Editor::single_line(window, cx);
45+
if let Some(text) = self.initial_text {
46+
editor.set_text(text, window, cx);
47+
}
48+
49+
if let Some(placeholder) = self.placeholder {
50+
editor.set_placeholder_text(placeholder, window, cx);
51+
}
52+
editor
53+
}
54+
});
55+
56+
let weak_editor = editor.downgrade();
57+
let theme_colors = cx.theme().colors();
58+
59+
div()
60+
.child(editor)
61+
.bg(theme_colors.editor_background)
62+
.border_1()
63+
.rounded_lg()
64+
.border_color(theme_colors.border)
65+
.when_some(self.confirm, |this, confirm| {
66+
this.on_action::<menu::Confirm>({
67+
move |_, _, cx| {
68+
let Some(editor) = weak_editor.upgrade() else {
69+
return;
70+
};
71+
let new_value = editor.read_with(cx, |editor, cx| editor.text(cx));
72+
let new_value = (!new_value.is_empty()).then_some(new_value);
73+
confirm(new_value, cx);
74+
}
75+
})
76+
})
77+
}
78+
}

0 commit comments

Comments
 (0)