1use std::path::Path;
51
52pub const BACKGROUND_KEY: &str = "app:runinbackground";
54pub const TRAY_KEY: &str = "app:showtray";
55
56const ENV_BACKGROUND: &str = "AGENTMUX_BACKGROUND_SERVICE";
57const ENV_TRAY: &str = "AGENTMUX_TRAY";
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub struct Settings {
62 pub run_in_background: bool,
63 pub show_tray: bool,
64}
65
66impl Default for Settings {
67 fn default() -> Self {
68 Settings {
69 run_in_background: false,
70 show_tray: true,
71 }
72 }
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub struct Resolved {
78 pub background: bool,
79 pub tray: bool,
80}
81
82pub fn resolve(env_background: bool, env_tray: bool, background_flag: bool, settings: Settings) -> Resolved {
84 let background = env_background || background_flag || settings.run_in_background;
85 let tray = background || env_tray || settings.show_tray;
86 Resolved { background, tray }
87}
88
89pub fn read_settings(settings_path: &Path) -> Settings {
92 let mut s = Settings::default();
93 let Ok(text) = std::fs::read_to_string(settings_path) else {
94 return s;
95 };
96 let Some(json) = agentmux_common::settings_jsonc::parse_jsonc(&text) else {
97 return s;
98 };
99 if let Some(v) = json.get(BACKGROUND_KEY).and_then(|v| v.as_bool()) {
100 s.run_in_background = v;
101 }
102 if let Some(v) = json.get(TRAY_KEY).and_then(|v| v.as_bool()) {
103 s.show_tray = v;
104 }
105 s
106}
107
108pub fn apply(args: &[String]) -> Resolved {
114 let env_background = std::env::var_os(ENV_BACKGROUND).is_some();
115 let env_tray = std::env::var_os(ENV_TRAY).is_some();
116 let flag = crate::autostart::background_requested(args);
117 let settings = std::panic::catch_unwind(|| {
118 crate::data_dir::settings_file()
119 .map(|f| read_settings(&f))
120 .unwrap_or_default()
121 })
122 .unwrap_or_default();
123
124 let r = resolve(env_background, env_tray, flag, settings);
125 if r.background {
126 std::env::set_var(ENV_BACKGROUND, "1");
127 }
128 if r.tray {
129 std::env::set_var(ENV_TRAY, "1");
130 }
131 r
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137 use std::path::PathBuf;
138
139 fn write_tmp(tag: &str, content: &str) -> PathBuf {
140 let p = std::env::temp_dir().join(format!(
141 "agentmux-bgcfg-{tag}-{}.json",
142 std::process::id()
143 ));
144 std::fs::write(&p, content).unwrap();
145 p
146 }
147
148 fn read(tag: &str, content: &str) -> Settings {
149 let p = write_tmp(tag, content);
150 let s = read_settings(&p);
151 let _ = std::fs::remove_file(&p);
152 s
153 }
154
155 const DEFAULTS: Settings = Settings {
156 run_in_background: false,
157 show_tray: true,
158 };
159
160 #[test]
163 fn defaults_are_tray_on_background_off() {
164 assert_eq!(read_settings(Path::new("/no/such/agentmux-settings.json")), DEFAULTS);
165 assert_eq!(read("absent", r#"{"term:fontsize": 14}"#), DEFAULTS);
166 assert_eq!(read("empty-obj", "{}"), DEFAULTS);
167 assert_eq!(
168 resolve(false, false, false, DEFAULTS),
169 Resolved { background: false, tray: true }
170 );
171 }
172
173 #[test]
174 fn each_key_is_read_independently() {
175 assert_eq!(
176 read("bg-on", r#"{"app:runinbackground": true}"#),
177 Settings { run_in_background: true, show_tray: true }
178 );
179 assert_eq!(
180 read("tray-off", r#"{"app:showtray": false}"#),
181 Settings { run_in_background: false, show_tray: false }
182 );
183 }
184
185 #[test]
186 fn a_bad_file_or_wrong_type_keeps_the_defaults() {
187 assert_eq!(read("bad", "not json {"), DEFAULTS);
188 assert_eq!(read("str", r#"{"app:runinbackground": "true", "app:showtray": "false"}"#), DEFAULTS);
190 }
191
192 #[test]
193 fn background_is_opt_in_from_any_single_source() {
194 assert!(resolve(true, false, false, DEFAULTS).background, "env override");
195 assert!(resolve(false, false, true, DEFAULTS).background, "--background");
196 let on = Settings { run_in_background: true, ..DEFAULTS };
197 assert!(resolve(false, false, false, on).background, "setting");
198 assert!(!resolve(false, true, false, DEFAULTS).background, "the tray alone never implies background");
199 }
200
201 #[test]
203 fn background_forces_the_tray_even_when_hidden_in_settings() {
204 let hidden = Settings { run_in_background: false, show_tray: false };
205 assert_eq!(resolve(false, false, false, hidden), Resolved { background: false, tray: false });
206 assert_eq!(resolve(false, false, true, hidden), Resolved { background: true, tray: true });
207 let both = Settings { run_in_background: true, show_tray: false };
208 assert_eq!(resolve(false, false, false, both), Resolved { background: true, tray: true });
209 }
210
211 #[test]
214 fn background_flag_reaches_launcher_tray_gate() {
215 let args = vec!["agentmux".to_string(), crate::autostart::BACKGROUND_FLAG.to_string()];
216 let prev_bg = std::env::var_os(ENV_BACKGROUND);
219 let prev_tray = std::env::var_os(ENV_TRAY);
220 std::env::remove_var(ENV_BACKGROUND);
221 std::env::remove_var(ENV_TRAY);
222
223 assert!(apply(&args).background);
224 assert!(crate::tray::should_enable(
225 crate::tray::tray_opt_in_from_env(),
226 crate::tray::background_service_from_env()
227 ));
228
229 match prev_bg {
230 Some(v) => std::env::set_var(ENV_BACKGROUND, v),
231 None => std::env::remove_var(ENV_BACKGROUND),
232 }
233 match prev_tray {
234 Some(v) => std::env::set_var(ENV_TRAY, v),
235 None => std::env::remove_var(ENV_TRAY),
236 }
237 }
238}