diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index ca4e5e63f..00405c6d3 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", ] @@ -7037,7 +7038,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 dea6b2eb5..4049b437e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -114,9 +114,10 @@ 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" +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-tauri/src/lib.rs b/src-tauri/src/lib.rs index 94593c809..e30edcf2c 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()); diff --git a/src/app/features/settings/desktop/Desktop.test.tsx b/src/app/features/settings/desktop/Desktop.test.tsx index a43ba2f5a..f1b1a036c 100644 --- a/src/app/features/settings/desktop/Desktop.test.tsx +++ b/src/app/features/settings/desktop/Desktop.test.tsx @@ -106,7 +106,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(); @@ -131,7 +131,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(); @@ -144,7 +144,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(); }); @@ -158,7 +158,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.'