From 4f60c07e0540f89654b469d134095c37e238d3e8 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 27 Aug 2025 20:32:51 +1000 Subject: plugin: create caching image manager No need for external proc --- components/filedialog/FolderContents.qml | 1 - components/images/CachingIconImage.qml | 45 ++++++++++++++++++++------------ components/images/CachingImage.qml | 37 ++++++++------------------ 3 files changed, 39 insertions(+), 44 deletions(-) (limited to 'components') diff --git a/components/filedialog/FolderContents.qml b/components/filedialog/FolderContents.qml index 1f67cd3..afd2ed1 100644 --- a/components/filedialog/FolderContents.qml +++ b/components/filedialog/FolderContents.qml @@ -148,7 +148,6 @@ Item { anchors.top: parent.top anchors.topMargin: Appearance.padding.normal - asynchronous: true implicitSize: Sizes.itemWidth - Appearance.padding.normal * 2 source: { if (!item.fileIsDir) diff --git a/components/images/CachingIconImage.qml b/components/images/CachingIconImage.qml index 522a947..715d379 100644 --- a/components/images/CachingIconImage.qml +++ b/components/images/CachingIconImage.qml @@ -1,31 +1,42 @@ +pragma ComponentBehavior: Bound + +import qs.utils +import Quickshell.Widgets import QtQuick Item { - property alias asynchronous: image.asynchronous - property alias status: image.status - property alias mipmap: image.mipmap - property alias backer: image + id: root - property real implicitSize + readonly property int status: loader.item?.status ?? Image.Null readonly property real actualSize: Math.min(width, height) - + property real implicitSize property url source - onSourceChanged: { - if (source?.toString().startsWith("image://icon/")) - // Directly skip the path prop and treat like a normal Image component - image.source = source; - else if (source) - image.path = source; - } - implicitWidth: implicitSize implicitHeight: implicitSize - CachingImage { - id: image + Loader { + id: loader anchors.fill: parent - fillMode: Image.PreserveAspectFit + sourceComponent: root.source ? root.source.toString().startsWith("image://icon/") ? iconImage : cachingImage : null + } + + Component { + id: cachingImage + + CachingImage { + path: Paths.strip(root.source) + fillMode: Image.PreserveAspectFit + } + } + + Component { + id: iconImage + + IconImage { + source: root.source + asynchronous: true + } } } diff --git a/components/images/CachingImage.qml b/components/images/CachingImage.qml index add459a..07b98b6 100644 --- a/components/images/CachingImage.qml +++ b/components/images/CachingImage.qml @@ -1,44 +1,29 @@ import qs.utils import Caelestia -import Quickshell -import Quickshell.Io import QtQuick Image { id: root - property string path - property string hash - readonly property url cachePath: `${Paths.imagecache}/${hash}@${effectiveWidth}x${effectiveHeight}.png` + property alias path: manager.path - readonly property real effectiveScale: QsWindow.window?.devicePixelRatio ?? 1 - readonly property int effectiveWidth: Math.ceil(width * effectiveScale) - readonly property int effectiveHeight: Math.ceil(height * effectiveScale) + property int sourceWidth + property int sourceHeight asynchronous: true fillMode: Image.PreserveAspectCrop - sourceSize.width: effectiveWidth - sourceSize.height: effectiveHeight - - onPathChanged: shaProc.exec(["sha256sum", Paths.strip(path)]) - - onCachePathChanged: { - if (hash) - source = cachePath; - } + sourceSize.width: sourceWidth + sourceSize.height: sourceHeight onStatusChanged: { - if (source == cachePath && status === Image.Error) - source = path; - else if (source == path && status === Image.Ready) - CUtils.saveItem(this, cachePath); + if (!manager.usingCache && status === Image.Ready) + CUtils.saveItem(this, manager.cachePath); } - Process { - id: shaProc + CachingImageManager { + id: manager - stdout: StdioCollector { - onStreamFinished: root.hash = text.split(" ")[0] - } + item: root + cacheDir: Paths.imagecache } } -- cgit v1.2.3-freya