From 280144f4eb758711be859a366dcb9e803a103f36 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 15 Aug 2021 10:10:05 +0800 Subject: [PATCH] macOS: handle file open event requested by Finder --- src/application.cpp | 23 +++++++++++++++++++++++ src/application.h | 22 ++++++++++++++++++++++ src/main.cpp | 9 +++++++-- src/src.pro | 2 ++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/application.cpp create mode 100644 src/application.h diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 00000000..02e4e86d --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,23 @@ +#include "application.h" + +#include +#include + +using namespace vnotex; + +Application::Application(int &p_argc, char **p_argv) + : QApplication(p_argc, p_argv) +{ +} + +bool Application::event(QEvent *p_event) +{ + // On macOS, we need this to open file from Finder. + if (p_event->type() == QEvent::FileOpen) { + QFileOpenEvent *openEvent = static_cast(p_event); + qDebug() << "request to open file" << openEvent->file(); + emit openFileRequested(openEvent->file()); + } + + return QApplication::event(p_event); +} diff --git a/src/application.h b/src/application.h new file mode 100644 index 00000000..4e08903e --- /dev/null +++ b/src/application.h @@ -0,0 +1,22 @@ +#ifndef APPLICATION_H +#define APPLICATION_H + +#include + +namespace vnotex +{ + class Application : public QApplication + { + Q_OBJECT + public: + Application(int &p_argc, char **p_argv); + + signals: + void openFileRequested(const QString &p_filePath); + + protected: + bool event(QEvent *p_event) Q_DECL_OVERRIDE; + }; +} + +#endif // APPLICATION_H diff --git a/src/main.cpp b/src/main.cpp index a24f1f24..e9092ca7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -22,6 +21,7 @@ #include #include #include "commandlineoptions.h" +#include "application.h" using namespace vnotex; @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) } #endif - QApplication app(argc, argv); + Application app(argc, argv); initWebEngineSettings(); @@ -162,6 +162,11 @@ int main(int argc, char *argv[]) QObject::connect(&guard, &SingleInstanceGuard::openFilesRequested, &window, &MainWindow::openFiles); + QObject::connect(&app, &Application::openFileRequested, + &window, [&window](const QString &p_filePath) { + window.openFiles(QStringList() << p_filePath); + }); + window.kickOffOnStart(cmdOptions.m_pathsToOpen); int ret = app.exec(); diff --git a/src/src.pro b/src/src.pro index 28b4ade5..6556b947 100644 --- a/src/src.pro +++ b/src/src.pro @@ -32,6 +32,7 @@ TRANSLATIONS += \ data/core/translations/vnote_ja.ts SOURCES += \ + application.cpp \ commandlineoptions.cpp \ main.cpp @@ -148,4 +149,5 @@ unix:!macx { } HEADERS += \ + application.h \ commandlineoptions.h