fix(tray): only close to background when a restore path exists

This commit is contained in:
Erwan Leboucher committed 2026-08-09 10:05:57 +02:00
1 parent 4545c0900f
commit dd4a824014
5 files changed
+89 -20

No files matched your search

+1
View File
@@ -5499,6 +5499,7 @@ dependencies = [
"ts-rs",
"webkit2gtk",
"windows 0.62.2",
"zbus",
"zip 8.6.0",
]
+1
View File
@@ -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 = [
+76 -13
View File
@@ -46,7 +46,11 @@ pub fn setup_close_to_background(webview_window: &WebviewWindow<crate::BrowserEn
return;
};
let state = window.state::<DesktopSettingsState>();
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<i32>) -> 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<i32>,
) -> 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<crate::BrowserEngine>) -> tauri::Result<()> {
#[cfg(target_os = "linux")]
if !appindicator_available() {
@@ -293,6 +327,15 @@ pub fn create_system_tray(app: &AppHandle<crate::BrowserEngine>) -> 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
);
}
@@ -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();
});
@@ -54,7 +54,11 @@ export function Desktop({ requestBack, requestClose }: DesktopProps) {
<SettingToggle
title="Close button keeps Sable running"
focusId="close-to-background-on-close"
description="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."
description={
type === 'macos'
? 'When enabled, closing the window keeps Sable running instead of exiting. Reopen it from the Dock.'
: '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.'
}
value={closeToBackgroundOnClose}
onChange={setCloseToBackgroundOnClose}
ariaLabel="close-to-background-on-close"
@@ -66,8 +70,8 @@ export function Desktop({ requestBack, requestClose }: DesktopProps) {
description={
trayFallback ? (
<Text as="span" style={{ color: color.Warning.Main }} size="T200">
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.
</Text>
) : (
'Show a system tray icon while Sable is running. Disable this if you want Sable to stay available without a tray icon.'