forked from quickshell/quickshell
		
	singleton: add reloadable Singleton type
This commit is contained in:
		
							parent
							
								
									463f9a297f
								
							
						
					
					
						commit
						211f454de9
					
				
					 6 changed files with 108 additions and 5 deletions
				
			
		| 
						 | 
					@ -65,13 +65,13 @@ find_package(Qt6 REQUIRED COMPONENTS ${QT_FPDEPS})
 | 
				
			||||||
qt_standard_project_setup(REQUIRES 6.6)
 | 
					qt_standard_project_setup(REQUIRES 6.6)
 | 
				
			||||||
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml_modules)
 | 
					set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml_modules)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
file(GENERATE
 | 
					 | 
				
			||||||
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp
 | 
					 | 
				
			||||||
	CONTENT ""
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# pch breaks clang-tidy..... somehow
 | 
					# pch breaks clang-tidy..... somehow
 | 
				
			||||||
if (NOT NO_PCH)
 | 
					if (NOT NO_PCH)
 | 
				
			||||||
 | 
						file(GENERATE
 | 
				
			||||||
 | 
							OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp
 | 
				
			||||||
 | 
							CONTENT ""
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	add_library(qt-pch ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp)
 | 
						add_library(qt-pch ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp)
 | 
				
			||||||
	target_link_libraries(qt-pch PRIVATE ${QT_DEPS})
 | 
						target_link_libraries(qt-pch PRIVATE ${QT_DEPS})
 | 
				
			||||||
	target_precompile_headers(qt-pch PUBLIC
 | 
						target_precompile_headers(qt-pch PUBLIC
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,7 @@ qt_add_library(quickshell-core STATIC
 | 
				
			||||||
	floatingwindow.cpp
 | 
						floatingwindow.cpp
 | 
				
			||||||
	panelinterface.cpp
 | 
						panelinterface.cpp
 | 
				
			||||||
	popupwindow.cpp
 | 
						popupwindow.cpp
 | 
				
			||||||
 | 
						singleton.cpp
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS GIT_REVISION="${GIT_REVISION}")
 | 
					set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS GIT_REVISION="${GIT_REVISION}")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,5 +13,6 @@ headers = [
 | 
				
			||||||
	"panelinterface.hpp",
 | 
						"panelinterface.hpp",
 | 
				
			||||||
	"floatingwindow.hpp",
 | 
						"floatingwindow.hpp",
 | 
				
			||||||
	"popupwindow.hpp",
 | 
						"popupwindow.hpp",
 | 
				
			||||||
 | 
						"singleton.hpp",
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
-----
 | 
					-----
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,7 @@
 | 
				
			||||||
#include "qmlglobal.hpp"
 | 
					#include "qmlglobal.hpp"
 | 
				
			||||||
#include "reload.hpp"
 | 
					#include "reload.hpp"
 | 
				
			||||||
#include "shell.hpp"
 | 
					#include "shell.hpp"
 | 
				
			||||||
 | 
					#include "singleton.hpp"
 | 
				
			||||||
#include "watcher.hpp"
 | 
					#include "watcher.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
RootWrapper::RootWrapper(QString rootPath)
 | 
					RootWrapper::RootWrapper(QString rootPath)
 | 
				
			||||||
| 
						 | 
					@ -49,6 +50,7 @@ void RootWrapper::reloadGraph(bool hard) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (this->root != nullptr) {
 | 
						if (this->root != nullptr) {
 | 
				
			||||||
		QuickshellSettings::reset();
 | 
							QuickshellSettings::reset();
 | 
				
			||||||
 | 
							SingletonRegistry::instance()->flip();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QDir::setCurrent(this->originalWorkingDirectory);
 | 
						QDir::setCurrent(this->originalWorkingDirectory);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										64
									
								
								src/core/singleton.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/core/singleton.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,64 @@
 | 
				
			||||||
 | 
					#include "singleton.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <qlogging.h>
 | 
				
			||||||
 | 
					#include <qmap.h>
 | 
				
			||||||
 | 
					#include <qobject.h>
 | 
				
			||||||
 | 
					#include <qqmlcontext.h>
 | 
				
			||||||
 | 
					#include <qqmlengine.h>
 | 
				
			||||||
 | 
					#include <qurl.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "reload.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Singleton::componentComplete() {
 | 
				
			||||||
 | 
						auto* context = QQmlEngine::contextForObject(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (context == nullptr) {
 | 
				
			||||||
 | 
							qWarning() << "not registering singleton not created in the qml context:" << this;
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						auto url = context->baseUrl();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (this->parent() != nullptr || context->contextObject() != this) {
 | 
				
			||||||
 | 
							qWarning() << "tried to register singleton" << this
 | 
				
			||||||
 | 
							           << "which is not the root component of its file" << url;
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						SingletonRegistry::instance()->install(url, this);
 | 
				
			||||||
 | 
						this->ReloadPropagator::componentComplete();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SingletonRegistry::~SingletonRegistry() {
 | 
				
			||||||
 | 
						delete this->previousRegistry;
 | 
				
			||||||
 | 
						delete this->currentRegistry;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SingletonRegistry::install(const QUrl& url, Singleton* singleton) {
 | 
				
			||||||
 | 
						QObject* old = nullptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (this->previousRegistry != nullptr) {
 | 
				
			||||||
 | 
							old = this->previousRegistry->value(url);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (this->currentRegistry == nullptr) {
 | 
				
			||||||
 | 
							this->currentRegistry = new QMap<QUrl, QObject*>();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this->currentRegistry->insert(url, singleton);
 | 
				
			||||||
 | 
						singleton->onReload(old);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SingletonRegistry::flip() {
 | 
				
			||||||
 | 
						delete this->previousRegistry;
 | 
				
			||||||
 | 
						this->previousRegistry = this->currentRegistry;
 | 
				
			||||||
 | 
						this->currentRegistry = nullptr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SingletonRegistry* SingletonRegistry::instance() {
 | 
				
			||||||
 | 
						static SingletonRegistry* instance = nullptr; // NOLINT
 | 
				
			||||||
 | 
						if (instance == nullptr) {
 | 
				
			||||||
 | 
							instance = new SingletonRegistry();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return instance;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										35
									
								
								src/core/singleton.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/core/singleton.hpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,35 @@
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <qobject.h>
 | 
				
			||||||
 | 
					#include <qqmlintegration.h>
 | 
				
			||||||
 | 
					#include <qtclasshelpermacros.h>
 | 
				
			||||||
 | 
					#include <qtmetamacros.h>
 | 
				
			||||||
 | 
					#include <qurl.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "reload.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					///! The root component for reloadable singletons.
 | 
				
			||||||
 | 
					/// All singletons should inherit from this type.
 | 
				
			||||||
 | 
					class Singleton: public ReloadPropagator {
 | 
				
			||||||
 | 
						Q_OBJECT;
 | 
				
			||||||
 | 
						QML_ELEMENT;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
						void componentComplete() override;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SingletonRegistry {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
						SingletonRegistry() = default;
 | 
				
			||||||
 | 
						~SingletonRegistry();
 | 
				
			||||||
 | 
						Q_DISABLE_COPY_MOVE(SingletonRegistry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void install(const QUrl& url, Singleton* singleton);
 | 
				
			||||||
 | 
						void flip();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						static SingletonRegistry* instance();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
						QMap<QUrl, QObject*>* previousRegistry = nullptr;
 | 
				
			||||||
 | 
						QMap<QUrl, QObject*>* currentRegistry = nullptr;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue