Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 69 additions & 24 deletions bin/omarchy-theme-set
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ OMARCHY_PACKAGED_THEMES_PATH="${OMARCHY_PACKAGED_THEMES_PATH:-/usr/share/omarchy
# theme file that is neither denied here nor recorded there as colour-only.
INSTALLED_THEME_DENIED=(alacritty.toml foot.ini ghostty.conf kitty.conf vscode.json)
IGNORED_THEME_FILES=()
PREPARED_INTRO_TOKEN=""

# Until the staged theme replaces the current one, a failure leaves the current
# theme as it was. Drop the staging, and cancel an intro cover prepared for it
# without applying a palette, before removing the snapshot that cover shows.
discard_theme_staging() {
rm -rf "$NEXT_THEME_PATH"
if [[ -n $PREPARED_INTRO_TOKEN ]]; then
shell_ipc shell cancelThemeIntro "$PREPARED_INTRO_TOKEN" || true
fi
[[ -z $OLD_BACKGROUND_SNAPSHOT ]] || rm -f "$OLD_BACKGROUND_SNAPSHOT"
[[ -z $PREPARED_BACKGROUND_SNAPSHOT ]] || rm -f "$PREPARED_BACKGROUND_SNAPSHOT"
}

fail() {
echo "omarchy-theme-set: $*" >&2
discard_theme_staging
exit 1
}

# A missing overlay is optional; a failed copy of an existing one is not.
# Keep the same non-hidden file selection as before, including user symlinks.
copy_theme_files() (
local source="$1"
local -a files

[[ -d $source ]] || return 0
[[ -r $source && -x $source ]] || return 1
shopt -s nullglob
files=("$source/"*)
if (( ${#files[@]} > 0 )); then
cp -r -- "${files[@]}" "$NEXT_THEME_PATH/"
fi
)

FAILED_THEME_COMMANDS=()

Expand Down Expand Up @@ -294,22 +328,26 @@ stage_installed_colors_from_alacritty() {
local scratch

if [[ -f $NEXT_THEME_PATH/colors.toml ]]; then
return
return 0
fi

if [[ ! -f $source/alacritty.toml || -L $source/alacritty.toml ]]; then
return
return 0
fi

scratch=$(mktemp -d)
cp "$source/alacritty.toml" "$scratch/alacritty.toml"
omarchy-theme-colors-from-alacritty "$scratch"

if [[ -f $scratch/colors.toml ]]; then
cp "$scratch/colors.toml" "$NEXT_THEME_PATH/colors.toml"
scratch=$(mktemp -d) || return 1
local status=0
if cp "$source/alacritty.toml" "$scratch/alacritty.toml" &&
omarchy-theme-colors-from-alacritty "$scratch"; then
if [[ -f $scratch/colors.toml ]]; then
cp "$scratch/colors.toml" "$NEXT_THEME_PATH/colors.toml" || status=1
fi
else
status=1
fi

rm -rf "$scratch"
return "$status"
}

# Copies a directory without ever following a symlink: in an installed theme one
Expand All @@ -320,16 +358,17 @@ stage_installed_dir() {
local dest="$2"
local entry name

mkdir -p "$dest"
[[ -r $source && -x $source ]] || return 1
mkdir -p "$dest" || return 1

for entry in "$source"/*; do
[[ -e $entry && ! -L $entry ]] || continue
name=${entry##*/}

if [[ -d $entry ]]; then
stage_installed_dir "$entry" "$dest/$name"
stage_installed_dir "$entry" "$dest/$name" || return 1
else
cp "$entry" "$dest/$name"
cp "$entry" "$dest/$name" || return 1
fi
done
}
Expand All @@ -350,6 +389,7 @@ stage_installed_theme() {
local entry name

[[ -d $source ]] || return 0
[[ -r $source && -x $source ]] || return 1

for entry in "$source"/*; do
[[ -e $entry ]] || continue
Expand All @@ -361,9 +401,9 @@ stage_installed_theme() {
*) IGNORED_THEME_FILES+=("$name") ;;
esac
elif [[ -d $entry ]]; then
stage_installed_dir "$entry" "$NEXT_THEME_PATH/$name"
stage_installed_dir "$entry" "$NEXT_THEME_PATH/$name" || return 1
else
cp "$entry" "$NEXT_THEME_PATH/$name"
cp "$entry" "$NEXT_THEME_PATH/$name" || return 1
fi
done

Expand Down Expand Up @@ -413,32 +453,33 @@ fi
PREVIOUS_THEME_NAME=$(cat "$CURRENT_THEME_NAME_PATH" 2>/dev/null || true)
remember_current_theme_background

# Setup clean next theme directory (for atomic theme config swapping)
rm -rf "$NEXT_THEME_PATH"
mkdir -p "$NEXT_THEME_PATH"
# Stage the whole theme before changing any active files, so a failure on the
# way leaves the current theme, its name and its files as they were.
rm -rf "$NEXT_THEME_PATH" || fail "could not clear theme staging"
mkdir -p "$NEXT_THEME_PATH" || fail "could not create theme staging"

# Copy official theme first, then overlay the user's theme on top
cp -r "$OMARCHY_THEMES_PATH/$THEME_NAME/"* "$NEXT_THEME_PATH/" 2>/dev/null
copy_theme_files "$OMARCHY_THEMES_PATH/$THEME_NAME" || fail "could not stage the built-in theme"
# The intros and loops ship from omarchy-themes-intros and omarchy-themes-loops
# into the installed tree, never the git checkout, so a dev-linked
# $OMARCHY_PATH takes them from there.
for videos in intros loops; do
packaged_videos="$OMARCHY_PACKAGED_THEMES_PATH/$THEME_NAME/backgrounds/$videos"
if [[ -d $packaged_videos && -d $NEXT_THEME_PATH/backgrounds && ! -d $NEXT_THEME_PATH/backgrounds/$videos ]]; then
cp -r "$packaged_videos" "$NEXT_THEME_PATH/backgrounds/"
cp -r "$packaged_videos" "$NEXT_THEME_PATH/backgrounds/" || fail "could not stage the packaged theme $videos"
fi
done

if theme_came_from_a_repo "$USER_THEMES_PATH/$THEME_NAME"; then
stage_installed_theme "$USER_THEMES_PATH/$THEME_NAME"
stage_installed_theme "$USER_THEMES_PATH/$THEME_NAME" || fail "could not stage the installed theme"
report_ignored_theme_files
else
cp -r "$USER_THEMES_PATH/$THEME_NAME/"* "$NEXT_THEME_PATH/" 2>/dev/null
copy_theme_files "$USER_THEMES_PATH/$THEME_NAME" || fail "could not stage the user theme"
fi

# Generate colors.toml from alacritty.toml if theme is missing colors.toml
if [[ ! -f $NEXT_THEME_PATH/colors.toml && -f $NEXT_THEME_PATH/alacritty.toml ]]; then
omarchy-theme-colors-from-alacritty "$NEXT_THEME_PATH"
omarchy-theme-colors-from-alacritty "$NEXT_THEME_PATH" || fail "could not generate the theme palette"
fi

CHOSEN_THEME_BACKGROUND=""
Expand Down Expand Up @@ -473,7 +514,11 @@ if [[ $THEME_HEADLESS != "1" && $OMARCHY_THEME_SKIP_BACKGROUND != "1" ]]; then
owe intro-prepare "$THEME_BACKGROUND_INTRO" >/dev/null 2>&1 9>&- &
INTRO_PREPARE_PID=$!
fi
shell_ipc shell prepareThemeIntro "$OLD_BACKGROUND_SNAPSHOT" "$(stat -Lc '%d:%i' "$NEXT_THEME_PATH")" "" "" || true
# Only a cover for now: the palette is not ready until the theme is
# published, and a failure cancels the cover by this token.
PREPARED_INTRO_TOKEN=$(stat -Lc '%d:%i' "$NEXT_THEME_PATH")
shell_ipc shell prepareThemeIntroCover "$OLD_BACKGROUND_SNAPSHOT" "$PREPARED_INTRO_TOKEN" ||
shell_ipc shell prepareThemeIntro "$OLD_BACKGROUND_SNAPSHOT" "$PREPARED_INTRO_TOKEN" "" "" || true
Comment on lines +520 to +521

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Failed switch leaves wallpaper frozen

If an update leaves the older shell running, this fallback prepares a cover through prepareThemeIntro, but that shell has no cancelThemeIntro. If rendering fails, cleanup ignores the failed cancellation and deletes the snapshot while the cover still uses it. The wallpaper can stay frozen until the shell’s ten-second fallback runs.

Keep the legacy path cancellable without applying a palette, or skip early cover preparation when cancellation is unavailable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the older-shell compatibility limit at e0ae4595, which Opus 5.5 High and muse-wonder via pi, thinking high both considered in their final review; GPT-6 in Codex (effort setting not exposed) checked it on a worker running the previous shell. The old shell has no token cancellation call. Its existing ten-second fallback releases the cover, and its themeColors || themeShell guard keeps the empty preparation payload from changing the palette. The failed render still preserves the active theme and its name.

Skipping preparation would restore the regression this fallback fixes: a successful switch could replace the still under a playing OWE intro before any cover is presented. Adding token cancellation to the new code cannot update a shell that is still running old code. I have kept the existing cover path and left the bounded freeze versus skipping that cover as a decision for the maintainer. The updated shell cancels immediately by token. No further code was changed for this finding.

elif background_transition_uses_snapshots "$CHOSEN_THEME_BACKGROUND"; then
OLD_BACKGROUND_SNAPSHOT=$(snapshot_current_background)

Expand All @@ -492,12 +537,12 @@ if [[ $THEME_HEADLESS != "1" && $OMARCHY_THEME_SKIP_BACKGROUND != "1" ]]; then
fi

# Generate dynamic configs
omarchy-theme-set-templates
omarchy-theme-set-templates || fail "could not render the theme"

# Replacing the theme's still can cancel an active OWE intro even before the
# background link changes. Present its cover before replacing either path.
if [[ $THEME_BACKGROUND_COVER == "true" ]]; then
wait_for_theme_intro loading themeIntroCoverStatus "$(stat -Lc '%d:%i' "$NEXT_THEME_PATH")"
wait_for_theme_intro loading themeIntroCoverStatus "$PREPARED_INTRO_TOKEN"
fi

# Swap next theme in as current
Expand Down
74 changes: 49 additions & 25 deletions bin/omarchy-theme-set-templates
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
# omarchy:summary=Generate themed config files from Omarchy templates
# omarchy:hidden=true

# Required reads and writes must fail the renderer, including those inside
# command substitutions. Optional palette helpers return success explicitly.
set -e -o pipefail
shopt -s inherit_errexit

TEMPLATES_DIR="$OMARCHY_PATH/default/themed"
USER_TEMPLATES_DIR="$HOME/.config/omarchy/themed"
NEXT_THEME_DIR="$HOME/.local/state/omarchy/current/next-theme"
Expand Down Expand Up @@ -47,7 +52,7 @@ resolve_theme_ref() {
local ref="$1"
local fallback="${2:-}"

if [[ -n ${THEME_COLORS[$ref]+_} ]]; then
if [[ -n $ref && -n ${THEME_COLORS[$ref]+_} ]]; then
REPLY="${THEME_COLORS[$ref]}"
elif [[ -n $fallback && -n ${THEME_COLORS[$fallback]+_} ]]; then
REPLY="${THEME_COLORS[$fallback]}"
Expand All @@ -60,7 +65,7 @@ resolve_theme_ref() {

resolve_gradient_color() {
trim "$1"
if [[ -n ${THEME_COLORS[$REPLY]+_} ]]; then
if [[ -n $REPLY && -n ${THEME_COLORS[$REPLY]+_} ]]; then
REPLY="${THEME_COLORS[$REPLY]}"
fi
}
Expand Down Expand Up @@ -97,9 +102,10 @@ color_to_shell_hex() {
elif [[ $color =~ ^[Rr][Gg][Bb][Aa]?\(([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?\)$ ]]; then
REPLY="#${BASH_REMATCH[1]}"
elif [[ $color =~ ^[Rr][Gg][Bb][Aa]?\(([0-9]+),([0-9]+),([0-9]+)(,[0-9.]+)?\)$ ]]; then
r=${BASH_REMATCH[1]}
g=${BASH_REMATCH[2]}
b=${BASH_REMATCH[3]}
# Decimal components may carry leading zeros, which bash reads as octal.
r=$((10#${BASH_REMATCH[1]}))
g=$((10#${BASH_REMATCH[2]}))
b=$((10#${BASH_REMATCH[3]}))
(( r > 255 )) && r=255
(( g > 255 )) && g=255
(( b > 255 )) && b=255
Expand Down Expand Up @@ -196,8 +202,8 @@ add_mix_value() {
start="${THEME_COLORS[$start_key]:-}"
end="${THEME_COLORS[$end_key]:-}"

[[ $start =~ ^#[0-9A-Fa-f]{6}$ && $end =~ ^#[0-9A-Fa-f]{6}$ ]] || return
[[ $fn == "mix" || $fn == "mix_strip" || $fn == "mix_rgb" ]] || return
[[ $start =~ ^#[0-9A-Fa-f]{6}$ && $end =~ ^#[0-9A-Fa-f]{6}$ ]] || return 0
[[ $fn == "mix" || $fn == "mix_strip" || $fn == "mix_rgb" ]] || return 0

printf 'mix\037%s\037%s\037%s\037%s\037%s\n' "$token" "$fn" "$start" "$end" "$amount" >>"$mix_table"
}
Expand Down Expand Up @@ -234,7 +240,10 @@ add_function_values() {
local token
local -A seen=()

(( ${#template_files[@]} > 0 )) || return
(( ${#template_files[@]} > 0 )) || return 0

# grep returns 1 for no tokens; read errors must stop activation.
grep -hEo "$MIX_TOKEN_PATTERN|$GRADIENT_TOKEN_PATTERN" "${template_files[@]}" >"$scratch/tokens" || [[ $? == 1 ]]

while IFS= read -r token; do
[[ -n ${seen[$token]:-} ]] && continue
Expand All @@ -245,7 +254,7 @@ add_function_values() {
else
add_gradient_function_value "$token"
fi
done < <(grep -hEo "$MIX_TOKEN_PATTERN|$GRADIENT_TOKEN_PATTERN" "${template_files[@]}" 2>/dev/null || true)
done <"$scratch/tokens"
}

strip_shell_section_header() {
Expand All @@ -268,16 +277,16 @@ apply_shell_section_override() {
local override="$1"
local section tmp body

[[ -f $NEXT_THEME_DIR/shell.toml ]] || return
[[ -f $override ]] || return
[[ -f $NEXT_THEME_DIR/shell.toml ]] || return 0
[[ -f $override ]] || return 0

section=$(basename "$override")
section="${section#shell.}"
section="${section%.toml}"
[[ $section =~ ^[A-Za-z0-9_-]+$ ]] || return
[[ $section =~ ^[A-Za-z0-9_-]+$ ]] || return 0

tmp=$(mktemp)
body=$(mktemp)
tmp="$scratch/shell.toml"
body="$scratch/section"

strip_shell_section_header "$section" "$override" >"$body"

Expand Down Expand Up @@ -343,7 +352,7 @@ render_templates() {
fi
done

(( ${#pairs[@]} > 0 )) || return
(( ${#pairs[@]} > 0 )) || return 0

awk -v value_table="$value_table" -v mix_table="$mix_table" '
function hex_value(char) {
Expand Down Expand Up @@ -402,13 +411,14 @@ render_templates() {
}

BEGIN {
while ((getline row < value_table) > 0) {
while ((status = getline row < value_table) > 0) {
sep = index(row, "\037")
values[substr(row, 1, sep - 1)] = substr(row, sep + 1)
}
if (status < 0) exit 1
close(value_table)

while ((getline row < mix_table) > 0) {
while ((status = getline row < mix_table) > 0) {
split(row, field, "\037")
value = mix_color(field[4], field[5], field[6])
if (field[3] == "mix_strip") {
Expand All @@ -418,43 +428,57 @@ render_templates() {
}
values[field[2]] = value
}
if (status < 0) exit 1
close(mix_table)

for (i = 1; i < ARGC; i += 2) {
template = ARGV[i]
output = ARGV[i + 1]
printf "" > output
# RT holds the terminator each line was read with, so a template that
# ends without a newline renders without one, as sed left it.
while ((getline line < template) > 0) printf "%s%s", render(line), RT > output
while ((status = getline line < template) > 0) printf "%s%s", render(line), RT > output
if (status < 0) exit 1
close(template)
close(output)
# A template with no lines still leaves its output file behind.
printf "" >> output
close(output)
if (close(output) != 0) exit 1
}
exit
}
' "${pairs[@]}"
}

scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
trap 'exit 1' HUP INT TERM

# Only generate dynamic templates for themes with a colors.toml definition
if [[ -f $COLORS_FILE ]]; then
value_table=$(mktemp)
mix_table=$(mktemp)
value_table="$scratch/values"
mix_table="$scratch/mixes"
: >"$value_table"
: >"$mix_table"
declare -A rendered=()

shopt -s nullglob
if [[ $ICONS_ONLY == "true" ]]; then
template_files=()
else
# A missing user directory is optional; an unreadable one is not empty.
for directory in "$USER_TEMPLATES_DIR" "$TEMPLATES_DIR"; do
if [[ -d $directory ]]; then
[[ -r $directory && -x $directory ]] || exit 1
fi
done
template_files=("$USER_TEMPLATES_DIR"/*.tpl "$TEMPLATES_DIR"/*.tpl)
fi

# Parsing and alias/fallback resolution (legacy colorN names, derived
# shades, mode/theme_type) is shared with every other colors.toml consumer.
resolved_colors=$(omarchy-theme-color --file "$COLORS_FILE" --all)
while IFS=$'\t' read -r key value; do
[[ -n $key ]] || continue
THEME_COLORS[$key]="$value"
done < <(omarchy-theme-color --file "$COLORS_FILE" --all)
done <<<"$resolved_colors"

# The app icons draw from roles derived from the palette, and only render
# when every role resolved.
Expand Down
2 changes: 2 additions & 0 deletions docs/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ A theme installed from a git repo is held to a much shorter list; see [What an i
5. Move the staging theme into `~/.local/state/omarchy/current/theme`, write
`~/.local/state/omarchy/current/theme.name`, and notify the running shell.

A failed copy, palette conversion or template render in steps 1-4 stops activation before step 5, so the current theme, its name and its background stay as they were. If the switch had already put up a background intro cover, the failure cancels that cover without applying a palette.

Template rendering only happens when the staged theme has `colors.toml`.
Existing files are never overwritten by a template, so a hand-written
`themes/<name>/shell.toml` or `hyprland.lua` wins over
Expand Down
Loading