[meld/ui-next] Move undo/redo to GActions



commit 0687dff7019a7fdef8ab2cc00adf8dadeeb8a099
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Feb 23 08:30:28 2019 +1000

    Move undo/redo to GActions

 data/ui/filediff-ui.xml |  3 ---
 data/ui/meldapp-ui.xml  |  3 ---
 meld/accelerators.py    |  2 ++
 meld/filediff.py        | 23 +++++++++++++++++++----
 meld/meldwindow.py      | 35 -----------------------------------
 5 files changed, 21 insertions(+), 45 deletions(-)
---
diff --git a/data/ui/filediff-ui.xml b/data/ui/filediff-ui.xml
index 3d126716..52367c47 100644
--- a/data/ui/filediff-ui.xml
+++ b/data/ui/filediff-ui.xml
@@ -39,9 +39,6 @@
   </menubar>
 
   <popup name="Popup">
-    <menuitem action="Undo"/>
-    <menuitem action="Redo"/>
-    <separator/>
     <menuitem action="Cut" />
     <menuitem action="Copy" />
     <menuitem action="Paste" />
diff --git a/data/ui/meldapp-ui.xml b/data/ui/meldapp-ui.xml
index fe4bf8d5..9c6bcee7 100644
--- a/data/ui/meldapp-ui.xml
+++ b/data/ui/meldapp-ui.xml
@@ -1,9 +1,6 @@
 <ui>
   <menubar name="Menubar">
     <menu action="EditMenu">
-      <menuitem action="Undo"/>
-      <menuitem action="Redo"/>
-      <separator/>
       <menuitem action="Cut"/>
       <menuitem action="Copy"/>
       <menuitem action="Paste"/>
diff --git a/meld/accelerators.py b/meld/accelerators.py
index c5f32974..3e196987 100644
--- a/meld/accelerators.py
+++ b/meld/accelerators.py
@@ -6,10 +6,12 @@ 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")),
+        ('view.redo', '<Primary><Shift>Z'),
         ("view.refresh", ("<control>R", "F5")),
         ('view.save', '<Primary>S'),
         ('view.save-all', '<Primary><Shift>L'),
         ('view.save-as', '<Primary><Shift>S'),
+        ('view.undo', '<Primary>Z'),
         ('win.close', '<Primary>W'),
         ("win.stop", "Escape"),
     )
diff --git a/meld/filediff.py b/meld/filediff.py
index 79ddb9be..767bf3f3 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -282,11 +282,13 @@ class FileDiff(Gtk.VBox, MeldDoc):
             ('next-change', self.action_next_change),
             ('open-external', self.action_open_external),
             ('previous-change', self.action_previous_change),
+            ('redo', self.action_redo),
             ('refresh', self.action_refresh),
             ('revert', self.action_revert),
             ('save', self.action_save),
             ('save-all', self.action_save_all),
             ('save-as', self.action_save_as),
+            ('undo', self.action_undo),
         )
         for name, callback in actions:
             action = Gio.SimpleAction.new(name, None)
@@ -340,8 +342,15 @@ class FileDiff(Gtk.VBox, MeldDoc):
 
         self.linediffer.connect("diffs-changed", self.on_diffs_changed)
         self.undosequence.connect("checkpointed", self.on_undo_checkpointed)
+        self.undosequence.connect("can-undo", self.on_can_undo)
+        self.undosequence.connect("can-redo", self.on_can_redo)
         self.connect("next-conflict-changed", self.on_next_conflict_changed)
 
+        # TODO: If UndoSequence expose can_undo and can_redo as
+        # GProperties instead, this would be much, much nicer.
+        self.set_action_enabled('redo', self.undosequence.can_redo())
+        self.set_action_enabled('undo', self.undosequence.can_undo())
+
         for statusbar, buf in zip(self.statusbar, self.textbuffer):
             buf.bind_property(
                 'language', statusbar, 'source-language',
@@ -1087,15 +1096,15 @@ class FileDiff(Gtk.VBox, MeldDoc):
             view = self.textview[buf_index]
             view.scroll_mark_onscreen(buf.get_insert())
 
-    def on_undo_activate(self):
+    def action_undo(self, *args):
         if self.undosequence.can_undo():
             actions = self.undosequence.undo()
-        self._scroll_to_actions(actions)
+            self._scroll_to_actions(actions)
 
-    def on_redo_activate(self):
+    def action_redo(self, *args):
         if self.undosequence.can_redo():
             actions = self.undosequence.redo()
-        self._scroll_to_actions(actions)
+            self._scroll_to_actions(actions)
 
     def on_text_insert_text(self, buf, it, text, textlen):
         self.undosequence.add_action(
@@ -1112,6 +1121,12 @@ class FileDiff(Gtk.VBox, MeldDoc):
         buf.set_modified(not checkpointed)
         self.recompute_label()
 
+    def on_can_undo(self, undosequence, can_undo):
+        self.set_action_enabled('undo', can_undo)
+
+    def on_can_redo(self, undosequence, can_redo):
+        self.set_action_enabled('redo', can_redo)
+
     @with_focused_pane
     def action_open_external(self, pane, *args):
         if not self.textbuffer[pane].data.gfile:
diff --git a/meld/meldwindow.py b/meld/meldwindow.py
index 9e7f3926..68964c36 100644
--- a/meld/meldwindow.py
+++ b/meld/meldwindow.py
@@ -58,12 +58,6 @@ class MeldWindow(Gtk.ApplicationWindow):
 
         actions = (
             ("EditMenu", None, _("_Edit")),
-            ("Undo", Gtk.STOCK_UNDO, None, "<Primary>Z",
-                _("Undo the last action"),
-                self.on_menu_undo_activate),
-            ("Redo", Gtk.STOCK_REDO, None, "<Primary><shift>Z",
-                _("Redo the last undone action"),
-                self.on_menu_redo_activate),
             ("Cut", Gtk.STOCK_CUT, None, None, _("Cut the selection"),
                 self.on_menu_cut_activate),
             ("Copy", Gtk.STOCK_COPY, None, None, _("Copy the selection"),
@@ -167,7 +161,6 @@ class MeldWindow(Gtk.ApplicationWindow):
         self.scheduler.connect("runnable", self.on_scheduler_runnable)
 
         self.ui.ensure_update()
-        self.undo_handlers = tuple()
 
     def do_realize(self):
         Gtk.ApplicationWindow.do_realize(self)
@@ -254,11 +247,6 @@ class MeldWindow(Gtk.ApplicationWindow):
 
     def handle_current_doc_switch(self, page):
         page.on_container_switch_out_event(self.ui, self)
-        if self.undo_handlers:
-            undoseq = page.undosequence
-            for handler in self.undo_handlers:
-                undoseq.disconnect(handler)
-            self.undo_handlers = tuple()
 
     @Template.Callback()
     def on_switch_page(self, notebook, page, which):
@@ -268,17 +256,6 @@ class MeldWindow(Gtk.ApplicationWindow):
             self.handle_current_doc_switch(olddoc)
 
         newdoc = notebook.get_nth_page(which) if which >= 0 else None
-        try:
-            undoseq = newdoc.undosequence
-            can_undo = undoseq.can_undo()
-            can_redo = undoseq.can_redo()
-            undo_handler = undoseq.connect("can-undo", self.on_can_undo)
-            redo_handler = undoseq.connect("can-redo", self.on_can_redo)
-            self.undo_handlers = (undo_handler, redo_handler)
-        except AttributeError:
-            can_undo, can_redo = False, False
-        self.actiongroup.get_action("Undo").set_sensitive(can_undo)
-        self.actiongroup.get_action("Redo").set_sensitive(can_redo)
 
         if newdoc:
             nbl = self.notebook.get_tab_label(newdoc)
@@ -303,12 +280,6 @@ class MeldWindow(Gtk.ApplicationWindow):
     def on_page_label_changed(self, notebook, label_text):
         self.set_title(label_text)
 
-    def on_can_undo(self, undosequence, can):
-        self.actiongroup.get_action("Undo").set_sensitive(can)
-
-    def on_can_redo(self, undosequence, can):
-        self.actiongroup.get_action("Redo").set_sensitive(can)
-
     def on_action_new_tab_activate(self, action, parameter):
         self.append_new_comparison()
 
@@ -318,12 +289,6 @@ class MeldWindow(Gtk.ApplicationWindow):
             page = self.notebook.get_nth_page(i)
             page.on_delete_event()
 
-    def on_menu_undo_activate(self, *extra):
-        self.current_doc().on_undo_activate()
-
-    def on_menu_redo_activate(self, *extra):
-        self.current_doc().on_redo_activate()
-
     def on_menu_find_activate(self, *extra):
         self.current_doc().on_find_activate()
 


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