From 1a650b73bc75a7b617d9e0c24d680eae47218f3d Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Mon, 5 May 2025 19:06:32 +1000 Subject: thumbnailer: better caching --- services/Thumbnailer.qml | 80 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 25 deletions(-) (limited to 'services') diff --git a/services/Thumbnailer.qml b/services/Thumbnailer.qml index 7f7a9a4..3bdcaf0 100644 --- a/services/Thumbnailer.qml +++ b/services/Thumbnailer.qml @@ -1,7 +1,9 @@ pragma Singleton +pragma ComponentBehavior: Bound import Quickshell import Quickshell.Io +import QtQuick import Qt.labs.platform Singleton { @@ -9,36 +11,64 @@ Singleton { readonly property string thumbDir: `${StandardPaths.standardLocations(StandardPaths.GenericCacheLocation)[0]}/caelestia/thumbnails`.slice(7) - function go(sha: string, path: string, width: int, height: int): string { - if (!sha || !path || !width || !height) - return ""; + function go(path: string, width: int, height: int): var { + return thumbComp.createObject(root, { + originalPath: path, + width: width, + height: height + }); + } - const thumbPath = `${thumbDir}/${sha}@${width}x${height}-exact.png`; + component Thumbnail: QtObject { + id: obj - thumbProc.path = path; - thumbProc.thumbPath = thumbPath; - thumbProc.width = width; - thumbProc.height = height; - thumbProc.startDetached(); + required property string originalPath + required property int width + required property int height - return thumbPath; - } + property string path - Process { - id: thumbProc + readonly property Process shaProc: Process { + running: true + command: ["sha1sum", obj.originalPath] + stdout: SplitParser { + onRead: data => { + const sha = data.split(" ")[0]; + obj.path = `${root.thumbDir}/${sha}@${obj.width}x${obj.height}-exact.png`; + obj.thumbProc.running = true; + } + } + } - property string path - property string thumbPath - property int width - property int height - - command: ["fish", "-c", ` -if ! test -f ${thumbPath} - set -l size (identify -ping -format '%w\n%h' ${path}) - if test $size[1] -gt ${width} -o $size[2] -gt ${height} - magick ${path} -thumbnail ${width}x${height}^ -background none -gravity center -extent ${width}x${height} -unsharp 0x.5 ${thumbPath} + readonly property Process thumbProc: Process { + command: ["fish", "-c", ` +if test -f ${obj.path} + exit 1 +else + set -l size (identify -ping -format '%w\n%h' ${obj.originalPath}) + if test $size[1] -gt ${obj.width} -o $size[2] -gt ${obj.height} + magick ${obj.originalPath} -${obj.width > 1024 || obj.height > 1024 ? "resize" : "thumbnail"} ${obj.width}x${obj.height}^ -background none -gravity center -extent ${obj.width}x${obj.height} -unsharp 0x.5 ${obj.path} + else + cp ${obj.originalPath} ${obj.path} end -end -`] +end`] + onExited: code => { + if (code === 0) { + const path = obj.path; + obj.path = ""; + obj.path = path; + } + } + } + + function reload(): void { + shaProc.running = true; + } + } + + Component { + id: thumbComp + + Thumbnail {} } } -- cgit v1.2.3-freya