fix: improve Linux tray support and add --no-tray option (#4958)

This commit is contained in:
Sline
2025-10-07 10:02:11 +08:00
committed by GitHub
parent c05395c258
commit d3477159a8
4 changed files with 47 additions and 2 deletions

View File

@@ -192,8 +192,18 @@ impl Tray {
let app_handle = handle::Handle::global() let app_handle = handle::Handle::global()
.app_handle() .app_handle()
.ok_or_else(|| anyhow::anyhow!("Failed to get app handle for tray initialization"))?; .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(())
}
}
} }
/// 更新托盘点击行为 /// 更新托盘点击行为

View File

@@ -282,6 +282,21 @@ pub fn run() {
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); 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") let desktop_env = std::env::var("XDG_CURRENT_DESKTOP")
.unwrap_or_default() .unwrap_or_default()
.to_uppercase(); .to_uppercase();

View File

@@ -2,5 +2,14 @@
fn main() { fn main() {
#[cfg(feature = "tokio-trace")] #[cfg(feature = "tokio-trace")]
console_subscriber::init(); console_subscriber::init();
// Check for --no-tray command line argument
let args: Vec<String> = std::env::args().collect();
if args.contains(&"--no-tray".to_string()) {
unsafe {
std::env::set_var("CLASH_VERGE_DISABLE_TRAY", "1");
}
}
app_lib::run(); app_lib::run();
} }

View File

@@ -204,6 +204,17 @@ pub async fn init_work_config() {
} }
pub(super) async fn init_tray() { 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!(info, Type::Setup, true, "Initializing system tray...");
logging_error!(Type::Setup, true, Tray::global().init().await); logging_error!(Type::Setup, true, Tray::global().init().await);
} }