diff --git a/plan2code-app/Cargo.lock b/plan2code-app/Cargo.lock index a3867c4..607e720 100644 --- a/plan2code-app/Cargo.lock +++ b/plan2code-app/Cargo.lock @@ -2651,6 +2651,9 @@ version = "3.2.6" dependencies = [ "chrono", "libc", + "objc2", + "objc2-app-kit", + "objc2-foundation", "portable-pty", "reqwest", "serde", diff --git a/plan2code-app/src-tauri/Cargo.toml b/plan2code-app/src-tauri/Cargo.toml index ec49e42..3a0610c 100644 --- a/plan2code-app/src-tauri/Cargo.toml +++ b/plan2code-app/src-tauri/Cargo.toml @@ -28,6 +28,13 @@ reqwest = { version = "0.13", default-features = false, features = ["blocking", [target.'cfg(unix)'.dependencies] libc = "0.2" +[target.'cfg(target_os = "macos")'.dependencies] +# Already pulled in by tauri itself; named directly here only to set the Dock +# icon (`set_app_icon` in main.rs), which tauri does for dev builds alone. +objc2 = "0.6" +objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSImage", "NSResponder"] } +objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSData", "NSGeometry"] } + [target.'cfg(windows)'.dependencies] # Already pulled in by portable-pty itself; named directly here only for the # Job Object APIs it does not expose (ending a `cmd /c` npm shim's whole diff --git a/plan2code-app/src-tauri/src/main.rs b/plan2code-app/src-tauri/src/main.rs index 693061c..b82758d 100644 --- a/plan2code-app/src-tauri/src/main.rs +++ b/plan2code-app/src-tauri/src/main.rs @@ -766,6 +766,17 @@ fn confirm_quit(app: &tauri::AppHandle) { QuitCase::Waiting => MessageDialogKind::Info, QuitCase::Working => MessageDialogKind::Warning, }; + // A close button pressed on a window in the background leaves the app inactive, and the + // window's web view does not take the first click on an inactive window, so the question + // would need a click to activate the window before Quit or Stay is heard. A minimized window + // would show it to nobody. So the window comes forward, as the key window, before it asks. + #[cfg(target_os = "macos")] + if let Some(window) = app.get_webview_window("main") { + if window.is_minimized().unwrap_or(false) { + let _ = window.unminimize(); + } + let _ = window.set_focus(); + } // Off Windows the shell's own modal asks, with Stay first and focused, so Stay is the default // answer everywhere (the plugin's box makes Quit the default). The OS box is the fallback for a // page that cannot show it. Both wait off the event loop. @@ -2090,6 +2101,49 @@ fn build_mac_menu(app: &mut tauri::App) -> tauri::Result<()> { Ok(()) } +// macOS takes an app's Dock and Cmd+Tab icon from its `.app` bundle, but the app ships as a bare +// program (`tauri build --no-bundle`), so without this the Dock shows the generic "exec" icon. +// Tauri sets the icon this way in dev builds only; this does the same in every build. +#[cfg(target_os = "macos")] +const APP_ICON_ICNS: &[u8] = include_bytes!("../icons/icon.icns"); + +#[cfg(target_os = "macos")] +fn app_icon_image() -> Option> { + use objc2::AllocAnyThread; + let data = objc2_foundation::NSData::with_bytes(APP_ICON_ICNS); + objc2_app_kit::NSImage::initWithData(objc2_app_kit::NSImage::alloc(), &data) +} + +// Runs from `setup`, on the main thread AppKit requires; anywhere else it only logs. +#[cfg(target_os = "macos")] +fn set_app_icon() { + let Some(mtm) = objc2::MainThreadMarker::new() else { + eprintln!("plan2code: the Dock icon can only be set on the main thread"); + return; + }; + let Some(icon) = app_icon_image() else { + eprintln!("plan2code: the app icon could not be read, so the Dock keeps the default one"); + return; + }; + let app = objc2_app_kit::NSApplication::sharedApplication(mtm); + // SAFETY: called on the main thread (checked above) with a valid image, as AppKit requires. + unsafe { app.setApplicationIconImage(Some(&icon)) }; +} + +// A bare program the launcher starts from a terminal is not made the active app, so its window +// opened behind that terminal. Once, at start: later shows of the window leave the active app +// alone. +#[cfg(target_os = "macos")] +fn activate_app() { + let Some(mtm) = objc2::MainThreadMarker::new() else { + eprintln!("plan2code: the app can only be activated on the main thread"); + return; + }; + // `activate` needs macOS 14; this one works on every macOS the app runs on. + #[allow(deprecated)] + objc2_app_kit::NSApplication::sharedApplication(mtm).activateIgnoringOtherApps(true); +} + fn main() { let args = std::env::args().collect::>(); tauri::Builder::default() @@ -2234,6 +2288,10 @@ fn main() { } #[cfg(target_os = "macos")] build_mac_menu(app)?; + #[cfg(target_os = "macos")] + set_app_icon(); + #[cfg(target_os = "macos")] + activate_app(); // A plain `kill`, Ctrl+C or a closed terminal runs the quit cleanup, then exits, // instead of ending the app with its consoles still running. `shutdown`, not // `quit_app`: a signal never starts a pending update's restart. @@ -2525,6 +2583,37 @@ mod tests { assert_eq!(state.announced.take_all(), vec!["other"]); } + #[cfg(target_os = "macos")] + #[test] + fn the_dock_icon_is_the_apps_own_icon_set_and_macos_can_read_it() { + let icon = app_icon_image().expect("NSImage reads the bundled icon.icns"); + let size = icon.size(); + assert!( + size.width > 0.0 && size.height > 0.0, + "a drawable image, not an empty one: {size:?}" + ); + // The same artwork the bundle lists for macOS (`bundle.icon` in + // tauri.conf.json), not a copy: a renamed bundle icon fails here + // instead of leaving the Dock and the bundle apart. + let config: serde_json::Value = serde_json::from_slice( + &std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/tauri.conf.json")).unwrap(), + ) + .unwrap(); + assert!( + config["bundle"]["icon"] + .as_array() + .is_some_and(|icons| icons + .iter() + .any(|icon| icon.as_str() == Some("icons/icon.icns"))), + "the bundle lists icons/icon.icns: {}", + config["bundle"]["icon"] + ); + assert_eq!( + APP_ICON_ICNS, + std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/icons/icon.icns")).unwrap() + ); + } + #[test] fn a_page_from_the_console_store_is_followed() { let state = state_for("project-a");