[meld/ui-next] Move next/previous change to GActions



commit a8a566d7a12860e71b1a376191e38dcea48ec590
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Feb 17 10:42:58 2019 +1000

    Move next/previous change to GActions

 data/ui/meldapp-ui.xml         |  5 -----
 meld/accelerators.py           | 12 +++++++++++
 meld/dirdiff.py                | 30 ++++++++++++++++++++------
 meld/filediff.py               | 25 ++++++++++++++++++----
 meld/meldapp.py                |  2 ++
 meld/melddoc.py                |  8 +++----
 meld/meldwindow.py             | 24 +--------------------
 meld/resources/ui/appwindow.ui | 48 ++++++++++++++++++++++++++++++++++++++++++
 meld/vcview.py                 | 30 ++++++++++++++++++++------
 9 files changed, 135 insertions(+), 49 deletions(-)
---
diff --git a/data/ui/meldapp-ui.xml b/data/ui/meldapp-ui.xml
index e16758fd..50b5fdb5 100644
--- a/data/ui/meldapp-ui.xml
+++ b/data/ui/meldapp-ui.xml
@@ -29,9 +29,6 @@
       <menuitem action="GoToLine"/>
     </menu>
     <menu action="ChangesMenu">
-      <menuitem action="PrevChange"/>
-      <menuitem action="NextChange"/>
-      <separator/>
       <placeholder name="ChangesActions" />
     </menu>
     <menu action="ViewMenu">
@@ -54,8 +51,6 @@
 
   <toolbar action="Toolbar">
     <placeholder name="GeneralActions" />
-    <toolitem action="PrevChange"/>
-    <toolitem action="NextChange"/>
     <toolitem action="Stop"/>
     <separator/>
     <placeholder name="SpecialActions" />
diff --git a/meld/accelerators.py b/meld/accelerators.py
new file mode 100644
index 00000000..222d02c1
--- /dev/null
+++ b/meld/accelerators.py
@@ -0,0 +1,12 @@
+
+from gi.repository import Gtk
+
+
+def register_accels(app: Gtk.Application):
+    view_accels = (
+        ("view.next-change", ("<Alt>Down", "<Alt>KP_Down", "<Primary>D")),
+        ("view.previous-change", ("<Alt>Up", "<Alt>KP_Up", "<Primary>E")),
+    )
+    for (name, accel) in view_accels:
+        accel = accel if isinstance(accel, tuple) else (accel,)
+        app.set_accels_for_action(name, accel)
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index 82f3b1a8..5f8ff84b 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -296,7 +296,6 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
     create_diff_signal = MeldDoc.create_diff_signal
     file_changed_signal = MeldDoc.file_changed_signal
     label_changed = MeldDoc.label_changed
-    next_diff_changed_signal = MeldDoc.next_diff_changed_signal
     tab_state_changed = MeldDoc.tab_state_changed
 
     __gsettings_bindings__ = (
@@ -411,6 +410,17 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
         self.ui_file = ui_file("dirdiff-ui.xml")
         self.actiongroup.set_translation_domain("meld")
 
+        # Manually handle GAction additions
+        actions = (
+            ('next-change', self.action_next_change),
+            ('previous-change', self.action_previous_change),
+        )
+        self.view_action_group = Gio.SimpleActionGroup()
+        for name, callback in actions:
+            action = Gio.SimpleAction.new(name, None)
+            action.connect('activate', callback)
+            self.view_action_group.add_action(action)
+
         self.name_filters = []
         self.text_filters = []
         self.create_name_filters()
@@ -1163,7 +1173,8 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
 
         cursor_path, cursor_col = self.treeview[pane].get_cursor()
         if not cursor_path:
-            self.next_diff_changed_signal.emit(False, False)
+            self.set_action_enabled("previous-change", False)
+            self.set_action_enabled("next-change", False)
             self.current_path = cursor_path
             return
 
@@ -1197,10 +1208,11 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
                         skip = self.prev_path < cursor_path < self.next_path
 
         if not skip:
-            prev, next = self.model._find_next_prev_diff(cursor_path)
-            self.prev_path, self.next_path = prev, next
-            have_next_diffs = (prev is not None, next is not None)
-            self.next_diff_changed_signal.emit(*have_next_diffs)
+            prev, next_ = self.model._find_next_prev_diff(cursor_path)
+            self.prev_path, self.next_path = prev, next_
+            self.set_action_enabled("previous-change", prev is not None)
+            self.set_action_enabled("next-change", next_ is not None)
+
         self.current_path = cursor_path
 
     @Template.Callback()
@@ -1675,6 +1687,12 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
             self.treeview[pane].expand_to_path(path)
             self.treeview[pane].set_cursor(path)
 
+    def action_previous_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.UP)
+
+    def action_next_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.DOWN)
+
     def on_refresh_activate(self, *extra):
         self.on_fileentry_file_set(None)
 
diff --git a/meld/filediff.py b/meld/filediff.py
index 1ddbb4c6..e9f8439a 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -105,7 +105,6 @@ class FileDiff(Gtk.VBox, MeldDoc):
     create_diff_signal = MeldDoc.create_diff_signal
     file_changed_signal = MeldDoc.file_changed_signal
     label_changed = MeldDoc.label_changed
-    next_diff_changed_signal = MeldDoc.next_diff_changed_signal
     tab_state_changed = MeldDoc.tab_state_changed
 
     __gsettings_bindings_view__ = (
@@ -275,13 +274,25 @@ class FileDiff(Gtk.VBox, MeldDoc):
         self.view_action_group = Gio.SimpleActionGroup()
         action = Gio.PropertyAction.new(
             'show-sourcemap', self, 'show-sourcemap')
+        self.view_action_group.add_action(action)
+
+        # Manually handle GAction additions
+        actions = (
+            ('next-change', self.action_next_change),
+            ('previous-change', self.action_previous_change),
+        )
+        for name, callback in actions:
+            action = Gio.SimpleAction.new(name, None)
+            action.connect('activate', callback)
+            self.view_action_group.add_action(action)
+
+        # Handle sourcemap visibility binding
         self.bind_property(
             'show-sourcemap', self.sourcemap_revealer, 'reveal-child',
             GObject.BindingFlags.DEFAULT | GObject.BindingFlags.SYNC_CREATE,
         )
         self.sourcemap_revealer.bind_property(
             'child-revealed', self.dummy_toolbar_sourcemap, 'visible')
-        self.view_action_group.add_action(action)
 
         for buf in self.textbuffer:
             buf.undo_sequence = self.undosequence
@@ -466,8 +477,8 @@ class FileDiff(Gtk.VBox, MeldDoc):
                 self.cursor.chunk = chunk
                 self.on_current_diff_changed()
             if prev != self.cursor.prev or next_ != self.cursor.next or force:
-                self.next_diff_changed_signal.emit(
-                    prev is not None, next_ is not None)
+                self.set_action_enabled("previous-change", prev is not None)
+                self.set_action_enabled("next-change", next_ is not None)
 
             prev_conflict, next_conflict = None, None
             for conflict in self.linediffer.conflicts:
@@ -616,6 +627,12 @@ class FileDiff(Gtk.VBox, MeldDoc):
                   else self.cursor.prev)
         self.go_to_chunk(target, centered=centered)
 
+    def action_previous_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.UP)
+
+    def action_next_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.DOWN)
+
     @Template.Callback()
     def action_previous_conflict(self, *args):
         self.go_to_chunk(self.cursor.prev_conflict, self.cursor.pane)
diff --git a/meld/meldapp.py b/meld/meldapp.py
index 2c77afa9..75e4f766 100644
--- a/meld/meldapp.py
+++ b/meld/meldapp.py
@@ -24,6 +24,7 @@ from gi.repository import Gio
 from gi.repository import GLib
 from gi.repository import Gtk
 
+import meld.accelerators
 import meld.conf
 from meld.conf import _
 from meld.filediff import FileDiff
@@ -52,6 +53,7 @@ class MeldApp(Gtk.Application):
 
     def do_startup(self):
         Gtk.Application.do_startup(self)
+        meld.accelerators.register_accels(self)
 
         actions = (
             ("preferences", self.preferences_callback),
diff --git a/meld/melddoc.py b/meld/melddoc.py
index 0bea3798..0f185e54 100644
--- a/meld/melddoc.py
+++ b/meld/melddoc.py
@@ -85,11 +85,6 @@ class MeldDoc(LabeledObjectMixin, GObject.GObject):
     def file_changed_signal(self, path: str) -> None:
         ...
 
-    @GObject.Signal('next-diff-changed')
-    def next_diff_changed_signal(
-            self, have_prev: bool, have_next: bool) -> None:
-        ...
-
     @GObject.Signal
     def tab_state_changed(self, old_state: int, new_state: int) -> None:
         ...
@@ -204,6 +199,9 @@ class MeldDoc(LabeledObjectMixin, GObject.GObject):
     def set_labels(self, lst):
         pass
 
+    def set_action_enabled(self, action, enabled):
+        self.view_action_group.lookup_action(action).set_enabled(enabled)
+
     def on_container_switch_in_event(self, uimanager, window):
         """Called when the container app switches to this tab.
         """
diff --git a/meld/meldwindow.py b/meld/meldwindow.py
index 1f83f4d5..235e80c2 100644
--- a/meld/meldwindow.py
+++ b/meld/meldwindow.py
@@ -97,12 +97,6 @@ class MeldWindow(Gtk.ApplicationWindow):
                 self.on_menu_go_to_line_activate),
 
             ("ChangesMenu", None, _("_Changes")),
-            ("NextChange", Gtk.STOCK_GO_DOWN, _("Next Change"), "<Alt>Down",
-                _("Go to the next change"),
-                self.on_menu_edit_down_activate),
-            ("PrevChange", Gtk.STOCK_GO_UP, _("Previous Change"), "<Alt>Up",
-                _("Go to the previous change"),
-                self.on_menu_edit_up_activate),
             ("OpenExternal", None, _("Open Externally"), None,
                 _("Open selected file or directory in the default external "
                   "application"),
@@ -146,10 +140,6 @@ class MeldWindow(Gtk.ApplicationWindow):
 
         # Alternate keybindings for a few commands.
         extra_accels = (
-            ("<Primary>D", self.on_menu_edit_down_activate),
-            ("<Primary>E", self.on_menu_edit_up_activate),
-            ("<Alt>KP_Down", self.on_menu_edit_down_activate),
-            ("<Alt>KP_Up", self.on_menu_edit_up_activate),
             ("F5", self.on_menu_refresh_activate),
         )
 
@@ -217,7 +207,6 @@ class MeldWindow(Gtk.ApplicationWindow):
         self.scheduler.connect("runnable", self.on_scheduler_runnable)
 
         self.ui.ensure_update()
-        self.diff_handler = None
         self.undo_handlers = tuple()
 
         # Set tooltip on map because the recentmenu is lazily created
@@ -295,7 +284,7 @@ class MeldWindow(Gtk.ApplicationWindow):
 
         self.actiongroup.get_action("Close").set_sensitive(bool(page))
         if not isinstance(page, MeldDoc):
-            for action in ("PrevChange", "NextChange", "Cut", "Copy", "Paste",
+            for action in ("Cut", "Copy", "Paste",
                            "Find", "FindNext", "FindPrevious", "Replace",
                            "Refresh", "GoToLine"):
                 self.actiongroup.get_action(action).set_sensitive(False)
@@ -308,8 +297,6 @@ class MeldWindow(Gtk.ApplicationWindow):
                 self.actiongroup.get_action(action).set_sensitive(is_filediff)
 
     def handle_current_doc_switch(self, page):
-        if self.diff_handler is not None:
-            page.disconnect(self.diff_handler)
         page.on_container_switch_out_event(self.ui, self)
         if self.undo_handlers:
             undoseq = page.undosequence
@@ -350,11 +337,6 @@ class MeldWindow(Gtk.ApplicationWindow):
         else:
             self.set_title("Meld")
 
-        if isinstance(newdoc, MeldDoc):
-            self.diff_handler = newdoc.next_diff_changed_signal.connect(
-                self.on_next_diff_changed)
-        else:
-            self.diff_handler = None
         if hasattr(newdoc, 'scheduler'):
             self.scheduler.add_task(newdoc.scheduler)
 
@@ -378,10 +360,6 @@ class MeldWindow(Gtk.ApplicationWindow):
     def on_can_redo(self, undosequence, can):
         self.actiongroup.get_action("Redo").set_sensitive(can)
 
-    def on_next_diff_changed(self, doc, have_prev, have_next):
-        self.actiongroup.get_action("PrevChange").set_sensitive(have_prev)
-        self.actiongroup.get_action("NextChange").set_sensitive(have_next)
-
     def on_action_new_tab_activate(self, action, parameter):
         self.append_new_comparison()
 
diff --git a/meld/resources/ui/appwindow.ui b/meld/resources/ui/appwindow.ui
index 96b9262e..cf0696ef 100644
--- a/meld/resources/ui/appwindow.ui
+++ b/meld/resources/ui/appwindow.ui
@@ -30,6 +30,54 @@
             <property name="pack-type">start</property>
           </packing>
         </child>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">True</property>
+            <property name="orientation">horizontal</property>
+            <property name="homogeneous">True</property>
+            <style>
+              <class name="linked"/>
+            </style>
+            <child>
+              <object class="GtkButton">
+                <property name="visible">True</property>
+                <property name="action-name">view.previous-change</property>
+                <property name="tooltip-text">Go to the previous change</property>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="icon-name">go-up-symbolic</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="pack-type">start</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton">
+                <property name="visible">True</property>
+                <property name="action-name">view.next-change</property>
+                <property name="tooltip-text">Go to the next change</property>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="icon-name">go-down-symbolic</property>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack-type">start</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkMenuButton" id="gear_menu_button">
             <property name="visible">true</property>
diff --git a/meld/vcview.py b/meld/vcview.py
index 380ef46b..24175334 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -134,7 +134,6 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
     create_diff_signal = MeldDoc.create_diff_signal
     file_changed_signal = MeldDoc.file_changed_signal
     label_changed = MeldDoc.label_changed
-    next_diff_changed_signal = MeldDoc.next_diff_changed_signal
     tab_state_changed = MeldDoc.tab_state_changed
 
     status_filters = GObject.Property(
@@ -193,6 +192,18 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
         self.ui_file = ui_file("vcview-ui.xml")
         self.actiongroup = self.VcviewActions
         self.actiongroup.set_translation_domain("meld")
+
+        # Manually handle GAction additions
+        actions = (
+            ('next-change', self.action_next_change),
+            ('previous-change', self.action_previous_change),
+        )
+        self.view_action_group = Gio.SimpleActionGroup()
+        for name, callback in actions:
+            action = Gio.SimpleAction.new(name, None)
+            action.connect('activate', callback)
+            self.view_action_group.add_action(action)
+
         self.model = VcTreeStore()
         self.connect("style-updated", self.model.on_style_updated)
         self.model.on_style_updated(self)
@@ -821,7 +832,8 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
     def on_treeview_cursor_changed(self, *args):
         cursor_path, cursor_col = self.treeview.get_cursor()
         if not cursor_path:
-            self.next_diff_changed_signal.emit(False, False)
+            self.set_action_enabled("previous-change", False)
+            self.set_action_enabled("next-change", False)
             self.current_path = cursor_path
             return
 
@@ -851,10 +863,10 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
                         skip = self.prev_path < cursor_path < self.next_path
 
         if not skip:
-            prev, next = self.model._find_next_prev_diff(cursor_path)
-            self.prev_path, self.next_path = prev, next
-            have_next_diffs = (prev is not None, next is not None)
-            self.next_diff_changed_signal.emit(*have_next_diffs)
+            prev, next_ = self.model._find_next_prev_diff(cursor_path)
+            self.prev_path, self.next_path = prev, next_
+            self.set_action_enabled("previous-change", prev is not None)
+            self.set_action_enabled("next-change", next_ is not None)
         self.current_path = cursor_path
 
     def next_diff(self, direction):
@@ -866,6 +878,12 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
             self.treeview.expand_to_path(path)
             self.treeview.set_cursor(path)
 
+    def action_previous_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.UP)
+
+    def action_next_change(self, *args):
+        self.next_diff(Gdk.ScrollDirection.DOWN)
+
     def on_refresh_activate(self, *extra):
         self.on_fileentry_file_set(self.fileentry)
 


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