fix(tray): debounce double-click to avoid wgpu TextureView panic

A tray double-click fired two activate calls within milliseconds. On Wayland
each toggle destroys and recreates the winit surface and the wgpu shader
underlay, so a render still in flight then use-after-frees a TextureView
(wgpu-core panic "TextureView ... is no longer alive"). Collapse a double-click
into a single toggle with a 400ms debounce; the first click always passes.

cc
This commit is contained in:
vicrodh committed 2026-06-29 13:18:51 -06:00
1 parent b76e2b8137
commit 158d6416ce
1 file changed
+24 -1
+24 -1
View File
@@ -14,7 +14,8 @@
//! show/hide toggles the winit window in place.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use slint::ComponentHandle;
@@ -36,6 +37,15 @@ pub(crate) type Runtime = Arc<AppRuntime<SlintAdapter>>;
/// even on backends where querying winit visibility is unreliable (Wayland).
static WINDOW_SHOWN: AtomicBool = AtomicBool::new(true);
/// Last tray-toggle timestamp, for the double-click debounce (see `toggle_window`).
static LAST_TOGGLE: Mutex<Option<Instant>> = Mutex::new(None);
/// Ignore a 2nd tray activation within this window. A tray double-click otherwise
/// fires two `Activate` in milliseconds; on Wayland each toggle destroys/recreates
/// the winit surface AND the wgpu shader underlay, and a render in flight then
/// use-after-frees a `TextureView` (wgpu-core panic). Collapsing the double-click
/// to one toggle avoids the churn.
const TOGGLE_DEBOUNCE_MS: u128 = 400;
/// Cross-thread handle to the live tray. Cloneable; mutators forward to the
/// platform backend (ksni on Linux) and are no-ops when the tray is disabled
/// or on a platform without a live-update path.
@@ -178,6 +188,19 @@ pub fn init(
/// Toggle the main window: hide if shown, else show + focus.
pub(crate) fn toggle_window(weak: &slint::Weak<AppWindow>) {
// Debounce: ignore the 2nd click of a tray double-click. Two rapid toggles
// churn the Wayland surface + wgpu shader underlay and use-after-free a
// TextureView (wgpu-core panic). The first click always passes.
{
let now = Instant::now();
let mut last = LAST_TOGGLE.lock().unwrap_or_else(|p| p.into_inner());
if let Some(prev) = *last {
if now.duration_since(prev).as_millis() < TOGGLE_DEBOUNCE_MS {
return;
}
}
*last = Some(now);
}
if WINDOW_SHOWN.load(Ordering::Relaxed) {
hide_window(weak);
} else {