mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 15:26:09 +00:00
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:
4 files changed
+112
-16
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -22,12 +22,18 @@ pub struct GroveTray {
|
||||
|
||||
pub type TrayHandle = Handle<GroveTray>;
|
||||
|
||||
/// Starts the tray service. Fails when there is no D-Bus session.
|
||||
pub fn spawn(on_action: Arc<dyn Fn(Action) + Send + Sync>) -> Result<TrayHandle, ksni::Error> {
|
||||
/// 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<dyn Fn(Action) + Send + Sync>,
|
||||
wait_for_host: bool,
|
||||
) -> Result<TrayHandle, ksni::Error> {
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user