Files
Ilija Chrchev 1d28fc6439 Refresh user icon cache after AppImage desktop integration (#1877) (#2115)
On Fedora (and other GTK desktops), the hicolor icon cache is not
rebuilt after the AppImage copies its icons into $XDG_DATA_HOME.
The desktop keeps showing a generic icon until the user logs out
or manually runs gtk-update-icon-cache.

Add a conditional cache refresh to the AppRun integration step:
- Track whether any icon file was actually copied (changed).
- Only refresh if the cache file (icon-theme.cache) already exists,
  so we never create one where the desktop hasn't built one yet.
- Skip silently when gtk-update-icon-cache is not installed.

The fix lives in the AppImage's own AppRun rather than in the
launcher because the launcher (appimagelauncher) is a separate
project with its own release cycle, and many users run AppImages
directly without it. Both fixes are complementary.

Tested on Fedora 41 (GNOME 47, Wayland) by launching from the app
grid after clearing the icon cache. The correct icon appeared on
the first launch without a logout.

Closes #1877
2026-11-19 20:02:02 -07:00

97 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
# Craft Anything AppImage entry point (packaging/linux/AppRun; installed by package.sh).
#
# On Wayland the compositor resolves a window's dock icon by taking the window's
# app_id (ai.storyteller.craft-anything) and looking up an installed .desktop file.
# An AppImage installs nothing, so every AppImage run would otherwise show the
# generic "Wayland" icon. Best-effort desktop integration: on first run
# (and whenever the AppImage's path changes) install the .desktop entry and the
# hicolor icons into $XDG_DATA_HOME, then launch. Every failure here must leave
# the launch untouched. Opt out with CRAFT_ANYTHING_NO_DESKTOP_INTEGRATION=1.
set -u
APPDIR="$(dirname "$(readlink -f "$0")")"
DESK_ID=ai.storyteller.craft-anything
integrate() {
case "${CRAFT_ANYTHING_NO_DESKTOP_INTEGRATION:-}" in
1 | true | yes) return 0 ;;
esac
# The AppImage runtime exports $APPIMAGE; without it (an extracted AppDir) there
# is no path to register and nothing to do.
appimage="${APPIMAGE:-}"
[ -n "$appimage" ] && [ -f "$appimage" ] || return 0
appimage="$(readlink -f "$appimage")" || return 0
# Exec= is parsed by the desktop: a path holding a quote, backquote, dollar, backslash,
# percent or a line break would need escaping (or could add lines), so skip integration.
case "$appimage" in
*[\"\`\$\\%]* | *'
'*) return 0 ;;
esac
home="${HOME:-}"
[ -n "$home" ] || return 0
data="${XDG_DATA_HOME:-$home/.local/share}"
src_desktop="$APPDIR/usr/share/applications/$DESK_ID.desktop"
[ -f "$src_desktop" ] || return 0
# Rewrite Exec to this AppImage (the binary is not on the host PATH) and drop
# TryExec, which would hide the entry; everything else is shipped as packaged.
apps="$data/applications"
mkdir -p "$apps" 2>/dev/null || return 0
tmp="$apps/.$DESK_ID.tmp.$$"
esc="$(printf '%s' "$appimage" | sed 's/[\\&|]/\\&/g')"
if sed -e "s|^Exec=.*|Exec=\"$esc\" %F|" -e '/^TryExec=/d' "$src_desktop" 2>/dev/null >"$tmp"; then
if cmp -s "$tmp" "$apps/$DESK_ID.desktop"; then
rm -f "$tmp"
else
if mv -f "$tmp" "$apps/$DESK_ID.desktop" 2>/dev/null; then
command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1
else
rm -f "$tmp"
fi
fi
else
rm -f "$tmp"
return 0
fi
# Icons: per-file compare so an unchanged install costs a handful of cmp calls.
# Track whether any icon was actually copied so the cache is refreshed only when
# something changed (#1877).
src_icons="$APPDIR/usr/share/icons/hicolor"
[ -d "$src_icons" ] || return 0
icons="$data/icons/hicolor"
icons_changed=false
while IFS= read -r rel; do
mkdir -p "$icons/$(dirname "$rel")" 2>/dev/null || continue
if ! cmp -s "$src_icons/$rel" "$icons/$rel"; then
cp "$src_icons/$rel" "$icons/$rel" 2>/dev/null && icons_changed=true
fi
done <<EOF
$(cd "$src_icons" && find . -type f)
EOF
# Refresh the icon cache only when an icon actually changed and the desktop
# has already built a cache (don't create one where there is none).
if $icons_changed && [ -f "$icons/icon-theme.cache" ]; then
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
gtk-update-icon-cache -q "$icons" >/dev/null 2>&1 || true
fi
fi
}
# The command-line tool ships beside the app but nothing puts it on PATH, and the launcher renames
# the AppImage on every update (#723). `<AppImage> --cli …` runs it, and so does a symlink to the
# AppImage named `craft-anything-cli` (the runtime passes the name it was started by as $ARGV0).
# It writes nothing to the desktop: a CLI or MCP run isn't a launch of the app.
case "${ARGV0:-}" in
*craft-anything-cli) exec "$APPDIR/usr/bin/craft-anything-cli" "$@" ;;
esac
if [ "${1:-}" = "--cli" ]; then
shift
exec "$APPDIR/usr/bin/craft-anything-cli" "$@"
fi
integrate
exec "$APPDIR/usr/bin/craft-anything" "$@"