diff --git a/bin/omarchy-agent-look-theme b/bin/omarchy-agent-look-theme index 69e95492c95b..49cbe7f6eee1 100755 --- a/bin/omarchy-agent-look-theme +++ b/bin/omarchy-agent-look-theme @@ -77,32 +77,48 @@ if mode == "dark" and luminance(bg) > 0.2: problems.append(f"mode is dark but background {bg} is light") bgdir = os.path.join(theme, "backgrounds") -images = sorted(f for f in os.listdir(bgdir) if not f.startswith("omarchy.") and os.path.isfile(os.path.join(bgdir, f))) if os.path.isdir(bgdir) else [] +images = sorted( + name for name in os.listdir(bgdir) + if name != "backgrounds.toml" and not name.startswith(("omarchy.", "omarchy@")) + and os.path.isfile(os.path.join(bgdir, name)) +) if os.path.isdir(bgdir) else [] if not images: - problems.append("no backgrounds (besides omarchy.*) in backgrounds/") + problems.append("no backgrounds (besides omarchy.*) in backgrounds/") + +render_directory = tempfile.TemporaryDirectory(prefix="look-backgrounds-") +preview_image = None for name in images: - p = os.path.join(bgdir, name) - try: - info = subprocess.run(["magick", p, "-format", "%w %h %[fx:mean] %[fx:standard_deviation]", "info:"], - capture_output=True, text=True, timeout=30).stdout.split() - w, h, mean, sd = int(info[0]), int(info[1]), float(info[2]), float(info[3]) - except Exception: - problems.append(f"backgrounds/{name} can't be read as an image") - continue - if w < 1920 or h < 1080: - problems.append(f"backgrounds/{name} is {w}x{h}, under 1920x1080, so it will look blurry") - if mode == "dark" and mean > 0.65: - problems.append(f"backgrounds/{name} is bright (mean {mean:.2f}) for a dark theme; light text on it will be hard to read") - if sd < 0.02: - problems.append(f"backgrounds/{name} is nearly flat (std dev {sd:.3f}); it looks empty") + image = os.path.abspath(os.path.join(bgdir, name)) + try: + # Resolve SVG image references relative to the background file. + if name.lower().endswith(".svg"): + rendered = os.path.join(render_directory.name, name + ".png") + subprocess.run(["rsvg-convert", "-o", rendered, image], capture_output=True, check=True, timeout=30) + image = rendered + info = subprocess.run( + ["magick", image, "-format", "%w %h %[fx:mean] %[fx:standard_deviation]", "info:"], + capture_output=True, text=True, check=True, timeout=30 + ).stdout.split() + w, h, mean, sd = int(info[0]), int(info[1]), float(info[2]), float(info[3]) + except (OSError, subprocess.SubprocessError, ValueError, IndexError): + problems.append(f"backgrounds/{name} can't be read as an image") + continue + if preview_image is None: + preview_image = image + if w < 1920 or h < 1080: + problems.append(f"backgrounds/{name} is {w}x{h}, under 1920x1080, so it will look blurry") + if mode == "dark" and mean > 0.65: + problems.append(f"backgrounds/{name} is bright (mean {mean:.2f}) for a dark theme; light text on it will be hard to read") + if sd < 0.02: + problems.append(f"backgrounds/{name} is nearly flat (std dev {sd:.3f}); it looks empty") W, H = 1280, 720 mono = subprocess.run(["fc-match", "-f", "%{file}", "monospace"], capture_output=True, text=True).stdout or "DejaVu-Sans-Mono" args = ["magick"] -if images: - args += [os.path.join(bgdir, images[0]), "-resize", f"{W}x{H}^", "-gravity", "center", "-extent", f"{W}x{H}", "-gravity", "NorthWest"] +if preview_image is not None: + args += [preview_image, "-resize", f"{W}x{H}^", "-gravity", "center", "-extent", f"{W}x{H}", "-gravity", "NorthWest"] else: - args += ["-size", f"{W}x{H}", f"xc:{c('darker_background', bg)}"] + args += ["-size", f"{W}x{H}", f"xc:{c('darker_background', bg)}"] draw = [] def rect(x0, y0, x1, y1, fill, stroke=None, width=0): diff --git a/bin/omarchy-bar-text-color b/bin/omarchy-bar-text-color index 4650469bb6c6..86aa4322f08d 100755 --- a/bin/omarchy-bar-text-color +++ b/bin/omarchy-bar-text-color @@ -11,9 +11,14 @@ text_color=${3:-} background_color=${4:-} background_path="" screen_size="" +scale="1" shift 4 2>/dev/null || true while (($# > 0)); do + if [[ $1 == "--background" || $1 == "--screen" || $1 == "--scale" ]] && (( $# < 2 )); then + printf '%s\n' "$text_color" + exit 0 + fi case "$1" in --background) background_path=${2:-} @@ -23,6 +28,10 @@ while (($# > 0)); do screen_size=${2:-} shift 2 ;; + --scale) + scale=${2:-} + shift 2 + ;; *) shift ;; @@ -72,17 +81,14 @@ valid_hex "$text_color" || fallback valid_hex "$background_color" || fallback [[ $position =~ ^(top|bottom|left|right)$ ]] || fallback [[ $bar_size =~ ^[0-9]+$ ]] || fallback -omarchy-cmd-present magick || fallback - -if [[ -z $background_path ]]; then - background_path=$(readlink -f "$HOME/.local/state/omarchy/current/background" 2>/dev/null || true) -fi -[[ -f $background_path ]] || fallback +[[ $scale =~ ^[0-9]+([.][0-9]+)?$ ]] || fallback +awk -v scale="$scale" 'BEGIN { exit !(scale > 0 && scale <= 10) }' || fallback if [[ -z $screen_size ]]; then - if omarchy-cmd-present hyprctl && omarchy-cmd-present jq; then - screen_size=$(hyprctl monitors -j 2>/dev/null | jq -r '.[0] | "\(.width)x\(.height)"' 2>/dev/null || true) - fi + # hyprctl reports the raw mode resolution; an odd transform (90/270 degree + # rotation) swaps the logical axes the shell renders with, so swap here too. + screen_size=$(hyprctl monitors -j 2>/dev/null | + jq -r '.[0] | if ((.transform // 0) % 2) == 1 then "\(.height)x\(.width)" else "\(.width)x\(.height)" end' 2>/dev/null || true) fi [[ $screen_size =~ ^([0-9]+)x([0-9]+)$ ]] || fallback @@ -90,9 +96,69 @@ screen_width=${BASH_REMATCH[1]} screen_height=${BASH_REMATCH[2]} ((screen_width > 0 && screen_height > 0 && bar_size > 0)) || fallback -# Only the bar strip's mean colour is needed, so sample on a canvas no wider than -# 1920 px; a 6K or 8K screen would otherwise add a full-size intermediate image. +# The resolver picks the per-screen variant and render metadata so the sampled +# pixels match what the shell actually paints on this screen. +sample_path="" +fill="crop" +backdrop="solid" +focal_x="0.5" +focal_y="0.5" +fill_color="$background_color" +resolver_args=(--screen "${screen_width}x${screen_height}" --fields) +[[ -n $background_path ]] && resolver_args+=(--canonical "$background_path") + +if resolved=$(omarchy-theme-bg-resolve "${resolver_args[@]}" 2>/dev/null); then + while IFS=$'\t' read -r key value; do + case "$key" in + path) + sample_path="$value" + ;; + fill) + [[ $value =~ ^(crop|fit|center|tile)$ ]] && fill="$value" + ;; + fill_color) + [[ $value =~ ^#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$ ]] && fill_color="$value" + ;; + backdrop) + [[ $value == "blur" ]] && backdrop="$value" + ;; + focal_x | focal_y) + if [[ $value =~ ^(0([.][0-9]+)?|1([.]0+)?)$ ]]; then + if [[ $key == "focal_x" ]]; then + focal_x="$value" + else + focal_y="$value" + fi + fi + ;; + esac + done <<<"$resolved" +fi + +# Resolver missing or failed: sample the canonical background as before. +if [[ ! -f $sample_path ]]; then + if [[ -z $background_path ]]; then + background_path=$(readlink -f "$HOME/.local/state/omarchy/current/background" 2>/dev/null || true) + fi + [[ -f $background_path && ${background_path,,} != *.svg ]] || fallback + sample_path="$background_path" + fill="crop" + backdrop="solid" + focal_x="0.5" + focal_y="0.5" +fi + +# Video wallpapers always use an aspect crop in BackgroundMedia. Sample their +# first frame with the same geometry, regardless of still-image metadata. +if [[ ${sample_path,,} =~ \.(mp4|m4v|mov|webm|mkv|avi)$ ]]; then + fill="crop" +fi + +# Resolve against the real display before bounding the sampling canvas. Native +# center/tile sizes shrink by the same ratio, preserving the displayed framing. +sample_scale=1 if ((screen_width > 1920)); then + sample_scale=$(awk -v width="$screen_width" 'BEGIN { print 1920 / width }') screen_height=$(((screen_height * 1920 + screen_width - 1) / screen_width)) bar_size=$(((bar_size * 1920 + screen_width - 1) / screen_width)) screen_width=1920 @@ -117,25 +183,48 @@ right) ;; esac -# Sample the first frame only. Without the selector a video background makes -# ImageMagick decode the whole file and emit one value per frame, and the match -# below then fails into the fallback colour. Let JPEG decode at about canvas size -# and box-scale to the canvas, so a 6K or 8K photo fits the limit without a -# second full-size resize buffer. Box scaling still averages every source pixel; -# point sampling would alias on fine stripes and pick the wrong text color. Do -# not let an oversized image spill its pixel cache into a memory map or /tmp (a -# tmpfs on Omarchy); fall back to the configured text color when the bounded -# sample cannot finish cheaply. +screen="${screen_width}x${screen_height}" +sample_format='%[fx:int(255*r)],%[fx:int(255*g)],%[fx:int(255*b)]' + +# Compose the same viewport as WallpaperImage before extracting the bar strip. +# Only the crop foreground uses focal offsets; the blurred cover stays centered. +cover=(-define jpeg:size="$screen" "$sample_path[0]" -auto-orient -scale "${screen}^") +composition=() +if [[ $fill != "crop" ]]; then + composition=(-size "$screen" "xc:$fill_color") + if [[ $backdrop == "blur" ]]; then + # Qt uses a subdued cover copy over the solid tint. Approximate its blur + # for contrast sampling; retain the same 35% opacity and full geometry. + blur_radius=$(awk -v ratio="$sample_scale" 'BEGIN { print 40 * ratio }') + composition+=(\( "${cover[@]}" -gravity center -extent "$screen" -blur "0x$blur_radius" -alpha set -channel A -evaluate multiply 0.35 +channel \) -compose Over -composite) + fi +fi + +native_percent=$(awk -v scale="$scale" -v ratio="$sample_scale" 'BEGIN { printf "%.6f%%", scale * ratio * 100 }') +case "$fill" in +fit) + composition+=(\( -define jpeg:size="$screen" "$sample_path[0]" -auto-orient -scale "$screen" \) -gravity center -compose Over -composite) + ;; +center) + composition+=(\( "$sample_path[0]" -auto-orient -resize "$native_percent" \) -gravity center -compose Over -composite) + ;; +tile) + # Resize the source tile to physical pixels before tiling on HiDPI screens. + composition+=(\( "$sample_path[0]" -auto-orient -resize "$native_percent" -write mpr:tile +delete -size "$screen" tile:mpr:tile \) -compose Over -composite) + ;; +*) + composition=("${cover[@]}" -gravity NorthWest + -crop "${screen}+%[fx:max(0,round((w-$screen_width)*$focal_x))]+%[fx:max(0,round((h-$screen_height)*$focal_y))]" +repage) + ;; +esac +# The transparent wallpaper layer is displayed over the compositor's black base. pixel=$(MAGICK_MEMORY_LIMIT=512MiB \ MAGICK_MAP_LIMIT=0 \ MAGICK_DISK_LIMIT=0 \ MAGICK_TIME_LIMIT=5 \ MAGICK_THREAD_LIMIT=2 \ - timeout --kill-after=1s 5s magick -define jpeg:size="${screen_width}x${screen_height}" "$background_path[0]" -auto-orient \ - -scale "${screen_width}x${screen_height}^" \ - -gravity center -extent "${screen_width}x${screen_height}" \ - -gravity NorthWest -crop "$crop" +repage \ - -resize '1x1!' -format '%[fx:int(255*r)],%[fx:int(255*g)],%[fx:int(255*b)]' info:- 2>/dev/null || true) + timeout --kill-after=1s 5s magick "${composition[@]}" -gravity NorthWest -crop "$crop" +repage \ + -background black -alpha remove -alpha off -resize '1x1!' -format "$sample_format" info:- 2>/dev/null || true) [[ $pixel =~ ^([0-9]+),([0-9]+),([0-9]+)$ ]] || fallback sample=$(printf '#%02x%02x%02x' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") diff --git a/bin/omarchy-menu-images b/bin/omarchy-menu-images index c6745f42c184..c737d254761e 100755 --- a/bin/omarchy-menu-images +++ b/bin/omarchy-menu-images @@ -117,8 +117,8 @@ cache_key=$(printf '%s' "$image_dirs_env" | md5sum | cut -d ' ' -f 1) rows_cache_file="$cache_dir/$cache_key.rows" rows_signature_file="$cache_dir/$cache_key.signature" rows_fast_signature_file="$cache_dir/$cache_key.fast-signature" -rows_signature="v4"$'\n' -rows_fast_signature="v4"$'\n' +rows_signature="v5"$'\n' +rows_fast_signature="v5"$'\n' rows_cacheable=true rows_cache_hit=false image_files=() @@ -142,7 +142,8 @@ else image_signature=$(stat -Lc '%s:%Y' "$image") || continue rows_signature+="$image:$image_signature"$'\n' done < <(find -L "$dir" -maxdepth 1 -type f \ - \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' \ + ! -name '*@*' \ + \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' -o -iname '*.svg' \ -o -iname '*.mp4' -o -iname '*.m4v' -o -iname '*.mov' -o -iname '*.webm' -o -iname '*.mkv' -o -iname '*.avi' \) \ -print0 2>/dev/null | sort -z) fi @@ -184,7 +185,7 @@ generate_thumbnail() { # stalled file must not hold it shut. A failed run drops the row. thumbnail_command=(timeout -k 5 10 ffmpegthumbnailer -i "$image" -o "$tmp" -s 1536 -q 8) else - thumbnail_command=(env VIPS_CONCURRENCY=1 vipsthumbnail "$image" --size 1536x864 --smartcrop=centre --path "$tmp[Q=82,strip]") + thumbnail_command=(env VIPS_CONCURRENCY=1 timeout --kill-after=2 30 vipsthumbnail "$image" --size 1536x864 --smartcrop=centre --path "$tmp[Q=82,strip]") fi # Close the lock fd for the converter: an orphaned or hung child must not diff --git a/bin/omarchy-theme-bg-boot-intro b/bin/omarchy-theme-bg-boot-intro index 9466b0449baf..122c9b70ed68 100755 --- a/bin/omarchy-theme-bg-boot-intro +++ b/bin/omarchy-theme-bg-boot-intro @@ -21,7 +21,7 @@ resolve_intro() { background=$(readlink -f "${1:-$current_background_link}" 2>/dev/null) || return 1 [[ -f $background ]] || return 1 case ${background,,} in - *.jpg | *.jpeg | *.png | *.bmp | *.webp) ;; + *.jpg | *.jpeg | *.png | *.bmp | *.webp | *.svg) ;; *) return 1 ;; esac @@ -59,7 +59,7 @@ prepare_theme_intro() { else mapfile -d '' -t names < <( find -L "$OMARCHY_PATH/themes/$theme/backgrounds" "$HOME/.config/omarchy/themes/$theme/backgrounds" -maxdepth 1 -type f \ - \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.bmp' -o -iname '*.webp' \) -printf '%f\0' 2>/dev/null | sort -zu + \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.bmp' -o -iname '*.webp' -o -iname '*.svg' \) ! -name '*@*' -printf '%f\0' 2>/dev/null | sort -zu ) (( ${#names[@]} > 0 )) || return 0 name=${names[0]} diff --git a/bin/omarchy-theme-bg-current b/bin/omarchy-theme-bg-current index a88bfe3bc61d..5f8efa59e659 100755 --- a/bin/omarchy-theme-bg-current +++ b/bin/omarchy-theme-bg-current @@ -6,7 +6,7 @@ BACKGROUND_PATH=$(readlink -f "$HOME/.local/state/omarchy/current/background" 2>/dev/null) if [[ -n $BACKGROUND_PATH ]]; then - basename -- "$BACKGROUND_PATH" | perl -pe 's/\.[^.\n]+$//; s/^\d+-//; s/-/ /g; s/\b(\w)/\U$1/g' + basename -- "$BACKGROUND_PATH" | perl -pe 's/\.[^.\n]+$//; s/@[^@]+$//; s/^\d+-//; s/-/ /g; s/\b(\w)/\U$1/g' else echo "Unknown" fi diff --git a/bin/omarchy-theme-bg-next b/bin/omarchy-theme-bg-next index 8984403a56b1..38cadcd2f6b8 100755 --- a/bin/omarchy-theme-bg-next +++ b/bin/omarchy-theme-bg-next @@ -10,7 +10,8 @@ CURRENT_BACKGROUND_LINK="$HOME/.local/state/omarchy/current/background" mapfile -d '' -t BACKGROUNDS < <( find -L "$USER_BACKGROUNDS_PATH" "$THEME_BACKGROUNDS_PATH" -maxdepth 1 -type f \ - \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' \ + ! -name '*@*' \ + \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' -o -iname '*.svg' \ -o -iname '*.mp4' -o -iname '*.m4v' -o -iname '*.mov' -o -iname '*.webm' -o -iname '*.mkv' -o -iname '*.avi' \) \ -print0 2>/dev/null | sort -z ) diff --git a/bin/omarchy-theme-bg-resolve b/bin/omarchy-theme-bg-resolve new file mode 100755 index 000000000000..e9490fac5922 --- /dev/null +++ b/bin/omarchy-theme-bg-resolve @@ -0,0 +1,546 @@ +#!/bin/bash + +# omarchy:summary=Resolve the background variant and render metadata for a screen +# omarchy:args=[--screen x] [--canonical ] [--fields] [--canonical-only] [--metadata-only] +# omarchy:hidden=true + +# The single implementation of background resolution, shared by the CLI and the +# shell: picks the aspect-ratio variant closest to the screen, reads render +# metadata from backgrounds.toml beside the image, and rasterizes SVG +# backgrounds to a cached PNG sized for the screen. Pure read plus cache write; +# the current-background symlink is never touched here. + +SCREEN="" +SCREEN_W=0 +SCREEN_H=0 +CANONICAL="" +OUTPUT="json" +CANONICAL_ONLY=0 +METADATA_ONLY=0 +MAX_SVG_RENDER_DIMENSION=16384 +MAX_SVG_RENDER_PIXELS=33554432 + +usage() { + echo "Usage: omarchy-theme-bg-resolve [--screen x] [--canonical ] [--fields] [--canonical-only] [--metadata-only]" +} + +while (( $# > 0 )); do + case "$1" in + --screen) + if (( $# < 2 )); then + usage >&2 + exit 1 + fi + + SCREEN="$2" + shift 2 + ;; + --canonical) + if (( $# < 2 )); then + usage >&2 + exit 1 + fi + + CANONICAL="$2" + shift 2 + ;; + --canonical-only) + CANONICAL_ONLY=1 + shift + ;; + --metadata-only) + METADATA_ONLY=1 + CANONICAL_ONLY=1 + shift + ;; + --fields) + OUTPUT="fields" + shift + ;; + -h | --help) + usage + exit 0 + ;; + *) + usage >&2 + exit 1 + ;; + esac +done + +if [[ -n $SCREEN ]]; then + if [[ $SCREEN =~ ^([1-9][0-9]*)x([1-9][0-9]*)$ ]]; then + SCREEN_W=${BASH_REMATCH[1]} + SCREEN_H=${BASH_REMATCH[2]} + else + usage >&2 + exit 1 + fi +fi + +[[ -n $CANONICAL ]] || CANONICAL="$HOME/.local/state/omarchy/current/background" +CANONICAL=$(readlink -f -- "$CANONICAL" 2>/dev/null) || CANONICAL="" +if [[ -z $CANONICAL || ! -f $CANONICAL ]]; then + exit 1 +fi + +BG_DIR=$(dirname "$CANONICAL") +CANONICAL_NAME=$(basename "$CANONICAL") +STEM="${CANONICAL_NAME%.*}" + +probe_dimensions() { + timeout --kill-after=2 10 magick identify -ping -format '%w %h' "${1}[0]" 2>/dev/null +} + +# Candidates are the canonical file plus its aspect-ratio variants: sibling +# regular files named @