diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp index 02bc774eff..594bcfad16 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp @@ -423,3 +423,32 @@ PDF::PDFErrorOr PDFViewerWidget::try_open_file(StringView path, NonnullOwn return {}; } + +void PDFViewerWidget::drag_enter_event(GUI::DragEvent& event) +{ + auto const& mime_types = event.mime_types(); + if (mime_types.contains_slow("text/uri-list"sv)) + event.accept(); +} + +void PDFViewerWidget::drop_event(GUI::DropEvent& event) +{ + event.accept(); + window()->move_to_front(); + + if (event.mime_data().has_urls()) { + auto urls = event.mime_data().urls(); + if (urls.is_empty()) + return; + if (urls.size() > 1) { + GUI::MessageBox::show(window(), "PDF Viewer can only open one file at a time!"sv, "One at a time please!"sv, GUI::MessageBox::Type::Error); + return; + } + + auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().serialize_path()); + if (response.is_error()) + return; + if (auto result = try_open_file(response.value().filename(), response.value().release_stream()); result.is_error()) + GUI::MessageBox::show(window(), "Unable to open file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); + } +} diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h index 0adfe692c2..0cdcf32942 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.h +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h @@ -30,6 +30,8 @@ public: private: PDFViewerWidget(); + virtual void drag_enter_event(GUI::DragEvent&) override; + virtual void drop_event(GUI::DropEvent&) override; void initialize_toolbar(GUI::Toolbar&); PDF::PDFErrorOr try_open_file(StringView path, NonnullOwnPtr file);