Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
settings ui: Add text field support to ui layer (#37868)
Anthony Eid707d0e6ebd75
diff --git a/Cargo.lock b/Cargo.lockindex cfb2ba816a4..6dd0ba84514 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -14871,6 +14871,7 @@ dependencies = ["editor","feature_flags","gpui",+ "menu","serde","serde_json","settings",diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.jsonindex 07539e9cbff..7c7692ac46f 100644--- a/assets/keymaps/default-linux.json+++ b/assets/keymaps/default-linux.json@@ -341,7 +341,7 @@"enter": "agent::Chat","shift-ctrl-r": "agent::OpenAgentDiff","ctrl-shift-y": "agent::KeepAll",- "ctrl-shift-n": "agent::RejectAll",+ "ctrl-shift-n": "agent::RejectAll"}},{diff --git a/crates/dap/src/debugger_settings.rs b/crates/dap/src/debugger_settings.rsindex 8d53fdea864..4b841450462 100644--- a/crates/dap/src/debugger_settings.rs+++ b/crates/dap/src/debugger_settings.rs@@ -12,7 +12,7 @@ pub enum DebugPanelDockPosition {Right,}-#[derive(Serialize, Deserialize, JsonSchema, Clone, Copy, SettingsUi, SettingsKey)]+#[derive(Serialize, Deserialize, JsonSchema, Clone, SettingsUi, SettingsKey)]#[serde(default)]// todo(settings_ui) @ben: I'm pretty sure not having the fields be optional here is a bug,// it means the defaults will override previously set values if a single key is missingdiff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 61d15cb3ed4..c230a4c7aa5 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -2578,7 +2578,7 @@ impl Window {&mut self,key: impl Into<ElementId>,cx: &mut App,- init: impl FnOnce(&mut Self, &mut App) -> S,+ init: impl FnOnce(&mut Self, &mut Context<S>) -> S,) -> Entity<S> {let current_view = self.current_view();self.with_global_id(key.into(), |global_id, window| {@@ -2611,7 +2611,7 @@ impl Window {pub fn use_state<S: 'static>(&mut self,cx: &mut App,- init: impl FnOnce(&mut Self, &mut App) -> S,+ init: impl FnOnce(&mut Self, &mut Context<S>) -> S,) -> Entity<S> {self.use_keyed_state(ElementId::CodeLocation(*core::panic::Location::caller()),diff --git a/crates/settings/src/settings_ui_core.rs b/crates/settings/src/settings_ui_core.rsindex 09505e63a3e..c3366641a89 100644--- a/crates/settings/src/settings_ui_core.rs+++ b/crates/settings/src/settings_ui_core.rs@@ -43,6 +43,7 @@ pub struct SettingsUiEntry {#[derive(Clone)]pub enum SettingsUiItemSingle {SwitchField,+ TextField,/// A numeric stepper for a specific type of numberNumericStepper(NumType),ToggleGroup {@@ -150,6 +151,18 @@ impl SettingsUi for Option<bool> {}}+impl SettingsUi for String {+ fn settings_ui_item() -> SettingsUiItem {+ SettingsUiItem::Single(SettingsUiItemSingle::TextField)+ }+}++impl SettingsUi for SettingsUiItem {+ fn settings_ui_item() -> SettingsUiItem {+ SettingsUiItem::Single(SettingsUiItemSingle::TextField)+ }+}+#[repr(u8)]#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub enum NumType {diff --git a/crates/settings_ui/Cargo.toml b/crates/settings_ui/Cargo.tomlindex 3ecef880d2b..1d8e5e11226 100644--- a/crates/settings_ui/Cargo.toml+++ b/crates/settings_ui/Cargo.toml@@ -21,8 +21,9 @@ command_palette_hooks.workspace = trueeditor.workspace = truefeature_flags.workspace = truegpui.workspace = true-serde_json.workspace = true+menu.workspace = trueserde.workspace = true+serde_json.workspace = truesettings.workspace = truesmallvec.workspace = truetheme.workspace = truediff --git a/crates/settings_ui/src/settings_ui.rs b/crates/settings_ui/src/settings_ui.rsindex 9866dcdb3eb..4c9419491c3 100644--- a/crates/settings_ui/src/settings_ui.rs+++ b/crates/settings_ui/src/settings_ui.rs@@ -6,7 +6,7 @@ use std::ops::{Not, Range};use anyhow::Context as _;use command_palette_hooks::CommandPaletteFilter;-use editor::EditorSettingsControls;+use editor::{Editor, EditorSettingsControls};use feature_flags::{FeatureFlag, FeatureFlagViewExt};use gpui::{App, Entity, EventEmitter, FocusHandle, Focusable, ReadGlobal, ScrollHandle, actions};use settings::{@@ -612,6 +612,7 @@ fn render_item_single(SettingsUiItemSingle::DropDown { .. } => {unimplemented!("This")}+ SettingsUiItemSingle::TextField => render_text_field(settings_value, window, cx),}}@@ -798,6 +799,54 @@ fn render_switch_field(.into_any_element()}+fn render_text_field(+ value: SettingsValue<serde_json::Value>,+ window: &mut Window,+ cx: &mut App,+) -> AnyElement {+ let value = downcast_any_item::<String>(value);+ let path = value.path.clone();+ let editor = window.use_state(cx, {+ let path = path.clone();+ move |window, cx| {+ let mut editor = Editor::single_line(window, cx);++ cx.observe_global_in::<SettingsStore>(window, move |editor, window, cx| {+ let user_settings = SettingsStore::global(cx).raw_user_settings();+ if let Some(value) = read_settings_value_from_path(&user_settings, &path).cloned()+ && let Some(value) = value.as_str()+ {+ editor.set_text(value, window, cx);+ }+ })+ .detach();++ editor.set_text(value.read().clone(), window, cx);+ editor+ }+ });++ let weak_editor = editor.downgrade();+ let theme_colors = cx.theme().colors();++ div()+ .child(editor)+ .bg(theme_colors.editor_background)+ .border_1()+ .rounded_lg()+ .border_color(theme_colors.border)+ .on_action::<menu::Confirm>({+ move |_, _, cx| {+ let new_value = weak_editor.read_with(cx, |editor, cx| editor.text(cx)).ok();++ if let Some(new_value) = new_value {+ SettingsValue::write_value(&path, serde_json::Value::String(new_value), cx);+ }+ }+ })+ .into_any_element()+}+fn render_toggle_button_group(value: SettingsValue<serde_json::Value>,variants: &'static [&'static str],