From 6aa5c537483e9cc9cbd66ac28fef8800ba306f97 Mon Sep 17 00:00:00 2001 From: toonn Date: Mon, 21 Mar 2022 13:26:19 +0100 Subject: [PATCH] desktopToDarwinBundle: Fall back to scaling available Sometimes scalable icons or icons within the thresholds from the desired resolutions aren't available. In this case it's still nicer to end up with a blocky scaled icon rather than the generic default. --- .../setup-hooks/desktop-to-darwin-bundle.sh | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/setup-hooks/desktop-to-darwin-bundle.sh b/pkgs/build-support/setup-hooks/desktop-to-darwin-bundle.sh index 0519d183ba94..d54af90b6888 100644 --- a/pkgs/build-support/setup-hooks/desktop-to-darwin-bundle.sh +++ b/pkgs/build-support/setup-hooks/desktop-to-darwin-bundle.sh @@ -50,8 +50,10 @@ convertIconTheme() { else echo "threshold $icon" fi - return 0 + elif [[ -a $icon ]]; then + echo "fallback $icon" fi + return 0 done done echo "scalable" @@ -101,6 +103,17 @@ convertIconTheme() { scalableIcon=('-') fi + # Tri-state variable, NONE means no icons have been found, an empty + # icns file will be generated, not sure that's necessary because macOS + # will default to a generic icon if no icon can be found. + # + # OTHER means an appropriate icon was found. + # + # Any other value is a path to an icon file that isn't scalable or + # within the threshold. This is used as a fallback in case no better + # icon can be found and will be scaled as much as + # necessary to result in appropriate icon sizes. + local foundIcon=NONE for iconSize in "${iconSizes[@]}"; do for scale in "${!scales[@]}"; do local iconResult=$(findIcon $iconSize $scale) @@ -112,6 +125,7 @@ convertIconTheme() { fixed) local density=$((72 * scale))x$((72 * scale)) magick convert -density "$density" -units PixelsPerInch "$icon" "$result" + foundIcon=OTHER ;; threshold) # Synthesize an icon of the exact size if a scalable icon is available @@ -119,15 +133,32 @@ convertIconTheme() { if ! synthesizeIcon "${scalableIcon[0]}" "$result" "$iconSize" "$scale"; then resizeIcon "$icon" "$result" "$iconSize" "$scale" fi + foundIcon=OTHER ;; scalable) synthesizeIcon "${scalableIcon[0]}" "$result" "$iconSize" "$scale" || true + foundIcon=OTHER + ;; + fallback) + # Use the largest size available to scale to + # appropriate sizes. + if [[ $foundIcon != OTHER ]]; then + foundIcon=$icon + fi ;; *) ;; esac done done + if [[ $foundIcon != NONE && $foundIcon != OTHER ]]; then + # Ideally we'd only resize to whatever the closest sizes are, + # starting from whatever icon sizes are available. + for iconSize in 16 32 128 256 512; do + local result=${resultdir}/${iconSize}x${iconSize}.png + resizeIcon "$foundIcon" "$result" "$iconSize" 1 + done + fi echo "$resultdir" }