From 1c764babd164fbe71d954801a619ccfc64515c93 Mon Sep 17 00:00:00 2001 From: scorpio810 Date: Sun, 21 Jun 2026 11:00:48 +0000 Subject: [PATCH] macOS: buffer QFileOpenEvent during cold launch before QETApp exists Follow-up to the eventFilter fix: testing showed double-click works when QET is already running, but a cold launch (app not running yet) still opens an empty window. Finder/Launch Services can deliver the QFileOpenEvent to the QApplication's native event loop before main() reaches the point where QETApp is constructed and its real eventFilter is installed -- there's a window between 'SingleApplication app(...)' and 'QETApp qetapp;' during which the event can arrive and be lost. Add a minimal EarlyFileOpenCatcher installed on app immediately after it's constructed (before anything else can run an event loop). It only buffers the file path. Once QETApp exists, main() swaps it out for the real QETApp::eventFilter and drains anything that was buffered via qetapp.openFiles(), so no cold-launch QFileOpenEvent is silently dropped. Confirmed by manual testing: - 'open app --args file' on .qet/.elmt/.titleblock: OK (already worked) - double-click while app already running: OK (already worked) - double-click cold launch: previously opened an empty window, this buffers and replays the event so it now opens the right editor. --- sources/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/sources/main.cpp b/sources/main.cpp index 940aa4e16..9a86400ff 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -28,6 +28,38 @@ #include #include +#ifdef Q_OS_MACOS +#include + +/** + @brief EarlyFileOpenCatcher + On macOS, a cold launch via Finder double-click can deliver the + QFileOpenEvent to QApplication before QETApp exists and before its + real eventFilter is installed (the event loop can start servicing + native/Cocoa events before our own code in main() reaches that + point). This tiny filter is installed immediately on `app` so no + QFileOpenEvent can slip through unseen; it just buffers the path. + Once QETApp is constructed, main() drains the buffer and installs + the real QETApp::eventFilter for any subsequent event. +*/ +class EarlyFileOpenCatcher : public QObject +{ + public: + using QObject::QObject; + QStringList bufferedFiles; + + protected: + bool eventFilter(QObject *object, QEvent *e) override + { + if (e->type() == QEvent::FileOpen) { + bufferedFiles << static_cast(e)->file(); + return true; + } + return QObject::eventFilter(object, e); + } +}; +#endif + /** @brief myMessageOutput for debugging @@ -217,6 +249,11 @@ QGuiApplication::setHighDpiScaleFactorRoundingPolicy(QetSettings::hdpiScaleFacto SingleApplication app(argc, argv, true); #ifdef Q_OS_MACOS app.setStyle(QStyleFactory::create("Fusion")); + // Installed as early as possible, before anything else can run an + // event loop, to catch a QFileOpenEvent that might be delivered + // during a cold launch before QETApp exists. + EarlyFileOpenCatcher early_catcher; + app.installEventFilter(&early_catcher); #endif if (app.isSecondary()) @@ -236,9 +273,13 @@ QGuiApplication::setHighDpiScaleFactorRoundingPolicy(QetSettings::hdpiScaleFacto #ifdef Q_OS_MACOS //Handle the opening of QET when user double click on a .qet .elmt .tbt file //or drop these same files to the QET icon of the dock. - //Installed here (after QETApp is fully constructed, before app.exec()) - //so there is no race: no QFileOpenEvent can be delivered before this point. + //Swap the early catcher (installed right after `app` was constructed, + //see above) for the real filter, then drain anything it buffered + //during the cold-launch window before QETApp existed. + app.removeEventFilter(&early_catcher); app.installEventFilter(&qetapp); + if (!early_catcher.bufferedFiles.isEmpty()) + qetapp.openFiles(QETArguments(early_catcher.bufferedFiles)); #endif QObject::connect(&app, &SingleApplication::receivedMessage, &qetapp, &QETApp::receiveMessage);