From 9708d8212a90ebd1436063099145914330820c7d Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Fri, 4 Jul 2025 15:58:41 -0700 Subject: [PATCH] core/reloader: trigger onPostReload if launched post-reload This is similar to the check in Reloadable, and fixes a number of hard to debug issues with Process, IpcHandler, NotificationServer, and GlobalShortcut not working depending on where you put them in a QML file. --- src/core/reload.cpp | 21 ++++++++++++------- src/core/reload.hpp | 17 ++++++++------- src/io/ipchandler.hpp | 6 ++---- src/io/process.cpp | 11 +++++----- src/io/process.hpp | 5 +---- src/io/test/process.cpp | 4 ++-- src/services/notifications/qml.hpp | 4 +--- src/wayland/hyprland/global_shortcuts/qml.hpp | 6 ++---- 8 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/core/reload.cpp b/src/core/reload.cpp index 25ab33f3..0bdf8fcb 100644 --- a/src/core/reload.cpp +++ b/src/core/reload.cpp @@ -126,12 +126,17 @@ QObject* Reloadable::getChildByReloadId(QObject* parent, const QString& reloadId return nullptr; } -void PostReloadHook::postReloadTree(QObject* root) { - for (auto* child: root->children()) { - PostReloadHook::postReloadTree(child); - } - - if (auto* self = dynamic_cast(root)) { - self->onPostReload(); - } +void PostReloadHook::componentComplete() { + auto* engineGeneration = EngineGeneration::findObjectGeneration(this); + if (!engineGeneration || engineGeneration->reloadComplete) this->postReload(); +} + +void PostReloadHook::postReload() { + this->isPostReload = true; + this->onPostReload(); +} + +void PostReloadHook::postReloadTree(QObject* root) { + for (auto* child: root->children()) PostReloadHook::postReloadTree(child); + if (auto* self = dynamic_cast(root)) self->postReload(); } diff --git a/src/core/reload.hpp b/src/core/reload.hpp index 560c8bd0..1d4e3751 100644 --- a/src/core/reload.hpp +++ b/src/core/reload.hpp @@ -119,16 +119,19 @@ private: }; /// Hook that runs after the old widget tree is dropped during a reload. -class PostReloadHook { +class PostReloadHook + : public QObject + , public QQmlParserStatus { public: - PostReloadHook() = default; - virtual ~PostReloadHook() = default; - PostReloadHook(PostReloadHook&&) = default; - PostReloadHook(const PostReloadHook&) = default; - PostReloadHook& operator=(PostReloadHook&&) = default; - PostReloadHook& operator=(const PostReloadHook&) = default; + PostReloadHook(QObject* parent = nullptr): QObject(parent) {} + void classBegin() override {} + void componentComplete() override; + void postReload(); virtual void onPostReload() = 0; static void postReloadTree(QObject* root); + +protected: + bool isPostReload = false; }; diff --git a/src/io/ipchandler.hpp b/src/io/ipchandler.hpp index e6b24ba1..1da3e712 100644 --- a/src/io/ipchandler.hpp +++ b/src/io/ipchandler.hpp @@ -154,9 +154,7 @@ class IpcHandlerRegistry; /// #### Properties /// Properties of an IpcHanlder can be read using `qs ipc prop get` as long as they are /// of an IPC compatible type. See the table above for compatible types. -class IpcHandler - : public QObject - , public PostReloadHook { +class IpcHandler: public PostReloadHook { Q_OBJECT; /// If the handler should be able to receive calls. Defaults to true. Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged); @@ -166,7 +164,7 @@ class IpcHandler QML_ELEMENT; public: - explicit IpcHandler(QObject* parent = nullptr): QObject(parent) {}; + explicit IpcHandler(QObject* parent = nullptr): PostReloadHook(parent) {}; ~IpcHandler() override; Q_DISABLE_COPY_MOVE(IpcHandler); diff --git a/src/io/process.cpp b/src/io/process.cpp index 1abbdb01..c8250c7e 100644 --- a/src/io/process.cpp +++ b/src/io/process.cpp @@ -15,10 +15,11 @@ #include "../core/generation.hpp" #include "../core/qmlglobal.hpp" +#include "../core/reload.hpp" #include "datastream.hpp" #include "processcore.hpp" -Process::Process(QObject* parent): QObject(parent) { +Process::Process(QObject* parent): PostReloadHook(parent) { QObject::connect( QuickshellSettings::instance(), &QuickshellSettings::workingDirectoryChanged, @@ -37,10 +38,7 @@ Process::~Process() { } } -void Process::onPostReload() { - this->postReload = true; - this->startProcessIfReady(); -} +void Process::onPostReload() { this->startProcessIfReady(); } bool Process::isRunning() const { return this->process != nullptr; } @@ -180,9 +178,10 @@ void Process::setStdinEnabled(bool enabled) { } void Process::startProcessIfReady() { - if (this->process != nullptr || !this->postReload || !this->targetRunning + if (this->process != nullptr || !this->isPostReload || !this->targetRunning || this->mCommand.isEmpty()) return; + this->targetRunning = false; auto& cmd = this->mCommand.first(); diff --git a/src/io/process.hpp b/src/io/process.hpp index b1151537..ab8763ee 100644 --- a/src/io/process.hpp +++ b/src/io/process.hpp @@ -31,9 +31,7 @@ /// } /// } /// ``` -class Process - : public QObject - , public PostReloadHook { +class Process: public PostReloadHook { Q_OBJECT; // clang-format off /// If the process is currently running. Defaults to false. @@ -258,5 +256,4 @@ private: bool targetRunning = false; bool mStdinEnabled = false; bool mClearEnvironment = false; - bool postReload = false; }; diff --git a/src/io/test/process.cpp b/src/io/test/process.cpp index fc7b834d..09fc9f7d 100644 --- a/src/io/test/process.cpp +++ b/src/io/test/process.cpp @@ -18,7 +18,7 @@ void TestProcess::startAfterReload() { QVERIFY(!process.isRunning()); QCOMPARE(startedSpy.count(), 0); - process.onPostReload(); + process.postReload(); QVERIFY(process.isRunning()); QVERIFY(startedSpy.wait(100)); @@ -29,7 +29,7 @@ void TestProcess::testExec() { auto startedSpy = QSignalSpy(&process, &Process::started); auto exitedSpy = QSignalSpy(&process, &Process::exited); - process.onPostReload(); + process.postReload(); process.setCommand({"sleep", "30"}); process.setRunning(true); diff --git a/src/services/notifications/qml.hpp b/src/services/notifications/qml.hpp index d7502101..feb33db3 100644 --- a/src/services/notifications/qml.hpp +++ b/src/services/notifications/qml.hpp @@ -21,9 +21,7 @@ namespace qs::service::notifications { /// The server does not advertise most capabilities by default. See the individual properties for details. /// /// [Desktop Notifications Specification]: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html -class NotificationServerQml - : public QObject - , public PostReloadHook { +class NotificationServerQml: public PostReloadHook { Q_OBJECT; // clang-format off /// If notifications should be re-emitted when quickshell reloads. Defaults to true. diff --git a/src/wayland/hyprland/global_shortcuts/qml.hpp b/src/wayland/hyprland/global_shortcuts/qml.hpp index a43d963e..f257632f 100644 --- a/src/wayland/hyprland/global_shortcuts/qml.hpp +++ b/src/wayland/hyprland/global_shortcuts/qml.hpp @@ -32,9 +32,7 @@ namespace qs::hyprland::global_shortcuts { /// /// [hyprland_global_shortcuts_v1]: https://github.com/hyprwm/hyprland-protocols/blob/main/protocols/hyprland-global-shortcuts-v1.xml /// [the wiki]: https://wiki.hyprland.org/Configuring/Binds/#dbus-global-shortcuts -class GlobalShortcut - : public QObject - , public PostReloadHook { +class GlobalShortcut: public PostReloadHook { using ShortcutImpl = impl::GlobalShortcut; Q_OBJECT; @@ -59,7 +57,7 @@ class GlobalShortcut QML_ELEMENT; public: - explicit GlobalShortcut(QObject* parent = nullptr): QObject(parent) {} + explicit GlobalShortcut(QObject* parent = nullptr): PostReloadHook(parent) {} ~GlobalShortcut() override; Q_DISABLE_COPY_MOVE(GlobalShortcut);