Skip to content

Commit

Permalink
gtk: accept dropping URLs from browsers onto the main window (#2232)
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
jonasmalacofilho committed Nov 26, 2021
1 parent 8d2ca76 commit 6cc9afe
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions gtk/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -815,11 +815,23 @@ void Application::Impl::on_drag_data_received(
{
auto const uris = selection_data.get_uris();

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);
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());

open_files(files);
if (!text.empty())
{
core_->add_from_url(text);
}
}

drag_context->drag_finish(true, false, time_);
}
Expand All @@ -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));
}

Expand Down

0 comments on commit 6cc9afe

Please sign in to comment.