summaryrefslogtreecommitdiff
path: root/plugin/src
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-08-30 14:45:42 +1000
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2025-08-30 14:45:42 +1000
commitd22576ee8273c0537116e69a3134af62db5589f1 (patch)
tree52cbdec833d58206ebf47b28a374e98f9355b75c /plugin/src
parentdev: reload on cmakelists change (diff)
downloadcaelestia-shell-d22576ee8273c0537116e69a3134af62db5589f1.tar.gz
caelestia-shell-d22576ee8273c0537116e69a3134af62db5589f1.tar.bz2
caelestia-shell-d22576ee8273c0537116e69a3134af62db5589f1.zip
plugin/cim: better cache impl
Diffstat (limited to 'plugin/src')
-rw-r--r--plugin/src/Caelestia/CMakeLists.txt2
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.cpp48
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.hpp4
3 files changed, 41 insertions, 13 deletions
diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt
index 3ea0225..dfc7f78 100644
--- a/plugin/src/Caelestia/CMakeLists.txt
+++ b/plugin/src/Caelestia/CMakeLists.txt
@@ -23,4 +23,4 @@ install(TARGETS "${module_plugin_target}" LIBRARY DESTINATION "${module_dir}" RU
install(FILES "${module_qmldir}" DESTINATION "${module_dir}")
install(FILES "${module_typeinfo}" DESTINATION "${module_dir}")
-target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml)
+target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml Qt::Gui)
diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp
index 325a1bd..01d518f 100644
--- a/plugin/src/Caelestia/cachingimagemanager.cpp
+++ b/plugin/src/Caelestia/cachingimagemanager.cpp
@@ -6,6 +6,8 @@
#include <QCryptographicHash>
#include <QThreadPool>
#include <QFile>
+#include <QDir>
+#include <QImageReader>
qreal CachingImageManager::effectiveScale() const {
if (m_item->window()) {
@@ -99,10 +101,9 @@ void CachingImageManager::updateSource(const QString& path) {
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());
+ int width = effectiveWidth();
+ int height = effectiveHeight();
+ const QString filename = QString("%1@%2x%3.png").arg(sha).arg(width).arg(height);
const QUrl cache = m_cacheDir.resolved(QUrl(filename));
if (m_cachePath == cache) {
@@ -123,10 +124,8 @@ void CachingImageManager::updateSource(const QString& path) {
m_item->setProperty("source", cache);
} else {
m_item->setProperty("source", QUrl::fromLocalFile(path));
+ createCache(path, cache.toLocalFile(), QSize(width, height));
}
-
- m_usingCache = cacheExists;
- emit usingCacheChanged();
}, Qt::QueuedConnection);
});
}
@@ -135,8 +134,39 @@ QUrl CachingImageManager::cachePath() const {
return m_cachePath;
}
-bool CachingImageManager::usingCache() const {
- return m_usingCache;
+void CachingImageManager::createCache(const QString& path, const QString& cache, const QSize& size) const {
+ QThreadPool::globalInstance()->start([path, cache, size] {
+ QImageReader reader(path);
+
+ QSize imgSize = reader.size();
+ if (!imgSize.isValid()) {
+ qWarning() << "CachingImageManager::createCache: unable to get size of" << path;
+ return;
+ }
+
+ qreal scale = std::max(
+ qreal(size.width()) / imgSize.width(),
+ qreal(size.height()) / imgSize.height()
+ );
+ QSizeF scaledSize(imgSize.width() * scale, imgSize.height() * scale);
+ qreal xOff = (scaledSize.width() - size.width()) / 2.0;
+ qreal yOff = (scaledSize.height() - size.height()) / 2.0;
+
+ reader.setScaledSize(scaledSize.toSize());
+ reader.setScaledClipRect(QRectF(xOff, yOff, size.width(), size.height()).toRect());
+
+ QImage image = reader.read();
+
+ if (image.isNull()) {
+ qWarning() << "CachingImageManager::createCache: failed to read" << path;
+ return;
+ }
+
+ const QString parent = QFileInfo(cache).absolutePath();
+ if (!QDir().mkpath(parent) || !image.save(cache)) {
+ qWarning() << "CachingImageManager::createCache: failed to save to" << cache;
+ }
+ });
}
QString CachingImageManager::sha256sum(const QString& path) const {
diff --git a/plugin/src/Caelestia/cachingimagemanager.hpp b/plugin/src/Caelestia/cachingimagemanager.hpp
index 602d862..c78dcda 100644
--- a/plugin/src/Caelestia/cachingimagemanager.hpp
+++ b/plugin/src/Caelestia/cachingimagemanager.hpp
@@ -13,7 +13,6 @@ class CachingImageManager : public QObject {
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged);
Q_PROPERTY(QUrl cachePath READ cachePath NOTIFY cachePathChanged);
- Q_PROPERTY(bool usingCache READ usingCache NOTIFY usingCacheChanged);
public:
explicit CachingImageManager(QObject* parent = nullptr): QObject(parent) {};
@@ -28,7 +27,6 @@ public:
void setPath(const QString& path);
[[nodiscard]] QUrl cachePath() const;
- [[nodiscard]] bool usingCache() const;
Q_INVOKABLE void updateSource();
Q_INVOKABLE void updateSource(const QString& path);
@@ -47,7 +45,6 @@ private:
QString m_path;
QUrl m_cachePath;
- bool m_usingCache;
QMetaObject::Connection m_widthConn;
QMetaObject::Connection m_heightConn;
@@ -56,5 +53,6 @@ private:
[[nodiscard]] int effectiveWidth() const;
[[nodiscard]] int effectiveHeight() const;
+ void createCache(const QString& path, const QString& cache, const QSize& size) const;
[[nodiscard]] QString sha256sum(const QString& path) const;
};