[gedit-latex] Make actions document- rather than app- specific



commit e6205cfebcc43469f80a0e268989a91b83220aa9
Author: Pietro Battiston <pbattiston src gnome org>
Date:   Sat Apr 30 17:33:16 2016 +0200

    Make actions document- rather than app- specific

 latex/appactivatable.py    |   23 +++++++++++-----
 latex/windowactivatable.py |   60 +++++++++++++++++++------------------------
 2 files changed, 43 insertions(+), 40 deletions(-)
---
diff --git a/latex/appactivatable.py b/latex/appactivatable.py
index ff6f06c..f217684 100644
--- a/latex/appactivatable.py
+++ b/latex/appactivatable.py
@@ -60,8 +60,8 @@ class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
         self.init_tools()
         
     def add_latex_menu(self):
-        self.menu_ext = self.extend_menu("preferences-section")
-        menu = Gio.MenuItem.new(_("LaTeX"))
+        self.menu_ext = self.extend_menu("tools-section-1")
+        menu = Gio.MenuItem.new(_("LaTeX"), "win.FileDummyAction")
         container = Gio.Menu.new()
         menu.set_submenu(container)
         self.menu_ext.append_menu_item(menu)
@@ -72,15 +72,21 @@ class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
         for clazz in MENUACTIONS:
             action = clazz(icon_factory=self._icon_factory)
             actionlink = "win." + clazz.__name__
-            container.append_item(Gio.MenuItem.new(_(action.label), actionlink))
+            item = Gio.MenuItem.new(_(action.label), actionlink)
+            container.append_item(item)
+            # FIXME: this is not working (it does work in init_tools() below):
+            item.set_attribute_value("hidden-when",
+                                    GLib.Variant.new_string("action-disabled"))
             if action.accelerator is not None:
                 self.app.add_accelerator(action.accelerator, actionlink, None)
-                
+
     def add_latex_tools_menu(self):
-        menu = Gio.MenuItem.new(_("LaTeX Tools"))
-        container = Gio.Menu.new()
+        menu = Gio.MenuItem.new(_("LaTeX Tools"), "win.ToolsDummyAction")
+        container = Gio.Menu()
         menu.set_submenu(container)
         self.latex_tools_menu = container
+        menu.set_attribute_value("hidden-when",
+                                    GLib.Variant.new_string("action-disabled"))
         self.menu_ext.append_menu_item(menu)
     
     def init_tools(self):
@@ -94,7 +100,10 @@ class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
             name = "Tool%sAction" % i
 
             actionlink = "win." + name
-            self.latex_tools_menu.append_item(Gio.MenuItem.new(_(tool.label), actionlink))
+            item = Gio.MenuItem.new(_(tool.label), actionlink)
+            item.set_attribute_value("hidden-when",
+                                    GLib.Variant.new_string("action-disabled"))
+            self.latex_tools_menu.append_item(item)
 
             accelerator = None
             if tool.accelerator and len(tool.accelerator) > 0:
diff --git a/latex/windowactivatable.py b/latex/windowactivatable.py
index 6d926c1..255f826 100644
--- a/latex/windowactivatable.py
+++ b/latex/windowactivatable.py
@@ -396,39 +396,33 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         # FIXME: we are dealing with sets so saving the index as selection state
         # is nonsense
         #
-
-        # disable all actions
-        for name in self._action_objects:
-            self._action_group.get_action(name).set_visible(False)
-
-        # disable all tool actions
-        for l in list(self._tool_action_extensions.values()):
-            for name in l:
-                self._tool_action_group.get_action(name).set_sensitive(False)
-
-        # enable the actions for all extensions
-        for name in self._action_extensions[None]:
-            self._action_group.get_action(name).set_visible(True)
-
-        # enable the actions registered for the extension
-        if extension:
-            try:
-                for name in self._action_extensions[extension]:
-                    self._action_group.get_action(name).set_visible(True)
-            except KeyError:
-                pass
-
-        # enable the tool actions that apply for all extensions
-        for name in self._tool_action_extensions[None]:
-            self._tool_action_group.get_action(name).set_sensitive(True)
-
-        # enable the tool actions that apply for this extension
-        if extension:
-            try:
-                for name in self._tool_action_extensions[extension]:
-                    self._tool_action_group.get_action(name).set_sensitive(True)
-            except KeyError:
-                pass
+        
+        for category, catalog in (('actions', self._action_extensions),
+                                  ('tools', self._tool_action_extensions)):
+            # enable actions for all extensions
+            to_enable = set(catalog[None])
+            # enable the actions registered for the extension
+            if extension:
+                try:
+                    to_enable.update(catalog[extension])
+                except KeyError:
+                    pass
+            for name in set().union(*catalog.values()):
+                action = self.window.lookup_action(name)
+                action.set_enabled(name in to_enable)
+            
+            if category == 'tools':
+                # Show/hide the tool menu itself onlyif any action is shown:
+                menu_action = self.window.lookup_action('ToolsDummyAction')
+                # FIXME: This line spits "Gtk-WARNING"s which I really don't
+                # understand:
+                # "Duplicate child name in GtkStack: LaTeX Tools"
+                menu_action.set_enabled(bool(to_enable))
+        
+        # The "new LaTeX document" is the only one we always want enabled:
+        # (and hence its menu):
+        self.window.lookup_action('FileDummyAction').set_enabled(True)
+        self.window.lookup_action('LaTeXNewAction').set_enabled(True)
 
         if extension:
             self.show_toolbar()


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