[gedit] Fix the refresh of external tools when editing with the manager



commit 6f8972d045ce0bbe77efddb1c45915df7163cac7
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Mar 13 22:26:30 2014 +0100

    Fix the refresh of external tools when editing with the manager
    
    The manager is per-app and so its action should be moved to
    AppActivatable. This also allows us to loop over every window to update
    the actions and then update the menu model of the app.

 plugins/externaltools/tools/appactivatable.py    |   37 ++++++++++++++-
 plugins/externaltools/tools/windowactivatable.py |   54 ++++-----------------
 2 files changed, 47 insertions(+), 44 deletions(-)
---
diff --git a/plugins/externaltools/tools/appactivatable.py b/plugins/externaltools/tools/appactivatable.py
index 528dda7..09b04de 100644
--- a/plugins/externaltools/tools/appactivatable.py
+++ b/plugins/externaltools/tools/appactivatable.py
@@ -18,8 +18,10 @@
 
 from gi.repository import GLib, Gio, GObject, Gtk, Gdk, Gedit
 from .library import ToolLibrary
+from .manager import Manager
 import os
 
+
 class ToolMenu(object):
     def __init__(self, library, menu):
         super(ToolMenu, self).__init__()
@@ -65,6 +67,7 @@ class ToolMenu(object):
         self._insert_directory(self._library.tree, self._menu)
 
 
+# FIXME: restore the launch of the manager on configure using PeasGtk.Configurable
 class AppActivatable(GObject.Object, Gedit.AppActivatable):
     __gtype_name__ = "ExternalToolsAppActivatable"
 
@@ -73,11 +76,17 @@ class AppActivatable(GObject.Object, Gedit.AppActivatable):
     def __init__(self):
         GObject.Object.__init__(self)
         self.menu = None
+        self._manager = None
+        self._manager_default_size = None
 
     def do_activate(self):
         self._library = ToolLibrary()
         self._library.set_locations(os.path.join(self.plugin_info.get_data_dir(), 'tools'))
 
+        action = Gio.SimpleAction(name="manage-tools")
+        action.connect("activate", lambda action, parameter: self._open_dialog())
+        self.app.add_action(action)
+
         self.css = Gtk.CssProvider()
         self.css.load_from_data("""
 .gedit-tool-manager-paned {
@@ -114,7 +123,7 @@ class AppActivatable(GObject.Object, Gedit.AppActivatable):
                                                  self.css, 600)
 
         self.menu_ext = self.extend_menu("preferences-section")
-        item = Gio.MenuItem.new(_("Manage _External Tools..."), "win.manage-tools")
+        item = Gio.MenuItem.new(_("Manage _External Tools..."), "app.manage-tools")
         self.menu_ext.append_menu_item(item)
 
         self.submenu_ext = self.extend_menu("tools-section-1")
@@ -130,7 +139,33 @@ class AppActivatable(GObject.Object, Gedit.AppActivatable):
         self.menu.deactivate()
         self.menu_ext = None
         self.submenu_ext = None
+
+        self.app.remove_action("manage-tools")
+
         Gtk.StyleContext.remove_provider_for_screen(Gdk.Screen.get_default(),
                                                     self.css)
 
+    def _open_dialog(self):
+        if not self._manager:
+            self._manager = Manager(self.plugin_info.get_data_dir())
+
+            if self._manager_default_size:
+                self._manager.dialog.set_default_size(*self._manager_default_size)
+
+            self._manager.dialog.connect('destroy', self._on_manager_destroy)
+            self._manager.connect('tools-updated', self._on_manager_tools_updated)
+
+        self._manager.run(self.app.get_active_window())
+
+        return self._manager.dialog
+
+    def _on_manager_destroy(self, dialog):
+        self._manager_default_size = self._manager.get_final_size()
+        self._manager = None
+
+    def _on_manager_tools_updated(self, manager):
+        for window in self.app.get_main_windows():
+            window.external_tools_window_activatable.update_actions()
+        self.menu.update()
+
 # ex:ts=4:et:
diff --git a/plugins/externaltools/tools/windowactivatable.py 
b/plugins/externaltools/tools/windowactivatable.py
index b82899a..555cb73 100644
--- a/plugins/externaltools/tools/windowactivatable.py
+++ b/plugins/externaltools/tools/windowactivatable.py
@@ -16,15 +16,15 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-__all__ = ('ExternalToolsPlugin', 'Manager', 'OutputPanel', 'Capture', 'UniqueById')
+__all__ = ('ExternalToolsPlugin', 'OutputPanel', 'Capture', 'UniqueById')
 
 from gi.repository import GLib, Gio, GObject, Gtk, Gedit, PeasGtk
-from .manager import Manager
 from .library import ToolLibrary
 from .outputpanel import OutputPanel
 from .capture import Capture
 from .functions import *
 
+
 class ToolActions(object):
     def __init__(self, library, window, panel):
         super(ToolActions, self).__init__()
@@ -60,10 +60,10 @@ class ToolActions(object):
     def filter_language(self, language, item):
         if not item.languages:
             return True
-        
+
         if not language and 'plain' in item.languages:
             return True
-        
+
         if language and (language.get_id() in item.languages):
             return True
         else:
@@ -81,7 +81,7 @@ class ToolActions(object):
 
         states = {
             'always': True,
-            'all' : document is not None,
+            'all': document is not None,
             'local': titled and not remote,
             'remote': titled and remote,
             'titled': titled,
@@ -94,7 +94,7 @@ class ToolActions(object):
                 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):
     __gtype_name__ = "ExternalToolsWindowActivatable"
 
@@ -102,18 +102,12 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
 
     def __init__(self):
         GObject.Object.__init__(self)
-        self._manager = None
-        self._manager_default_size = None
         self.actions = None
 
     def do_activate(self):
-        # Ugly hack... we need to get access to the activatable to update the menu actions
-        self.window._external_tools_window_activatable = self
-        self._library = ToolLibrary()
+        self.window.external_tools_window_activatable = self
 
-        action = Gio.SimpleAction(name="manage-tools")
-        action.connect("activate", lambda action, parameter: self.open_dialog())
-        self.window.add_action(action)
+        self._library = ToolLibrary()
 
         # Create output console
         self._output_buffer = OutputPanel(self.plugin_info.get_data_dir(), self.window)
@@ -128,38 +122,12 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
             self.actions.filter(self.window.get_active_document())
 
     def do_deactivate(self):
-        self.window._external_tools_window_activatable = None
         self.actions.deactivate()
-        self.window.remove_action("manage-tools")
-
         bottom = self.window.get_bottom_panel()
         bottom.remove(self._output_buffer.panel)
+        self.window.external_tools_window_activatable = None
 
-    def open_dialog(self):
-        if not self._manager:
-            self._manager = Manager(self.plugin_info.get_data_dir())
-
-            if self._manager_default_size:
-                self._manager.dialog.set_default_size(*self._manager_default_size)
-
-            self._manager.dialog.connect('destroy', self.on_manager_destroy)
-            self._manager.connect('tools-updated', self.on_manager_tools_updated)
-
-        window = Gio.Application.get_default().get_active_window()
-        self._manager.run(window)
-
-        return self._manager.dialog
-
-    def update_manager(self, tool):
-        if self._manager:
-            self._manager.tool_changed(tool, True)
-
-    def on_manager_destroy(self, dialog):
-        self._manager_default_size = self._manager.get_final_size()
-        self._manager = None
-
-    def on_manager_tools_updated(self, manager):
-        for window in Gio.Application.get_default().get_main_windows():
-            window._external_tools_window_activatable.actions.update()
+    def update_actions(self):
+        self.actions.update()
 
 # ex:ts=4:et:


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