diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06df4bf..f21f1a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,9 @@ jobs: - uses: actions/checkout@v4 - name: Shell syntax run: for f in spikes/*.sh; do bash -n "$f"; done + - name: System libraries for the GUI (Slint needs fontconfig headers) + if: hashFiles('Cargo.toml') != '' + run: sudo apt-get update -qq && sudo apt-get install -y -qq libfontconfig-dev - name: Rust (once a Cargo workspace exists) if: hashFiles('Cargo.toml') != '' run: | diff --git a/crates/grove-gui/src/main.rs b/crates/grove-gui/src/main.rs index a1682c1..50cc510 100644 --- a/crates/grove-gui/src/main.rs +++ b/crates/grove-gui/src/main.rs @@ -489,7 +489,12 @@ fn toast(ui: &MainWindow, msg: &str, bad: bool) { }); } -fn show_window(ui: &MainWindow, core: &Core) { +/// Shows (or re-maps) the window. `why` goes to the diagnostics log. +fn show_window(ui: &MainWindow, core: &Core, why: &str) { + log::line(&format!( + "showing the window ({why}; visible before: {})", + ui.window().is_visible() + )); // Slint exposes no xdg-activation, so an already visible window is raised by re-mapping it: // a fresh toplevel is what the compositor focuses. if core.visible.load(Ordering::SeqCst) && ui.window().is_visible() { @@ -1399,6 +1404,11 @@ fn run() -> Result<()> { return Ok(()); } let hidden = args.iter().any(|a| a == "--hidden"); + let started = Instant::now(); + log::line(&format!( + "session type: {}", + std::env::var("XDG_SESSION_TYPE").unwrap_or_else(|_| "unknown".into()) + )); let inst_path = arg("--instance-socket") .map(PathBuf::from) .unwrap_or_else(instance::default_socket_path); @@ -1413,15 +1423,25 @@ fn run() -> Result<()> { { let (late, pending) = (late.clone(), pending_show.clone()); std::thread::spawn(move || { - instance::serve(listener, move || match late.lock().unwrap().clone() { - Some(c) => { - let _ = slint::invoke_from_event_loop(move || { - if let Some(ui) = c.weak.upgrade() { - show_window(&ui, &c.core); - } - }); + instance::serve(listener, move || { + let age = started.elapsed(); + if !model::accept_instance_show(hidden, age) { + log::line(&format!( + "ignored a show request from another launch {:.1} s after a --hidden start (session restore?)", + age.as_secs_f32() + )); + return; + } + match late.lock().unwrap().clone() { + Some(c) => { + let _ = slint::invoke_from_event_loop(move || { + if let Some(ui) = c.weak.upgrade() { + show_window(&ui, &c.core, "another launch asked"); + } + }); + } + None => pending.store(true, Ordering::SeqCst), } - None => pending.store(true, Ordering::SeqCst), }) }); } @@ -1472,7 +1492,7 @@ fn run() -> Result<()> { let c = ctx.clone(); let _ = slint::invoke_from_event_loop(move || { if let Some(ui) = c.weak.upgrade() { - show_window(&ui, &c.core); + show_window(&ui, &c.core, "tray"); } }); } @@ -1487,7 +1507,8 @@ fn run() -> Result<()> { } }) }; - let tray = match tray::spawn(on_action.clone()) { + // Autostart runs before Plasma's tray is ready: wait for it instead of opening the window. + let tray = match tray::spawn(on_action.clone(), hidden) { Ok(h) => { core.tray_ok.store(true, Ordering::SeqCst); Some(h) @@ -1516,7 +1537,13 @@ fn run() -> Result<()> { let (c, w) = (core.clone(), ui.as_weak()); let respawn = on_action.clone(); std::thread::spawn(move || { - worker(c, rx, tray, move || tray::spawn(respawn.clone()).ok(), w) + worker( + c, + rx, + tray, + move || tray::spawn(respawn.clone(), true).ok(), + w, + ) }); } { @@ -1525,9 +1552,19 @@ fn run() -> Result<()> { } *late.lock().unwrap() = Some(ctx.clone()); - if !hidden || pending_show.load(Ordering::SeqCst) || !core.tray_ok.load(Ordering::SeqCst) { - show_window(&ui, &core); + let why = if !hidden { + Some("started without --hidden") + } else if pending_show.load(Ordering::SeqCst) { + Some("another launch asked during start-up") + } else if !core.tray_ok.load(Ordering::SeqCst) { + Some("no tray") } else { + None + }; + if let Some(why) = why { + show_window(&ui, &core, why); + } else { + log::line("started hidden in the tray"); core.wake(); } let r = slint::run_event_loop_until_quit(); diff --git a/crates/grove-gui/src/model.rs b/crates/grove-gui/src/model.rs index 8bf2e51..c17e8b7 100644 --- a/crates/grove-gui/src/model.rs +++ b/crates/grove-gui/src/model.rs @@ -336,6 +336,16 @@ pub fn group_name(entry: &str) -> &str { entry.split(" → ").next().unwrap_or(entry) } +/// After a `--hidden` start (autostart at login), show requests from other launches are ignored +/// for this long: they come from the desktop's session restore, and a window mapped while the +/// compositor is still starting shows up in the task bar but never on screen. +pub const LOGIN_GRACE: std::time::Duration = std::time::Duration::from_secs(20); + +/// Whether a show request from another launch, `since_start` after this one started, is honoured. +pub fn accept_instance_show(hidden_start: bool, since_start: std::time::Duration) -> bool { + !hidden_start || since_start >= LOGIN_GRACE +} + /// What the "Add or change a namespace" form holds. pub struct GroupForm { pub name: String, @@ -549,6 +559,15 @@ mod tests { assert_eq!(group_name("work → gitea"), "work"); } + #[test] + fn early_show_requests_are_ignored_only_after_a_hidden_start() { + use std::time::Duration; + assert!(accept_instance_show(false, Duration::ZERO)); + assert!(!accept_instance_show(true, Duration::from_secs(3))); + assert!(accept_instance_show(true, LOGIN_GRACE)); + assert!(accept_instance_show(true, Duration::from_secs(600))); + } + #[test] fn group_form_becomes_group_add_args() { let mut f = GroupForm { diff --git a/crates/grove-gui/src/tray.rs b/crates/grove-gui/src/tray.rs index 51752fa..d03809a 100644 --- a/crates/grove-gui/src/tray.rs +++ b/crates/grove-gui/src/tray.rs @@ -22,12 +22,18 @@ pub struct GroveTray { pub type TrayHandle = Handle; -/// Starts the tray service. Fails when there is no D-Bus session. -pub fn spawn(on_action: Arc) -> Result { +/// Starts the tray service. Fails when there is no D-Bus session. With `wait_for_host` a +/// missing StatusNotifier watcher/host is not an error: the item registers as soon as the +/// desktop's tray comes up (an autostarted app usually runs before Plasma's tray is ready). +pub fn spawn( + on_action: Arc, + wait_for_host: bool, +) -> Result { GroveTray { agg: Aggregate::default(), on_action, } + .assume_sni_available(wait_for_host) .spawn() } @@ -48,6 +54,17 @@ impl ksni::Tray for GroveTray { "grove".into() } + fn watcher_online(&self) { + crate::log::line("tray: StatusNotifier watcher is online; icon registered"); + } + + fn watcher_offline(&self, reason: ksni::OfflineReason) -> bool { + crate::log::line(&format!( + "tray: StatusNotifier watcher offline ({reason:?}); waiting for it" + )); + true + } + fn title(&self) -> String { "Grove".into() } diff --git a/docs/phase7.md b/docs/phase7.md index a02f6c2..1420082 100644 --- a/docs/phase7.md +++ b/docs/phase7.md @@ -156,3 +156,26 @@ tray errors and restarts, panics with a backtrace (from any thread) and why the repeated page, not on a short page. For a user that is not the login, GitHub/Gitea listing uses `/user/repos` (collaborator repos included) and says when `/orgs/{owner}` is a 404. - The GUI hands `grove add` the canonical path it validated. + +## Autostart at login (Plasma Wayland) + +Seen on Plasma 6 / Wayland: after an autostart (`grove-gui --hidden`) the window had a task bar +entry, but clicking it showed nothing; opening it from the tray worked (that path hides and +re-shows the window). A window mapped while the compositor is still starting ends up in that +state. Two ways it got shown at login, both handled now: + +- No tray yet: Plasma's StatusNotifier watcher/host can come up after autostart, and a failed tray + start used to fall back to opening the window. A `--hidden` start now registers the tray item + with ksni's `assume_sni_available`: it waits for the watcher and registers when it appears + (`tray: StatusNotifier watcher …` lines in the log). Without a D-Bus session at all the window + still opens, as before. The tray respawn after a lost D-Bus connection also waits. +- Session restore: the desktop relaunches `grove-gui` (no `--hidden`) at login, which asks the + running instance to show its window. During the first 20 s after a `--hidden` start + (`model::LOGIN_GRACE`) such requests are ignored and logged. The tray's "Open Grove" is never + ignored. + +Every time the window is shown, `grove-gui.log` records why (start without `--hidden`, another +launch, tray, no tray) and whether it was visible before; the session type is logged at start. +Checked under Xvfb with a private D-Bus session and no tray host (starts hidden, an early second +launch is ignored, a later one opens the window). The watcher appearing later was not simulated, +and the original symptom was not reproduced here (no Plasma).