Skip to content

Commit 9708d82

Browse files
committed
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.
1 parent 0e6518a commit 9708d82

File tree

8 files changed

+35
-37
lines changed

8 files changed

+35
-37
lines changed

src/core/reload.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,17 @@ QObject* Reloadable::getChildByReloadId(QObject* parent, const QString& reloadId
126126
return nullptr;
127127
}
128128

129-
void PostReloadHook::postReloadTree(QObject* root) {
130-
for (auto* child: root->children()) {
131-
PostReloadHook::postReloadTree(child);
132-
}
129+
void PostReloadHook::componentComplete() {
130+
auto* engineGeneration = EngineGeneration::findObjectGeneration(this);
131+
if (!engineGeneration || engineGeneration->reloadComplete) this->postReload();
132+
}
133133

134-
if (auto* self = dynamic_cast<PostReloadHook*>(root)) {
135-
self->onPostReload();
136-
}
134+
void PostReloadHook::postReload() {
135+
this->isPostReload = true;
136+
this->onPostReload();
137+
}
138+
139+
void PostReloadHook::postReloadTree(QObject* root) {
140+
for (auto* child: root->children()) PostReloadHook::postReloadTree(child);
141+
if (auto* self = dynamic_cast<PostReloadHook*>(root)) self->postReload();
137142
}

src/core/reload.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ class ReloadPropagator: public Reloadable {
119119
};
120120

121121
/// Hook that runs after the old widget tree is dropped during a reload.
122-
class PostReloadHook {
122+
class PostReloadHook
123+
: public QObject
124+
, public QQmlParserStatus {
123125
public:
124-
PostReloadHook() = default;
125-
virtual ~PostReloadHook() = default;
126-
PostReloadHook(PostReloadHook&&) = default;
127-
PostReloadHook(const PostReloadHook&) = default;
128-
PostReloadHook& operator=(PostReloadHook&&) = default;
129-
PostReloadHook& operator=(const PostReloadHook&) = default;
126+
PostReloadHook(QObject* parent = nullptr): QObject(parent) {}
127+
void classBegin() override {}
128+
void componentComplete() override;
130129

130+
void postReload();
131131
virtual void onPostReload() = 0;
132132

133133
static void postReloadTree(QObject* root);
134+
135+
protected:
136+
bool isPostReload = false;
134137
};

src/io/ipchandler.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ class IpcHandlerRegistry;
154154
/// #### Properties
155155
/// Properties of an IpcHanlder can be read using `qs ipc prop get` as long as they are
156156
/// of an IPC compatible type. See the table above for compatible types.
157-
class IpcHandler
158-
: public QObject
159-
, public PostReloadHook {
157+
class IpcHandler: public PostReloadHook {
160158
Q_OBJECT;
161159
/// If the handler should be able to receive calls. Defaults to true.
162160
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged);
@@ -166,7 +164,7 @@ class IpcHandler
166164
QML_ELEMENT;
167165

168166
public:
169-
explicit IpcHandler(QObject* parent = nullptr): QObject(parent) {};
167+
explicit IpcHandler(QObject* parent = nullptr): PostReloadHook(parent) {};
170168
~IpcHandler() override;
171169
Q_DISABLE_COPY_MOVE(IpcHandler);
172170

src/io/process.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
#include "../core/generation.hpp"
1717
#include "../core/qmlglobal.hpp"
18+
#include "../core/reload.hpp"
1819
#include "datastream.hpp"
1920
#include "processcore.hpp"
2021

21-
Process::Process(QObject* parent): QObject(parent) {
22+
Process::Process(QObject* parent): PostReloadHook(parent) {
2223
QObject::connect(
2324
QuickshellSettings::instance(),
2425
&QuickshellSettings::workingDirectoryChanged,
@@ -37,10 +38,7 @@ Process::~Process() {
3738
}
3839
}
3940

40-
void Process::onPostReload() {
41-
this->postReload = true;
42-
this->startProcessIfReady();
43-
}
41+
void Process::onPostReload() { this->startProcessIfReady(); }
4442

4543
bool Process::isRunning() const { return this->process != nullptr; }
4644

@@ -180,9 +178,10 @@ void Process::setStdinEnabled(bool enabled) {
180178
}
181179

182180
void Process::startProcessIfReady() {
183-
if (this->process != nullptr || !this->postReload || !this->targetRunning
181+
if (this->process != nullptr || !this->isPostReload || !this->targetRunning
184182
|| this->mCommand.isEmpty())
185183
return;
184+
186185
this->targetRunning = false;
187186

188187
auto& cmd = this->mCommand.first();

src/io/process.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
/// }
3232
/// }
3333
/// ```
34-
class Process
35-
: public QObject
36-
, public PostReloadHook {
34+
class Process: public PostReloadHook {
3735
Q_OBJECT;
3836
// clang-format off
3937
/// If the process is currently running. Defaults to false.
@@ -258,5 +256,4 @@ private slots:
258256
bool targetRunning = false;
259257
bool mStdinEnabled = false;
260258
bool mClearEnvironment = false;
261-
bool postReload = false;
262259
};

src/io/test/process.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void TestProcess::startAfterReload() {
1818
QVERIFY(!process.isRunning());
1919
QCOMPARE(startedSpy.count(), 0);
2020

21-
process.onPostReload();
21+
process.postReload();
2222

2323
QVERIFY(process.isRunning());
2424
QVERIFY(startedSpy.wait(100));
@@ -29,7 +29,7 @@ void TestProcess::testExec() {
2929
auto startedSpy = QSignalSpy(&process, &Process::started);
3030
auto exitedSpy = QSignalSpy(&process, &Process::exited);
3131

32-
process.onPostReload();
32+
process.postReload();
3333

3434
process.setCommand({"sleep", "30"});
3535
process.setRunning(true);

src/services/notifications/qml.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ namespace qs::service::notifications {
2121
/// The server does not advertise most capabilities by default. See the individual properties for details.
2222
///
2323
/// [Desktop Notifications Specification]: https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html
24-
class NotificationServerQml
25-
: public QObject
26-
, public PostReloadHook {
24+
class NotificationServerQml: public PostReloadHook {
2725
Q_OBJECT;
2826
// clang-format off
2927
/// If notifications should be re-emitted when quickshell reloads. Defaults to true.

src/wayland/hyprland/global_shortcuts/qml.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ namespace qs::hyprland::global_shortcuts {
3232
///
3333
/// [hyprland_global_shortcuts_v1]: https://github.com/hyprwm/hyprland-protocols/blob/main/protocols/hyprland-global-shortcuts-v1.xml
3434
/// [the wiki]: https://wiki.hyprland.org/Configuring/Binds/#dbus-global-shortcuts
35-
class GlobalShortcut
36-
: public QObject
37-
, public PostReloadHook {
35+
class GlobalShortcut: public PostReloadHook {
3836
using ShortcutImpl = impl::GlobalShortcut;
3937

4038
Q_OBJECT;
@@ -59,7 +57,7 @@ class GlobalShortcut
5957
QML_ELEMENT;
6058

6159
public:
62-
explicit GlobalShortcut(QObject* parent = nullptr): QObject(parent) {}
60+
explicit GlobalShortcut(QObject* parent = nullptr): PostReloadHook(parent) {}
6361
~GlobalShortcut() override;
6462
Q_DISABLE_COPY_MOVE(GlobalShortcut);
6563

0 commit comments

Comments
 (0)