Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

macOS: keep bundled Dock icon after eframe loads (storytold/photocraft#7253)

nrq6993-a11y4b88e94f9101
diff --git a/apps/photocraft/src/app_icon.rs b/apps/photocraft/src/app_icon.rs
index 54eea2d6b..142c47b9d 100644
--- a/apps/photocraft/src/app_icon.rs
+++ b/apps/photocraft/src/app_icon.rs
@@ -34,6 +34,17 @@ pub fn sets_runtime_icon(exe: Option<&std::path::Path>) -> bool {
!(cfg!(target_os = "macos") && exe.is_some_and(in_app_bundle))
}
+/// Value for [`egui::ViewportBuilder::with_icon`]: [`window_icon`] when [`sets_runtime_icon`]
+/// is true; otherwise [`egui::IconData::default`] so eframe does not substitute its own icon
+/// on the Dock after the window opens (#7253).
+pub fn viewport_icon(exe: Option<&std::path::Path>) -> egui::IconData {
+ if sets_runtime_icon(exe) {
+ window_icon()
+ } else {
+ egui::IconData::default()
+ }
+}
+
/// 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());
@@ -80,6 +91,19 @@ mod tests {
assert_eq!(sets_runtime_icon(Some(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft"))), !cfg!(target_os = "macos"));
}
+ /// #7253: omitting `with_icon` made eframe paint its default glyph on the Dock once the window loaded.
+ #[test]
+ fn viewport_icon_blocks_eframe_default_when_runtime_icon_is_skipped() {
+ use std::path::Path;
+ let bundled = Some(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft"));
+ if sets_runtime_icon(bundled) {
+ let icon = viewport_icon(bundled);
+ assert!(icon.width >= 256 && icon.width == icon.height);
+ } else {
+ assert_eq!(viewport_icon(bundled), egui::IconData::default());
+ }
+ }
+
#[test]
fn window_icon_decodes() {
let icon = window_icon();
diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs
index 05355e55b..f3bf62592 100644
--- a/apps/photocraft/src/main.rs
+++ b/apps/photocraft/src/main.rs
@@ -103,9 +103,7 @@ 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 };
+ let viewport = egui::ViewportBuilder::default().with_icon(app_icon::viewport_icon(std::env::current_exe().ok().as_deref()));
eframe::NativeOptions {
viewport: viewport
.with_app_id(APP_ID)