From 69759cfbceaad0717ff25554639ce97de43f1dbc Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 01:44:55 +0300 Subject: [PATCH] windows: the app's Settings page: the whole configuration Upstream's option tree as JSON in a code editor (highlighted, folding), loaded with its revision; Check asks the service to validate without taking it; Save sends it against the revision loaded, so a change made elsewhere meanwhile is refused, not overwritten, and the page says so. The validator's notes and an apply failure are shown under the editor; redacted secrets are explained (left as they are, they keep their values). Every option the other pages do not show is editable here. Co-Authored-By: Claude Opus 5.5 --- windows/crates/ps-gui/src/ui/mod.rs | 9 +- windows/crates/ps-gui/src/ui/settings.rs | 190 +++++++++++++++++++++++ 2 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 windows/crates/ps-gui/src/ui/settings.rs diff --git a/windows/crates/ps-gui/src/ui/mod.rs b/windows/crates/ps-gui/src/ui/mod.rs index d599acf..55a2cda 100644 --- a/windows/crates/ps-gui/src/ui/mod.rs +++ b/windows/crates/ps-gui/src/ui/mod.rs @@ -5,6 +5,7 @@ mod dpi; mod logs; mod outbounds; mod overview; +mod settings; mod telegram; mod widgets; @@ -63,6 +64,8 @@ pub struct AppView { outbounds: Entity, /// Made when first shown: following the logs starts then. logs: Option>, + /// Made when first shown, as it loads the configuration into its editor then. + settings: Option>, _subscriptions: Vec, } @@ -81,7 +84,7 @@ impl AppView { let dpi = cx.new(|cx| dpi::DpiPage::new(model.clone(), window, cx)); let telegram = cx.new(|cx| telegram::TelegramPage::new(model.clone(), window, cx)); let outbounds = cx.new(|cx| outbounds::OutboundsPage::new(model.clone(), window, cx)); - Self { model, page: Page::Overview, dpi, telegram, outbounds, logs: None, _subscriptions: subscriptions } + Self { model, page: Page::Overview, dpi, telegram, outbounds, logs: None, settings: None, _subscriptions: subscriptions } } fn sidebar(&self, cx: &mut Context) -> impl IntoElement { @@ -106,6 +109,10 @@ impl Render for AppView { let model = self.model.clone(); self.logs.get_or_insert_with(|| cx.new(|cx| logs::LogsPage::new(model, cx))).clone().into_any_element() } + Page::Settings => { + let model = self.model.clone(); + self.settings.get_or_insert_with(|| cx.new(|cx| settings::SettingsPage::new(model, window, cx))).clone().into_any_element() + } other => widgets::page(other.title(), cx).child(div().text_color(cx.theme().muted_foreground).child("Not here yet.")).into_any_element(), }; div() diff --git a/windows/crates/ps-gui/src/ui/settings.rs b/windows/crates/ps-gui/src/ui/settings.rs new file mode 100644 index 0000000..91ee83e --- /dev/null +++ b/windows/crates/ps-gui/src/ui/settings.rs @@ -0,0 +1,190 @@ +//! Settings: the whole configuration, upstream's option tree as JSON, edited here and checked +//! by the service before it takes it. Every option the other pages show is here too, and the +//! ones they do not. + +use gpui_kit::assets::IconName; +use gpui_kit::component::button::{Button, ButtonVariants as _}; +use gpui_kit::component::input::{Editor, EditorState, InputEvent}; +use gpui_kit::component::*; +use gpui_kit::prelude::FluentBuilder as _; +use gpui_kit::*; +use ps_client::Issue; +use ps_ipc::Request; +use serde_json::Value; + +use super::widgets::{card, hint, page}; +use crate::model::Model; + +/// What the service shows in place of a secret the caller may not read; saved back as it is, +/// the secret keeps its value. +const REDACTED: &str = "\u{2022}\u{2022}\u{2022}redacted\u{2022}\u{2022}\u{2022}"; + +enum Verdict { + None, + Working(&'static str), + Issues(Vec), + Failed(String), + Saved { revision: u64, issues: Vec, apply_error: Option }, +} + +pub struct SettingsPage { + model: Entity, + editor: Entity, + /// The text and revision last loaded: what is saved against, and what "changed" means. + loaded: Option<(String, u64)>, + verdict: Verdict, + _subscriptions: Vec, +} + +impl SettingsPage { + pub fn new(model: Entity, window: &mut Window, cx: &mut Context) -> Self { + let editor = cx.new(|cx| EditorState::new(window, cx).language("json").line_number(true).folding(true)); + // Save is offered once the text differs from what was loaded. + let subscriptions = vec![cx.subscribe(&editor, |_, _, event: &InputEvent, cx| { + if matches!(event, InputEvent::Change) { + cx.notify(); + } + })]; + let mut this = Self { model, editor, loaded: None, verdict: Verdict::None, _subscriptions: subscriptions }; + this.reload(window, cx); + this + } + + fn reload(&mut self, window: &mut Window, cx: &mut Context) { + self.verdict = Verdict::Working("Loading…"); + cx.notify(); + let job = self.model.read(cx).bridge().call(Request::ConfigGet); + cx.spawn_in(window, async move |this, cx| { + let got = job.await; + this.update_in(cx, |this, window, cx| { + match got { + Ok(r) => { + let text = serde_json::to_string_pretty(&r["config"]).unwrap_or_default(); + this.loaded = Some((text.clone(), r["revision"].as_u64().unwrap_or(0))); + this.verdict = Verdict::None; + this.editor.update(cx, |s, cx| s.set_value(text, window, cx)); + } + Err(e) => this.verdict = Verdict::Failed(format!("{e:#}")), + } + cx.notify(); + }) + .ok(); + }) + .detach(); + } + + /// The editor's text as JSON, or where it is not. + fn parsed(&self, cx: &App) -> Result { + serde_json::from_str(&self.editor.read(cx).value()).map_err(|e| format!("Not valid JSON: {e}")) + } + + fn validate(&mut self, cx: &mut Context) { + let config = match self.parsed(cx) { + Ok(c) => c, + Err(e) => { + self.verdict = Verdict::Failed(e); + return cx.notify(); + } + }; + self.verdict = Verdict::Working("Checking…"); + cx.notify(); + let job = self.model.read(cx).bridge().call(Request::ConfigValidate { config }); + cx.spawn(async move |this, cx| { + let r = job.await; + this.update(cx, |this, cx| { + this.verdict = match r { + Ok(r) => Verdict::Issues(ps_client::issues(&r)), + Err(e) => Verdict::Failed(format!("{e:#}")), + }; + cx.notify(); + }) + .ok(); + }) + .detach(); + } + + fn save(&mut self, cx: &mut Context) { + let config = match self.parsed(cx) { + Ok(c) => c, + Err(e) => { + self.verdict = Verdict::Failed(e); + return cx.notify(); + } + }; + let base = self.loaded.as_ref().map(|(_, r)| *r); + self.verdict = Verdict::Working("Saving…"); + cx.notify(); + let job = self.model.read(cx).bridge().call(Request::ConfigSet { config, base }); + let model = self.model.clone(); + cx.spawn(async move |this, cx| { + let r = job.await; + this.update(cx, |this, cx| { + this.verdict = match r { + Ok(r) => { + let revision = r["revision"].as_u64().unwrap_or(0); + let text = this.editor.read(cx).value().to_string(); + this.loaded = Some((text, revision)); + Verdict::Saved { revision, issues: ps_client::issues(&r), apply_error: r["error"].as_str().map(str::to_owned) } + } + Err(e) if e.to_string().contains("changed since revision") => { + Verdict::Failed("The configuration was changed elsewhere since it was loaded. Reload to see it (your edits here are then lost; copy them first).".into()) + } + Err(e) => Verdict::Failed(format!("{e:#}")), + }; + cx.notify(); + }) + .ok(); + model.update(cx, |m, cx| { + m.refresh(cx); + m.load_config(cx); + }); + }) + .detach(); + } +} + +fn issue_list(issues: &[Issue], cx: &App) -> Div { + let mut list = div().v_flex().gap_1(); + for i in issues { + let colour = if i.severity == "error" { cx.theme().danger } else { cx.theme().warning }; + list = list.child(div().text_sm().h_flex().gap_2().child(div().text_color(colour).child(i.severity.clone())).child(div().font_family("monospace").child(i.path.clone())).child(div().flex_1().min_w_0().child(i.message.clone()))); + } + list +} + +impl Render for SettingsPage { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let text = self.editor.read(cx).value(); + let changed = self.loaded.as_ref().is_some_and(|(t, _)| t.as_str() != text.as_ref()); + let redacted = text.contains(REDACTED); + + let actions = div() + .h_flex() + .gap_2() + .child(Button::new("cfg-reload").small().ghost().icon(IconName::RefreshCw).label("Reload").on_click(cx.listener(|this, _, window, cx| this.reload(window, cx)))) + .child(Button::new("cfg-validate").small().label("Check").on_click(cx.listener(|this, _, _, cx| this.validate(cx)))) + .child(Button::new("cfg-save").small().primary().icon(IconName::Save).label("Save").disabled(!changed).on_click(cx.listener(|this, _, _, cx| this.save(cx)))); + + let verdict = match &self.verdict { + Verdict::None => div(), + Verdict::Working(what) => hint(*what, cx), + Verdict::Failed(e) => div().text_sm().text_color(cx.theme().danger).child(e.clone()), + Verdict::Issues(issues) if issues.is_empty() => div().text_sm().text_color(cx.theme().success).child("It is valid."), + Verdict::Issues(issues) => issue_list(issues, cx), + Verdict::Saved { revision, issues, apply_error } => div() + .v_flex() + .gap_1() + .child(div().text_sm().text_color(cx.theme().success).child(format!("Saved as revision {revision}."))) + .when_some(apply_error.clone(), |d, e| d.child(div().text_sm().text_color(cx.theme().danger).child(format!("Saved, but not applied: {e}")))) + .child(issue_list(issues, cx)), + }; + + page("Settings", cx).child( + card("Configuration", Some(actions.into_any_element()), cx) + .child(hint("Upstream's options (services.proxy-suite…) as JSON, as proxy-ctl config get prints them. The service checks them before taking them; options that run programs or read files need an administrator.", cx)) + .when(redacted, |d| d.child(hint("Values shown as •••redacted••• are secrets you may not read here; left as they are, they keep their values.", cx))) + .child(div().h(px(480.)).border_1().border_color(cx.theme().border).rounded_md().child(Editor::new(&self.editor).h_full())) + .child(verdict), + ) + } +}