From 609064fa5aedab6efb4e1409fbbe0f2888c4ec89 Mon Sep 17 00:00:00 2001 From: Ziteng Wang Date: Sat, 2 Apr 2022 19:07:40 -0700 Subject: [PATCH] Add support for open-with feature --- pdf_viewer/OpenWithApplication.h | 22 ++++++++++++++++++ pdf_viewer/main.cpp | 19 +++++++++++++++- pdf_viewer_build_config.pro | 4 +++- resources/Info.plist | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 pdf_viewer/OpenWithApplication.h create mode 100644 resources/Info.plist diff --git a/pdf_viewer/OpenWithApplication.h b/pdf_viewer/OpenWithApplication.h new file mode 100644 index 000000000..f78e32f04 --- /dev/null +++ b/pdf_viewer/OpenWithApplication.h @@ -0,0 +1,22 @@ +#ifndef OPEN_WITH_APP_H +#define OPEN_WITH_APP_H + +#include + +class OpenWithApplication : public QApplication +{ + Q_OBJECT +public: + QString file_name; + OpenWithApplication(int &argc, char **argv) + : QApplication(argc, argv) + { + } +signals: + void fileReady(QString fn); + +protected: + bool event(QEvent *event) override; +}; + +#endif // OPEN_WITH_APP_H \ No newline at end of file diff --git a/pdf_viewer/main.cpp b/pdf_viewer/main.cpp index c24fa1630..e43915903 100644 --- a/pdf_viewer/main.cpp +++ b/pdf_viewer/main.cpp @@ -74,6 +74,7 @@ #include "path.h" #include "RunGuard.h" #include "checksum.h" +#include "OpenWithApplication.h" #define FTS_FUZZY_MATCH_IMPLEMENTATION #include "fts_fuzzy_match.h" @@ -357,6 +358,17 @@ void add_paths_to_file_system_watcher(QFileSystemWatcher& watcher, const Path& d } } +bool OpenWithApplication::event(QEvent *event) +{ + if (event->type() == QEvent::FileOpen) { + QFileOpenEvent *openEvent = static_cast(event); + file_name = openEvent->file(); + emit fileReady(file_name); // the file is ready + } + + return QApplication::event(event); +} + int main(int argc, char* args[]) { QSurfaceFormat format; @@ -365,7 +377,7 @@ int main(int argc, char* args[]) { QSurfaceFormat::setDefaultFormat(format); QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true); - QApplication app(argc, args); + OpenWithApplication app(argc, args); QCommandLineParser* parser = get_command_line_parser(); parser->process(app.arguments()); @@ -488,6 +500,11 @@ int main(int argc, char* args[]) { main_widget.handle_args(app.arguments()); + // load input file from `QFileOpenEvent` for macOS drag and drop & "open with" + QObject::connect(&app, &OpenWithApplication::fileReady, [&main_widget](QString fileName) { + main_widget.open_document(fileName.toStdWString()); + }); + // live reload the config files QObject::connect(&pref_file_watcher, &QFileSystemWatcher::fileChanged, [&]() { diff --git a/pdf_viewer_build_config.pro b/pdf_viewer_build_config.pro index 0b30ef80b..296016f28 100644 --- a/pdf_viewer_build_config.pro +++ b/pdf_viewer_build_config.pro @@ -37,7 +37,8 @@ HEADERS += pdf_viewer/book.h \ pdf_viewer/utf8/unchecked.h \ pdf_viewer/synctex/synctex_parser.h \ pdf_viewer/synctex/synctex_parser_utils.h \ - pdf_viewer/RunGuard.h + pdf_viewer/RunGuard.h \ + pdf_viewer/OpenWithApplication.h SOURCES += pdf_viewer/book.cpp \ pdf_viewer/config.cpp \ @@ -92,5 +93,6 @@ mac { CONFIG+=sdk_no_version_check QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.15 ICON = pdf_viewer\icon2.ico + QMAKE_INFO_PLIST = resources/Info.plist } diff --git a/resources/Info.plist b/resources/Info.plist new file mode 100644 index 000000000..6c9898ce9 --- /dev/null +++ b/resources/Info.plist @@ -0,0 +1,39 @@ + + + + + CFBundleExecutable + sioyek + CFBundleIconFile + icon2.ico + CFBundleIdentifier + info.sioyek.sioyek + CFBundlePackageType + APPL + CFBundleSignature + ???? + CFBundleShortVersionString + 1.1.0 + LSMinimumSystemVersion + 10.15 + NSPrincipalClass + NSApplication + NSSupportsAutomaticGraphicsSwitching + + CFBundleDocumentTypes + + + CFBundleTypeName + PDF Document + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + LSItemContentTypes + + com.adobe.pdf + + + + +