[gnome-terminal] all: Improve mnemonic handling



commit f1c46cb52560db106b85a93346c8bcf340ac05f1
Author: Egmont Koblinger <egmont gmail com>
Date:   Sun Feb 4 21:00:18 2018 +0100

    all: Improve mnemonic handling
    
    Disable mnemonics for the submenus (Alt+F for File etc.) when the menubar
    is shown by the shell (e.g. Unity) so that they are usable in the terminal.
    (As of the GMenu port, disabling them via the config option didn't work.)
    Also hide the corresponding preference checkbox.
    
    Make mnemonics work in the Find and Preferences windows.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=792978

 po/POTFILES.in                                     |    2 +-
 src/Makefile.am                                    |   10 +++++-
 src/terminal-app.c                                 |   26 +++++++++++----
 ...{terminal-menubar.ui => terminal-menubar.ui.in} |   21 ++++++++----
 src/terminal-prefs.c                               |   14 +++++---
 src/terminal-window.c                              |   35 ++++++++++++++++---
 src/terminal.gresource.xml                         |    3 +-
 7 files changed, 83 insertions(+), 28 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ff8f72c..e906bbb 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -16,7 +16,7 @@ src/terminal-app.c
 src/terminal.c
 src/terminal-encoding.c
 src/terminal-mdi-container.c
-[type: gettext/glade]src/terminal-menubar.ui
+[type: gettext/glade]src/terminal-menubar.ui.in
 src/terminal-menu-button.c
 src/terminal-nautilus.c
 src/terminal-notebook.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 836a36f..c02439b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,6 +30,8 @@ gnome_terminal_server_built = \
        terminal-gdbus-generated.h \
        terminal-resources.c \
        terminal-resources.h \
+       terminal-menubar-with-mnemonics.ui \
+       terminal-menubar-without-mnemonics.ui \
        $(NULL)
 BUILT_SOURCES += $(gnome_terminal_server_built)
 
@@ -167,6 +169,12 @@ terminal-gdbus-generated.c terminal-gdbus-generated.h: org.gnome.Terminal.xml Ma
                        --generate-c-code terminal-gdbus-generated \
                        $<
 
+terminal-menubar-with-mnemonics.ui: terminal-menubar.ui.in
+       sed -e 's|<WITH_MNEMONIC>||g' -e 's|</WITH_MNEMONIC>||g' -e 's|<WITHOUT_MNEMONIC>|<!-- |g' -e 
's|</WITHOUT_MNEMONIC>| -->|g' $< > $@
+
+terminal-menubar-without-mnemonics.ui: terminal-menubar.ui.in
+       sed -e 's|<WITH_MNEMONIC>|<!-- |g' -e 's|</WITH_MNEMONIC>| -->|g' -e 's|<WITHOUT_MNEMONIC>||g' -e 
's|</WITHOUT_MNEMONIC>||g' $< > $@
+
 terminal-resources.h terminal-resources.c: terminal.gresource.xml Makefile $(shell $(GLIB_COMPILE_RESOURCES) 
--generate-dependencies --sourcedir $(srcdir) $(srcdir)/terminal.gresource.xml)
        $(AM_V_GEN) XMLLINT=$(XMLLINT) $(GLIB_COMPILE_RESOURCES) --target $@ --sourcedir $(srcdir) --generate 
--c-name terminal $<
 
@@ -354,7 +362,7 @@ CLEANFILES = \
 EXTRA_DIST = \
        terminal.about \
        terminal-appmenu.ui \
-       terminal-menubar.ui \
+       terminal-menubar.ui.in \
        terminal-notebook-menu.ui \
        terminal-window.ui \
        terminal.gresource.xml \
diff --git a/src/terminal-app.c b/src/terminal-app.c
index 8f44b6b..4af540b 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -689,13 +689,30 @@ terminal_app_startup (GApplication *application)
 
   app_load_css (application);
 
+  /* Figure out whether the shell shows appmenu/menubar */
+  gboolean shell_shows_appmenu, shell_shows_menubar;
+  g_object_get (gtk_settings_get_default (),
+                "gtk-shell-shows-app-menu", &shell_shows_appmenu,
+                "gtk-shell-shows-menubar", &shell_shows_menubar,
+                NULL);
+
   /* App menu */
   GMenu *appmenu_new_terminal_section = gtk_application_get_menu_by_id (gtk_application,
                                                                         "new-terminal-section");
   fill_new_terminal_section (appmenu_new_terminal_section, NULL, 0); /* no submenu */
 
   /* Menubar */
-  terminal_util_load_objects_resource ("/org/gnome/terminal/ui/menubar.ui",
+  /* 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,
@@ -710,12 +727,7 @@ terminal_app_startup (GApplication *application)
   /* Install the encodings submenu */
   terminal_encodings_append_menu (app->menubar_set_encoding_submenu);
 
-  /* If the shell wants to show the appmenu/menubar, make it available */
-  gboolean shell_shows_appmenu, shell_shows_menubar;
-  g_object_get (gtk_settings_get_default (),
-                "gtk-shell-shows-app-menu", &shell_shows_appmenu,
-                "gtk-shell-shows-menubar", &shell_shows_menubar,
-                NULL);
+  /* Show/hide the appmenu/menubar as appropriate */
   if (!shell_shows_appmenu)
     gtk_application_set_app_menu (GTK_APPLICATION (app), NULL);
   if (shell_shows_menubar)
diff --git a/src/terminal-menubar.ui b/src/terminal-menubar.ui.in
similarity index 86%
rename from src/terminal-menubar.ui
rename to src/terminal-menubar.ui.in
index b374a94..3904894 100644
--- a/src/terminal-menubar.ui
+++ b/src/terminal-menubar.ui.in
@@ -18,7 +18,8 @@
 <interface>
   <menu id="menubar">
     <submenu>
-      <attribute name="label" translatable="yes">_File</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_File</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">File</attribute></WITHOUT_MNEMONIC>
       <section id="new-terminal-section" />
       <section>
         <item>
@@ -51,7 +52,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">_Edit</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_Edit</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">Edit</attribute></WITHOUT_MNEMONIC>
       <section>
         <item>
           <attribute name="label" translatable="yes">_Copy</attribute>
@@ -87,7 +89,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">_View</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_View</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">View</attribute></WITHOUT_MNEMONIC>
       <section>
         <item>
           <attribute name="label" translatable="yes">Show _Menubar</attribute>
@@ -115,7 +118,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">_Search</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_Search</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">Search</attribute></WITHOUT_MNEMONIC>
       <section>
         <item>
           <attribute name="label" translatable="yes">_Find…</attribute>
@@ -136,7 +140,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">_Terminal</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_Terminal</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">Terminal</attribute></WITHOUT_MNEMONIC>
       <section>
         <section id="set-profile-section" />
         <item>
@@ -190,7 +195,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">Ta_bs</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">Ta_bs</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">Tabs</attribute></WITHOUT_MNEMONIC>
       <attribute name="action">win.tabs-menu</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
       <section>
@@ -221,7 +227,8 @@
       </section>
     </submenu>
     <submenu>
-      <attribute name="label" translatable="yes">_Help</attribute>
+      <WITH_MNEMONIC><attribute name="label" translatable="yes">_Help</attribute></WITH_MNEMONIC>
+      <WITHOUT_MNEMONIC><attribute name="label" translatable="yes">Help</attribute></WITHOUT_MNEMONIC>
       <section>
         <item>
           <attribute name="label" translatable="yes">_Contents</attribute>
diff --git a/src/terminal-prefs.c b/src/terminal-prefs.c
index 07d8b36..b5db148 100644
--- a/src/terminal-prefs.c
+++ b/src/terminal-prefs.c
@@ -859,11 +859,15 @@ terminal_prefs_show_preferences (GSettings *profile, const char *widget_name)
   gtk_widget_set_visible (new_terminal_mode_combo, FALSE);
 #endif
 
-  g_settings_bind (settings,
-                   TERMINAL_SETTING_ENABLE_MNEMONICS_KEY,
-                   disable_mnemonics_button,
-                   "active",
-                   G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+  if (shell_shows_menubar) {
+    gtk_widget_set_visible (disable_mnemonics_button, FALSE);
+  } else {
+    g_settings_bind (settings,
+                     TERMINAL_SETTING_ENABLE_MNEMONICS_KEY,
+                     disable_mnemonics_button,
+                     "active",
+                     G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+  }
   g_settings_bind (settings,
                    TERMINAL_SETTING_ENABLE_MENU_BAR_ACCEL_KEY,
                    disable_menu_accel_button,
diff --git a/src/terminal-window.c b/src/terminal-window.c
index a9c0a20..a8ff746 100644
--- a/src/terminal-window.c
+++ b/src/terminal-window.c
@@ -1381,6 +1381,23 @@ enable_menubar_accel_changed_cb (GSettings *settings,
 #endif
 }
 
+/* The menubar is shown by the app, and the use of mnemonics (e.g. Alt+F for File) is toggled.
+ * The mnemonic modifier is per window, so it doesn't affect the Find or Preferences windows.
+ * If the menubar is shown by the shell, a non-mnemonic variant of the menu is loaded instead
+ * in terminal-app.c. See over there for further details. */
+static void
+enable_mnemonics_changed_cb (GSettings *settings,
+                             const char *key,
+                             TerminalWindow *window)
+{
+  gboolean enabled = g_settings_get_boolean (settings, key);
+
+  if (enabled)
+    gtk_window_set_mnemonic_modifier (GTK_WINDOW (window), GDK_MOD1_MASK);
+  else
+    gtk_window_set_mnemonic_modifier (GTK_WINDOW (window), GDK_MODIFIER_MASK & ~GDK_RELEASE_MASK);
+}
+
 static void
 app_setting_notify_destroy_cb (GtkSettings *gtk_settings)
 {
@@ -2041,12 +2058,6 @@ terminal_window_screen_update (TerminalWindow *window,
                           (GDestroyNotify) app_setting_notify_destroy_cb);
 
   g_settings_bind (settings,
-                   TERMINAL_SETTING_ENABLE_MNEMONICS_KEY,
-                   gtk_settings,
-                   "gtk-enable-mnemonics",
-                   G_SETTINGS_BIND_GET);
-
-  g_settings_bind (settings,
                    TERMINAL_SETTING_ENABLE_SHORTCUTS_KEY,
                    gtk_settings,
                    "gtk-enable-accels",
@@ -2232,6 +2243,11 @@ terminal_window_init (TerminalWindow *window)
 
   g_simple_action_set_enabled (lookup_action (window, "leave-fullscreen"), FALSE);
 
+  GSettings *global_settings = terminal_app_get_global_settings (app);
+  enable_mnemonics_changed_cb (global_settings, TERMINAL_SETTING_ENABLE_MNEMONICS_KEY, window);
+  g_signal_connect (global_settings, "changed::" TERMINAL_SETTING_ENABLE_MNEMONICS_KEY,
+                    G_CALLBACK (enable_mnemonics_changed_cb), window);
+
   /* Hide "menubar-visible" when the menubar is shown by the shell */
   g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)),
                 "gtk-shell-shows-menubar", &shell_shows_menubar,
@@ -2337,6 +2353,13 @@ terminal_window_dispose (GObject *object)
   TerminalWindowPrivate *priv = window->priv;
   TerminalApp *app = terminal_app_get ();
 
+  if (!priv->disposed) {
+    GSettings *global_settings = terminal_app_get_global_settings (app);
+    g_signal_handlers_disconnect_by_func (global_settings,
+                                          G_CALLBACK (enable_mnemonics_changed_cb),
+                                          window);
+  }
+
   priv->disposed = TRUE;
 
   if (priv->clipboard != NULL) {
diff --git a/src/terminal.gresource.xml b/src/terminal.gresource.xml
index 503e1a5..7b84a5f 100644
--- a/src/terminal.gresource.xml
+++ b/src/terminal.gresource.xml
@@ -19,7 +19,8 @@
   <gresource prefix="/org/gnome/terminal">
     <file alias="css/terminal.css" compressed="true">terminal.common.css</file>
     <file alias="gtk/menus.ui" compressed="true" preprocess="xml-stripblanks">terminal-appmenu.ui</file>
-    <file alias="ui/menubar.ui" compressed="true" preprocess="xml-stripblanks">terminal-menubar.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>
     <file alias="ui/preferences.ui" compressed="true" preprocess="xml-stripblanks">preferences.ui</file>
     <file alias="ui/search-popover.ui" compressed="true" 
preprocess="xml-stripblanks">search-popover.ui</file>


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