diff options
Diffstat (limited to 'components/images')
| -rw-r--r-- | components/images/CachingIconImage.qml | 31 | ||||
| -rw-r--r-- | components/images/CachingImage.qml | 41 |
2 files changed, 72 insertions, 0 deletions
diff --git a/components/images/CachingIconImage.qml b/components/images/CachingIconImage.qml new file mode 100644 index 0000000..522a947 --- /dev/null +++ b/components/images/CachingIconImage.qml @@ -0,0 +1,31 @@ +import QtQuick + +Item { + property alias asynchronous: image.asynchronous + property alias status: image.status + property alias mipmap: image.mipmap + property alias backer: image + + property real implicitSize + readonly property real actualSize: Math.min(width, height) + + 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 + + anchors.fill: parent + fillMode: Image.PreserveAspectFit + } +} diff --git a/components/images/CachingImage.qml b/components/images/CachingImage.qml new file mode 100644 index 0000000..1d42238 --- /dev/null +++ b/components/images/CachingImage.qml @@ -0,0 +1,41 @@ +import qs.utils +import Quickshell.Io +import QtQuick + +Image { + id: root + + property string path + property string hash + readonly property string cachePath: `${Paths.stringify(Paths.imagecache)}/${hash}@${width}x${height}.png` + + asynchronous: true + fillMode: Image.PreserveAspectCrop + sourceSize.width: width + sourceSize.height: height + + onPathChanged: shaProc.exec(["sha256sum", Paths.strip(path)]) + + onCachePathChanged: { + if (hash) + source = cachePath; + } + + onStatusChanged: { + if (source == cachePath && status === Image.Error) + source = path; + else if (source == path && status === Image.Ready) { + Paths.mkdir(Paths.imagecache); + const grabPath = cachePath; + grabToImage(res => res.saveToFile(grabPath)); + } + } + + Process { + id: shaProc + + stdout: StdioCollector { + onStreamFinished: root.hash = text.split(" ")[0] + } + } +} |