From 5155599caa8b4f879414a15a8d469d63df8c8f1f Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 9 Aug 2026 09:59:37 +0200 Subject: [PATCH 1/3] fix(cef): reinstall X error handlers after gtk::init --- src-tauri/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 69e8b33ce..c4892d9f6 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -384,9 +384,13 @@ pub fn run() { mobile::set_app_handle(app.handle().clone()); // CEF is initialized during runtime construction; initialize GTK afterward - // on the main thread for the Linux tray menu. + // on the main thread for the Linux tray menu. GTK's X11 backend replaces the + // X error handlers during init, so the runtime's have to go back on after. #[cfg(all(feature = "cef", target_os = "linux"))] - gtk::init()?; + { + gtk::init()?; + tauri_runtime_cef::install_x_error_handlers(); + } network::native_upload::cleanup_uploads(app.handle()); From 4545c0900f4bf12bcbe82ace20faefc34f183a3a Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 9 Aug 2026 10:03:09 +0200 Subject: [PATCH 2/3] fix(cef): pin the runtime to the chrome runtime style fix so the app exits --- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 7bfd9f29c..c9a58656c 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -7037,7 +7037,7 @@ dependencies = [ [[package]] name = "tauri-runtime-cef" version = "0.1.0" -source = "git+https://github.com/SableClient/tauri-runtime-cef?branch=main#8d5870aaa48575bf1d07d8bbf8654703c16317ad" +source = "git+https://github.com/SableClient/tauri-runtime-cef?rev=6fb4fabcc759c1f5407def192540ce149f666fe8#6fb4fabcc759c1f5407def192540ce149f666fe8" dependencies = [ "base64 0.22.1", "cef", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7a258bff7..0b13bce0d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -114,7 +114,7 @@ webkit2gtk = { version = "2.0", optional = true } # Fork of tauri's unreleased feat/cef branch, ported onto published crates. # https://github.com/SableClient/tauri-runtime-cef [target.'cfg(target_os = "linux")'.dependencies] -tauri-runtime-cef = { git = "https://github.com/SableClient/tauri-runtime-cef", branch = "main", optional = true } +tauri-runtime-cef = { git = "https://github.com/SableClient/tauri-runtime-cef", rev = "6fb4fabcc759c1f5407def192540ce149f666fe8", optional = true } cef = { version = "=150.2.1", optional = true } libloading = "0.9" From dd4a824014a2b4790cff37f87ac7e5ae36e85459 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 9 Aug 2026 10:05:57 +0200 Subject: [PATCH 3/3] fix(tray): only close to background when a restore path exists --- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/desktop/tray.rs | 89 ++++++++++++++++--- .../settings/desktop/Desktop.test.tsx | 8 +- src/app/features/settings/desktop/Desktop.tsx | 10 ++- 5 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c9a58656c..e1402e22e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -5499,6 +5499,7 @@ dependencies = [ "ts-rs", "webkit2gtk", "windows 0.62.2", + "zbus", "zip 8.6.0", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 0b13bce0d..1c2b2f696 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -117,6 +117,7 @@ webkit2gtk = { version = "2.0", optional = true } tauri-runtime-cef = { git = "https://github.com/SableClient/tauri-runtime-cef", rev = "6fb4fabcc759c1f5407def192540ce149f666fe8", optional = true } cef = { version = "=150.2.1", optional = true } libloading = "0.9" +zbus = "5" [target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies] tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "057d323862d815a0c7459a1f1acd5a3c08750a51", features = [ diff --git a/src-tauri/src/desktop/tray.rs b/src-tauri/src/desktop/tray.rs index 83bc82f0e..5a7dacda0 100644 --- a/src-tauri/src/desktop/tray.rs +++ b/src-tauri/src/desktop/tray.rs @@ -46,7 +46,11 @@ pub fn setup_close_to_background(webview_window: &WebviewWindow(); - if state.close_to_background_on_close.load(Ordering::Relaxed) { + if state.close_to_background_on_close.load(Ordering::Relaxed) + && can_restore_from_background(DesktopRuntimeState { + tray_available: state.tray_available.load(Ordering::Relaxed), + }) + { api.prevent_close(); let _ = window.hide(); } @@ -59,8 +63,19 @@ enum ExitRequestAction { CloseWindowsToBackground, } -fn exit_request_action(settings: DesktopSettings, code: Option) -> ExitRequestAction { - if code.is_some() || !settings.close_to_background_on_close { +fn can_restore_from_background(runtime: DesktopRuntimeState) -> bool { + cfg!(target_os = "macos") || runtime.tray_available +} + +fn exit_request_action( + settings: DesktopSettings, + runtime: DesktopRuntimeState, + code: Option, +) -> ExitRequestAction { + if code.is_some() + || !settings.close_to_background_on_close + || !can_restore_from_background(runtime) + { ExitRequestAction::AllowExit } else { ExitRequestAction::CloseWindowsToBackground @@ -216,7 +231,8 @@ fn handle_exit_request( api: &tauri::ExitRequestApi, ) { let settings = current_desktop_settings(app); - if exit_request_action(settings, code) == ExitRequestAction::CloseWindowsToBackground { + let runtime = desktop_runtime_state(app); + if exit_request_action(settings, runtime, code) == ExitRequestAction::CloseWindowsToBackground { api.prevent_exit(); close_all_windows(app); } @@ -283,6 +299,24 @@ fn appindicator_available() -> bool { .any(|name| unsafe { libloading::Library::new(*name) }.is_ok()) } +// The AppImage bundles libayatana-appindicator, so the library probe passes on +// every desktop. Without a StatusNotifierItem host the icon is created and never +// rendered, which strands the window with no way to restore it. +#[cfg(target_os = "linux")] +fn status_notifier_host_available() -> bool { + const WATCHER: &str = "org.kde.StatusNotifierWatcher"; + let Ok(name) = zbus::names::BusName::try_from(WATCHER) else { + return false; + }; + let Ok(connection) = zbus::blocking::Connection::session() else { + return false; + }; + let Ok(dbus) = zbus::blocking::fdo::DBusProxy::new(&connection) else { + return false; + }; + dbus.name_has_owner(name).unwrap_or(false) +} + pub fn create_system_tray(app: &AppHandle) -> tauri::Result<()> { #[cfg(target_os = "linux")] if !appindicator_available() { @@ -293,6 +327,15 @@ pub fn create_system_tray(app: &AppHandle) -> tauri::Resul .into()); } + #[cfg(target_os = "linux")] + if !status_notifier_host_available() { + return Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "no StatusNotifierWatcher on the session bus; skipping system tray", + ) + .into()); + } + let show_item = MenuItem::with_id(app, TRAY_MENU_SHOW_ID, "Show", true, None::<&str>)?; let quit_item = MenuItem::with_id(app, TRAY_MENU_QUIT_ID, "Quit", true, None::<&str>)?; let tray_menu = Menu::with_items(app, &[&show_item, &quit_item])?; @@ -335,16 +378,23 @@ mod tests { desktop_settings_from_values, tray_available_for_session, DesktopSettings, }; + const TRAY_UP: DesktopRuntimeState = DesktopRuntimeState { + tray_available: true, + }; + const NO_TRAY: DesktopRuntimeState = DesktopRuntimeState { + tray_available: false, + }; + #[test] fn close_behavior_keeps_sable_running() { let settings = DesktopSettings { close_to_background_on_close: true, - show_system_tray_icon: false, + show_system_tray_icon: true, use_custom_title_bar: false, }; assert_eq!( - exit_request_action(settings, None), + exit_request_action(settings, TRAY_UP, None), ExitRequestAction::CloseWindowsToBackground ); } @@ -358,24 +408,37 @@ mod tests { }; assert_eq!( - exit_request_action(settings, None), + exit_request_action(settings, TRAY_UP, None), ExitRequestAction::AllowExit ); } #[test] - fn tray_failure_still_closes_to_background_when_requested() { + fn tray_failure_allows_exit_instead_of_stranding_the_process() { let settings = DesktopSettings { close_to_background_on_close: true, show_system_tray_icon: true, use_custom_title_bar: false, }; - assert_eq!( - exit_request_action(settings, None), - ExitRequestAction::CloseWindowsToBackground - ); assert!(!tray_available_for_session(settings, false)); + assert_eq!( + exit_request_action(settings, NO_TRAY, None), + if cfg!(target_os = "macos") { + ExitRequestAction::CloseWindowsToBackground + } else { + ExitRequestAction::AllowExit + } + ); + } + + #[test] + fn macos_closes_to_background_without_a_tray() { + assert_eq!( + can_restore_from_background(NO_TRAY), + cfg!(target_os = "macos") + ); + assert!(can_restore_from_background(TRAY_UP)); } #[test] @@ -387,7 +450,7 @@ mod tests { }; assert_eq!( - exit_request_action(settings, Some(0)), + exit_request_action(settings, TRAY_UP, Some(0)), ExitRequestAction::AllowExit ); } diff --git a/src/app/features/settings/desktop/Desktop.test.tsx b/src/app/features/settings/desktop/Desktop.test.tsx index d8e22b33a..6c75aa128 100644 --- a/src/app/features/settings/desktop/Desktop.test.tsx +++ b/src/app/features/settings/desktop/Desktop.test.tsx @@ -105,7 +105,7 @@ describe('Desktop', () => { ).toBeInTheDocument(); expect( screen.getByText( - 'When enabled, closing the window keeps Sable running instead of exiting. If the tray icon is enabled and available, Sable stays in the system tray. Otherwise it continues running in the background.' + 'When enabled, closing the window keeps Sable running in the system tray instead of exiting. This needs the tray icon below: without a tray to restore from, closing exits Sable.' ) ).toBeInTheDocument(); expect(screen.getByText('Show system tray icon')).toBeInTheDocument(); @@ -130,7 +130,7 @@ describe('Desktop', () => { expect( screen.getByText( - 'System tray is unavailable on this system. Sable can still keep running in the background without it.' + 'System tray is unavailable on this system. Without it, closing the window exits Sable.' ) ).toBeInTheDocument(); expect(screen.getByRole('switch', { name: 'show-system-tray-icon' })).toBeDisabled(); @@ -143,7 +143,7 @@ describe('Desktop', () => { expect( screen.queryByText( - 'System tray is unavailable on this system. Sable can still keep running in the background without it.' + 'System tray is unavailable on this system. Without it, closing the window exits Sable.' ) ).not.toBeInTheDocument(); }); @@ -157,7 +157,7 @@ describe('Desktop', () => { expect( screen.queryByText( - 'System tray is unavailable on this system. Sable can still keep running in the background without it.' + 'System tray is unavailable on this system. Without it, closing the window exits Sable.' ) ).not.toBeInTheDocument(); }); diff --git a/src/app/features/settings/desktop/Desktop.tsx b/src/app/features/settings/desktop/Desktop.tsx index 615703eaf..72ad97c2c 100644 --- a/src/app/features/settings/desktop/Desktop.tsx +++ b/src/app/features/settings/desktop/Desktop.tsx @@ -54,7 +54,11 @@ export function Desktop({ requestBack, requestClose }: DesktopProps) { - System tray is unavailable on this system. Sable can still keep running in - the background without it. + System tray is unavailable on this system. Without it, closing the window + exits Sable. ) : ( 'Show a system tray icon while Sable is running. Disable this if you want Sable to stay available without a tray icon.'