Skip to main content

agentmux_launcher/
splash_config.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! "Disable the splash" preference, resolved by the launcher *before* it spawns
5//! the splash (and before the host/srv exist), per
6//! `SPEC_SPLASH_USERINFO_AND_DISABLE_2026_06_21.md` §6.
7//!
8//! Precedence: env override wins; otherwise `settings.json` `"splash:disabled"`.
9//! **Fail-safe:** any error reading/parsing settings resolves to *enabled* — a
10//! broken read must never silently suppress the splash.
11
12use std::path::Path;
13
14/// True when the splash should be suppressed entirely (no window created).
15pub fn splash_disabled() -> bool {
16    // A test build (AGENTMUX_TEST_OFFSCREEN) shows nothing on a real display, the splash included.
17    if agentmux_common::test_offscreen::active() {
18        return true;
19    }
20    if let Some(forced) = env_override() {
21        return forced; // env is authoritative (Some(true)=disable, Some(false)=force-on)
22    }
23    // A login start is quiet: no window, so no splash either
24    // (SPEC_START_WITH_OS_2026_09_25.md §3.3). On macOS this also routes the
25    // start through the headless AppKit pump the menu-bar item needs.
26    if crate::autostart::login_start() {
27        return true;
28    }
29    // Best-effort settings.json read. Wrapped so a resolver panic/IO error can
30    // never take down launcher startup — on any failure we keep the splash.
31    std::panic::catch_unwind(|| {
32        crate::data_dir::settings_file()
33            .map(|f| settings_splash_disabled(&f))
34            .unwrap_or(false)
35    })
36    .unwrap_or(false)
37}
38
39/// `Some(true)` = disable, `Some(false)` = force-enable, `None` = no opinion.
40fn env_override() -> Option<bool> {
41    if let Ok(v) = std::env::var("AGENTMUX_NO_SPLASH") {
42        if truthy(&v) {
43            return Some(true);
44        }
45    }
46    if let Ok(v) = std::env::var("AGENTMUX_SPLASH") {
47        return match v.trim().to_ascii_lowercase().as_str() {
48            "0" | "false" | "off" | "no" => Some(true),
49            "1" | "true" | "on" | "yes" => Some(false),
50            _ => None,
51        };
52    }
53    None
54}
55
56fn truthy(v: &str) -> bool {
57    matches!(
58        v.trim().to_ascii_lowercase().as_str(),
59        "1" | "true" | "on" | "yes"
60    )
61}
62
63/// Read `"splash:disabled"` (flat `namespace:key`) from a settings.json file.
64/// Missing file / parse error / missing key → `false` (enabled).
65pub fn settings_splash_disabled(settings_path: &Path) -> bool {
66    let Ok(text) = std::fs::read_to_string(settings_path) else {
67        return false;
68    };
69    let Some(json) = agentmux_common::settings_jsonc::parse_jsonc(&text) else {
70        return false;
71    };
72    json.get("splash:disabled")
73        .and_then(|v| v.as_bool())
74        .unwrap_or(false)
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use std::path::PathBuf;
81
82    fn write_tmp(tag: &str, content: &str) -> PathBuf {
83        let p = std::env::temp_dir().join(format!("agentmux-splashcfg-{tag}-{}.json", std::process::id()));
84        std::fs::write(&p, content).unwrap();
85        p
86    }
87
88    #[test]
89    fn settings_disabled_true() {
90        let p = write_tmp("on", r#"{"splash:disabled": true}"#);
91        assert!(settings_splash_disabled(&p));
92        let _ = std::fs::remove_file(&p);
93    }
94
95    /// The seeded settings.json has comments and trailing commas (#1903).
96    #[test]
97    fn settings_disabled_read_from_a_commented_file() {
98        let p = write_tmp("jsonc", "{
99  // Splash
100  \"splash:disabled\": true,
101}
102");
103        assert!(settings_splash_disabled(&p));
104    }
105
106    #[test]
107    fn settings_disabled_false_when_key_false() {
108        let p = write_tmp("off", r#"{"splash:disabled": false}"#);
109        assert!(!settings_splash_disabled(&p));
110        let _ = std::fs::remove_file(&p);
111    }
112
113    #[test]
114    fn settings_default_enabled_when_key_absent() {
115        let p = write_tmp("absent", r#"{"term:fontsize": 14}"#);
116        assert!(!settings_splash_disabled(&p));
117        let _ = std::fs::remove_file(&p);
118    }
119
120    #[test]
121    fn settings_fail_safe_enabled_on_missing_or_bad() {
122        // Missing file and malformed JSON both resolve to enabled (false).
123        assert!(!settings_splash_disabled(Path::new("/no/such/agentmux-settings.json")));
124        let p = write_tmp("bad", "this is not json {");
125        assert!(!settings_splash_disabled(&p));
126        let _ = std::fs::remove_file(&p);
127    }
128}