gtk: accept dropping URLs from browsers onto the main window (#2232)

Unlike files, which come as a URI list, links dragged from browsers have
textual selection types:

- Firefox: text/plain;charset=utf-8
- Chrome: UTF8_STRING

If the URI list for files is empty, try to pass the selection text to
add_from_url().
This commit is contained in:
Jonas Malaco 2021-11-26 12:59:10 -03:00 committed by GitHub
parent 8d2ca760e6
commit 6cc9afe1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 4 deletions

View File

@ -815,11 +815,23 @@ void Application::Impl::on_drag_data_received(
{
auto const uris = selection_data.get_uris();
if (!uris.empty())
{
auto files = std::vector<Glib::RefPtr<Gio::File>>();
files.reserve(uris.size());
std::transform(uris.begin(), uris.end(), std::back_inserter(files), &Gio::File::create_for_uri);
open_files(files);
}
else
{
auto const text = gtr_str_strip(selection_data.get_text());
if (!text.empty())
{
core_->add_from_url(text);
}
}
drag_context->drag_finish(true, false, time_);
}
@ -840,6 +852,7 @@ void Application::Impl::main_window_setup()
/* register to handle URIs that get dragged onto our main window */
wind_->drag_dest_set(Gtk::DEST_DEFAULT_ALL, Gdk::ACTION_COPY);
wind_->drag_dest_add_uri_targets();
wind_->drag_dest_add_text_targets(); /* links dragged from browsers are text */
wind_->signal_drag_data_received().connect(sigc::mem_fun(*this, &Impl::on_drag_data_received));
}