diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-27 22:13:21 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-27 22:13:21 +1000 |
| commit | 573423742a77816699f219817ba462c7f6f5d33a (patch) | |
| tree | 9461ebfe7cb097439489a21094cebc4b3d430d59 /plugin/src/Caelestia/cutils.cpp | |
| parent | plugin/cim: fix cache dir (diff) | |
| download | caelestia-shell-573423742a77816699f219817ba462c7f6f5d33a.tar.gz caelestia-shell-573423742a77816699f219817ba462c7f6f5d33a.tar.bz2 caelestia-shell-573423742a77816699f219817ba462c7f6f5d33a.zip | |
plugin: make thread safe
Diffstat (limited to 'plugin/src/Caelestia/cutils.cpp')
| -rw-r--r-- | plugin/src/Caelestia/cutils.cpp | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index 2415f80..dfe5ee8 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -7,27 +7,27 @@ #include <QQmlEngine> #include <QDir> -void CUtils::saveItem(QQuickItem* target, const QUrl& path) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path) { this->saveItem(target, path, QRect(), QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) { this->saveItem(target, path, rect, QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) { this->saveItem(target, path, QRect(), onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) { this->saveItem(target, path, QRect(), onSaved, onFailed); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) { this->saveItem(target, path, rect, onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) { if (!target) { qWarning() << "CUtils::saveItem: a target is required"; return; @@ -40,10 +40,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); - QObject::connect( - grabResult.data(), - &QQuickItemGrabResult::ready, - this, + QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rect, path, onSaved, onFailed, this]() { QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { QImage image = grabResult->image(); @@ -54,16 +51,20 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q const QString file = path.toLocalFile(); const QString parent = QFileInfo(file).absolutePath(); - if (QDir().mkpath(parent) && image.save(file)) { - if (onSaved.isCallable()) { - onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - } else { - qWarning() << "CUtils::saveItem: failed to save" << path; - if (onFailed.isCallable()) { - onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + bool success = QDir().mkpath(parent) && image.save(file); + + QMetaObject::invokeMethod(this, [file, success, path, onSaved, onFailed, this]() { + if (success) { + if (onSaved.isCallable()) { + onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } + } else { + qWarning() << "CUtils::saveItem: failed to save" << path; + if (onFailed.isCallable()) { + onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } } - } + }, Qt::QueuedConnection); }); } ); |