fix(gui): keep an autostarted window from opening during login

On Plasma Wayland an autostarted grove-gui could end up with a task bar
entry that shows nothing (window mapped while the compositor starts).

- A --hidden start waits for the StatusNotifier watcher instead of opening
  the window when the tray is not up yet (ksni assume_sni_available)
- Show requests from other launches in the first 20 s after a --hidden start
  (session restore) are ignored and logged
- Every window show is logged with its reason; the session type at start

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GZUAGhGYoiLvmfXweQnDY
This commit is contained in:
Claude committed 2026-11-25 14:35:02 +00:00
1 parent 31c8d5b4f7
commit 6b294e80d9
4 files changed
+112 -16

No files matched your search

+51 -14
View File
@@ -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: // Slint exposes no xdg-activation, so an already visible window is raised by re-mapping it:
// a fresh toplevel is what the compositor focuses. // a fresh toplevel is what the compositor focuses.
if core.visible.load(Ordering::SeqCst) && ui.window().is_visible() { if core.visible.load(Ordering::SeqCst) && ui.window().is_visible() {
@@ -1399,6 +1404,11 @@ fn run() -> Result<()> {
return Ok(()); return Ok(());
} }
let hidden = args.iter().any(|a| a == "--hidden"); 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") let inst_path = arg("--instance-socket")
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(instance::default_socket_path); .unwrap_or_else(instance::default_socket_path);
@@ -1413,15 +1423,25 @@ fn run() -> Result<()> {
{ {
let (late, pending) = (late.clone(), pending_show.clone()); let (late, pending) = (late.clone(), pending_show.clone());
std::thread::spawn(move || { std::thread::spawn(move || {
instance::serve(listener, move || match late.lock().unwrap().clone() { instance::serve(listener, move || {
Some(c) => { let age = started.elapsed();
let _ = slint::invoke_from_event_loop(move || { if !model::accept_instance_show(hidden, age) {
if let Some(ui) = c.weak.upgrade() { log::line(&format!(
show_window(&ui, &c.core); "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 c = ctx.clone();
let _ = slint::invoke_from_event_loop(move || { let _ = slint::invoke_from_event_loop(move || {
if let Some(ui) = c.weak.upgrade() { 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) => { Ok(h) => {
core.tray_ok.store(true, Ordering::SeqCst); core.tray_ok.store(true, Ordering::SeqCst);
Some(h) Some(h)
@@ -1516,7 +1537,13 @@ fn run() -> Result<()> {
let (c, w) = (core.clone(), ui.as_weak()); let (c, w) = (core.clone(), ui.as_weak());
let respawn = on_action.clone(); let respawn = on_action.clone();
std::thread::spawn(move || { 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()); *late.lock().unwrap() = Some(ctx.clone());
if !hidden || pending_show.load(Ordering::SeqCst) || !core.tray_ok.load(Ordering::SeqCst) { let why = if !hidden {
show_window(&ui, &core); 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 { } else {
None
};
if let Some(why) = why {
show_window(&ui, &core, why);
} else {
log::line("started hidden in the tray");
core.wake(); core.wake();
} }
let r = slint::run_event_loop_until_quit(); let r = slint::run_event_loop_until_quit();
+19
View File
@@ -336,6 +336,16 @@ pub fn group_name(entry: &str) -> &str {
entry.split(" → ").next().unwrap_or(entry) 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. /// What the "Add or change a namespace" form holds.
pub struct GroupForm { pub struct GroupForm {
pub name: String, pub name: String,
@@ -549,6 +559,15 @@ mod tests {
assert_eq!(group_name("work → gitea"), "work"); 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] #[test]
fn group_form_becomes_group_add_args() { fn group_form_becomes_group_add_args() {
let mut f = GroupForm { let mut f = GroupForm {
+19 -2
View File
@@ -22,12 +22,18 @@ pub struct GroveTray {
pub type TrayHandle = Handle<GroveTray>; pub type TrayHandle = Handle<GroveTray>;
/// Starts the tray service. Fails when there is no D-Bus session. /// Starts the tray service. Fails when there is no D-Bus session. With `wait_for_host` a
pub fn spawn(on_action: Arc<dyn Fn(Action) + Send + Sync>) -> Result<TrayHandle, ksni::Error> { /// 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<dyn Fn(Action) + Send + Sync>,
wait_for_host: bool,
) -> Result<TrayHandle, ksni::Error> {
GroveTray { GroveTray {
agg: Aggregate::default(), agg: Aggregate::default(),
on_action, on_action,
} }
.assume_sni_available(wait_for_host)
.spawn() .spawn()
} }
@@ -48,6 +54,17 @@ impl ksni::Tray for GroveTray {
"grove".into() "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 { fn title(&self) -> String {
"Grove".into() "Grove".into()
} }
+23
View File
@@ -155,3 +155,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 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. `/user/repos` (collaborator repos included) and says when `/orgs/{owner}` is a 404.
- The GUI hands `grove add` the canonical path it validated. - 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).