PhotoCraft: An open-source, photo editor written by scratch in pure Rust
macOS: a bundled app keeps its own Dock icon (#5849) (#5879)
Michael Higgins8e2c5ddb40ff
diff --git a/apps/photocraft/src/app_icon.rs b/apps/photocraft/src/app_icon.rsindex 90ce94ea0..54eea2d6b 100644--- a/apps/photocraft/src/app_icon.rs+++ b/apps/photocraft/src/app_icon.rs@@ -26,6 +26,25 @@ pub fn window_icon() -> egui::IconData {}}+/// Whether the window gets [`window_icon`] at runtime: everywhere except inside a macOS app+/// bundle. There the Dock shows the bundle's `PhotoCraft.icns` (`CFBundleIconFile`), which macOS+/// renders in its own style (Liquid Glass and its tinted and dark variants); a runtime PNG+/// replaced it with a flat picture (#5849). `exe` is the running executable.+pub fn sets_runtime_icon(exe: Option<&std::path::Path>) -> bool {+ !(cfg!(target_os = "macos") && exe.is_some_and(in_app_bundle))+}++/// Whether `exe` is an app bundle's executable: `…/Name.app/Contents/MacOS/binary`.+fn in_app_bundle(exe: &std::path::Path) -> bool {+ let name = |p: Option<&std::path::Path>| p.and_then(|p| p.file_name()).map(|n| n.to_string_lossy().into_owned());+ let macos = exe.parent();+ let contents = macos.and_then(std::path::Path::parent);+ let bundle = contents.and_then(std::path::Path::parent);+ name(macos).as_deref() == Some("MacOS")+ && name(contents).as_deref() == Some("Contents")+ && name(bundle).is_some_and(|n| n.len() > 4 && n.to_ascii_lowercase().ends_with(".app"))+}+#[cfg(target_os = "macos")]const PNG: &[u8] = include_bytes!("../../../assets/app-icon/photocraft-1024.png");#[cfg(not(target_os = "macos"))]@@ -45,6 +64,22 @@ mod tests {u32::from_le_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) as usize}+ /// #5849: a macOS app bundle keeps its .icns in the Dock; unbundled runs (cargo run, the+ /// Linux and Windows builds) still set the runtime icon.+ #[test]+ fn only_a_macos_app_bundle_skips_the_runtime_icon() {+ use std::path::Path;+ assert!(in_app_bundle(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft")));+ assert!(in_app_bundle(Path::new("/Users/a/Downloads/PhotoCraft Beta.APP/Contents/MacOS/photocraft")));+ assert!(!in_app_bundle(Path::new("/Users/a/src/photocraft/target/release/photocraft")));+ assert!(!in_app_bundle(Path::new("/opt/Contents/MacOS/photocraft")), "not inside a .app");+ assert!(!in_app_bundle(Path::new("/.app/Contents/MacOS/photocraft")), "a bare `.app` is no bundle name");+ assert!(!in_app_bundle(Path::new("photocraft")));+ assert!(sets_runtime_icon(None));+ assert!(sets_runtime_icon(Some(Path::new("/usr/bin/photocraft"))));+ assert_eq!(sets_runtime_icon(Some(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft"))), !cfg!(target_os = "macos"));+ }+#[test]fn window_icon_decodes() {let icon = window_icon();diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rsindex 173956815..de1155edb 100644--- a/apps/photocraft/src/main.rs+++ b/apps/photocraft/src/main.rs@@ -78,9 +78,11 @@ fn custom_titlebar(prefs_file: Option<&std::path::Path>) -> bool {/// different offset every launch (#419). A later launch restores the saved position instead/// (`main` turns `centered` off, #4826). Wayland compositors place windows themselves.fn native_options(custom_titlebar: bool) -> eframe::NativeOptions {+ let viewport = egui::ViewportBuilder::default();+ // Not inside a macOS app bundle: its Dock icon is the bundle's .icns (`app_icon`, #5849).+ let viewport = if app_icon::sets_runtime_icon(std::env::current_exe().ok().as_deref()) { viewport.with_icon(app_icon::window_icon()) } else { viewport };eframe::NativeOptions {- viewport: egui::ViewportBuilder::default()- .with_icon(app_icon::window_icon())+ viewport: viewport.with_app_id(APP_ID).with_title("PhotoCraft").with_inner_size([1440.0, 900.0])