[meld: 6/8] filediff: Update syncpoint dynamic menu population




commit 90f2273dbd34c9ab8e303aba36940c0b70abd56e
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Mar 19 09:10:28 2022 +1000

    filediff: Update syncpoint dynamic menu population

 meld/filediff.py                    | 77 +++++++++++++++++++------------------
 meld/resources/ui/filediff-menus.ui |  8 ----
 2 files changed, 40 insertions(+), 45 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 8b79d9dd..14f95d6f 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -44,6 +44,7 @@ from meld.meldbuffer import (
     BufferLines,
 )
 from meld.melddoc import ComparisonState, MeldDoc, open_files_external
+from meld.menuhelpers import replace_menu_section
 from meld.misc import user_critical, with_focused_pane
 from meld.patchdialog import PatchDialog
 from meld.recent import RecentType
@@ -222,8 +223,6 @@ class FileDiff(Gtk.VBox, MeldDoc):
         default=False,
     )
 
-    ADD_SYNCPOINT_OFFSET = 10
-
     def __init__(
         self,
         num_panes,
@@ -367,22 +366,10 @@ class FileDiff(Gtk.VBox, MeldDoc):
 
         builder = Gtk.Builder.new_from_resource(
             '/org/gnome/meld/ui/filediff-menus.ui')
-        context_menu = builder.get_object('filediff-context-menu')
-        self.popup_menu = Gtk.Menu.new_from_model(context_menu)
+        self.popup_menu_model = builder.get_object('filediff-context-menu')
+        self.popup_menu = Gtk.Menu.new_from_model(self.popup_menu_model)
         self.popup_menu.attach_to_widget(self)
 
-        popup_options = self.popup_menu.get_children()
-
-        offset = FileDiff.ADD_SYNCPOINT_OFFSET
-        self.add_sync_point_opt = popup_options[offset]
-        self.move_sync_point_opt = popup_options[offset + 1]
-        self.match_sync_point_opt = popup_options[offset + 2]
-        self.remove_sync_point_opt = popup_options[offset + 3]
-
-        self.popup_menu.remove(self.move_sync_point_opt)
-        self.popup_menu.remove(self.match_sync_point_opt)
-        self.popup_menu.remove(self.remove_sync_point_opt)
-
         builder = Gtk.Builder.new_from_resource(
             '/org/gnome/meld/ui/filediff-actions.ui')
         self.toolbar_actions = builder.get_object('view-toolbar')
@@ -1437,34 +1424,50 @@ class FileDiff(Gtk.VBox, MeldDoc):
             return self._syncpoint_action(self.textview.index(self.focus_pane))
 
     def set_syncpoint_menuitem(self, pane):
-        action = self._syncpoint_action(pane)
+        menu_actions = {
+            SyncpointState.CAN_ADD: [
+                _("Add Synchronization Point"),
+                "view.add-sync-point"
+            ],
+            SyncpointState.CAN_DELETE: [
+                _("Remove Synchronization Point"),
+                "view.remove-sync-point"
+            ],
+            SyncpointState.CAN_MOVE: [
+                _("Move Synchronization Point"),
+                "view.add-sync-point"
+            ],
+            SyncpointState.CAN_MATCH: [
+                _("Match Synchronization Point"),
+                "view.add-sync-point"
+            ],
+            SyncpointState.DISABLED: [
+                _("Add Synchronization Point"),
+                "view.add-sync-point"
+            ],
+        }
 
-        popup_options = self.popup_menu.get_children()
-        current_option = popup_options[FileDiff.ADD_SYNCPOINT_OFFSET]
-        next_option = None
-
-        if action == SyncpointState.CAN_ADD:
-            next_option = self.add_sync_point_opt
-        elif action == SyncpointState.CAN_MATCH:
-            next_option = self.match_sync_point_opt
-        elif action == SyncpointState.CAN_DELETE:
-            next_option = self.remove_sync_point_opt
-        elif action == SyncpointState.CAN_MOVE:
-            next_option = self.move_sync_point_opt
-        elif action is SyncpointState.DISABLED:
-            next_option = self.move_sync_point_opt
+        action = self._syncpoint_action(pane)
 
         self.set_action_enabled(
             "add-sync-point",
             action != SyncpointState.DISABLED
         )
 
-        if next_option is not current_option:
-            self.popup_menu.insert(
-                next_option,
-                FileDiff.ADD_SYNCPOINT_OFFSET + 1
-            )
-            self.popup_menu.remove(current_option)
+        label, action_id = menu_actions[action]
+
+        syncpoint_menu = Gio.Menu()
+        syncpoint_menu.append(label=label, detailed_action=action_id)
+        syncpoint_menu.append(
+            label=_("Clear Synchronization Points"),
+            detailed_action='view.clear-sync-point',
+        )
+        section = Gio.MenuItem.new_section(None, syncpoint_menu)
+        section.set_attribute([("id", "s", "syncpoint-section")])
+        replace_menu_section(self.popup_menu_model, section)
+
+        self.popup_menu = Gtk.Menu.new_from_model(self.popup_menu_model)
+        self.popup_menu.attach_to_widget(self)
 
     def _syncpoint_action(self, pane):
         def get_mark():
diff --git a/meld/resources/ui/filediff-menus.ui b/meld/resources/ui/filediff-menus.ui
index bc1fb3cc..00693ed0 100644
--- a/meld/resources/ui/filediff-menus.ui
+++ b/meld/resources/ui/filediff-menus.ui
@@ -65,14 +65,6 @@
         <attribute name="label" translatable="yes">Add Synchronization Point</attribute>
         <attribute name="action">view.add-sync-point</attribute>
       </item>
-      <item>
-        <attribute name="label" translatable="yes">Move Synchronization Point</attribute>
-        <attribute name="action">view.add-sync-point</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Match Synchronization Point</attribute>
-        <attribute name="action">view.add-sync-point</attribute>
-      </item>
       <item>
         <attribute name="label" translatable="yes">Remove Synchronization Point</attribute>
         <attribute name="action">view.remove-sync-point</attribute>


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