[gnome-terminal/wip/headerbar: 3/10] window: Add primary menu to headerbar



commit 4c97ed0854c423b952ca555827e00a9e4acf36f7
Author: Florian Müllner <fmuellner gnome org>
Date:   Tue Nov 6 20:10:12 2018 +0100

    window: Add primary menu to headerbar
    
    Commit 4b9f6d398d removed the app menu in line with the corresponding
    initiative[0]. The recommendation is to make the menu's action available
    under a primary menu button in the headerbar, so do that now that we
    have one.
    
    [0] https://gitlab.gnome.org/GNOME/Initiatives/wikis/App-Menu-Retirement
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756798

 src/Makefile.am            |   1 +
 src/terminal-app.c         | 132 ++++++++++++++++++++++++++++++++++-----------
 src/terminal-app.h         |   2 +
 src/terminal-headerbar.c   |  10 +++-
 src/terminal-headerbar.ui  |  20 +++++++
 src/terminal-headermenu.ui |  36 +++++++++++++
 src/terminal.gresource.xml |   1 +
 7 files changed, 170 insertions(+), 32 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 30b7cebd..b36cc8e3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -328,6 +328,7 @@ CLEANFILES = \
 EXTRA_DIST = \
        terminal.about \
        terminal-headerbar.ui \
+       terminal-headermenu.ui \
        terminal-menubar.ui.in \
        terminal-notebook-menu.ui \
        terminal-window.ui \
diff --git a/src/terminal-app.c b/src/terminal-app.c
index bb232e09..d35665fc 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -108,6 +108,12 @@ struct _TerminalApp
   GMenu *menubar_new_terminal_section;
   GMenu *menubar_set_profile_section;
   GMenu *menubar_set_encoding_submenu;
+
+  GMenuModel *headermenu;
+  GMenu *headermenu_new_terminal_section;
+  GMenu *headermenu_set_profile_section;
+  GMenu *headermenu_set_encoding_submenu;
+
   GMenu *set_profile_menu;
 
   GtkClipboard *clipboard;
@@ -467,8 +473,6 @@ set_profile_submenu_new (ProfileData *data,
 static void
 terminal_app_update_profile_menus (TerminalApp *app)
 {
-  g_menu_remove_all (G_MENU (app->menubar_new_terminal_section));
-  g_menu_remove_all (G_MENU (app->menubar_set_profile_section));
   g_clear_object (&app->set_profile_menu);
 
   /* Get profiles list and sort by label */
@@ -485,16 +489,83 @@ terminal_app_update_profile_menus (TerminalApp *app)
   ProfileData *profiles = (ProfileData*) array->data;
   guint n_profiles = array->len;
 
-  fill_new_terminal_section (app, app->menubar_new_terminal_section, profiles, n_profiles);
-
   app->set_profile_menu = set_profile_submenu_new (profiles, n_profiles);
 
-  if (app->set_profile_menu != NULL) {
-    g_menu_append_submenu (app->menubar_set_profile_section, _("Change _Profile"),
-                           G_MENU_MODEL (app->set_profile_menu));
+  if (app->menubar != NULL) {
+    g_menu_remove_all (G_MENU (app->menubar_new_terminal_section));
+    fill_new_terminal_section (app, app->menubar_new_terminal_section, profiles, n_profiles);
+
+    g_menu_remove_all (G_MENU (app->menubar_set_profile_section));
+    if (app->set_profile_menu != NULL) {
+      g_menu_append_submenu (app->menubar_set_profile_section, _("Change _Profile"),
+                             G_MENU_MODEL (app->set_profile_menu));
+    }
+  }
+
+  if (app->headermenu != NULL) {
+    g_menu_remove_all (G_MENU (app->headermenu_new_terminal_section));
+    fill_new_terminal_section (app, app->headermenu_new_terminal_section, profiles, n_profiles);
+#if 0
+    g_menu_remove_all (G_MENU (app->headermenu_set_profile_section));
+    if (app->set_profile_menu != NULL) {
+      g_menu_append_submenu (app->headermenu_set_profile_section, _("Change _Profile"),
+                             G_MENU_MODEL (app->set_profile_menu));
+    }
+#endif
   }
 }
 
+static GMenuModel *
+terminal_app_create_menubar (TerminalApp *app,
+                             gboolean shell_shows_menubar)
+{
+  /* If the menubar is shown by the shell, omit mnemonics for the submenus. This is because Alt+F etc.
+   * are more important to be usable in the terminal, the menu cannot be replaced runtime (to toggle
+   * between mnemonic and non-mnemonic versions), gtk-enable-mnemonics or gtk_window_set_mnemonic_modifier()
+   * don't effect the menubar either, so there wouldn't be a way to disable Alt+F for File etc. otherwise.
+   * Furthermore, the menu would even grab mnemonics from the File and Preferences windows.
+   * In Unity, Alt+F10 opens the menubar, this should be good enough for keyboard navigation.
+   * If the menubar is shown by the app, toggling mnemonics is handled in terminal-window.c using
+   * gtk_window_set_mnemonic_modifier().
+   * See bug 792978 for details. */
+  terminal_util_load_objects_resource (shell_shows_menubar ? 
"/org/gnome/terminal/ui/menubar-without-mnemonics.ui"
+                                                           : 
"/org/gnome/terminal/ui/menubar-with-mnemonics.ui",
+                                       "menubar", &app->menubar,
+                                       "new-terminal-section", &app->menubar_new_terminal_section,
+                                       "set-profile-section", &app->menubar_set_profile_section,
+                                       "set-encoding-submenu", &app->menubar_set_encoding_submenu,
+                                       NULL);
+
+  /* Install the encodings submenu */
+  terminal_encodings_append_menu (app->menubar_set_encoding_submenu);
+
+  /* Install profile sections */
+  terminal_app_update_profile_menus (app);
+
+  return app->menubar;
+}
+
+static void
+terminal_app_create_headermenu (TerminalApp *app)
+{
+  terminal_util_load_objects_resource ("/org/gnome/terminal/ui/headerbar-menu.ui",
+                                       "headermenu", &app->headermenu,
+                                       "new-terminal-section", &app->headermenu_new_terminal_section,
+#if 0
+                                       "set-profile-section", &app->headermenu_set_profile_section,
+                                       "set-encoding-submenu", &app->headermenu_set_encoding_submenu,
+#endif
+                                       NULL);
+
+#if 0
+  /* Install the encodings submenu */
+  terminal_encodings_append_menu (app->headermenu_set_encoding_submenu);
+#endif
+
+  /* Install profile sections */
+  terminal_app_update_profile_menus (app);
+}
+
 /* Clipboard */
 
 static void
@@ -645,35 +716,17 @@ terminal_app_startup (GApplication *application)
                 "gtk-shell-shows-menubar", &shell_shows_menubar,
                 NULL);
 
-  /* Menubar */
-  /* If the menubar is shown by the shell, omit mnemonics for the submenus. This is because Alt+F etc.
-   * are more important to be usable in the terminal, the menu cannot be replaced runtime (to toggle
-   * between mnemonic and non-mnemonic versions), gtk-enable-mnemonics or gtk_window_set_mnemonic_modifier()
-   * don't effect the menubar either, so there wouldn't be a way to disable Alt+F for File etc. otherwise.
-   * Furthermore, the menu would even grab mnemonics from the File and Preferences windows.
-   * In Unity, Alt+F10 opens the menubar, this should be good enough for keyboard navigation.
-   * If the menubar is shown by the app, toggling mnemonics is handled in terminal-window.c using
-   * gtk_window_set_mnemonic_modifier().
-   * See bug 792978 for details. */
-  terminal_util_load_objects_resource (shell_shows_menubar ? 
"/org/gnome/terminal/ui/menubar-without-mnemonics.ui"
-                                                           : 
"/org/gnome/terminal/ui/menubar-with-mnemonics.ui",
-                                       "menubar", &app->menubar,
-                                       "new-terminal-section", &app->menubar_new_terminal_section,
-                                       "set-profile-section", &app->menubar_set_profile_section,
-                                       "set-encoding-submenu", &app->menubar_set_encoding_submenu,
-                                       NULL);
+  /* Create menubar */
+  terminal_app_create_menubar (app, shell_shows_menubar);
 
-  /* Create dynamic menus and keep them updated */
-  terminal_app_update_profile_menus (app);
+  /* Keep dynamic menus updated */
   g_signal_connect_swapped (app->profiles_list, "children-changed",
                             G_CALLBACK (terminal_app_update_profile_menus), app);
 
-  /* Install the encodings submenu */
-  terminal_encodings_append_menu (app->menubar_set_encoding_submenu);
-
   /* Show/hide the menubar as appropriate: If the shell wants to show the menubar, make it available. */
   if (shell_shows_menubar)
-    gtk_application_set_menubar (GTK_APPLICATION (app), app->menubar);
+    gtk_application_set_menubar (GTK_APPLICATION (app),
+                                 terminal_app_get_menubar (app));
 
   _terminal_debug_print (TERMINAL_DEBUG_SERVER, "Startup complete\n");
 }
@@ -760,6 +813,10 @@ terminal_app_finalize (GObject *object)
   g_clear_object (&app->menubar_new_terminal_section);
   g_clear_object (&app->menubar_set_profile_section);
   g_clear_object (&app->menubar_set_encoding_submenu);
+  g_clear_object (&app->headermenu);
+  g_clear_object (&app->headermenu_new_terminal_section);
+  g_clear_object (&app->headermenu_set_profile_section);
+  g_clear_object (&app->headermenu_set_encoding_submenu);
   g_clear_object (&app->set_profile_menu);
 
   terminal_accels_shutdown ();
@@ -1073,6 +1130,21 @@ terminal_app_get_menubar (TerminalApp *app)
   return app->menubar;
 }
 
+/**
+ * terminal_app_get_headermenu:
+ * @app: a #TerminalApp
+ *
+ * Returns: (tranfer none): the main window headerbar menu bar as a #GMenuModel
+ */
+GMenuModel *
+terminal_app_get_headermenu (TerminalApp *app)
+{
+  if (app->headermenu == NULL)
+    terminal_app_create_headermenu (app);
+
+  return app->headermenu;
+}
+
 /**
  * terminal_app_get_profile_section:
  * @app: a #TerminalApp
diff --git a/src/terminal-app.h b/src/terminal-app.h
index 975b876c..4e354e85 100644
--- a/src/terminal-app.h
+++ b/src/terminal-app.h
@@ -101,6 +101,8 @@ TerminalSettingsList *terminal_app_get_profiles_list (TerminalApp *app);
 
 GMenuModel *terminal_app_get_menubar (TerminalApp *app);
 
+GMenuModel *terminal_app_get_headermenu (TerminalApp *app);
+
 GMenuModel *terminal_app_get_profile_section (TerminalApp *app);
 
 gboolean terminal_app_get_menu_unified (TerminalApp *app);
diff --git a/src/terminal-headerbar.c b/src/terminal-headerbar.c
index 58132e31..66173c20 100644
--- a/src/terminal-headerbar.c
+++ b/src/terminal-headerbar.c
@@ -20,6 +20,7 @@
 #include <glib/gi18n.h>
 
 #include "terminal-headerbar.h"
+#include "terminal-app.h"
 #include "terminal-libgsystem.h"
 
 typedef struct _TerminalHeaderbarPrivate TerminalHeaderbarPrivate;
@@ -36,7 +37,7 @@ struct _TerminalHeaderbarClass
 
 struct _TerminalHeaderbarPrivate
 {
-  gpointer dummy;
+  GtkWidget *menubutton;
 };
 
 enum {
@@ -60,10 +61,14 @@ G_DEFINE_TYPE_WITH_PRIVATE (TerminalHeaderbar, terminal_headerbar, GTK_TYPE_HEAD
 static void
 terminal_headerbar_init (TerminalHeaderbar *headerbar)
 {
-  //  TerminalHeaderbarPrivate *priv = PRIV (headerbar);
+  TerminalHeaderbarPrivate *priv = PRIV (headerbar);
   GtkWidget *widget = GTK_WIDGET (headerbar);
+  TerminalApp *app = terminal_app_get ();
 
   gtk_widget_init_template (widget);
+
+  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (priv->menubutton),
+                                  terminal_app_get_headermenu (app));
 }
 
 static void
@@ -118,6 +123,7 @@ terminal_headerbar_class_init (TerminalHeaderbarClass *klass)
   /* g_object_class_install_properties (gobject_class, G_N_ELEMENTS (pspecs), pspecs); */
 
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/terminal/ui/headerbar.ui");
+  gtk_widget_class_bind_template_child_private (widget_class, TerminalHeaderbar, menubutton);
 }
 
 /* public API */
diff --git a/src/terminal-headerbar.ui b/src/terminal-headerbar.ui
index b75ee6b5..284bc187 100644
--- a/src/terminal-headerbar.ui
+++ b/src/terminal-headerbar.ui
@@ -39,6 +39,26 @@
         </child>
       </object>
     </child>
+    <child>
+      <object class="GtkMenuButton" id="menubutton">
+        <property name="visible">True</property>
+        <property name="focus_on_click">False</property>
+        <property name="can_focus">True</property>
+        <property name="receives_default">True</property>
+        <style>
+          <class name="image-button"/>
+        </style>
+        <child>
+          <object class="GtkImage">
+            <property name="visible">True</property>
+            <property name="icon_name">open-menu-symbolic</property>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="pack_type">end</property>
+      </packing>
+    </child>
     <child>
       <object class="GtkButton">
         <property name="visible">True</property>
diff --git a/src/terminal-headermenu.ui b/src/terminal-headermenu.ui
new file mode 100644
index 00000000..5f1e39b3
--- /dev/null
+++ b/src/terminal-headermenu.ui
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright © 2012 Christian Persch
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+-->
+<interface>
+  <menu id="headermenu">
+    <section id="new-terminal-section" />
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Help</attribute>
+        <attribute name="action">app.help</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_About</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/src/terminal.gresource.xml b/src/terminal.gresource.xml
index ba26f3c8..8fd50407 100644
--- a/src/terminal.gresource.xml
+++ b/src/terminal.gresource.xml
@@ -19,6 +19,7 @@
   <gresource prefix="/org/gnome/terminal">
     <file alias="css/terminal.css" compressed="true">terminal.common.css</file>
     <file alias="ui/headerbar.ui" compressed="true" preprocess="xml-stripblanks">terminal-headerbar.ui</file>
+    <file alias="ui/headerbar-menu.ui" compressed="true" 
preprocess="xml-stripblanks">terminal-headermenu.ui</file>
     <file alias="ui/menubar-with-mnemonics.ui" compressed="true" 
preprocess="xml-stripblanks">terminal-menubar-with-mnemonics.ui</file>
     <file alias="ui/menubar-without-mnemonics.ui" compressed="true" 
preprocess="xml-stripblanks">terminal-menubar-without-mnemonics.ui</file>
     <file alias="ui/notebook-menu.ui" compressed="true" 
preprocess="xml-stripblanks">terminal-notebook-menu.ui</file>


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