[gedit/wip/redesign2] wip



commit 807345f9d026f1714e9520b601feaab2a2219623
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Jan 2 20:35:23 2014 +0100

    wip

 plugins/externaltools/tools/functions.py         |    2 +-
 plugins/externaltools/tools/library.py           |    4 +-
 plugins/externaltools/tools/windowactivatable.py |   97 +++++++---------------
 3 files changed, 33 insertions(+), 70 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 07064da..b1f2113 100644
--- a/plugins/externaltools/tools/library.py
+++ b/plugins/externaltools/tools/library.py
@@ -485,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/windowactivatable.py 
b/plugins/externaltools/tools/windowactivatable.py
index da8089f..3f88659 100644
--- a/plugins/externaltools/tools/windowactivatable.py
+++ b/plugins/externaltools/tools/windowactivatable.py
@@ -31,8 +31,8 @@ class ToolMenu(object):
         self._library = library
         self._window = window
         self._panel = panel
-
-        self._signals = []
+        self._menu = menu
+        self._action_tools = {}
 
         self.update()
 
@@ -40,75 +40,40 @@ 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()
-
-        for s in self._signals:
-            accelmap.disconnect(s)
-
-        self._signals = []
-
-    def _insert_directory(self, directory, path):
-        ui_manager = self._window.get_ui_manager()
+        self._menu.remove_all()
 
-        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)
+        for name, tool in self._action_tools.items():
+            self._window.remove_action(name)
 
-            ui_manager.add_ui(self._merge_id, path,
-                              action_name, action_name,
-                              Gtk.UIManagerItemType.MENU, False)
-                           
-            self._insert_directory(item, path + '/' + action_name)
+            if tool.shortcut:
+                app = Gio.Application.get_default()
+                app.remove_accelerator(tool.shortcut)
 
-        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)
+    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)
 
-            # Attach the item and the handler to the action object
-            action._tool_item = item
-            action._tool_handler = handler
+            self._insert_directory(d, submenu)
 
-            # 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))
+        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
 
-            self._action_group.add_action_with_accel(action, item.shortcut)
+            action = Gio.SimpleAction(name=action_name)
+            action.connect('activate', capture_menu_action, self._window, self._panel, tool)
+            self._window.add_action(action)
 
-            ui_manager.add_ui(self._merge_id, path,
-                              action_name, action_name,
-                              Gtk.UIManagerItemType.MENUITEM, False)
+            menu.append(tool.name.replace('_', '__'), "win.%s" % action_name)
 
-    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):
@@ -142,10 +107,10 @@ 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)
+            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):
@@ -178,8 +143,7 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
         # 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")
+        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)
@@ -191,7 +155,6 @@ 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


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