Files
glassMxrph/quickshell/services/WallpaperService.qml
T
mxandClaude Opus 5.5 728a9d336c Apply native wallpapers to every output when they share a file
The decode check bound its source to the pending request, so a second
output using the same file never reloaded it and was silently skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 15:37:02 +02:00

169 lines
6.9 KiB
QML

pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
import "../core"
Singleton {
id: root
property var images: []
property var observed: ({})
property string backend: "none"
property string error: ""
property string notice: ""
// "all" applies to every connected output in turn; "primary" or an output name targets one.
property string selectedMonitor: "all"
property var queue: []
readonly property var output: ScreenService.resolve(selectedMonitor)
readonly property string monitor: output ? output.name : ""
readonly property var paletteOutput: ConfigService.ready ? ScreenService.resolve(ConfigService.data.wallpaper.paletteMonitor) : null
readonly property string palettePath: paletteOutput ? (observed[paletteOutput.name] || (ConfigService.ready ? ConfigService.data.wallpaper.byMonitor[paletteOutput.name] : "") || "") : ""
readonly property string currentPath: observed[monitor] || (ConfigService.ready ? ConfigService.data.wallpaper.byMonitor[monitor] : "") || ""
property var nativePending: null
readonly property bool busy: applyJob.running || nativePending !== null || queue.length > 0
readonly property bool scanning: scanJob.running
function request(action, target) {
const name = target || monitor;
return {
action: action,
monitor: name,
backend: ConfigService.data.wallpaper.backend,
command: ConfigService.data.wallpaper.command,
current: observed[name] || ConfigService.data.wallpaper.byMonitor[name] || ""
};
}
function refresh() {
if (ConfigService.ready && monitor)
probeJob.start(request("provider"));
}
function scan() {
if (ConfigService.ready)
scanJob.start({
action: "scan",
directories: ConfigService.data.wallpaper.directories
});
}
function changed(output, path) {
const next = Object.assign({}, observed);
next[output] = path;
observed = next;
}
function apply(path) {
const outputs = selectedMonitor === "all" ? Quickshell.screens.map(s => s.name) : [monitor];
if (!ConfigService.ready || !ConfigService.writable || !outputs.length || !outputs[0] || busy)
return false;
error = "";
queue = outputs.slice(1).map(name => ({
path: path,
monitor: name
}));
const req = request("apply", outputs[0]);
req.path = path;
return applyJob.start(req);
}
// Starts the next queued output once the previous one has fully applied.
function applyNext() {
if (!queue.length)
return;
const next = queue[0];
const req = request("apply", next.monitor);
req.path = next.path;
queue = queue.slice(1);
applyJob.start(req);
}
onMonitorChanged: refresh()
// The monitor resolves before the configuration loads, so also probe once the
// configured backend is known (startup) and whenever it changes.
readonly property string configuredBackend: ConfigService.ready ? ConfigService.data.wallpaper.backend : ""
onConfiguredBackendChanged: refresh()
ProviderJob {
id: probeJob
onFinished: (request, result) => {
if (!result.ok) {
root.error = result.error;
return;
}
root.backend = result.backend;
root.notice = result.notice || "";
if (result.current)
root.changed(request.monitor, ConfigService.data.wallpaper.providerPaths[request.monitor] === result.current ? (ConfigService.data.wallpaper.byMonitor[request.monitor] || result.current) : result.current);
}
}
ProviderJob {
id: scanJob
onFinished: (request, result) => {
if (result.ok) {
root.images = result.images;
root.notice = (result.notice || "") + (result.limited ? " Showing the first 1,000 images." : "");
} else
root.error = result.error;
}
}
ProviderJob {
id: applyJob
onFinished: (request, result) => {
if (!result.ok) {
root.error = result.error;
root.queue = [];
return;
}
if(result.backend === "native") {
// Reload even when the previous output used the same file, so every output gets its decode check.
root.nativePending = {request:request,result:result};
decodeCheck.source = "";
decodeCheck.source = result.url;
} else root.finishApply(request,result);
}
}
function finishApply(request,result) {
const next = Object.assign({}, ConfigService.data.wallpaper.byMonitor);
next[request.monitor] = result.path;
ConfigService.setValue("wallpaper.byMonitor", next);
const providerPaths = Object.assign({}, ConfigService.data.wallpaper.providerPaths);
providerPaths[request.monitor] = result.providerPath;
ConfigService.setValue("wallpaper.providerPaths", providerPaths);
root.backend = result.backend;
root.changed(request.monitor, result.path);
root.error = "";
ConfigService.setValue("wallpaper.history",[result.path].concat(ConfigService.data.wallpaper.history.filter(p=>p!==result.path)).slice(0,50));
Qt.callLater(root.applyNext);
}
// Confirms a native wallpaper decodes before it is saved; set imperatively by applyJob.
Image {
id: decodeCheck
visible:false
asynchronous:true
sourceSize:Qt.size(32,32)
onStatusChanged: {
if(!root.nativePending || source.toString()!==root.nativePending.result.url)return;
if(status===Image.Ready) {const pending=root.nativePending;root.nativePending=null;Qt.callLater(()=>root.finishApply(pending.request,pending.result));}
else if(status===Image.Error) {root.error="Could not decode this wallpaper";root.nativePending=null;root.queue=[];}
}
}
IpcHandler {
target: "wallpaper"
function picker(): void {ShellState.openWallpaperPicker();}
function changed(monitor: string, path: string): void {
root.changed(monitor, path);
}
function refresh(): void {
root.refresh();
root.scan();
}
function apply(path: string): bool {
return root.apply(path);
}
function status(): string {
return JSON.stringify({
backend: root.backend,
current: root.currentPath,
palettePath: root.palettePath,
busy: root.busy,
images: root.images.length,
error: root.error,
notice: root.notice
});
}
}
}