summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--components/images/CachingImage.qml9
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.cpp82
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.hpp10
3 files changed, 73 insertions, 28 deletions
diff --git a/components/images/CachingImage.qml b/components/images/CachingImage.qml
index 07b98b6..2aa0558 100644
--- a/components/images/CachingImage.qml
+++ b/components/images/CachingImage.qml
@@ -1,5 +1,6 @@
import qs.utils
import Caelestia
+import Quickshell
import QtQuick
Image {
@@ -20,6 +21,14 @@ Image {
CUtils.saveItem(this, manager.cachePath);
}
+ Connections {
+ target: QsWindow.window
+
+ function onDevicePixelRatioChanged(): void {
+ manager.updateSource();
+ }
+ }
+
CachingImageManager {
id: manager
diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp
index d95af27..325a1bd 100644
--- a/plugin/src/Caelestia/cachingimagemanager.cpp
+++ b/plugin/src/Caelestia/cachingimagemanager.cpp
@@ -8,8 +8,8 @@
#include <QFile>
qreal CachingImageManager::effectiveScale() const {
- if (m_item->window() && m_item->window()->screen()) {
- return m_item->window()->screen()->devicePixelRatio();
+ if (m_item->window()) {
+ return m_item->window()->devicePixelRatio();
}
return 1.0;
@@ -36,8 +36,21 @@ void CachingImageManager::setItem(QQuickItem* item) {
return;
}
+ if (m_widthConn) {
+ disconnect(m_widthConn);
+ }
+ if (m_heightConn) {
+ disconnect(m_heightConn);
+ }
+
m_item = item;
emit itemChanged();
+
+ if (item) {
+ m_widthConn = connect(item, &QQuickItem::widthChanged, this, [this]() { updateSource(); });
+ m_heightConn = connect(item, &QQuickItem::heightChanged, this, [this]() { updateSource(); });
+ updateSource();
+ }
}
QUrl CachingImageManager::cacheDir() const {
@@ -69,36 +82,53 @@ void CachingImageManager::setPath(const QString& path) {
emit pathChanged();
if (!path.isEmpty()) {
- QThreadPool::globalInstance()->start([path, this] {
- const QString sha = sha256sum(path);
+ updateSource(path);
+ }
+}
- QMetaObject::invokeMethod(this, [path, sha, this]() {
- const QString filename = QString("%1@%2x%3.png")
- .arg(sha)
- .arg(effectiveWidth())
- .arg(effectiveHeight());
+void CachingImageManager::updateSource() {
+ updateSource(m_path);
+}
- m_cachePath = m_cacheDir.resolved(QUrl(filename));
- emit cachePathChanged();
+void CachingImageManager::updateSource(const QString& path) {
+ if (path.isEmpty()) {
+ return;
+ }
- if (!m_cachePath.isLocalFile()) {
- qWarning() << "CachingImageManager::setPath: cachePath" << m_cachePath << "is not a local file";
- return;
- }
+ QThreadPool::globalInstance()->start([path, this] {
+ const QString sha = sha256sum(path);
- bool cacheExists = QFile::exists(m_cachePath.toLocalFile());
+ QMetaObject::invokeMethod(this, [path, sha, this]() {
+ const QString filename = QString("%1@%2x%3.png")
+ .arg(sha)
+ .arg(effectiveWidth())
+ .arg(effectiveHeight());
- if (cacheExists) {
- m_item->setProperty("source", m_cachePath);
- } else {
- m_item->setProperty("source", QUrl::fromLocalFile(path));
- }
+ const QUrl cache = m_cacheDir.resolved(QUrl(filename));
+ if (m_cachePath == cache) {
+ return;
+ }
- m_usingCache = cacheExists;
- emit usingCacheChanged();
- });
- });
- }
+ m_cachePath = cache;
+ emit cachePathChanged();
+
+ if (!cache.isLocalFile()) {
+ qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file";
+ return;
+ }
+
+ bool cacheExists = QFile::exists(cache.toLocalFile());
+
+ if (cacheExists) {
+ m_item->setProperty("source", cache);
+ } else {
+ m_item->setProperty("source", QUrl::fromLocalFile(path));
+ }
+
+ m_usingCache = cacheExists;
+ emit usingCacheChanged();
+ }, Qt::QueuedConnection);
+ });
}
QUrl CachingImageManager::cachePath() const {
diff --git a/plugin/src/Caelestia/cachingimagemanager.hpp b/plugin/src/Caelestia/cachingimagemanager.hpp
index d2d7f74..602d862 100644
--- a/plugin/src/Caelestia/cachingimagemanager.hpp
+++ b/plugin/src/Caelestia/cachingimagemanager.hpp
@@ -30,6 +30,9 @@ public:
[[nodiscard]] QUrl cachePath() const;
[[nodiscard]] bool usingCache() const;
+ Q_INVOKABLE void updateSource();
+ Q_INVOKABLE void updateSource(const QString& path);
+
signals:
void itemChanged();
void cacheDirChanged();
@@ -46,9 +49,12 @@ private:
QUrl m_cachePath;
bool m_usingCache;
+ QMetaObject::Connection m_widthConn;
+ QMetaObject::Connection m_heightConn;
+
[[nodiscard]] qreal effectiveScale() const;
- int effectiveWidth() const;
- int effectiveHeight() const;
+ [[nodiscard]] int effectiveWidth() const;
+ [[nodiscard]] int effectiveHeight() const;
[[nodiscard]] QString sha256sum(const QString& path) const;
};