From 6cc9afe1c285ccd0c107a6292b9846438eedb41e Mon Sep 17 00:00:00 2001 From: Jonas Malaco Date: Fri, 26 Nov 2021 12:59:10 -0300 Subject: [PATCH] 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(). --- gtk/Application.cc | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/gtk/Application.cc b/gtk/Application.cc index 0125ea564..4dff344a9 100644 --- a/gtk/Application.cc +++ b/gtk/Application.cc @@ -815,11 +815,23 @@ void Application::Impl::on_drag_data_received( { auto const uris = selection_data.get_uris(); - auto files = std::vector>(); - files.reserve(uris.size()); - std::transform(uris.begin(), uris.end(), std::back_inserter(files), &Gio::File::create_for_uri); + if (!uris.empty()) + { + auto files = std::vector>(); + files.reserve(uris.size()); + std::transform(uris.begin(), uris.end(), std::back_inserter(files), &Gio::File::create_for_uri); - open_files(files); + 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)); }