From d3477159a80e6b5363d8a51dd4fc90d71b1f485c Mon Sep 17 00:00:00 2001 From: Sline Date: Tue, 7 Oct 2025 10:02:11 +0800 Subject: [PATCH] fix: improve Linux tray support and add --no-tray option (#4958) --- src-tauri/src/core/tray/mod.rs | 14 ++++++++++++-- src-tauri/src/lib.rs | 15 +++++++++++++++ src-tauri/src/main.rs | 9 +++++++++ src-tauri/src/utils/resolve/mod.rs | 11 +++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 3df6a8204..97dfb2cc9 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -192,8 +192,18 @@ impl Tray { let app_handle = handle::Handle::global() .app_handle() .ok_or_else(|| anyhow::anyhow!("Failed to get app handle for tray initialization"))?; - self.create_tray_from_handle(&app_handle).await?; - Ok(()) + + match self.create_tray_from_handle(&app_handle).await { + Ok(_) => { + log::info!(target: "app", "System tray created successfully"); + Ok(()) + } + Err(e) => { + log::warn!(target: "app", "System tray creation failed: {}, Application will continue running without tray icon", e); + // Don't return error, let application continue running without tray + Ok(()) + } + } } /// 更新托盘点击行为 diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f4e316783..ba4237902 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -282,6 +282,21 @@ pub fn run() { 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(); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6a088a2ef..a2a91c7ed 100755 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,5 +2,14 @@ fn main() { #[cfg(feature = "tokio-trace")] console_subscriber::init(); + + // Check for --no-tray command line argument + let args: Vec = std::env::args().collect(); + if args.contains(&"--no-tray".to_string()) { + unsafe { + std::env::set_var("CLASH_VERGE_DISABLE_TRAY", "1"); + } + } + app_lib::run(); } diff --git a/src-tauri/src/utils/resolve/mod.rs b/src-tauri/src/utils/resolve/mod.rs index 7744de265..bdb7fa5ea 100644 --- a/src-tauri/src/utils/resolve/mod.rs +++ b/src-tauri/src/utils/resolve/mod.rs @@ -204,6 +204,17 @@ pub async fn init_work_config() { } pub(super) async fn init_tray() { + // Check if tray should be disabled via environment variable + if std::env::var("CLASH_VERGE_DISABLE_TRAY").unwrap_or_default() == "1" { + logging!( + info, + Type::Setup, + true, + "System tray disabled via --no-tray flag" + ); + return; + } + logging!(info, Type::Setup, true, "Initializing system tray..."); logging_error!(Type::Setup, true, Tray::global().init().await); }