From 9750cd3ce66c58916ff6e2e841e9c2021a8d1831 Mon Sep 17 00:00:00 2001 From: Sline Date: Wed, 8 Oct 2025 22:02:17 +0800 Subject: [PATCH] fix: multi-monitor-crash (#4980) --- src-tauri/src/lib.rs | 67 +++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 54769bc4c..ca5359da9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -247,31 +247,60 @@ pub fn run() { // Set Linux environment variable #[cfg(target_os = "linux")] { - unsafe { - std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); - } - - // Force X11 backend for tray icon compatibility on Wayland - let is_wayland = std::env::var("WAYLAND_DISPLAY").is_ok(); - if is_wayland { - unsafe { - std::env::set_var("GDK_BACKEND", "x11"); - std::env::remove_var("WAYLAND_DISPLAY"); - } - logging!( - info, - Type::Setup, - true, - "Wayland detected: Forcing X11 backend for tray icon compatibility" - ); - } - let desktop_env = std::env::var("XDG_CURRENT_DESKTOP") .unwrap_or_default() .to_uppercase(); let is_kde_desktop = desktop_env.contains("KDE"); let is_plasma_desktop = desktop_env.contains("PLASMA"); + let is_wayland_session = std::env::var("XDG_SESSION_TYPE") + .map(|value| value.eq_ignore_ascii_case("wayland")) + .unwrap_or(false) + || std::env::var("WAYLAND_DISPLAY").is_ok(); + let prefer_native_wayland = is_wayland_session && (is_kde_desktop || is_plasma_desktop); + let dmabuf_override = std::env::var("WEBKIT_DISABLE_DMABUF_RENDERER"); + + if prefer_native_wayland { + if matches!(dmabuf_override.as_deref(), Ok("1")) { + unsafe { + std::env::remove_var("WEBKIT_DISABLE_DMABUF_RENDERER"); + } + logging!( + info, + Type::Setup, + true, + "Wayland + KDE detected: Re-enabled WebKit DMABUF renderer to avoid Cairo surface failures." + ); + } else { + logging!( + info, + Type::Setup, + true, + "Wayland + KDE detected: Using native Wayland backend for reliable rendering." + ); + } + } else { + if dmabuf_override.is_err() { + unsafe { + std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); + } + } + + // Force X11 backend for tray icon compatibility on Wayland + if is_wayland_session { + unsafe { + std::env::set_var("GDK_BACKEND", "x11"); + std::env::remove_var("WAYLAND_DISPLAY"); + } + logging!( + info, + Type::Setup, + true, + "Wayland detected: Forcing X11 backend for tray icon compatibility" + ); + } + } + if is_kde_desktop || is_plasma_desktop { unsafe { std::env::set_var("GTK_CSD", "0");