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
28 changes: 23 additions & 5 deletions bin/omarchy-theme-set
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ is_video_path() {
snapshot_background_path() {
local background="$1"
local name="$2"
local snapshot extension
local snapshot extension twin

[[ -f $background ]] || return
is_video_path "$background" && return
Expand All @@ -100,9 +100,27 @@ snapshot_background_path() {
extension=${background##*.}
snapshot="$BACKGROUND_TRANSITION_CACHE/$name-$$.$extension"
ln "$background" "$snapshot" 2>/dev/null || cp "$background" "$snapshot"

# A portrait twin rides along under the same name, where the shell looks
# for it on screens taller than wide. Only backgrounds folders have twins.
twin="${background%/*}/portrait/${background##*/}"
if [[ ${background%/*} == */backgrounds || ${background%/*} == */omarchy/backgrounds/* ]] && [[ -f $twin ]]; then
mkdir -p "$BACKGROUND_TRANSITION_CACHE/portrait"
ln "$twin" "$BACKGROUND_TRANSITION_CACHE/portrait/${snapshot##*/}" 2>/dev/null || cp "$twin" "$BACKGROUND_TRANSITION_CACHE/portrait/${snapshot##*/}"
fi

echo "$snapshot"
}

remove_background_snapshots() {
local snapshot

for snapshot in "$@"; do
[[ -n $snapshot ]] || continue
rm -f "$snapshot" "${snapshot%/*}/portrait/${snapshot##*/}"
done
}

snapshot_current_background() {
local current_background snapshot

Expand Down Expand Up @@ -247,7 +265,7 @@ set_theme_background() {
if [[ -n $PREPARED_BACKGROUND_SNAPSHOT && $PREPARED_BACKGROUND == "$new_background" ]]; then
new_background_snapshot="$PREPARED_BACKGROUND_SNAPSHOT"
else
[[ -z $PREPARED_BACKGROUND_SNAPSHOT ]] || rm -f "$PREPARED_BACKGROUND_SNAPSHOT"
remove_background_snapshots "$PREPARED_BACKGROUND_SNAPSHOT"
if [[ $BACKGROUND_TRANSITION_SNAPSHOTS == "true" ]]; then
new_background_snapshot=$(snapshot_background_path "$new_background" "next")
fi
Expand All @@ -256,16 +274,16 @@ set_theme_background() {
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") &
(sleep 3; remove_background_snapshots "$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") &
(sleep 3; remove_background_snapshots "$new_background_snapshot") &
else
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") &
(sleep 3; remove_background_snapshots "$OLD_BACKGROUND_SNAPSHOT") &
fi
fi

Expand Down
94 changes: 83 additions & 11 deletions shell/plugins/background/Background.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Item {
property var nativeSizes: ({})
property var sizeQueue: []
property bool finishingTransition: false
// A theme switch keeps the durable path but swaps the file and its portrait
// twin behind it, so what was probed for the previous theme is read again.
property bool refreshFinal: false
property int backgroundVersion: 0
property int reloadVersion: 0
property int revealStartedVersion: -1
Expand Down Expand Up @@ -115,6 +118,7 @@ Item {
requestNativeSize(fromPath || displayedBackground)
requestNativeSize(finalPath)
currentBackground = finalPath
refreshFinal = force
backgroundVersion += 1
failedBackground = ""
revealStartedVersion = -1
Expand All @@ -135,6 +139,7 @@ Item {
oldBackground = ""
incomingBackground = ""
preparedBackground = ""
if (force) forgetNativeSize(finalPath)
displayedBackground = finalPath
revealProgress = 1
return
Expand Down Expand Up @@ -227,11 +232,44 @@ Item {
}

function requestNativeSize(path) {
if (!path || isVideo(path) || nativeSizes[path] !== undefined || sizeQueue.indexOf(path) !== -1) return
if (!path || isVideo(path)) return
queueSizeProbe(path)
// The probe doubles as the existence check for a portrait twin.
queueSizeProbe(twinPath(path))
}

function queueSizeProbe(path) {
if (!path || nativeSizes[path] !== undefined || sizeQueue.indexOf(path) !== -1) return
sizeQueue = sizeQueue.concat([path])
probeNextSize()
}

// A theme may ship backgrounds/portrait/<same name> for screens taller than
// wide, as may ~/.config/omarchy/backgrounds/<theme>/. omarchy-theme-set
// snapshots the twin next to its plain file, so a theme switch finds it
// under background-transitions/portrait/ the same way.
function twinPath(path) {
var match = String(path || "").match(/^(.*\/(?:backgrounds|omarchy\/backgrounds\/[^/]+|background-transitions))\/([^/]+)$/)
return match && !isVideo(path) ? match[1] + "/portrait/" + match[2] : ""
}

function forgetNativeSize(path) {
var known = Object.assign({}, nativeSizes)
delete known[path]
delete known[twinPath(path)]
nativeSizes = known
requestNativeSize(path)
}

// A twin the probe found but Qt cannot decode gives way to the plain file,
// which recovers like any other background if it fails too.
function dropTwin(path) {
if (nativeSizes[path] === undefined) return
var known = Object.assign({}, nativeSizes)
known[path] = { width: 0, height: 0, found: false }
nativeSizes = known
}

function probeNextSize() {
if (sizeProbe.running || sizeQueue.length === 0) return
sizeProbe.path = sizeQueue[0]
Expand All @@ -244,6 +282,7 @@ Item {
function pruneNativeSizes() {
var kept = {}
var paths = [displayedBackground, incomingBackground, oldBackground, preparedBackground]
paths = paths.concat(paths.map(twinPath))
for (var i = 0; i < paths.length; i++) {
if (paths[i] && nativeSizes[paths[i]] !== undefined) kept[paths[i]] = nativeSizes[paths[i]]
}
Expand Down Expand Up @@ -279,8 +318,9 @@ Item {
var width = exitCode === 0 ? parseInt(parts[0], 10) : 0
var height = exitCode === 0 ? parseInt(parts[1], 10) : 0
var known = Object.assign({}, root.nativeSizes)
// An unreadable header records 0x0, which decodes at screen size.
known[path] = { width: width > 0 ? width : 0, height: height > 0 ? height : 0 }
// An unreadable header records 0x0, which decodes at screen size. A
// missing file is recorded as not found, so no screen uses it as a twin.
known[path] = { width: width > 0 ? width : 0, height: height > 0 ? height : 0, found: exitCode === 0 }
root.nativeSizes = known
root.sizeQueue = root.sizeQueue.filter(function(queued) { return queued !== sizeProbe.path })
root.probeNextSize()
Expand Down Expand Up @@ -375,6 +415,9 @@ Item {
easing.type: Easing.OutCubic
onFinished: {
if (root.incomingBackground) {
// Read the durable path again while the incoming frame still covers
// every screen, so no base frame changes file during the reveal.
if (root.refreshFinal) root.forgetNativeSize(root.currentBackground)
// After a video was chosen during the reveal, the incoming still
// stays until OWE takes the background.
root.displayedBackground = root.currentBackground && !root.isVideo(root.currentBackground)
Expand Down Expand Up @@ -436,6 +479,24 @@ Item {
readonly property int decodeWidth: sized ? Math.ceil(width * screen.devicePixelRatio) : 0
readonly property int decodeHeight: sized ? Math.ceil(height * screen.devicePixelRatio) : 0

// Screens taller than wide show a portrait twin once the probe has found
// it. Until the probe answers the frame waits, as it does for the size.
property bool portrait: screen.height > screen.width
readonly property string basePath: base.path

function oriented(path) {
var twin = portrait ? root.twinPath(path) : ""
if (!twin) return path
var probed = root.nativeSizes[twin]
return probed === undefined ? "" : probed.found ? twin : path
}

// A transition frame whose twin fails to decode drops the twin, so
// the frame binds the plain file instead of waiting on an error.
function frameFailed(frame, plain) {
if (frame.status === Image.Error && frame.framePath !== plain) root.dropTwin(frame.framePath)
}

function decodeSize(path) {
if (!sized || !path) return Qt.size(0, 0)
var native = root.nativeSizes[path]
Expand All @@ -462,20 +523,25 @@ Item {
BackgroundMedia {
id: base
anchors.fill: parent
path: root.displayedBackground
path: panel.oriented(root.displayedBackground)
version: root.reloadVersion
cached: true
constrainDecode: true
decodeSize: panel.decodeSize(root.displayedBackground)
onFailedChanged: if (failed) root.recoverBackground(path)
decodeSize: panel.decodeSize(base.path)
onFailedChanged: {
if (!failed) return
if (path !== root.displayedBackground) root.dropTwin(path)
else root.recoverBackground(path)
}
onReadyChanged: panel.readyFrames = 0
}

Image {
id: oldFrame
anchors.fill: parent
readonly property size decode: panel.decodeSize(root.oldBackground)
source: decode.width > 0 ? root.imageUrl(root.oldBackground) : ""
readonly property string framePath: panel.oriented(root.oldBackground)
readonly property size decode: panel.decodeSize(framePath)
source: decode.width > 0 ? root.imageUrl(framePath) : ""
sourceSize.width: decode.width
sourceSize.height: decode.height
fillMode: Image.PreserveAspectCrop
Expand All @@ -484,7 +550,10 @@ Item {
smooth: true
mipmap: true
visible: root.oldBackground !== "" && root.revealProgress < 1
onStatusChanged: panel.maybeStartReveal()
onStatusChanged: {
panel.frameFailed(oldFrame, root.oldBackground)
panel.maybeStartReveal()
}
}

Item {
Expand All @@ -507,7 +576,7 @@ Item {
anchors.fill: parent
// The same URL and size as a prepared frame keeps its decoded
// image, so a transition to it can reveal at once.
readonly property string framePath: root.incomingBackground || root.preparedBackground
readonly property string framePath: panel.oriented(root.incomingBackground || root.preparedBackground)
readonly property size decode: panel.decodeSize(framePath)
source: decode.width > 0 ? root.imageUrl(framePath) : ""
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
sourceSize.width: decode.width
Expand All @@ -517,7 +586,10 @@ Item {
cache: false
smooth: true
mipmap: true
onStatusChanged: panel.maybeStartReveal()
onStatusChanged: {
panel.frameFailed(incomingFrame, root.incomingBackground || root.preparedBackground)
panel.maybeStartReveal()
}
}
}

Expand Down
66 changes: 66 additions & 0 deletions test/shell.d/background-portrait-twins-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
Comment on lines +1 to +5

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 New suite breaks test rules

This adds a separate test suite even though background-pending-theme-test.sh already tests this plugin’s reveals and video-background-test.sh tests its snapshots. The repository requires extending an existing test file for the area rather than adding another *-test.sh. Move these checks into those existing suites before merging.

File Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


# A theme switch snapshots the portrait twin next to its plain snapshot, and
# removes both together.
test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT
source <(awk '/^(is_video_path|snapshot_background_path|remove_background_snapshots)\(\) \{/ { c=1 } c { print } c && /^}$/ { c=0 }' "$ROOT/bin/omarchy-theme-set")
BACKGROUND_TRANSITION_CACHE="$test_tmp/cache/background-transitions"
mkdir -p "$test_tmp/theme/backgrounds/portrait"
printf 'plain\n' >"$test_tmp/theme/backgrounds/1-moonrise.png"
printf 'twin\n' >"$test_tmp/theme/backgrounds/portrait/1-moonrise.png"
printf 'lonely\n' >"$test_tmp/theme/backgrounds/2-lonely.png"
mkdir -p "$test_tmp/config/omarchy/backgrounds/tokyo/portrait" "$test_tmp/Pictures/portrait"
printf 'user\n' >"$test_tmp/config/omarchy/backgrounds/tokyo/3-user.png"
printf 'user twin\n' >"$test_tmp/config/omarchy/backgrounds/tokyo/portrait/3-user.png"
printf 'photo\n' >"$test_tmp/Pictures/beach.png"
printf 'other photo\n' >"$test_tmp/Pictures/portrait/beach.png"
twin_snapshot=$(snapshot_background_path "$test_tmp/theme/backgrounds/1-moonrise.png" next)
lonely_snapshot=$(snapshot_background_path "$test_tmp/theme/backgrounds/2-lonely.png" previous)
[[ $(cat "$BACKGROUND_TRANSITION_CACHE/portrait/${twin_snapshot##*/}" 2>/dev/null) == twin ]] || fail "a theme switch snapshots the portrait twin next to its plain snapshot"
[[ ! -e $BACKGROUND_TRANSITION_CACHE/portrait/${lonely_snapshot##*/} ]] || fail "a background without a twin snapshots no twin"
user_snapshot=$(snapshot_background_path "$test_tmp/config/omarchy/backgrounds/tokyo/3-user.png" user)
[[ $(cat "$BACKGROUND_TRANSITION_CACHE/portrait/${user_snapshot##*/}" 2>/dev/null) == "user twin" ]] || fail "a user background snapshots its portrait twin"
photo_snapshot=$(snapshot_background_path "$test_tmp/Pictures/beach.png" photo)
[[ ! -e $BACKGROUND_TRANSITION_CACHE/portrait/${photo_snapshot##*/} ]] || fail "a picture outside a backgrounds folder snapshots no twin"
remove_background_snapshots "$twin_snapshot" "$lonely_snapshot" "$user_snapshot" "$photo_snapshot"
[[ -z $(find "$BACKGROUND_TRANSITION_CACHE" -type f) ]] || fail "removing snapshots removes their twins"
pass "theme switch snapshots and removes portrait twins with their backgrounds"

# The plugin itself, on the running compositor: the first screen is made
# portrait and shows the twin, the others keep the plain file.
require_compositor "background portrait twins"
require_command quickshell
require_command magick

stage=$(mktemp -d)
trap 'rm -rf "$test_tmp" "$stage"' EXIT
mkdir -p "$stage/home/.local/state/omarchy/current" "$stage/home/theme/backgrounds/portrait"
ln -s "$ROOT/shell/plugins/background" "$stage/background"
ln -s "$ROOT/shell/Commons" "$stage/Commons"
ln -s "$ROOT/shell/Ui" "$stage/Ui"
magick -size 256x128 xc:red "$stage/home/theme/backgrounds/1-red.png"
magick -size 128x256 xc:blue "$stage/home/theme/backgrounds/portrait/1-red.png"
magick -size 256x128 xc:green "$stage/home/theme/backgrounds/2-green.png"
magick -size 128x256 xc:yellow "$stage/home/theme/backgrounds/portrait/2-green.png"
magick -size 256x128 xc:magenta "$stage/home/theme/backgrounds/3-lonely.png"
# Passes the size probe (magick identify -ping) but fails to decode.
magick -size 256x128 xc:cyan "$stage/home/theme/backgrounds/4-broken.png"
magick -size 128x256 xc:cyan "$stage/home/theme/backgrounds/portrait/4-broken.png"
python3 -I -c 'import sys; p = sys.argv[1]; d = bytearray(open(p, "rb").read()); i = d.find(b"IDAT") + 8; d[i:-16] = bytes(b ^ 0x5a for b in d[i:-16]); open(p, "wb").write(d)' "$stage/home/theme/backgrounds/portrait/4-broken.png"
ln -s "$stage/home/theme" "$stage/home/.local/state/omarchy/current/theme"
ln -s "$stage/home/theme/backgrounds/1-red.png" "$stage/home/.local/state/omarchy/current/background"
cp "$SHELL_TEST_DIR/fixtures/background-portrait-twins/shell.qml" "$stage/shell.qml"
output=$(HOME="$stage/home" timeout 20 quickshell -p "$stage" --no-color 2>&1) || fail "portrait twin fixture exits cleanly" "$output"
[[ $output == *"RESULT pass"* && $output != *"RESULT fail"* ]] || fail "portrait screens show the twin and landscape screens the plain file" "$output"
if rg -q 'ReferenceError|TypeError|Unable to assign|Binding loop' <<<"$output"; then
fail "portrait twin fixture has no QML errors" "$output"
fi
pass "a portrait screen shows the twin the probe found, through boot and reveals"
pass "a background without a twin shows the plain file on every screen"
pass "a forced reload of the same path probes the twin again"
pass "a twin that fails to decode gives way to the plain file during a reveal"
Loading