Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/core/qmlglobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qqmllist.h>
#include <qquickitemgrabresult.h>
#include <qscreen.h>
#include <qtenvironmentvariables.h>
#include <qthreadpool.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <qurl.h>
#include <qvariant.h>
#include <qwindowdefs.h>
#include <unistd.h>
Expand Down Expand Up @@ -313,6 +316,25 @@ QString QuickshellGlobal::iconPath(const QString& icon, const QString& fallback)
return IconImageProvider::requestString(icon, "", fallback);
}

void QuickshellGlobal::saveToFile(
const QQuickItemGrabResult* grabResult,
const QUrl& filePath
) {
if (!filePath.isLocalFile()) {
qWarning() << "saveToFile can only save to a file on the local filesystem";
return;
}

const QString localFile = filePath.toLocalFile();
QImage image = grabResult->image();

QThreadPool::globalInstance()->start([image, localFile] {
if (!image.save(localFile)) {
qWarning() << "Failed to save image to" << localFile;
}
});
}

QuickshellGlobal* QuickshellGlobal::create(QQmlEngine* engine, QJSEngine* /*unused*/) {
auto* qsg = new QuickshellGlobal();
auto* generation = EngineGeneration::findEngineGeneration(engine);
Expand Down
10 changes: 10 additions & 0 deletions src/core/qmlglobal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <qqmlengine.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qquickitemgrabresult.h>
#include <qscreen.h>
#include <qtmetamacros.h>
#include <qtypes.h>
Expand Down Expand Up @@ -184,6 +185,15 @@ class QuickshellGlobal: public QObject {
/// This function is equivalent to @@Quickshell.Io.Process.startDetached().
Q_INVOKABLE static void execDetached(const qs::io::process::ProcessContext& context);

/// Save the contents of an ItemGrabResult to a file asynchronously.
///
/// > [!INFO] This function is equivalent to an asynchronous version of
/// > @@QtQuick.ItemGrabResult.saveToFile().
/// >
/// > You can use it in a similar way: `Item.grabToImage(res => Quickshell.saveToFile(res, path))`
Q_INVOKABLE static void
saveToFile(const QQuickItemGrabResult* grabResult, const QUrl& filePath);

/// Returns a string usable for a @@QtQuick.Image.source for a given system icon.
///
/// > [!INFO] By default, icons are loaded from the theme selected by the qt platform theme,
Expand Down