forked from quickshell/quickshell
		
	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.
This commit is contained in:
		
							parent
							
								
									0e6518a706
								
							
						
					
					
						commit
						9708d8212a
					
				
					 8 changed files with 36 additions and 38 deletions
				
			
		| 
						 | 
				
			
			@ -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<PostReloadHook*>(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<PostReloadHook*>(root)) self->postReload();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue