[meld] filediff: Support drag-and-drop directly on to textviews (#177)



commit 32e39ec7e3ef2d10fe5943219cc00a7b45ccdfbc
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Jul 22 06:59:24 2018 +1000

    filediff: Support drag-and-drop directly on to textviews (#177)
    
    The behaviour here is hopefully fairly intuitive. If the user drops a
    single URI on a file pane, we will load it into that pane. If they drop
    several URIs, we'll check to see whether they've dropped the right
    number to load all panes, and if so then load them all.
    
    This doesn't handle correctly prelighting of drop zones to e.g., show
    the user that they can't drop two files on to a three-way comparison.
    This omission is partly due to laziness, but mostly because I don't
    think it's a big deal and the drag-motion signal is a bit wild given
    that we'd have to coordinate our handling with GtkTextView and also need
    to handle the request data dance with drag-data-received.

 meld/filediff.py   | 18 ++++++++++++++++++
 meld/sourceview.py |  3 +++
 2 files changed, 21 insertions(+)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 97e0a8c9..d4ecbb01 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -205,6 +205,8 @@ class FileDiff(MeldDoc, Component):
         for t in self.textview:
             t.connect("focus-in-event", self.on_current_diff_changed)
             t.connect("focus-out-event", self.on_current_diff_changed)
+            t.connect(
+                "drag_data_received", self.on_textview_drag_data_received)
 
         # Bind all overwrite properties together, so that toggling
         # overwrite mode is per-FileDiff.
@@ -768,6 +770,22 @@ class FileDiff(MeldDoc, Component):
         except AttributeError:
             pass
 
+    def on_textview_drag_data_received(
+            self, widget, context, x, y, selection_data, info, time):
+        uris = selection_data.get_uris()
+        if uris:
+            gfiles = [Gio.File.new_for_uri(uri) for uri in uris]
+
+            if len(gfiles) == self.num_panes:
+                if self.check_save_modified() == Gtk.ResponseType.OK:
+                    self.set_files(gfiles)
+            elif len(gfiles) == 1:
+                pane = self.textview.index(widget)
+                buffer = self.textbuffer[pane]
+                if self.check_save_modified([buffer]) == Gtk.ResponseType.OK:
+                    self.set_file(pane, gfiles[0])
+            return True
+
     def on_textview_focus_in_event(self, view, event):
         self.focus_pane = view
         self.findbar.textview = view
diff --git a/meld/sourceview.py b/meld/sourceview.py
index a3f3c21d..c28a70d7 100644
--- a/meld/sourceview.py
+++ b/meld/sourceview.py
@@ -147,6 +147,9 @@ class MeldSourceView(GtkSource.View):
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
+
+        self.drag_dest_add_uri_targets()
+
         binding_set = Gtk.binding_set_find('GtkSourceView')
         for key, modifiers in self.replaced_entries:
             Gtk.binding_entry_remove(binding_set, key, modifiers)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]