forked from quickshell/quickshell
		
	feat: add hard reload signal and wrap root object
This commit is contained in:
		
							parent
							
								
									d14258df8e
								
							
						
					
					
						commit
						9a5ad44aa9
					
				
					 6 changed files with 116 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -26,6 +26,7 @@ qt_add_executable(qtshell
 | 
			
		|||
	src/cpp/main.cpp
 | 
			
		||||
	src/cpp/shell.cpp
 | 
			
		||||
	src/cpp/variants.cpp
 | 
			
		||||
	src/cpp/rootwrapper.cpp
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
qt_add_qml_module(qtshell URI QtShell)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,10 +5,11 @@
 | 
			
		|||
#include <qguiapplication.h>
 | 
			
		||||
#include <qlogging.h>
 | 
			
		||||
#include <qobject.h>
 | 
			
		||||
#include <qqmlapplicationengine.h>
 | 
			
		||||
#include <qquickwindow.h>
 | 
			
		||||
#include <qstandardpaths.h>
 | 
			
		||||
#include <qstring.h>
 | 
			
		||||
 | 
			
		||||
#include "rootwrapper.hpp"
 | 
			
		||||
#include "shell.hpp"
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
| 
						 | 
				
			
			@ -43,21 +44,11 @@ int main(int argc, char** argv) {
 | 
			
		|||
	CONFIG_PATH = configPath;
 | 
			
		||||
 | 
			
		||||
	LayerShellQt::Shell::useLayerShell();
 | 
			
		||||
	// Base window transparency appears to be additive.
 | 
			
		||||
	// Use a fully transparent window with a colored rect.
 | 
			
		||||
	QQuickWindow::setDefaultAlphaBuffer(true);
 | 
			
		||||
 | 
			
		||||
	auto engine = QQmlApplicationEngine();
 | 
			
		||||
	engine.load(configPath);
 | 
			
		||||
 | 
			
		||||
	if (engine.rootObjects().isEmpty()) {
 | 
			
		||||
		qCritical() << "failed to load config file";
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	auto* shellobj = qobject_cast<QtShell*>(engine.rootObjects().first());
 | 
			
		||||
 | 
			
		||||
	if (shellobj == nullptr) {
 | 
			
		||||
		qCritical() << "root item was not a QtShell";
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
	auto root = RootWrapper(configPath);
 | 
			
		||||
 | 
			
		||||
	return QGuiApplication::exec();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										64
									
								
								src/cpp/rootwrapper.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/cpp/rootwrapper.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,64 @@
 | 
			
		|||
#include "rootwrapper.hpp"
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <utility>
 | 
			
		||||
 | 
			
		||||
#include <qlogging.h>
 | 
			
		||||
#include <qobject.h>
 | 
			
		||||
#include <qqmlcomponent.h>
 | 
			
		||||
#include <qqmlengine.h>
 | 
			
		||||
#include <qurl.h>
 | 
			
		||||
 | 
			
		||||
#include "shell.hpp"
 | 
			
		||||
 | 
			
		||||
RootWrapper::RootWrapper(QUrl rootUrl):
 | 
			
		||||
    QObject(nullptr), rootUrl(std::move(rootUrl)), engine(this) {
 | 
			
		||||
	this->reloadGraph();
 | 
			
		||||
 | 
			
		||||
	if (this->activeRoot == nullptr) {
 | 
			
		||||
		qCritical() << "could not create scene graph, exiting";
 | 
			
		||||
		exit(-1); // NOLINT
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RootWrapper::reloadGraph() {
 | 
			
		||||
	if (this->activeRoot != nullptr) {
 | 
			
		||||
		this->engine.clearComponentCache();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	auto component = QQmlComponent(&this->engine, this->rootUrl);
 | 
			
		||||
	auto* obj = component.beginCreate(this->engine.rootContext());
 | 
			
		||||
	if (obj == nullptr) {
 | 
			
		||||
		qWarning() << "failed to create root component";
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	auto* qtsobj = qobject_cast<QtShell*>(obj);
 | 
			
		||||
	if (qtsobj == nullptr) {
 | 
			
		||||
		qWarning() << "root component was not a QtShell";
 | 
			
		||||
		delete obj;
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	component.completeCreate();
 | 
			
		||||
 | 
			
		||||
	if (this->activeRoot != nullptr) {
 | 
			
		||||
		this->activeRoot->deleteLater();
 | 
			
		||||
		this->activeRoot = nullptr;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this->activeRoot = qtsobj;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RootWrapper::changeRoot(QtShell* newRoot) {
 | 
			
		||||
	if (this->activeRoot != nullptr) {
 | 
			
		||||
		QObject::disconnect(this->destroyConnection);
 | 
			
		||||
		this->activeRoot->deleteLater();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (newRoot != nullptr) {
 | 
			
		||||
		this->activeRoot = newRoot;
 | 
			
		||||
		QObject::connect(this->activeRoot, &QtShell::destroyed, this, &RootWrapper::destroy);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RootWrapper::destroy() { this->deleteLater(); }
 | 
			
		||||
							
								
								
									
										28
									
								
								src/cpp/rootwrapper.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/cpp/rootwrapper.hpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <qobject.h>
 | 
			
		||||
#include <qobjectdefs.h>
 | 
			
		||||
#include <qqmlengine.h>
 | 
			
		||||
#include <qtmetamacros.h>
 | 
			
		||||
#include <qurl.h>
 | 
			
		||||
 | 
			
		||||
#include "shell.hpp"
 | 
			
		||||
 | 
			
		||||
class RootWrapper: public QObject {
 | 
			
		||||
	Q_OBJECT;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
	explicit RootWrapper(QUrl rootUrl);
 | 
			
		||||
 | 
			
		||||
	void reloadGraph();
 | 
			
		||||
	void changeRoot(QtShell* newRoot);
 | 
			
		||||
 | 
			
		||||
private slots:
 | 
			
		||||
	void destroy();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	QUrl rootUrl;
 | 
			
		||||
	QQmlEngine engine;
 | 
			
		||||
	QtShell* activeRoot = nullptr;
 | 
			
		||||
	QMetaObject::Connection destroyConnection;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -11,18 +11,25 @@
 | 
			
		|||
#include <qqmllist.h>
 | 
			
		||||
#include <qtmetamacros.h>
 | 
			
		||||
 | 
			
		||||
#include "rootwrapper.hpp"
 | 
			
		||||
 | 
			
		||||
QString CONFIG_PATH; // NOLINT
 | 
			
		||||
 | 
			
		||||
QtShell::QtShell(): QObject(nullptr), path(CONFIG_PATH), dir(QFileInfo(this->path).dir()) {
 | 
			
		||||
	CONFIG_PATH = "";
 | 
			
		||||
}
 | 
			
		||||
QtShell::QtShell(): QObject(nullptr), path(CONFIG_PATH), dir(QFileInfo(this->path).dir()) {}
 | 
			
		||||
 | 
			
		||||
void QtShell::componentComplete() {
 | 
			
		||||
	if (this->path.isEmpty()) {
 | 
			
		||||
		qWarning() << "Multiple QtShell objects were created. You should not do this.";
 | 
			
		||||
void QtShell::reload() {
 | 
			
		||||
	auto* rootobj = QQmlEngine::contextForObject(this)->engine()->parent();
 | 
			
		||||
	auto* root = qobject_cast<RootWrapper*>(rootobj);
 | 
			
		||||
 | 
			
		||||
	if (root == nullptr) {
 | 
			
		||||
		qWarning() << "cannot find RootWrapper for reload, ignoring request";
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	root->reloadGraph();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void QtShell::componentComplete() {
 | 
			
		||||
	for (auto* component: this->mComponents) {
 | 
			
		||||
		component->prepare(this->dir);
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,11 +23,14 @@ class QtShell: public QObject, public QQmlParserStatus {
 | 
			
		|||
public:
 | 
			
		||||
	explicit QtShell();
 | 
			
		||||
 | 
			
		||||
	void classBegin() override {};
 | 
			
		||||
	void classBegin() override {}
 | 
			
		||||
	void componentComplete() override;
 | 
			
		||||
 | 
			
		||||
	QQmlListProperty<ShellComponent> components();
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
	void reload();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	static void appendComponent(QQmlListProperty<ShellComponent>* list, ShellComponent* component);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue