Skip to content

Commit 0687dff

Browse files
committed
Move undo/redo to GActions
1 parent 05b531a commit 0687dff

5 files changed

Lines changed: 21 additions & 45 deletions

File tree

data/ui/filediff-ui.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
</menubar>
4040

4141
<popup name="Popup">
42-
<menuitem action="Undo"/>
43-
<menuitem action="Redo"/>
44-
<separator/>
4542
<menuitem action="Cut" />
4643
<menuitem action="Copy" />
4744
<menuitem action="Paste" />

data/ui/meldapp-ui.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<ui>
22
<menubar name="Menubar">
33
<menu action="EditMenu">
4-
<menuitem action="Undo"/>
5-
<menuitem action="Redo"/>
6-
<separator/>
74
<menuitem action="Cut"/>
85
<menuitem action="Copy"/>
96
<menuitem action="Paste"/>

meld/accelerators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ def register_accels(app: Gtk.Application):
66
view_accels = (
77
("view.next-change", ("<Alt>Down", "<Alt>KP_Down", "<Primary>D")),
88
("view.previous-change", ("<Alt>Up", "<Alt>KP_Up", "<Primary>E")),
9+
('view.redo', '<Primary><Shift>Z'),
910
("view.refresh", ("<control>R", "F5")),
1011
('view.save', '<Primary>S'),
1112
('view.save-all', '<Primary><Shift>L'),
1213
('view.save-as', '<Primary><Shift>S'),
14+
('view.undo', '<Primary>Z'),
1315
('win.close', '<Primary>W'),
1416
("win.stop", "Escape"),
1517
)

meld/filediff.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ def __init__(self, num_panes):
282282
('next-change', self.action_next_change),
283283
('open-external', self.action_open_external),
284284
('previous-change', self.action_previous_change),
285+
('redo', self.action_redo),
285286
('refresh', self.action_refresh),
286287
('revert', self.action_revert),
287288
('save', self.action_save),
288289
('save-all', self.action_save_all),
289290
('save-as', self.action_save_as),
291+
('undo', self.action_undo),
290292
)
291293
for name, callback in actions:
292294
action = Gio.SimpleAction.new(name, None)
@@ -340,8 +342,15 @@ def __init__(self, num_panes):
340342

341343
self.linediffer.connect("diffs-changed", self.on_diffs_changed)
342344
self.undosequence.connect("checkpointed", self.on_undo_checkpointed)
345+
self.undosequence.connect("can-undo", self.on_can_undo)
346+
self.undosequence.connect("can-redo", self.on_can_redo)
343347
self.connect("next-conflict-changed", self.on_next_conflict_changed)
344348

349+
# TODO: If UndoSequence expose can_undo and can_redo as
350+
# GProperties instead, this would be much, much nicer.
351+
self.set_action_enabled('redo', self.undosequence.can_redo())
352+
self.set_action_enabled('undo', self.undosequence.can_undo())
353+
345354
for statusbar, buf in zip(self.statusbar, self.textbuffer):
346355
buf.bind_property(
347356
'language', statusbar, 'source-language',
@@ -1087,15 +1096,15 @@ def _scroll_to_actions(self, actions):
10871096
view = self.textview[buf_index]
10881097
view.scroll_mark_onscreen(buf.get_insert())
10891098

1090-
def on_undo_activate(self):
1099+
def action_undo(self, *args):
10911100
if self.undosequence.can_undo():
10921101
actions = self.undosequence.undo()
1093-
self._scroll_to_actions(actions)
1102+
self._scroll_to_actions(actions)
10941103

1095-
def on_redo_activate(self):
1104+
def action_redo(self, *args):
10961105
if self.undosequence.can_redo():
10971106
actions = self.undosequence.redo()
1098-
self._scroll_to_actions(actions)
1107+
self._scroll_to_actions(actions)
10991108

11001109
def on_text_insert_text(self, buf, it, text, textlen):
11011110
self.undosequence.add_action(
@@ -1112,6 +1121,12 @@ def on_undo_checkpointed(self, undosequence, buf, checkpointed):
11121121
buf.set_modified(not checkpointed)
11131122
self.recompute_label()
11141123

1124+
def on_can_undo(self, undosequence, can_undo):
1125+
self.set_action_enabled('undo', can_undo)
1126+
1127+
def on_can_redo(self, undosequence, can_redo):
1128+
self.set_action_enabled('redo', can_redo)
1129+
11151130
@with_focused_pane
11161131
def action_open_external(self, pane, *args):
11171132
if not self.textbuffer[pane].data.gfile:

meld/meldwindow.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ def __init__(self):
5858

5959
actions = (
6060
("EditMenu", None, _("_Edit")),
61-
("Undo", Gtk.STOCK_UNDO, None, "<Primary>Z",
62-
_("Undo the last action"),
63-
self.on_menu_undo_activate),
64-
("Redo", Gtk.STOCK_REDO, None, "<Primary><shift>Z",
65-
_("Redo the last undone action"),
66-
self.on_menu_redo_activate),
6761
("Cut", Gtk.STOCK_CUT, None, None, _("Cut the selection"),
6862
self.on_menu_cut_activate),
6963
("Copy", Gtk.STOCK_COPY, None, None, _("Copy the selection"),
@@ -167,7 +161,6 @@ def __init__(self):
167161
self.scheduler.connect("runnable", self.on_scheduler_runnable)
168162

169163
self.ui.ensure_update()
170-
self.undo_handlers = tuple()
171164

172165
def do_realize(self):
173166
Gtk.ApplicationWindow.do_realize(self)
@@ -254,11 +247,6 @@ def _update_page_action_sensitivity(self):
254247

255248
def handle_current_doc_switch(self, page):
256249
page.on_container_switch_out_event(self.ui, self)
257-
if self.undo_handlers:
258-
undoseq = page.undosequence
259-
for handler in self.undo_handlers:
260-
undoseq.disconnect(handler)
261-
self.undo_handlers = tuple()
262250

263251
@Template.Callback()
264252
def on_switch_page(self, notebook, page, which):
@@ -268,17 +256,6 @@ def on_switch_page(self, notebook, page, which):
268256
self.handle_current_doc_switch(olddoc)
269257

270258
newdoc = notebook.get_nth_page(which) if which >= 0 else None
271-
try:
272-
undoseq = newdoc.undosequence
273-
can_undo = undoseq.can_undo()
274-
can_redo = undoseq.can_redo()
275-
undo_handler = undoseq.connect("can-undo", self.on_can_undo)
276-
redo_handler = undoseq.connect("can-redo", self.on_can_redo)
277-
self.undo_handlers = (undo_handler, redo_handler)
278-
except AttributeError:
279-
can_undo, can_redo = False, False
280-
self.actiongroup.get_action("Undo").set_sensitive(can_undo)
281-
self.actiongroup.get_action("Redo").set_sensitive(can_redo)
282259

283260
if newdoc:
284261
nbl = self.notebook.get_tab_label(newdoc)
@@ -303,12 +280,6 @@ def after_page_reordered(self, notebook, page, page_num):
303280
def on_page_label_changed(self, notebook, label_text):
304281
self.set_title(label_text)
305282

306-
def on_can_undo(self, undosequence, can):
307-
self.actiongroup.get_action("Undo").set_sensitive(can)
308-
309-
def on_can_redo(self, undosequence, can):
310-
self.actiongroup.get_action("Redo").set_sensitive(can)
311-
312283
def on_action_new_tab_activate(self, action, parameter):
313284
self.append_new_comparison()
314285

@@ -318,12 +289,6 @@ def action_close(self, *extra):
318289
page = self.notebook.get_nth_page(i)
319290
page.on_delete_event()
320291

321-
def on_menu_undo_activate(self, *extra):
322-
self.current_doc().on_undo_activate()
323-
324-
def on_menu_redo_activate(self, *extra):
325-
self.current_doc().on_redo_activate()
326-
327292
def on_menu_find_activate(self, *extra):
328293
self.current_doc().on_find_activate()
329294

0 commit comments

Comments
 (0)