[gedit/wip/redesign2: 1/13] Port external tools to new menu api



commit f2341ed087595f35ff7283412008d248b346175b
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Jan 2 19:50:53 2014 +0100

    Port external tools to new menu api

 plugins/externaltools/tools/functions.py         |    2 +-
 plugins/externaltools/tools/library.py           |    5 +-
 plugins/externaltools/tools/manager.py           |   14 +-
 plugins/externaltools/tools/tools.ui             |   27 +----
 plugins/externaltools/tools/windowactivatable.py |  152 ++++++---------------
 5 files changed, 58 insertions(+), 142 deletions(-)
---
diff --git a/plugins/externaltools/tools/functions.py b/plugins/externaltools/tools/functions.py
index 5fa6a1f..4b758dc 100644
--- a/plugins/externaltools/tools/functions.py
+++ b/plugins/externaltools/tools/functions.py
@@ -286,7 +286,7 @@ class MultipleDocumentsSaver:
         if self._counter == 0 and not self._error:
             run_external_tool(self._window, self._panel, self._node)
 
-def capture_menu_action(action, window, panel, node):
+def capture_menu_action(action, parameter, window, panel, node):
     if node.save_files == 'document' and window.get_active_document():
         MultipleDocumentsSaver(window, panel, False, node)
         return
diff --git a/plugins/externaltools/tools/library.py b/plugins/externaltools/tools/library.py
index 1e2a1aa..b1f2113 100644
--- a/plugins/externaltools/tools/library.py
+++ b/plugins/externaltools/tools/library.py
@@ -477,6 +477,7 @@ class Tool(object):
 
 if __name__ == '__main__':
     library = ToolLibrary()
+    library.set_locations(os.path.expanduser("~/.config/gedit/tools"))
 
     def print_tool(t, indent):
         print(indent * "  " + "%s: %s" % (t.filename, t.name))
@@ -484,9 +485,9 @@ if __name__ == '__main__':
     def print_dir(d, indent):
         print(indent * "  " + d.dirname + '/')
         for i in d.subdirs:
-            print_dir(i, indent+1)
+            print_dir(i, indent + 1)
         for i in d.tools:
-            print_tool(i, indent+1)
+            print_tool(i, indent + 1)
 
     print_dir(library.tree, 0)
 
diff --git a/plugins/externaltools/tools/manager.py b/plugins/externaltools/tools/manager.py
index a654172..56406bc 100644
--- a/plugins/externaltools/tools/manager.py
+++ b/plugins/externaltools/tools/manager.py
@@ -299,8 +299,8 @@ class Manager(GObject.Object):
 
     def build(self):
         callbacks = {
-            'on_action_add_tool_activated'    : self.on_action_add_tool_activated,
-            'on_action_remove_tool_activated' : self.on_action_remove_tool_activated,
+            'on_add_tool_button_clicked'    : self.on_add_tool_button_clicked,
+            'on_remove_tool_button_clicked' : self.on_remove_tool_button_clicked,
             'on_tool_manager_dialog_response' : self.on_tool_manager_dialog_response,
             'on_tool_manager_dialog_focus_out': self.on_tool_manager_dialog_focus_out,
             'on_tool_manager_dialog_configure_event': self.on_tool_manager_dialog_configure_event,
@@ -629,8 +629,8 @@ class Manager(GObject.Object):
 
         removable = node is not None and node.is_local()
 
-        self['remove-tool-action'].set_sensitive(removable)
-        self['revert-tool-action'].set_sensitive(removable)
+        self['remove-tool-button'].set_sensitive(removable)
+        self['revert-tool-button'].set_sensitive(removable)
 
         if node is not None and node.is_global():
             self['remove-tool-button'].hide()
@@ -675,7 +675,7 @@ class Manager(GObject.Object):
 
         return self.language_id_from_iter(piter)
 
-    def on_action_add_tool_activated(self, action):
+    def on_add_tool_button_clicked(self, button):
         self.save_current_tool()
 
         # block handlers while inserting a new item
@@ -707,7 +707,7 @@ class Manager(GObject.Object):
 
         self.update_remove_revert()
 
-    def on_action_remove_tool_activated(self, action):
+    def on_remove_tool_button_clicked(self, button):
         piter, node = self.get_selected_tool()
 
         if not node:
@@ -720,7 +720,7 @@ class Manager(GObject.Object):
                 self.remove_accelerator(node, shortcut)
                 self.add_accelerator(node)
 
-                self['revert-tool-action'].set_sensitive(False)
+                self['revert-tool-button'].set_sensitive(False)
                 self.fill_fields()
 
                 self.tool_changed(node)
diff --git a/plugins/externaltools/tools/tools.ui b/plugins/externaltools/tools/tools.ui
index a77bfb3..1a91753 100644
--- a/plugins/externaltools/tools/tools.ui
+++ b/plugins/externaltools/tools/tools.ui
@@ -1,9 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <object class="GtkAction" id="add-tool-action">
-    <signal name="activate" handler="on_action_add_tool_activated" swapped="no"/>
-  </object>
   <object class="GtkListStore" id="model_applicability">
     <columns>
       <!-- column-name gchararray -->
@@ -132,12 +129,6 @@
       </row>
     </data>
   </object>
-  <object class="GtkAction" id="remove-tool-action">
-    <signal name="activate" handler="on_action_remove_tool_activated" swapped="no"/>
-  </object>
-  <object class="GtkAction" id="revert-tool-action">
-    <signal name="activate" handler="on_action_remove_tool_activated" swapped="no"/>
-  </object>
   <object class="GeditDocument" id="commands_buffer"/>
   <object class="GtkDialog" id="tool-manager-dialog">
     <property name="can_focus">False</property>
@@ -164,7 +155,6 @@
             <child>
               <object class="GtkButton" id="button1">
                 <property name="label">gtk-help</property>
-                <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="can_default">True</property>
@@ -181,7 +171,6 @@
             <child>
               <object class="GtkButton" id="button2">
                 <property name="label">gtk-close</property>
-                <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="can_default">True</property>
@@ -279,16 +268,13 @@
                     </style>
                     <child>
                       <object class="GtkToolButton" id="add-tool-button">
-                        <property name="use_action_appearance">False</property>
-                        <property name="related_action">add-tool-action</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <property name="tooltip_text" translatable="yes">Add a new tool</property>
-                        <property name="related_action">add-tool-action</property>
-                        <property name="use_action_appearance">False</property>
                         <property name="label" translatable="yes">Add Tool</property>
                         <property name="use_underline">True</property>
                         <property name="icon_name">list-add-symbolic</property>
+                        <signal name="clicked" handler="on_add_tool_button_clicked" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">False</property>
@@ -297,16 +283,13 @@
                     </child>
                     <child>
                       <object class="GtkToolButton" id="remove-tool-button">
-                        <property name="use_action_appearance">False</property>
-                        <property name="related_action">remove-tool-action</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <property name="tooltip_text" translatable="yes">Remove selected tool</property>
-                        <property name="related_action">remove-tool-action</property>
-                        <property name="use_action_appearance">False</property>
                         <property name="label" translatable="yes">Remove Tool</property>
                         <property name="use_underline">True</property>
                         <property name="icon_name">list-remove-symbolic</property>
+                        <signal name="clicked" handler="on_remove_tool_button_clicked" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">False</property>
@@ -315,16 +298,13 @@
                     </child>
                     <child>
                       <object class="GtkToolButton" id="revert-tool-button">
-                        <property name="use_action_appearance">False</property>
-                        <property name="related_action">revert-tool-action</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <property name="tooltip_text" translatable="yes">Revert tool</property>
-                        <property name="related_action">revert-tool-action</property>
-                        <property name="use_action_appearance">False</property>
                         <property name="label" translatable="yes">Revert Tool</property>
                         <property name="use_underline">True</property>
                         <property name="icon_name">edit-undo-symbolic</property>
+                        <signal name="clicked" handler="on_remove_tool_button_clicked" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">False</property>
@@ -431,7 +411,6 @@
                                 <property name="can_focus">False</property>
                                 <child>
                                   <object class="GtkButton" id="languages_button">
-                                    <property name="use_action_appearance">False</property>
                                     <property name="visible">True</property>
                                     <property name="can_focus">False</property>
                                     <property name="receives_default">False</property>
diff --git a/plugins/externaltools/tools/windowactivatable.py 
b/plugins/externaltools/tools/windowactivatable.py
index 431dc76..816ce59 100644
--- a/plugins/externaltools/tools/windowactivatable.py
+++ b/plugins/externaltools/tools/windowactivatable.py
@@ -26,16 +26,13 @@ from .capture import Capture
 from .functions import *
 
 class ToolMenu(object):
-    def __init__(self, library, window, panel, menupath):
+    def __init__(self, library, window, panel, menu):
         super(ToolMenu, self).__init__()
         self._library = library
         self._window = window
         self._panel = panel
-        self._menupath = menupath
-
-        self._merge_id = 0
-        self._action_group = Gtk.ActionGroup("ExternalToolsPluginToolActions")
-        self._signals = []
+        self._menu = menu
+        self._action_tools = {}
 
         self.update()
 
@@ -43,75 +40,41 @@ class ToolMenu(object):
         self.remove()
 
     def remove(self):
-        if self._merge_id != 0:
-            self._window.get_ui_manager().remove_ui(self._merge_id)
-            self._window.get_ui_manager().remove_action_group(self._action_group)
-            self._merge_id = 0
-
-        for action in self._action_group.list_actions():
-            if action._tool_handler is not None:
-                action.disconnect(action._tool_handler)
-
-            action._tool_item = None
-            action._tool_handler = None
-
-            self._action_group.remove_action(action)
-
-        accelmap = Gtk.AccelMap.get()
+        self._menu.remove_all()
 
-        for s in self._signals:
-            accelmap.disconnect(s)
+        for name, tool in self._action_tools.items():
+            self._window.remove_action(name)
 
-        self._signals = []
+            if tool.shortcut:
+                app = Gio.Application.get_default()
+                app.remove_accelerator(tool.shortcut)
+        self._action_tools = {}
 
-    def _insert_directory(self, directory, path):
-        ui_manager = self._window.get_ui_manager()
+    def _insert_directory(self, directory, menu):
+        for d in sorted(directory.subdirs, key=lambda x: x.name.lower()):
+            submenu = Gio.Menu()
+            item = Gio.MenuItem.new_submenu(d.name.replace('_', '__'), submenu)
+            menu.append_item(item)
 
-        for item in sorted(directory.subdirs, key=lambda x: x.name.lower()):
-            action_name = 'ExternalToolDirectory_%X_%X' % (id(item), id(item.name))
-            action = Gtk.Action(action_name, item.name.replace('_', '__'), None, None)
-            self._action_group.add_action(action)
+            self._insert_directory(d, submenu)
 
-            ui_manager.add_ui(self._merge_id, path,
-                              action_name, action_name,
-                              Gtk.UIManagerItemType.MENU, False)
-                           
-            self._insert_directory(item, path + '/' + action_name)
+        for tool in sorted(directory.tools, key=lambda x: x.name.lower()):
+            action_name = 'external-tool_%X_%X' % (id(tool), id(tool.name))
+            self._action_tools[action_name] = tool
 
-        for item in sorted(directory.tools, key=lambda x: x.name.lower()):
-            action_name = 'ExternalToolTool_%X_%X' % (id(item), id(item.name))
-            action = Gtk.Action(action_name, item.name.replace('_', '__'), item.comment, None)
-            handler = action.connect("activate", capture_menu_action, self._window, self._panel, item)
+            action = Gio.SimpleAction(name=action_name)
+            action.connect('activate', capture_menu_action, self._window, self._panel, tool)
+            self._window.add_action(action)
 
-            # Attach the item and the handler to the action object
-            action._tool_item = item
-            action._tool_handler = handler
+            menu.append(tool.name.replace('_', '__'), "win.%s" % action_name)
 
-            # Make sure to replace accel
-            accelpath = '<Actions>/ExternalToolsPluginToolActions/%s' % (action_name, )
-            
-            if item.shortcut:
-                key, mod = Gtk.accelerator_parse(item.shortcut)
-                Gtk.AccelMap.change_entry(accelpath, key, mod, True)
-                
-                self._signals.append(Gtk.AccelMap.get().connect('changed::%s' % (accelpath,), 
self.on_accelmap_changed, item))
-
-            self._action_group.add_action_with_accel(action, item.shortcut)
-
-            ui_manager.add_ui(self._merge_id, path,
-                              action_name, action_name,
-                              Gtk.UIManagerItemType.MENUITEM, False)
-
-    def on_accelmap_changed(self, accelmap, path, key, mod, tool):
-        tool.shortcut = Gtk.accelerator_name(key, mod)
-        tool.save()
-        self._window._external_tools_window_activatable.update_manager(tool)
+            if tool.shortcut:
+                app = Gio.Application.get_default()
+                app.add_accelerator(tool.shortcut, "win.%s" % action_name, None)
 
     def update(self):
         self.remove()
-        self._merge_id = self._window.get_ui_manager().new_merge_id()
-        self._insert_directory(self._library.tree, self._menupath)
-        self._window.get_ui_manager().insert_action_group(self._action_group, -1)
+        self._insert_directory(self._library.tree, self._menu)
         self.filter(self._window.get_active_document())
 
     def filter_language(self, language, item):
@@ -145,10 +108,11 @@ class ToolMenu(object):
             'untitled': not titled,
         }
 
-        for action in self._action_group.list_actions():
-            if action._tool_item is not None:
-                action.set_visible(states[action._tool_item.applicability] and
-                                   self.filter_language(language, action._tool_item))
+        for name, tool in self._action_tools.items():
+            action = self._window.lookup_action(name)
+            if action:
+                action.set_enabled(states[tool.applicability] and
+                                   self.filter_language(language, tool))
 
 # FIXME: restore the launch of the manager on configure using PeasGtk.Configurable
 class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
@@ -167,45 +131,21 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
         self.window._external_tools_window_activatable = self
         self._library = ToolLibrary()
 
-        ui_manager = self.window.get_ui_manager()
-
-        self._action_group = Gtk.ActionGroup(name='ExternalToolsPluginActions')
-        self._action_group.set_translation_domain('gedit')
-        self._action_group.add_actions([('ExternalToolManager', None,
-                                         _('Manage _External Tools...'), None,
-                                         _("Opens the External Tools Manager"),
-                                         lambda action: self.open_dialog()),
-                                        ('ExternalTools', None,
-                                         _('External _Tools'), None,
-                                         _("External tools"), None)])
-        ui_manager.insert_action_group(self._action_group, -1)
-
-        ui_string = """
-            <ui>
-              <menubar name="MenuBar">
-                <menu name="ToolsMenu" action="Tools">
-                  <placeholder name="ToolsOps_4">
-                    <separator/>
-                    <menu name="ExternalToolsMenu" action="ExternalTools">
-                        <placeholder name="ExternalToolPlaceholder"/>
-                    </menu>
-                    <separator/>
-                  </placeholder>
-                  <placeholder name="ToolsOps_5">
-                    <menuitem name="ExternalToolManager" action="ExternalToolManager"/>
-                  </placeholder>
-                </menu>
-              </menubar>
-            </ui>"""
-
-        self._merge_id = ui_manager.add_ui_from_string(ui_string)
+        action = Gio.SimpleAction(name="manage_tools")
+        action.connect("activate", lambda action, parameter: self.open_dialog())
+        self.window.add_action(action)
+
+        self.gear_menu = self.extend_gear_menu("ext9")
+        item = Gio.MenuItem.new(_("Manage _External Tools..."), "win.manage_tools")
+        self.gear_menu.add_menu_item(item)
+        external_tools_submenu = Gio.Menu()
+        item = Gio.MenuItem.new_submenu(_("External _Tools"), external_tools_submenu)
+        self.gear_menu.add_menu_item(item)
 
         # Create output console
         self._output_buffer = OutputPanel(self.plugin_info.get_data_dir(), self.window)
 
-        self.menu = ToolMenu(self._library, self.window, self._output_buffer,
-                             "/MenuBar/ToolsMenu/ToolsOps_4/ExternalToolsMenu/ExternalToolPlaceholder")
-        ui_manager.ensure_update()
+        self.menu = ToolMenu(self._library, self.window, self._output_buffer, external_tools_submenu)
 
         bottom = self.window.get_bottom_panel()
         image = Gtk.Image.new_from_icon_name("system-run-symbolic", Gtk.IconSize.MENU)
@@ -217,15 +157,11 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
     def do_update_state(self):
         if self.menu is not None:
             self.menu.filter(self.window.get_active_document())
-            self.window.get_ui_manager().ensure_update()
 
     def do_deactivate(self):
         self.window._external_tools_window_activatable = None
-        ui_manager = self.window.get_ui_manager()
         self.menu.deactivate()
-        ui_manager.remove_ui(self._merge_id)
-        ui_manager.remove_action_group(self._action_group)
-        ui_manager.ensure_update()
+        self.window.remove_action("manage_tools")
 
         bottom = self.window.get_bottom_panel()
         bottom.remove_item(self._output_buffer.panel)


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