forked from quickshell/quickshell
		
	core/menu: opening platform menus w/o QApplication no longer crashes
An error is displayed instead.
This commit is contained in:
		
							parent
							
								
									1d2bf5d7b4
								
							
						
					
					
						commit
						f89c504b55
					
				
					 2 changed files with 30 additions and 2 deletions
				
			
		| 
						 | 
					@ -4,7 +4,9 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <qaction.h>
 | 
					#include <qaction.h>
 | 
				
			||||||
#include <qactiongroup.h>
 | 
					#include <qactiongroup.h>
 | 
				
			||||||
 | 
					#include <qapplication.h>
 | 
				
			||||||
#include <qcontainerfwd.h>
 | 
					#include <qcontainerfwd.h>
 | 
				
			||||||
 | 
					#include <qcoreapplication.h>
 | 
				
			||||||
#include <qicon.h>
 | 
					#include <qicon.h>
 | 
				
			||||||
#include <qlogging.h>
 | 
					#include <qlogging.h>
 | 
				
			||||||
#include <qmenu.h>
 | 
					#include <qmenu.h>
 | 
				
			||||||
| 
						 | 
					@ -74,7 +76,13 @@ void PlatformMenuEntry::registerCreationHook(std::function<void(PlatformMenuQMen
 | 
				
			||||||
bool PlatformMenuEntry::display(QObject* parentWindow, int relativeX, int relativeY) {
 | 
					bool PlatformMenuEntry::display(QObject* parentWindow, int relativeX, int relativeY) {
 | 
				
			||||||
	QWindow* window = nullptr;
 | 
						QWindow* window = nullptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (this->qmenu == nullptr) {
 | 
						if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
 | 
				
			||||||
 | 
							qCritical() << "Cannot display PlatformMenuEntry as quickshell was not started in "
 | 
				
			||||||
 | 
							               "QApplication mode.";
 | 
				
			||||||
 | 
							qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
 | 
				
			||||||
 | 
							               "root QML file and restart quickshell.";
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						} else if (this->qmenu == nullptr) {
 | 
				
			||||||
		qCritical() << "Cannot display PlatformMenuEntry as it is not a menu.";
 | 
							qCritical() << "Cannot display PlatformMenuEntry as it is not a menu.";
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	} else if (parentWindow == nullptr) {
 | 
						} else if (parentWindow == nullptr) {
 | 
				
			||||||
| 
						 | 
					@ -113,7 +121,13 @@ bool PlatformMenuEntry::display(QObject* parentWindow, int relativeX, int relati
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool PlatformMenuEntry::display(PopupAnchor* anchor) {
 | 
					bool PlatformMenuEntry::display(PopupAnchor* anchor) {
 | 
				
			||||||
	if (!anchor->backingWindow() || !anchor->backingWindow()->isVisible()) {
 | 
						if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
 | 
				
			||||||
 | 
							qCritical() << "Cannot display PlatformMenuEntry as quickshell was not started in "
 | 
				
			||||||
 | 
							               "QApplication mode.";
 | 
				
			||||||
 | 
							qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
 | 
				
			||||||
 | 
							               "root QML file and restart quickshell.";
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						} else if (!anchor->backingWindow() || !anchor->backingWindow()->isVisible()) {
 | 
				
			||||||
		qCritical() << "Cannot display PlatformMenuEntry on anchor without visible window.";
 | 
							qCritical() << "Cannot display PlatformMenuEntry on anchor without visible window.";
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -140,6 +154,10 @@ bool PlatformMenuEntry::display(PopupAnchor* anchor) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void PlatformMenuEntry::relayout() {
 | 
					void PlatformMenuEntry::relayout() {
 | 
				
			||||||
 | 
						if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (this->menu->hasChildren()) {
 | 
						if (this->menu->hasChildren()) {
 | 
				
			||||||
		delete this->qaction;
 | 
							delete this->qaction;
 | 
				
			||||||
		this->qaction = nullptr;
 | 
							this->qaction = nullptr;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,7 @@
 | 
				
			||||||
#include "qsmenuanchor.hpp"
 | 
					#include "qsmenuanchor.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <qapplication.h>
 | 
				
			||||||
 | 
					#include <qcoreapplication.h>
 | 
				
			||||||
#include <qlogging.h>
 | 
					#include <qlogging.h>
 | 
				
			||||||
#include <qobject.h>
 | 
					#include <qobject.h>
 | 
				
			||||||
#include <qtmetamacros.h>
 | 
					#include <qtmetamacros.h>
 | 
				
			||||||
| 
						 | 
					@ -15,6 +17,14 @@ namespace qs::menu {
 | 
				
			||||||
QsMenuAnchor::~QsMenuAnchor() { this->onClosed(); }
 | 
					QsMenuAnchor::~QsMenuAnchor() { this->onClosed(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void QsMenuAnchor::open() {
 | 
					void QsMenuAnchor::open() {
 | 
				
			||||||
 | 
						if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
 | 
				
			||||||
 | 
							qCritical() << "Cannot call QsMenuAnchor.open() as quickshell was not started in "
 | 
				
			||||||
 | 
							               "QApplication mode.";
 | 
				
			||||||
 | 
							qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
 | 
				
			||||||
 | 
							               "root QML file and restart quickshell.";
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (this->mOpen) {
 | 
						if (this->mOpen) {
 | 
				
			||||||
		qCritical() << "Cannot call QsMenuAnchor.open() as it is already open.";
 | 
							qCritical() << "Cannot call QsMenuAnchor.open() as it is already open.";
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue