[gedit] Allow plugins to extend the appmenu



commit 57b9342f7739e49bc09f6e7d649d12bb5f25b14f
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Feb 4 00:33:12 2014 +0100

    Allow plugins to extend the appmenu
    
    Rename the API to just be extend_menu instead of extend_gear_menu
    and look for the requested extension point in all the menus we have.
    Plugins are updated for the API change and snippets and tools now
    put their pref dialogs in the app menu.
    We still need to rename all the extension points to something more
    meaningful of "ext1" and we probably should add similar API to the
    app-actiavatable.

 gedit/gedit-menu.ui                              |    3 +
 gedit/gedit-window-activatable.c                 |   53 +++++++++++++++-------
 gedit/gedit-window-activatable.h                 |   12 +++--
 gedit/gedit-window.ui                            |    8 +++-
 plugins/docinfo/gedit-docinfo-plugin.c           |    4 +-
 plugins/externaltools/tools/windowactivatable.py |   14 +++---
 plugins/quickopen/quickopen/__init__.py          |    2 +-
 plugins/snippets/snippets/windowactivatable.py   |    2 +-
 plugins/sort/gedit-sort-plugin.c                 |    4 +-
 plugins/spell/gedit-spell-plugin.c               |    4 +-
 plugins/time/gedit-time-plugin.c                 |    4 +-
 11 files changed, 73 insertions(+), 37 deletions(-)
---
diff --git a/gedit/gedit-menu.ui b/gedit/gedit-menu.ui
index 5551a1e..9c7288a 100644
--- a/gedit/gedit-menu.ui
+++ b/gedit/gedit-menu.ui
@@ -3,18 +3,21 @@
   <!-- interface-requires gtk+ 3.0 -->
   <menu id="appmenu">
     <section>
+      <attribute name="id">appmenuext1</attribute>
       <item>
         <attribute name="label" translatable="yes">_New Window</attribute>
         <attribute name="action">app.new-window</attribute>
       </item>
     </section>
     <section>
+      <attribute name="id">appmenuext2</attribute>
       <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
         <attribute name="action">app.preferences</attribute>
       </item>
     </section>
     <section>
+      <attribute name="id">appmenuext3</attribute>
       <item>
         <attribute name="label" translatable="yes">_Help</attribute>
         <attribute name="action">app.help</attribute>
diff --git a/gedit/gedit-window-activatable.c b/gedit/gedit-window-activatable.c
index c3354ca..4fb483d 100644
--- a/gedit/gedit-window-activatable.c
+++ b/gedit/gedit-window-activatable.c
@@ -124,40 +124,61 @@ gedit_window_activatable_update_state (GeditWindowActivatable *activatable)
        }
 }
 
+static GMenuModel *
+find_extension_point_section (GMenuModel  *model,
+                              const gchar *extension_point)
+{
+       gint i, n_items;
+       GMenuModel *section = NULL;
+
+       n_items = g_menu_model_get_n_items (model);
+
+       for (i = 0; i < n_items && !section; i++)
+       {
+               gchar *id = NULL;
+
+               if (g_menu_model_get_item_attribute (model, i, "id", "s", &id) &&
+                   strcmp (id, extension_point) == 0)
+               {
+                       section = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION);
+               }
+
+               g_free (id);
+       }
+
+       return section;
+}
+
 GeditMenuExtension *
-gedit_window_activatable_extend_gear_menu (GeditWindowActivatable *activatable,
-                                           const gchar            *extension_point)
+gedit_window_activatable_extend_menu (GeditWindowActivatable *activatable,
+                                      const gchar            *extension_point)
 {
-       GeditMenuExtension *menu = NULL;
        GeditWindow *window;
        GMenuModel *model;
-       gint i, n_items;
+       GMenuModel *section;
 
        g_return_val_if_fail (GEDIT_IS_WINDOW_ACTIVATABLE (activatable), NULL);
        g_return_val_if_fail (extension_point != NULL, NULL);
 
+       /* First look in the gear menu */
        g_object_get (G_OBJECT (activatable), "window", &window, NULL);
        model = _gedit_window_get_gear_menu (window);
        g_object_unref (window);
 
-       n_items = g_menu_model_get_n_items (model);
+       section = find_extension_point_section (model, extension_point);
 
-       for (i = 0; i < n_items; i++)
+       /* otherwise look in the app menu */
+       if (section == NULL)
        {
-               gchar *id = NULL;
+               model = gtk_application_get_app_menu (GTK_APPLICATION (g_application_get_default ()));
 
-               if (g_menu_model_get_item_attribute (model, i, "id", "s", &id) &&
-                   strcmp (id, extension_point) == 0)
+               if (model != NULL)
                {
-                       GMenuModel *section;
-
-                       section = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION);
-                       menu = _gedit_menu_extension_new (G_MENU (section));
+                       section = find_extension_point_section (model, extension_point);
                }
-
-               g_free (id);
        }
 
-       return menu;
+       return section != NULL ? _gedit_menu_extension_new (G_MENU (section)) : NULL;
 }
 
+/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-window-activatable.h b/gedit/gedit-window-activatable.h
index c218fa3..3e13b66 100644
--- a/gedit/gedit-window-activatable.h
+++ b/gedit/gedit-window-activatable.h
@@ -58,17 +58,21 @@ void         gedit_window_activatable_deactivate    (GeditWindowActivatable *activatable);
 void    gedit_window_activatable_update_state  (GeditWindowActivatable *activatable);
 
 /**
- * gedit_window_activatable_extend_gear_menu:
+ * gedit_window_activatable_extend_menu:
  * @activatable: A #GeditWindowActivatable.
  * @extension_point: the extension point section of the menu to get.
  *
- * Gets the #GeditMenuExtension for the gear menu @extension_point.
+ * Gets the #GeditMenuExtension for the menu @extension_point. Note that
+ * the extension point could be in different menus (gear menu, app menu, etc)
+ * depending on the platform.
  *
  * Returns: (transfer full): a #GeditMenuExtension for the specific section or %NULL if not found.
  */
-GeditMenuExtension *gedit_window_activatable_extend_gear_menu (GeditWindowActivatable *activatable,
-                                                                const gchar            *extension_point);
+GeditMenuExtension *gedit_window_activatable_extend_menu (GeditWindowActivatable *activatable,
+                                                          const gchar            *extension_point);
 
 G_END_DECLS
 
 #endif /* __GEDIT_WINDOW_ACTIVATABLE_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-window.ui b/gedit/gedit-window.ui
index a1e94c8..50bc38f 100644
--- a/gedit/gedit-window.ui
+++ b/gedit/gedit-window.ui
@@ -181,13 +181,19 @@
     </section>
     <section>
       <attribute name="id">ext11</attribute>
+    </section>
+    <section>
+      <attribute name="id">ext12</attribute>
+    </section>
+    <section>
+      <attribute name="id">appmenuext2</attribute>
       <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
         <attribute name="action">app.preferences</attribute>
       </item>
     </section>
     <section>
-      <attribute name="id">ext12</attribute>
+      <attribute name="id">appmenuext3</attribute>
       <item>
         <attribute name="label" translatable="yes">_Help</attribute>
         <attribute name="action">app.help</attribute>
diff --git a/plugins/docinfo/gedit-docinfo-plugin.c b/plugins/docinfo/gedit-docinfo-plugin.c
index d69754e..97539bf 100644
--- a/plugins/docinfo/gedit-docinfo-plugin.c
+++ b/plugins/docinfo/gedit-docinfo-plugin.c
@@ -530,8 +530,8 @@ gedit_docinfo_plugin_activate (GeditWindowActivatable *activatable)
        g_action_map_add_action (G_ACTION_MAP (priv->window),
                                 G_ACTION (priv->action));
 
-       priv->menu = gedit_window_activatable_extend_gear_menu (activatable,
-                                                               "ext9");
+       priv->menu = gedit_window_activatable_extend_menu (activatable,
+                                                          "ext9");
        item = g_menu_item_new (_("_Document Statistics"), "win.docinfo");
        gedit_menu_extension_append_menu_item (priv->menu, item);
        g_object_unref (item);
diff --git a/plugins/externaltools/tools/windowactivatable.py 
b/plugins/externaltools/tools/windowactivatable.py
index 676df96..f409d30 100644
--- a/plugins/externaltools/tools/windowactivatable.py
+++ b/plugins/externaltools/tools/windowactivatable.py
@@ -134,16 +134,18 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
         self.window._external_tools_window_activatable = self
         self._library = ToolLibrary()
 
-        action = Gio.SimpleAction(name="manage_tools")
+        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.append_menu_item(item)
+        self.menu_ext = self.extend_menu("appmenuext2")
+        item = Gio.MenuItem.new(_("Manage _External Tools..."), "win.manage-tools")
+        self.menu_ext.append_menu_item(item)
+
+        self.submenu_ext = self.extend_menu("ext9")
         external_tools_submenu = Gio.Menu()
         item = Gio.MenuItem.new_submenu(_("External _Tools"), external_tools_submenu)
-        self.gear_menu.append_menu_item(item)
+        self.submenu_ext.append_menu_item(item)
         external_tools_submenu_section = Gio.Menu()
         external_tools_submenu.append_section(None, external_tools_submenu_section)
 
@@ -162,7 +164,7 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
     def do_deactivate(self):
         self.window._external_tools_window_activatable = None
         self.menu.deactivate()
-        self.window.remove_action("manage_tools")
+        self.window.remove_action("manage-tools")
 
         bottom = self.window.get_bottom_panel()
         bottom.remove(self._output_buffer.panel)
diff --git a/plugins/quickopen/quickopen/__init__.py b/plugins/quickopen/quickopen/__init__.py
index 439df46..e110156 100644
--- a/plugins/quickopen/quickopen/__init__.py
+++ b/plugins/quickopen/quickopen/__init__.py
@@ -66,7 +66,7 @@ class QuickOpenPlugin(GObject.Object, Gedit.WindowActivatable):
         item = Gio.MenuItem.new(_("Quick Open..."), "win.quickopen")
         item.set_attribute_value("accel", GLib.Variant.new_string("<Primary><Alt>O"))
 
-        self.menu = self.extend_gear_menu("ext2")
+        self.menu = self.extend_menu("ext2")
         self.menu.prepend_menu_item(item)
 
     def _create_popup(self):
diff --git a/plugins/snippets/snippets/windowactivatable.py b/plugins/snippets/snippets/windowactivatable.py
index ab5e038..54521c8 100644
--- a/plugins/snippets/snippets/windowactivatable.py
+++ b/plugins/snippets/snippets/windowactivatable.py
@@ -151,7 +151,7 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable, Signals):
                 self.window.add_action(action)
 
                 item = Gio.MenuItem.new(_("Manage _Snippets..."), "win.snippets")
-                self.menu = self.extend_gear_menu("ext9")
+                self.menu = self.extend_menu("appmenuext2")
                 self.menu.append_menu_item(item)
 
         def remove_menu(self):
diff --git a/plugins/sort/gedit-sort-plugin.c b/plugins/sort/gedit-sort-plugin.c
index 734090a..fb8bcc6 100644
--- a/plugins/sort/gedit-sort-plugin.c
+++ b/plugins/sort/gedit-sort-plugin.c
@@ -428,8 +428,8 @@ gedit_sort_plugin_activate (GeditWindowActivatable *activatable)
        g_action_map_add_action (G_ACTION_MAP (priv->window),
                                 G_ACTION (priv->action));
 
-       priv->menu = gedit_window_activatable_extend_gear_menu (activatable,
-                                                               "ext9");
+       priv->menu = gedit_window_activatable_extend_menu (activatable,
+                                                          "ext9");
        item = g_menu_item_new (_("S_ort..."), "win.sort");
        gedit_menu_extension_append_menu_item (priv->menu, item);
        g_object_unref (item);
diff --git a/plugins/spell/gedit-spell-plugin.c b/plugins/spell/gedit-spell-plugin.c
index 0a19f43..53ca966 100644
--- a/plugins/spell/gedit-spell-plugin.c
+++ b/plugins/spell/gedit-spell-plugin.c
@@ -1131,8 +1131,8 @@ gedit_spell_plugin_activate (GeditWindowActivatable *activatable)
                                         G_N_ELEMENTS (action_entries),
                                         activatable);
 
-       priv->menu = gedit_window_activatable_extend_gear_menu (activatable,
-                                                               "ext5");
+       priv->menu = gedit_window_activatable_extend_menu (activatable,
+                                                          "ext5");
 
        item = g_menu_item_new (_("_Check Spelling..."), "win.check_spell");
        gedit_menu_extension_append_menu_item (priv->menu, item);
diff --git a/plugins/time/gedit-time-plugin.c b/plugins/time/gedit-time-plugin.c
index 6771209..8eeb837 100644
--- a/plugins/time/gedit-time-plugin.c
+++ b/plugins/time/gedit-time-plugin.c
@@ -270,8 +270,8 @@ gedit_time_plugin_activate (GeditWindowActivatable *activatable)
        g_action_map_add_action (G_ACTION_MAP (priv->window),
                                 G_ACTION (priv->action));
 
-       priv->menu = gedit_window_activatable_extend_gear_menu (activatable,
-                                                               "ext5");
+       priv->menu = gedit_window_activatable_extend_menu (activatable,
+                                                          "ext5");
        item = g_menu_item_new (_("In_sert Date and Time..."), "win.time");
        gedit_menu_extension_append_menu_item (priv->menu, item);
        g_object_unref (item);


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