plugin/cim: update on size change

This commit is contained in:
Soramane 2025-08-29 23:37:50 +10:00
parent 11bc8d0464
commit 4a6fc3c49c
3 changed files with 78 additions and 33 deletions

View file

@ -1,5 +1,6 @@
import qs.utils import qs.utils
import Caelestia import Caelestia
import Quickshell
import QtQuick import QtQuick
Image { Image {
@ -20,6 +21,14 @@ Image {
CUtils.saveItem(this, manager.cachePath); CUtils.saveItem(this, manager.cachePath);
} }
Connections {
target: QsWindow.window
function onDevicePixelRatioChanged(): void {
manager.updateSource();
}
}
CachingImageManager { CachingImageManager {
id: manager id: manager

View file

@ -8,8 +8,8 @@
#include <QFile> #include <QFile>
qreal CachingImageManager::effectiveScale() const { qreal CachingImageManager::effectiveScale() const {
if (m_item->window() && m_item->window()->screen()) { if (m_item->window()) {
return m_item->window()->screen()->devicePixelRatio(); return m_item->window()->devicePixelRatio();
} }
return 1.0; return 1.0;
@ -36,8 +36,21 @@ void CachingImageManager::setItem(QQuickItem* item) {
return; return;
} }
if (m_widthConn) {
disconnect(m_widthConn);
}
if (m_heightConn) {
disconnect(m_heightConn);
}
m_item = item; m_item = item;
emit itemChanged(); 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 { QUrl CachingImageManager::cacheDir() const {
@ -69,6 +82,19 @@ void CachingImageManager::setPath(const QString& path) {
emit pathChanged(); emit pathChanged();
if (!path.isEmpty()) { if (!path.isEmpty()) {
updateSource(path);
}
}
void CachingImageManager::updateSource() {
updateSource(m_path);
}
void CachingImageManager::updateSource(const QString& path) {
if (path.isEmpty()) {
return;
}
QThreadPool::globalInstance()->start([path, this] { QThreadPool::globalInstance()->start([path, this] {
const QString sha = sha256sum(path); const QString sha = sha256sum(path);
@ -78,27 +104,31 @@ void CachingImageManager::setPath(const QString& path) {
.arg(effectiveWidth()) .arg(effectiveWidth())
.arg(effectiveHeight()); .arg(effectiveHeight());
m_cachePath = m_cacheDir.resolved(QUrl(filename)); const QUrl cache = m_cacheDir.resolved(QUrl(filename));
emit cachePathChanged(); if (m_cachePath == cache) {
if (!m_cachePath.isLocalFile()) {
qWarning() << "CachingImageManager::setPath: cachePath" << m_cachePath << "is not a local file";
return; return;
} }
bool cacheExists = QFile::exists(m_cachePath.toLocalFile()); 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) { if (cacheExists) {
m_item->setProperty("source", m_cachePath); m_item->setProperty("source", cache);
} else { } else {
m_item->setProperty("source", QUrl::fromLocalFile(path)); m_item->setProperty("source", QUrl::fromLocalFile(path));
} }
m_usingCache = cacheExists; m_usingCache = cacheExists;
emit usingCacheChanged(); emit usingCacheChanged();
}, Qt::QueuedConnection);
}); });
});
}
} }
QUrl CachingImageManager::cachePath() const { QUrl CachingImageManager::cachePath() const {

View file

@ -30,6 +30,9 @@ public:
[[nodiscard]] QUrl cachePath() const; [[nodiscard]] QUrl cachePath() const;
[[nodiscard]] bool usingCache() const; [[nodiscard]] bool usingCache() const;
Q_INVOKABLE void updateSource();
Q_INVOKABLE void updateSource(const QString& path);
signals: signals:
void itemChanged(); void itemChanged();
void cacheDirChanged(); void cacheDirChanged();
@ -46,9 +49,12 @@ private:
QUrl m_cachePath; QUrl m_cachePath;
bool m_usingCache; bool m_usingCache;
QMetaObject::Connection m_widthConn;
QMetaObject::Connection m_heightConn;
[[nodiscard]] qreal effectiveScale() const; [[nodiscard]] qreal effectiveScale() const;
int effectiveWidth() const; [[nodiscard]] int effectiveWidth() const;
int effectiveHeight() const; [[nodiscard]] int effectiveHeight() const;
[[nodiscard]] QString sha256sum(const QString& path) const; [[nodiscard]] QString sha256sum(const QString& path) const;
}; };