From 4a6fc3c49c1b803bbd8669ca1c8cef717038b3fa Mon Sep 17 00:00:00 2001 From: Soramane <61896496+soramanew@users.noreply.github.com> Date: Fri, 29 Aug 2025 23:37:50 +1000 Subject: plugin/cim: update on size change --- plugin/src/Caelestia/cachingimagemanager.cpp | 92 ++++++++++++++++++---------- plugin/src/Caelestia/cachingimagemanager.hpp | 10 ++- 2 files changed, 69 insertions(+), 33 deletions(-) (limited to 'plugin') 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 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,38 +82,55 @@ void CachingImageManager::setPath(const QString& path) { emit pathChanged(); if (!path.isEmpty()) { - QThreadPool::globalInstance()->start([path, this] { - const QString sha = sha256sum(path); - - QMetaObject::invokeMethod(this, [path, sha, this]() { - const QString filename = QString("%1@%2x%3.png") - .arg(sha) - .arg(effectiveWidth()) - .arg(effectiveHeight()); - - m_cachePath = m_cacheDir.resolved(QUrl(filename)); - emit cachePathChanged(); - - if (!m_cachePath.isLocalFile()) { - qWarning() << "CachingImageManager::setPath: cachePath" << m_cachePath << "is not a local file"; - return; - } - - bool cacheExists = QFile::exists(m_cachePath.toLocalFile()); - - if (cacheExists) { - m_item->setProperty("source", m_cachePath); - } else { - m_item->setProperty("source", QUrl::fromLocalFile(path)); - } - - m_usingCache = cacheExists; - emit usingCacheChanged(); - }); - }); + updateSource(path); } } +void CachingImageManager::updateSource() { + updateSource(m_path); +} + +void CachingImageManager::updateSource(const QString& path) { + if (path.isEmpty()) { + return; + } + + QThreadPool::globalInstance()->start([path, this] { + const QString sha = sha256sum(path); + + QMetaObject::invokeMethod(this, [path, sha, this]() { + const QString filename = QString("%1@%2x%3.png") + .arg(sha) + .arg(effectiveWidth()) + .arg(effectiveHeight()); + + const QUrl cache = m_cacheDir.resolved(QUrl(filename)); + if (m_cachePath == cache) { + return; + } + + 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 { return m_cachePath; } 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; }; -- cgit v1.2.3-freya