Speed up changes

This commit is contained in:
David Heinemeier Hansson
2026-05-17 17:46:42 +02:00
parent 3887c53b41
commit c24b9b0e31
2 changed files with 128 additions and 119 deletions
+46 -18
View File
@@ -13,6 +13,7 @@ CURRENT_THEME_PATH="$HOME/.config/omarchy/current/theme"
NEXT_THEME_PATH="$HOME/.config/omarchy/current/next-theme"
CURRENT_BACKGROUND_LINK="$HOME/.config/omarchy/current/background"
BACKGROUND_TRANSITION_CACHE="$HOME/.cache/omarchy/background-transitions"
THEME_SET_LOCK="${XDG_RUNTIME_DIR:-/tmp}/omarchy-theme-set.lock"
USER_THEMES_PATH="$HOME/.config/omarchy/themes"
OMARCHY_THEMES_PATH="$OMARCHY_PATH/themes"
@@ -30,22 +31,34 @@ run_parallel() {
done
}
snapshot_current_background() {
local current_background snapshot extension
shell_ipc() {
timeout 2 omarchy-shell-ipc --if-running "$@" >/dev/null 2>&1
}
current_background=$(readlink -f "$CURRENT_BACKGROUND_LINK" 2>/dev/null || true)
[[ -f $current_background ]] || return
snapshot_background_path() {
local background="$1"
local name="$2"
local snapshot extension
[[ -f $background ]] || return
mkdir -p "$BACKGROUND_TRANSITION_CACHE"
extension=${current_background##*.}
snapshot="$BACKGROUND_TRANSITION_CACHE/previous-$$.$extension"
ln "$current_background" "$snapshot" 2>/dev/null || cp "$current_background" "$snapshot"
extension=${background##*.}
snapshot="$BACKGROUND_TRANSITION_CACHE/$name-$$.$extension"
ln "$background" "$snapshot" 2>/dev/null || cp "$background" "$snapshot"
echo "$snapshot"
}
snapshot_current_background() {
local current_background
current_background=$(readlink -f "$CURRENT_BACKGROUND_LINK" 2>/dev/null || true)
snapshot_background_path "$current_background" "previous"
}
set_theme_background() {
local backgrounds=()
local current_background index new_background next_index
local current_background index new_background next_index new_background_snapshot
mapfile -d '' -t backgrounds < <(
find -L "$HOME/.config/omarchy/backgrounds/$THEME_NAME/" "$CURRENT_THEME_PATH/backgrounds/" -maxdepth 1 -type f \
@@ -55,7 +68,7 @@ set_theme_background() {
if (( ${#backgrounds[@]} == 0 )); then
omarchy-notification-send "No background was found for theme" -t 2000
omarchy-shell-ipc-fast shell applyTheme "$colors_payload" "$shell_payload" >/dev/null 2>&1 || true
shell_ipc shell applyTheme "$colors_payload" "$shell_payload" || true
return
fi
@@ -75,16 +88,25 @@ set_theme_background() {
new_background="${backgrounds[$next_index]}"
fi
ln -nsf "$new_background" "$CURRENT_BACKGROUND_LINK"
new_background_snapshot=$(snapshot_background_path "$new_background" "next")
if [[ -f $OLD_BACKGROUND_SNAPSHOT ]]; then
omarchy-shell-ipc-fast background themeTransition "$OLD_BACKGROUND_SNAPSHOT" "$new_background" "$colors_payload" "$shell_payload" >/dev/null 2>&1 || \
omarchy-shell-ipc-fast shell applyTheme "$colors_payload" "$shell_payload" >/dev/null 2>&1 || true
(sleep 3; rm -f "$OLD_BACKGROUND_SNAPSHOT") &
if [[ -f $OLD_BACKGROUND_SNAPSHOT && -f $new_background_snapshot ]]; then
shell_ipc background themeTransition "$OLD_BACKGROUND_SNAPSHOT" "$new_background_snapshot" "$new_background" "$colors_payload" "$shell_payload" || \
shell_ipc shell applyTheme "$colors_payload" "$shell_payload" || true
(sleep 3; rm -f "$OLD_BACKGROUND_SNAPSHOT" "$new_background_snapshot") &
elif [[ -f $new_background_snapshot ]]; then
shell_ipc background themeTransition "" "$new_background_snapshot" "$new_background" "$colors_payload" "$shell_payload" || \
shell_ipc shell applyTheme "$colors_payload" "$shell_payload" || true
(sleep 3; rm -f "$new_background_snapshot") &
else
omarchy-shell-ipc-fast background themeTransition "" "$new_background" "$colors_payload" "$shell_payload" >/dev/null 2>&1 || \
omarchy-shell-ipc-fast shell applyTheme "$colors_payload" "$shell_payload" >/dev/null 2>&1 || true
shell_ipc background themeTransition "$OLD_BACKGROUND_SNAPSHOT" "$new_background" "$new_background" "$colors_payload" "$shell_payload" || \
shell_ipc shell applyTheme "$colors_payload" "$shell_payload" || true
if [[ -f $OLD_BACKGROUND_SNAPSHOT ]]; then
(sleep 3; rm -f "$OLD_BACKGROUND_SNAPSHOT") &
fi
fi
ln -nsf "$new_background" "$CURRENT_BACKGROUND_LINK"
}
THEME_NAME=$(echo "$1" | sed -E 's/<[^>]+>//g' | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
@@ -94,9 +116,15 @@ if [[ ! -d $OMARCHY_THEMES_PATH/$THEME_NAME ]] && [[ ! -d $USER_THEMES_PATH/$THE
exit 1
fi
# Serialize theme changes. Theme switching rebuilds a shared next-theme staging
# directory and updates current theme/background symlinks; concurrent calls can
# otherwise race and make one selection appear to be ignored.
exec 9>"$THEME_SET_LOCK"
flock 9
# Prevent file-watch reloads from changing shell colors before the background
# reveal is ready. The theme IPC below resumes reloads as it applies colors.
omarchy-shell-ipc --if-running shell suspendThemeReloads >/dev/null 2>&1 || true
shell_ipc shell suspendThemeReloads || true
# Setup clean next theme directory (for atomic theme config swapping)
rm -rf "$NEXT_THEME_PATH"
@@ -131,7 +159,7 @@ echo "$THEME_NAME" >"$HOME/.config/omarchy/current/theme.name"
colors_payload=$([[ -f $CURRENT_THEME_PATH/colors.toml ]] && base64 -w 0 "$CURRENT_THEME_PATH/colors.toml")
shell_payload=$([[ -f $CURRENT_THEME_PATH/shell.toml ]] && base64 -w 0 "$CURRENT_THEME_PATH/shell.toml")
if [[ $OMARCHY_THEME_SKIP_BACKGROUND == "1" ]]; then
omarchy-shell-ipc-fast shell applyTheme "$colors_payload" "$shell_payload" >/dev/null 2>&1 || true
shell_ipc shell applyTheme "$colors_payload" "$shell_payload" || true
else
set_theme_background
fi