plugin/cim: safe async this

Fixes wallpaper picker crash on fast input
This commit is contained in:
2 * r + 2 * t 2025-08-30 23:03:55 +10:00
parent e5722af7db
commit c72338655f

View file

@ -108,34 +108,35 @@ void CachingImageManager::updateSource(const QString& path) {
m_shaPath = path; m_shaPath = path;
QThreadPool::globalInstance()->start([path, this] { QPointer<CachingImageManager> self(this);
const QString sha = sha256sum(path); QThreadPool::globalInstance()->start([path, self] {
const QString sha = self->sha256sum(path);
QMetaObject::invokeMethod(this, [path, sha, this]() { QMetaObject::invokeMethod(self, [path, sha, self]() {
if (m_path != path) { if (!self || self->m_path != path) {
// Path has changed, ignore // Object is destroyed or path has changed, ignore
return; return;
} }
int width = effectiveWidth(); int width = self->effectiveWidth();
int height = effectiveHeight(); int height = self->effectiveHeight();
if (!m_item || !width || !height) { if (!self->m_item || !width || !height) {
return; return;
} }
const QString fillMode = m_item->property("fillMode").toString(); const QString fillMode = self->m_item->property("fillMode").toString();
const QString filename = QString("%1@%2x%3-%4.png") const QString filename = QString("%1@%2x%3-%4.png")
.arg(sha).arg(width).arg(height) .arg(sha).arg(width).arg(height)
.arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch"); .arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch");
const QUrl cache = m_cacheDir.resolved(QUrl(filename)); const QUrl cache = self->m_cacheDir.resolved(QUrl(filename));
if (m_cachePath == cache) { if (self->m_cachePath == cache) {
return; return;
} }
m_cachePath = cache; self->m_cachePath = cache;
emit cachePathChanged(); emit self->cachePathChanged();
if (!cache.isLocalFile()) { if (!cache.isLocalFile()) {
qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file"; qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file";
@ -145,15 +146,15 @@ void CachingImageManager::updateSource(const QString& path) {
bool cacheExists = QFile::exists(cache.toLocalFile()); bool cacheExists = QFile::exists(cache.toLocalFile());
if (cacheExists) { if (cacheExists) {
m_item->setProperty("source", cache); self->m_item->setProperty("source", cache);
} else { } else {
m_item->setProperty("source", QUrl::fromLocalFile(path)); self->m_item->setProperty("source", QUrl::fromLocalFile(path));
createCache(path, cache.toLocalFile(), fillMode, QSize(width, height)); self->createCache(path, cache.toLocalFile(), fillMode, QSize(width, height));
} }
// Clear current running sha if same // Clear current running sha if same
if (m_shaPath == path) { if (self->m_shaPath == path) {
m_shaPath = QString(); self->m_shaPath = QString();
} }
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
}); });