1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-08-01 19:18:28 -04:00

wallpaper: optimize caching and performance of wallpaper tab

This commit is contained in:
bbedward
2026-07-06 13:52:31 -04:00
parent cc0bec2682
commit 405ea708b3
6 changed files with 62 additions and 212 deletions
+13 -16
View File
@@ -8,11 +8,14 @@ Item {
property int maxCacheSize: 512
property int status: isAnimated ? animatedImg.status : staticImg.status
property int fillMode: Image.PreserveAspectCrop
// AnimatedImage decodes full-size on the GUI thread and is never cached;
// disable for thumbnail grids
property bool animate: true
property bool _fromCache: false
readonly property bool isRemoteUrl: imagePath.startsWith("http://") || imagePath.startsWith("https://")
readonly property bool isAnimated: {
if (!imagePath)
if (!animate || !imagePath)
return false;
const lower = imagePath.toLowerCase();
return lower.endsWith(".gif") || lower.endsWith(".webp");
@@ -75,7 +78,6 @@ Item {
if (!root._fromCache)
return;
root._fromCache = false;
CacheUtils.forgetCachedFile(root.cacheFileName);
source = root.encodedImagePath;
return;
case Image.Ready:
@@ -83,19 +85,16 @@ Item {
return;
if (!visible || width <= 0 || height <= 0 || !Window.window?.visible)
return;
Paths.mkdir(Paths.imagecache);
const grabPath = root.cachePath;
const grabName = root.cacheFileName;
grabToImage(res => {
if (res.saveToFile(grabPath))
CacheUtils.recordCachedFile(grabName);
res.saveToFile(grabPath);
});
return;
}
}
}
onImagePathChanged: {
function resolveSource() {
if (!imagePath) {
_fromCache = false;
staticImg.source = "";
@@ -113,14 +112,12 @@ Item {
staticImg.source = encodedImagePath;
return;
}
Paths.mkdir(Paths.imagecache);
// Read cache only when present; else load source and cache it on Ready
if (CacheUtils.hasCachedFile(cacheFileName)) {
_fromCache = true;
staticImg.source = cachePath;
} else {
_fromCache = false;
staticImg.source = encodedImagePath;
}
// Cache-first; a miss errors and falls back to encodedImagePath
_fromCache = true;
staticImg.source = cachePath;
}
onImagePathChanged: resolveSource()
// During creation onImagePathChanged fires before sibling properties (maxCacheSize) initialize
onCachePathChanged: resolveSource()
}
@@ -1,94 +0,0 @@
pragma ComponentBehavior: Bound
import QtQuick
import qs.Common
// Preload the CachingImage disk cache for a folder of wallpapers via ffmpegthumbnailer
// so the switcher grid renders instantly. No-op (graceful fallback) if the tool is absent.
Item {
id: root
visible: false
property var paths: []
property int cacheSize: 256
property bool autoStart: true
property int maxConcurrent: 3
property int _active: 0
property var _queue: []
property int _toolState: -1 // -1 unknown, 0 unavailable, 1 available
onPathsChanged: if (autoStart)
preload()
// Must match djb2Hash + cachePath in Widgets/CachingImage.qml.
function _hash(str) {
if (!str)
return "";
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) + hash) + str.charCodeAt(i);
hash = hash & 0x7FFFFFFF;
}
return hash.toString(16).padStart(8, '0');
}
function _cachePathFor(path) {
const hash = _hash(path);
if (!hash)
return "";
return `${Paths.stringify(Paths.imagecache)}/${hash}@${cacheSize}x${cacheSize}.png`;
}
function _isAnimated(path) {
const lower = path.toLowerCase();
return lower.endsWith(".gif") || lower.endsWith(".webp");
}
function preload() {
if (!paths || paths.length === 0 || _toolState === 0)
return;
if (_toolState === -1) {
Proc.runCommand("wallpaperThumbToolCheck", ["sh", "-c", "command -v ffmpegthumbnailer"], function (out, code) {
root._toolState = code === 0 ? 1 : 0;
if (root._toolState === 1)
root._start();
});
return;
}
_start();
}
function _start() {
Paths.mkdir(Paths.imagecache);
const q = [];
for (let i = 0; i < paths.length; i++) {
const p = paths[i];
if (!p || p.startsWith("#") || _isAnimated(p))
continue;
q.push(p);
}
_queue = q;
for (let i = 0; i < maxConcurrent; i++)
_pump();
}
function _pump() {
if (_queue.length === 0)
return;
const path = _queue.shift();
const cachePath = _cachePathFor(path);
if (!cachePath) {
_pump();
return;
}
_active++;
// One process per file: skip if already cached, otherwise generate.
const script = "test -f \"$1\" || ffmpegthumbnailer -i \"$2\" -o \"$1\" -s " + cacheSize;
Proc.runCommand(null, ["sh", "-c", script, "thumb", cachePath, path], function (out, code) {
root._active--;
root._pump();
}, 0, 20000);
}
}