Skip to content

Commit

Permalink
Merge pull request #173 from mistzzt/fix-macos-drag-n-openwith
Browse files Browse the repository at this point in the history
Add support for `QEvent::FileOpen` (#115)
  • Loading branch information
ahrm authored Apr 3, 2022
2 parents 92ac67a + 609064f commit fe81559
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pdf_viewer/OpenWithApplication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef OPEN_WITH_APP_H
#define OPEN_WITH_APP_H

#include <qapplication.h>

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
19 changes: 18 additions & 1 deletion pdf_viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<QFileOpenEvent *>(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;
Expand All @@ -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());
Expand Down Expand Up @@ -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, [&]() {

Expand Down
4 changes: 3 additions & 1 deletion pdf_viewer_build_config.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
}

39 changes: 39 additions & 0 deletions resources/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>sioyek</string>
<key>CFBundleIconFile</key>
<string>icon2.ico</string>
<key>CFBundleIdentifier</key>
<string>info.sioyek.sioyek</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<key>LSMinimumSystemVersion</key>
<string>10.15</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSSupportsAutomaticGraphicsSwitching</key>
<true/>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
</dict>
</plist>

0 comments on commit fe81559

Please sign in to comment.