core/qmlglobal: add execDetached functions for spawning processes

This commit is contained in:
outfoxxed 2025-06-15 00:07:01 -07:00
parent 0140356d99
commit 0499518143
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
10 changed files with 167 additions and 30 deletions

View file

@ -8,8 +8,10 @@
#include <qguiapplication.h>
#include <qicon.h>
#include <qjsengine.h>
#include <qlist.h>
#include <qlogging.h>
#include <qobject.h>
#include <qprocess.h>
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qqmllist.h>
@ -21,6 +23,7 @@
#include <qwindowdefs.h>
#include <unistd.h>
#include "../io/processcore.hpp"
#include "generation.hpp"
#include "iconimageprovider.hpp"
#include "paths.hpp"
@ -246,6 +249,36 @@ QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
return qEnvironmentVariable(vstr.data());
}
void QuickshellGlobal::execDetached(QList<QString> command) {
QuickshellGlobal::execDetached(ProcessContext(std::move(command)));
}
void QuickshellGlobal::execDetached(const ProcessContext& context) {
if (context.command.isEmpty()) {
qWarning() << "Cannot start process as command is empty.";
return;
}
const auto& cmd = context.command.first();
auto args = context.command.sliced(1);
QProcess process;
qs::core::process::setupProcessEnvironment(
&process,
context.clearEnvironment,
context.environment
);
if (!context.workingDirectory.isEmpty()) {
process.setWorkingDirectory(context.workingDirectory);
}
process.setProgram(cmd);
process.setArguments(args);
process.startDetached();
}
QString QuickshellGlobal::iconPath(const QString& icon) {
return IconImageProvider::requestString(icon);
}

View file

@ -1,8 +1,12 @@
#pragma once
#include <utility>
#include <qclipboard.h>
#include <qcontainerfwd.h>
#include <qhash.h>
#include <qjsengine.h>
#include <qlist.h>
#include <qobject.h>
#include <qqmlengine.h>
#include <qqmlintegration.h>
@ -15,6 +19,25 @@
#include "qmlscreen.hpp"
class ProcessContext {
Q_PROPERTY(QList<QString> command MEMBER command);
Q_PROPERTY(QHash<QString, QVariant> environment MEMBER environment);
Q_PROPERTY(bool clearEnvironment MEMBER clearEnvironment);
Q_PROPERTY(QString workingDirectory MEMBER workingDirectory);
Q_GADGET;
QML_STRUCTURED_VALUE;
QML_VALUE_TYPE(processContext);
public:
ProcessContext() = default;
explicit ProcessContext(QList<QString> command): command(std::move(command)) {}
QList<QString> command;
QHash<QString, QVariant> environment;
bool clearEnvironment = false;
QString workingDirectory;
};
///! Accessor for some options under the Quickshell type.
class QuickshellSettings: public QObject {
Q_OBJECT;
@ -152,6 +175,46 @@ public:
/// Returns the string value of an environment variable or null if it is not set.
Q_INVOKABLE QVariant env(const QString& variable);
/// Launch a process detached from Quickshell.
///
/// Each command argument is its own string, meaning arguments do
/// not have to be escaped.
///
/// > [!WARNING] This does not run command in a shell. All arguments to the command
/// > must be in separate values in the list, e.g. `["echo", "hello"]`
/// > and not `["echo hello"]`.
/// >
/// > Additionally, shell scripts must be run by your shell,
/// > e.g. `["sh", "script.sh"]` instead of `["script.sh"]` unless the script
/// > has a shebang.
///
/// > [!INFO] You can use `["sh", "-c", <your command>]` to execute your command with
/// > the system shell.
///
/// This function is equivalent to @@Quickshell.Io.Process.startDetached().
Q_INVOKABLE static void execDetached(QList<QString> command);
/// Launch a process detached from Quickshell.
///
/// The context parameter is a JS object with the following fields:
/// - `command`: A list containing the command and all its arguments. See @@Quickshell.Io.Process.command.
/// - `environment`: Changes to make to the process environment. See @@Quickshell.Io.Process.environment.
/// - `clearEnvironment`: Removes all variables from the environment if true.
/// - `workingDirectory`: The working directory the command should run in.
///
/// > [!WARNING] This does not run command in a shell. All arguments to the command
/// > must be in separate values in the list, e.g. `["echo", "hello"]`
/// > and not `["echo hello"]`.
/// >
/// > Additionally, shell scripts must be run by your shell,
/// > e.g. `["sh", "script.sh"]` instead of `["script.sh"]` unless the script
/// > has a shebang.
///
/// > [!INFO] You can use `["sh", "-c", <your command>]` to execute your command with
/// > the system shell.
///
/// This function is equivalent to @@Quickshell.Io.Process.startDetached().
Q_INVOKABLE static void execDetached(const ProcessContext& context);
/// 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,

View file

@ -1,6 +1,6 @@
function (qs_test name)
add_executable(${name} ${ARGN})
target_link_libraries(${name} PRIVATE Qt::Quick Qt::Test quickshell-core quickshell-window quickshell-ui)
target_link_libraries(${name} PRIVATE Qt::Quick Qt::Test quickshell-core quickshell-window quickshell-ui quickshell-io)
add_test(NAME ${name} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" COMMAND $<TARGET_FILE:${name}>)
endfunction()