windows: the app leaves a system proxy set by hand alone; a calmer tray

- The check for a system proxy a killed app left on the local proxy ran
  on every refresh while the option was off, so a proxy the user pointed
  at the listener by hand was switched off within two seconds. It now
  runs once, at the start. The registry sits behind a small trait, and
  the state machine has tests.
- The tray's tooltip and menu are only touched when what they say
  changes (a menu rebuilt while open would close).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-27 02:24:01 +03:00
1 parent adcd734479
commit c0b089dea8
2 files changed
+114 -14

No files matched your search

+10 -4
View File
@@ -17,8 +17,10 @@ struct Shell {
model: Entity<Model>,
window: Option<WindowHandle<AppView>>,
tray: Option<Tray>,
/// The icon's colour now, so it is only replaced when that changes.
/// The icon's colour and the tooltip now: the tray is only touched when they change (a
/// menu rebuilt while open would close).
colour: &'static str,
tooltip: String,
system_proxy: SystemProxy,
/// The system proxy's last failure, said once.
system_proxy_error: Option<String>,
@@ -28,7 +30,7 @@ impl Global for Shell {}
pub fn start(model: Entity<Model>, open: bool, cx: &mut App) {
cx.on_action(open_window).on_action(toggle_proxy).on_action(toggle_dpi).on_action(toggle_telegram).on_action(quit);
cx.set_global(Shell { model: model.clone(), window: None, tray: None, colour: "", system_proxy: SystemProxy::default(), system_proxy_error: None });
cx.set_global(Shell { model: model.clone(), window: None, tray: None, colour: "", tooltip: String::new(), system_proxy: SystemProxy::default(), system_proxy_error: None });
let mut builder = Tray::builder().tooltip("Proxy Suite").on_activate(OpenWindow).menu(menu);
if let Some(icon) = icon("#6b7280", cx) {
@@ -123,8 +125,12 @@ fn refresh(cx: &mut App) {
} else {
"Proxy Suite — the service is not running".into()
};
let _ = tray.set_tooltip(Some(tip), cx);
let _ = tray.refresh_menu(cx);
// The tooltip says what the menu's checks do: the same news.
if tip != cx.global::<Shell>().tooltip {
cx.global_mut::<Shell>().tooltip = tip.clone();
let _ = tray.set_tooltip(Some(tip), cx);
let _ = tray.refresh_menu(cx);
}
}
fn menu(cx: &mut App) -> Vec<MenuItem> {
+104 -10
View File
@@ -11,12 +11,39 @@ struct Settings {
bypass: Option<String>,
}
#[derive(Default)]
/// Where the settings live: the registry, or a test's stand-in.
trait Store {
fn read(&mut self) -> Result<Settings, String>;
fn write(&mut self, s: &Settings) -> Result<(), String>;
}
struct Registry;
impl Store for Registry {
fn read(&mut self) -> Result<Settings, String> {
platform::read()
}
fn write(&mut self, s: &Settings) -> Result<(), String> {
platform::write(s)
}
}
pub struct SystemProxy {
store: Box<dyn Store>,
/// The user's own settings, from before the app changed them.
saved: Option<Settings>,
/// What the app set, while it is set.
applied: Option<Settings>,
/// Whether a setting left behind by a killed app has been looked for: once, at the start,
/// so a proxy the user sets by hand later is theirs to keep.
looked: bool,
}
impl Default for SystemProxy {
fn default() -> Self {
Self { store: Box::new(Registry), saved: None, applied: None, looked: false }
}
}
impl SystemProxy {
@@ -24,26 +51,29 @@ impl SystemProxy {
/// when not. A setting the app left behind (it was killed) is cleared too.
pub fn update(&mut self, want: bool, listener: &str, bypass: &[String]) -> Result<(), String> {
let ours = Settings { enable: true, server: Some(listener.to_owned()), bypass: Some(bypass_list(bypass)) };
let first = !self.looked;
self.looked = true;
if want {
if self.applied.as_ref() == Some(&ours) {
return Ok(());
}
if self.saved.is_none() {
// One the app left set (it was killed) is not the user's: theirs was off.
let now = platform::read()?;
let left = now.enable && now.server == ours.server;
// At the start, one the app left set (it was killed) is not the user's: theirs
// was off.
let now = self.store.read()?;
let left = first && now.enable && now.server == ours.server;
self.saved = Some(if left { Settings { enable: false, ..now } } else { now });
}
platform::write(&ours)?;
self.store.write(&ours)?;
self.applied = Some(ours);
} else if self.applied.take().is_some() {
if let Some(saved) = self.saved.take() {
platform::write(&saved)?;
self.store.write(&saved)?;
}
} else if self.saved.is_none() {
let now = platform::read()?;
} else if first {
let now = self.store.read()?;
if now.enable && now.server.as_deref() == Some(listener) {
platform::write(&Settings { enable: false, ..now })?;
self.store.write(&Settings { enable: false, ..now })?;
}
}
Ok(())
@@ -53,7 +83,7 @@ impl SystemProxy {
if self.applied.take().is_some()
&& let Some(saved) = self.saved.take()
{
let _ = platform::write(&saved);
let _ = self.store.write(&saved);
}
}
}
@@ -174,8 +204,72 @@ mod platform {
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use super::*;
/// The registry, in memory, with a count of reads.
#[derive(Clone)]
struct Fake(Rc<RefCell<(Settings, usize)>>);
impl Store for Fake {
fn read(&mut self) -> Result<Settings, String> {
let mut s = self.0.borrow_mut();
s.1 += 1;
Ok(s.0.clone())
}
fn write(&mut self, s: &Settings) -> Result<(), String> {
self.0.borrow_mut().0 = s.clone();
Ok(())
}
}
fn with(settings: Settings) -> (SystemProxy, Fake) {
let fake = Fake(Rc::new(RefCell::new((settings, 0))));
(SystemProxy { store: Box::new(fake.clone()), saved: None, applied: None, looked: false }, fake)
}
fn set(server: &str, enable: bool) -> Settings {
Settings { enable, server: Some(server.into()), bypass: None }
}
#[test]
fn a_proxy_the_user_sets_by_hand_is_left_alone() {
let (mut sp, fake) = with(set("10.0.0.1:3128", true));
sp.update(false, "127.0.0.1:1080", &[]).unwrap();
// Later the user points it at the local proxy themselves.
fake.0.borrow_mut().0 = set("127.0.0.1:1080", true);
for _ in 0..3 {
sp.update(false, "127.0.0.1:1080", &[]).unwrap();
}
assert_eq!(fake.0.borrow().0, set("127.0.0.1:1080", true));
assert_eq!(fake.0.borrow().1, 1, "looked once, at the start");
}
#[test]
fn one_left_behind_is_cleared_at_the_start() {
let (mut sp, fake) = with(set("127.0.0.1:1080", true));
sp.update(false, "127.0.0.1:1080", &[]).unwrap();
assert!(!fake.0.borrow().0.enable);
}
#[test]
fn the_users_own_comes_back() {
let (mut sp, fake) = with(set("10.0.0.1:3128", true));
sp.update(true, "127.0.0.1:1080", &[]).unwrap();
assert_eq!(fake.0.borrow().0.server.as_deref(), Some("127.0.0.1:1080"));
sp.update(true, "127.0.0.1:1080", &[]).unwrap();
sp.update(false, "127.0.0.1:1080", &[]).unwrap();
assert_eq!(fake.0.borrow().0, set("10.0.0.1:3128", true));
// Left behind at the start (killed while on): the user's was off.
let (mut sp, fake) = with(Settings { bypass: Some("<local>".into()), ..set("127.0.0.1:1080", true) });
sp.update(true, "127.0.0.1:1080", &[]).unwrap();
sp.restore();
assert!(!fake.0.borrow().0.enable);
}
#[test]
fn bypass_always_has_local_names() {
assert_eq!(bypass_list(&[]), "<local>");