[gnome-terminal/wip/fmuellner/headerbar: 5/14] app: Refactor dynamic menu creation



commit eade7ed424b0b746a7c27dfbdb10d4942715fb82
Author: Christian Persch <chpe src gnome org>
Date:   Mon Nov 5 23:24:50 2018 +0100

    app: Refactor dynamic menu creation
    
    Profiles and encodings sections in the menubar are filled in
    dynamically. We'll need similar code to expose those options
    in the headerbar, prepare for that by shuffling the existing
    code around a bit.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756798

 src/terminal-app.c | 74 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 30 deletions(-)
---
diff --git a/src/terminal-app.c b/src/terminal-app.c
index bb232e09..de9f767f 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -467,8 +467,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 +483,50 @@ 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));
+    }
   }
 }
 
+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;
+}
+
 /* Clipboard */
 
 static void
@@ -645,35 +677,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");
 }


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