From 89a957d03b3272a6cc69941cf05d931c6b18b765 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 25 Jun 2026 23:05:50 +0800 Subject: [PATCH] fix(desktop): improve AppImage Wayland startup --- src-tauri/src/lib.rs | 74 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 97ccb365de..65bc4b2f9f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -74,13 +74,45 @@ fn native_window_decorations_override(target_os: &str) -> Option { } } +fn linux_webkit_rendering_workarounds() -> &'static [(&'static str, &'static str)] { + &[ + // WebKitGTK's DMABUF renderer can produce a blank AppImage window or + // Wayland protocol errors on Fedora/Wayland/NVIDIA systems. + ("WEBKIT_DISABLE_DMABUF_RENDERER", "1"), + // Tauri's Linux graphics guidance recommends this for Wayland explicit + // sync issues that can prevent WebKitGTK from creating a usable surface. + ("__NV_DISABLE_EXPLICIT_SYNC", "1"), + ] +} + +fn linux_appimage_wayland_backend_override( + appimage: Option<&std::ffi::OsStr>, + wayland_display: Option<&std::ffi::OsStr>, + gdk_backend: Option<&std::ffi::OsStr>, +) -> Option<&'static str> { + if appimage.is_some() && wayland_display.is_some() && gdk_backend.is_none() { + // AppImage uses the host GTK/WebKitGTK stack. Prefer XWayland for the + // affected Wayland/EGL path, but keep Wayland and other compiled + // backends as fallbacks for systems without XWayland. + Some("x11,wayland,*") + } else { + None + } +} + #[cfg(target_os = "linux")] fn apply_linux_webkit_rendering_workarounds() { - const DISABLE_DMABUF_RENDERER: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; - if std::env::var_os(DISABLE_DMABUF_RENDERER).is_none() { - // WebKitGTK's DMABUF renderer can produce a blank AppImage window on - // Fedora/Wayland/NVIDIA systems. This must be set before WebKit starts. - std::env::set_var(DISABLE_DMABUF_RENDERER, "1"); + for (key, value) in linux_webkit_rendering_workarounds() { + if std::env::var_os(key).is_none() { + std::env::set_var(key, value); + } + } + if let Some(gdk_backend) = linux_appimage_wayland_backend_override( + std::env::var_os("APPIMAGE").as_deref(), + std::env::var_os("WAYLAND_DISPLAY").as_deref(), + std::env::var_os("GDK_BACKEND").as_deref(), + ) { + std::env::set_var("GDK_BACKEND", gdk_backend); } } @@ -202,9 +234,11 @@ pub(crate) fn apply_desktop_settings(app: &tauri::AppHandle, desktop_settings: & #[allow(clippy::items_after_test_module)] mod tests { use super::{ + linux_appimage_wayland_backend_override, linux_webkit_rendering_workarounds, native_window_decorations_override, should_hide_window_on_close, should_setup_desktop_tray, should_show_main_window_after_setup, }; + use std::ffi::OsStr; #[test] fn hides_window_on_close_for_windows_and_macos() { @@ -237,6 +271,36 @@ mod tests { assert_eq!(native_window_decorations_override("linux"), Some(true)); assert_eq!(native_window_decorations_override("macos"), None); } + + #[test] + fn applies_linux_webkit_rendering_workarounds_before_webkit_starts() { + assert_eq!( + linux_webkit_rendering_workarounds(), + &[("WEBKIT_DISABLE_DMABUF_RENDERER", "1"), ("__NV_DISABLE_EXPLICIT_SYNC", "1")] + ); + } + + #[test] + fn prefers_x11_for_appimage_wayland_when_backend_is_not_user_configured() { + assert_eq!( + linux_appimage_wayland_backend_override( + Some(OsStr::new("/tmp/DBX.AppImage")), + Some(OsStr::new("wayland-0")), + None + ), + Some("x11,wayland,*") + ); + assert_eq!( + linux_appimage_wayland_backend_override( + Some(OsStr::new("/tmp/DBX.AppImage")), + Some(OsStr::new("wayland-0")), + Some(OsStr::new("wayland")) + ), + None + ); + assert_eq!(linux_appimage_wayland_backend_override(Some(OsStr::new("/tmp/DBX.AppImage")), None, None), None); + assert_eq!(linux_appimage_wayland_backend_override(None, Some(OsStr::new("wayland-0")), None), None); + } } #[cfg_attr(mobile, tauri::mobile_entry_point)]