Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(desktop): don't close service when closing settings (#4403)

Elijah Potter55eda0858669
diff --git a/harper-desktop/src-tauri/Cargo.lock b/harper-desktop/src-tauri/Cargo.lock
index 1bf478672..bd8d21ae3 100644
--- a/harper-desktop/src-tauri/Cargo.lock
+++ b/harper-desktop/src-tauri/Cargo.lock
@@ -3825,6 +3825,7 @@ dependencies = [
"harper-core",
"harper-dictionary-wordlist",
"is-macro",
+ "objc2 0.6.4",
"objc2-app-kit 0.3.2",
"objc2-foundation 0.3.2",
"pollster",
diff --git a/harper-desktop/src-tauri/Cargo.toml b/harper-desktop/src-tauri/Cargo.toml
index c1339df26..e4153d381 100644
--- a/harper-desktop/src-tauri/Cargo.toml
+++ b/harper-desktop/src-tauri/Cargo.toml
@@ -53,14 +53,18 @@ is-macro = "0.3.7"
accessibility = "0.2.0"
send_wrapper = "0.6.0"
core-foundation = "0.10.1"
+objc2 = "0.6.4"
objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"std",
"libc",
+ "NSApplication",
+ "NSImage",
"NSRunningApplication",
"NSWorkspace",
] }
objc2-foundation = { version = "0.3.2", default-features = false, features = [
"std",
+ "NSData",
"NSGeometry",
"objc2-core-foundation",
] }
diff --git a/harper-desktop/src-tauri/src/lib.rs b/harper-desktop/src-tauri/src/lib.rs
index de6f3d261..7dbe3f3b2 100644
--- a/harper-desktop/src-tauri/src/lib.rs
+++ b/harper-desktop/src-tauri/src/lib.rs
@@ -199,10 +199,33 @@ pub fn run_tauri() {
windows::show_settings_window(app.handle())?;
}
+ #[cfg(target_os = "macos")]
+ windows::sync_dock_visibility(app.handle(), None)?;
+
Ok(())
})
- .run(tauri::generate_context!())
- .expect("error while running tauri application");
+ .build(tauri::generate_context!())
+ .expect("error while building tauri application")
+ .run(|_app, event| {
+ #[cfg(target_os = "macos")]
+ if let tauri::RunEvent::WindowEvent {
+ label,
+ event: tauri::WindowEvent::Destroyed,
+ ..
+ } = &event
+ {
+ let _ = windows::sync_dock_visibility(_app, Some(label))
+ .inspect_err(|err| error!("Could not update Dock visibility: {err}"));
+ }
+
+ // Keep the tray and service alive after the last window closes, but allow Quit.
+ if let tauri::RunEvent::ExitRequested {
+ code: None, api, ..
+ } = event
+ {
+ api.prevent_exit();
+ }
+ });
}
/// Run as a highlighter process.
diff --git a/harper-desktop/src-tauri/src/windows.rs b/harper-desktop/src-tauri/src/windows.rs
index fb91d32e5..c40d866cf 100644
--- a/harper-desktop/src-tauri/src/windows.rs
+++ b/harper-desktop/src-tauri/src/windows.rs
@@ -1,42 +1,110 @@
//! Functions to manage the main windows involved in Harper Desktop
-use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder};
+use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
use tauri_plugin_opener::OpenerExt;
use tracing::error;
+/// Synchronize macOS Dock visibility with the application's open windows.
+///
+/// Minimized windows still count as open. Exclude a destroyed window explicitly because
+/// it may still be registered when its destruction event is delivered. Highlighter
+/// windows belong to a separate process and do not affect this policy.
+#[cfg(target_os = "macos")]
+pub fn sync_dock_visibility(app: &AppHandle, destroyed_label: Option<&str>) -> tauri::Result<()> {
+ let windows = app.webview_windows();
+ let policy = activation_policy_for_windows(windows.keys().map(String::as_str), destroyed_label);
+ let restore_icon = matches!(policy, tauri::ActivationPolicy::Regular);
+ app.set_activation_policy(policy)?;
+
+ if restore_icon {
+ let _ = app
+ .run_on_main_thread(restore_dock_icon)
+ .inspect_err(|err| error!("Could not schedule Dock icon restoration: {err}"));
+ }
+
+ Ok(())
+}
+
+/// Reapply the bundled icon after restoring the app's Dock presence.
+///
+/// Tauri assigns the development-mode icon only at startup; recreating the Dock
+/// entry can lose that image. Run this on the main thread after selecting Regular
+/// policy, loading the embedded icon rather than relying on cached native state.
+#[cfg(target_os = "macos")]
+fn restore_dock_icon() {
+ use objc2::MainThreadMarker;
+ use objc2_app_kit::{NSApplication, NSImage};
+ use objc2_foundation::NSData;
+
+ let Some(mtm) = MainThreadMarker::new() else {
+ error!("Dock icon restoration must run on the main thread");
+ return;
+ };
+ let data = NSData::with_bytes(include_bytes!("../icons/icon.icns"));
+ let Some(image) = NSImage::initWithData(mtm.alloc(), &data) else {
+ error!("Could not decode the bundled Dock icon");
+ return;
+ };
+
+ // SAFETY: The setter receives a non-nil image, and AppKit is accessed on the main thread.
+ unsafe { NSApplication::sharedApplication(mtm).setApplicationIconImage(Some(&image)) };
+}
+
+/// Select the activation policy from open window labels, ignoring a destroyed window.
+#[cfg(target_os = "macos")]
+fn activation_policy_for_windows<'a>(
+ mut labels: impl Iterator<Item = &'a str>,
+ destroyed_label: Option<&str>,
+) -> tauri::ActivationPolicy {
+ if labels.any(|label| Some(label) != destroyed_label) {
+ tauri::ActivationPolicy::Regular
+ } else {
+ tauri::ActivationPolicy::Accessory
+ }
+}
+
+/// Present a registered window after restoring its macOS Dock presence.
+///
+/// New windows are built hidden so activation policy changes precede showing and
+/// focusing them. Failed window creation therefore leaves the policy unchanged.
+fn show_window(window: WebviewWindow) -> tauri::Result<()> {
+ #[cfg(target_os = "macos")]
+ sync_dock_visibility(window.app_handle(), None)?;
+
+ window.show()?;
+ window.set_focus()
+}
+
/// Open the editor window, focusing it if it already exists.
pub fn show_editor_window(app: &tauri::AppHandle) -> tauri::Result<()> {
if let Some(window) = app.get_webview_window("editor") {
- window.show()?;
- window.set_focus()?;
- return Ok(());
+ return show_window(window);
}
let window = WebviewWindowBuilder::new(app, "editor", WebviewUrl::App("index.html".into()))
.title("Harper")
.inner_size(800.0, 600.0)
+ .visible(false)
.build()?;
- window.set_focus()?;
- Ok(())
+ show_window(window)
}
/// Open the settings window, focusing it if it already exists.
pub fn show_settings_window(app: &tauri::AppHandle) -> tauri::Result<()> {
if let Some(window) = app.get_webview_window("settings") {
- window.show()?;
- window.set_focus()?;
- return Ok(());
+ return show_window(window);
}
- WebviewWindowBuilder::new(app, "settings", WebviewUrl::App("index.html".into()))
+ let window = WebviewWindowBuilder::new(app, "settings", WebviewUrl::App("index.html".into()))
.title("Harper Settings")
.inner_size(920.0, 680.0)
.min_inner_size(780.0, 520.0)
.center()
+ .visible(false)
.build()?;
- Ok(())
+ show_window(window)
}
/// Open the browser to an issue report page.