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() 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") root.nativePending = {request:request,result:result}; 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); } Image { visible:false asynchronous:true sourceSize:Qt.size(32,32) source:root.nativePending ? root.nativePending.result.url : "" onStatusChanged: { if(!root.nativePending)return; if(status===Image.Ready) {const pending=root.nativePending;root.nativePending=null;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 }); } } }