From 2fdbf96d6ff9828c946197e17d803f45510c2aa4 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sun, 27 Aug 2023 17:10:39 +0200 Subject: Act as a drop destination for drag & drop For now we support text and URIs. If file:// URIs are dropped, the file:// prefix will be removed from each, and they will be unescaped and quoted for use in the shell. --- terminal.ui | 1 + terminal.vala | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/terminal.ui b/terminal.ui index 6d5d206..af94317 100644 --- a/terminal.ui +++ b/terminal.ui @@ -139,6 +139,7 @@ + diff --git a/terminal.vala b/terminal.vala index 67256d4..fcb456c 100644 --- a/terminal.vala +++ b/terminal.vala @@ -70,6 +70,13 @@ class Terminal : Gtk.Overlay { uri_context_menu.attach_to_widget(vte, null); standard_context_menu.attach_to_widget(vte, null); + Gtk.TargetList targets = new Gtk.TargetList({}); + targets.add_text_targets(0); + targets.add_uri_targets(1); + + Gtk.drag_dest_set(vte, Gtk.DestDefaults.ALL, {}, Gdk.DragAction.COPY); + Gtk.drag_dest_set_target_list(vte, targets); + clipboard = Gtk.Clipboard.get_default(window.get_display()); primary = Gtk.Clipboard.get_for_display(window.get_display(), Gdk.SELECTION_PRIMARY); @@ -591,4 +598,31 @@ class Terminal : Gtk.Overlay { window.set_urgency_hint(false); window.set_urgency_hint(true); } + + [GtkCallback] + void vte_drag_data_received(Gdk.DragContext context, int x, int y, Gtk.SelectionData selection_data, uint info, uint time_) { + if (info == 0) { + string text = selection_data.get_text(); + + if (text != null) { + vte.paste_text(text); + } + } else if (info == 1) { + string[] uris = selection_data.get_uris(); + string text = ""; + + foreach (var uri in uris) { + if (uri == null || uri.length == 0) + continue; + + if (uri.has_prefix("file://")) { + uri = Shell.quote(Uri.unescape_string(uri.substring(7))); + } + + text = string.join(" ", text, uri); + } + + vte.paste_text(text.strip()); + } + } } -- cgit v1.2.3-2-gb3c3