PDFViewer: Accept file drops
This commit is contained in:
parent
fa0606ea36
commit
40d732a73c
Notes:
sideshowbarker
2024-07-17 02:08:15 +09:00
Author: https://github.com/krkk Commit: https://github.com/SerenityOS/serenity/commit/40d732a73c Pull-request: https://github.com/SerenityOS/serenity/pull/20834
2 changed files with 31 additions and 0 deletions
|
@ -423,3 +423,32 @@ PDF::PDFErrorOr<void> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<void> try_open_file(StringView path, NonnullOwnPtr<Core::File> file);
|
||||
|
|
Loading…
Add table
Reference in a new issue