Skip to contentSkip to conversation

gtk: make it possible to hide the menu bar

PATCH In review Peter Wu 3 participants 7 messages May 10, 2018 – May 15, 2018

lore
Share & tools
lore

Peter Wu

author
#0 lore raw

PATCH 1/2 gtk: make it possible to hide the menu bar

Saves some space and disables the F10 button as side-effect.

---
 ui/gtk.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
ui/gtk.c +45 -1
index bb3214cffb..9e5390f2c2 100644
@@ -145,6 +145,7 @@
 #define GDK_KEY_2 GDK_2
 #define GDK_KEY_f GDK_f
 #define GDK_KEY_g GDK_g
+#define GDK_KEY_m GDK_m
 #define GDK_KEY_q GDK_q
 #define GDK_KEY_plus GDK_plus
 #define GDK_KEY_equal GDK_equal
@@ -208,6 +209,7 @@ struct GtkDisplayState {
 
     GtkWidget *show_tabs_item;
     GtkWidget *untabify_item;
+    GtkWidget *show_menubar_item;
 
     GtkWidget *vbox;
     GtkWidget *notebook;
@@ -1387,6 +1389,30 @@ static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
     }
 }
 
+static void gd_menu_show_menubar(GtkMenuItem *item, void *opaque)
+{
+    GtkDisplayState *s = opaque;
+    VirtualConsole *vc = gd_vc_find_current(s);
+
+    if (s->full_screen) {
+        return;
+    }
+
+    if (gtk_check_menu_item_get_active(
+                GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
+        gtk_widget_show(s->menu_bar);
+    } else {
+        gtk_widget_hide(s->menu_bar);
+    }
+    gd_update_windowsize(vc);
+}
+
+static void gd_accel_show_menubar(void *opaque)
+{
+    GtkDisplayState *s = opaque;
+    gtk_menu_item_activate(GTK_MENU_ITEM(s->show_menubar_item));
+}
+
 static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
 {
     GtkDisplayState *s = opaque;
@@ -1403,7 +1429,10 @@ static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
     } else {
         gtk_window_unfullscreen(GTK_WINDOW(s->window));
         gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
-        gtk_widget_show(s->menu_bar);
+        if (gtk_check_menu_item_get_active(
+                    GTK_CHECK_MENU_ITEM(s->show_menubar_item))) {
+            gtk_widget_show(s->menu_bar);
+        }
         s->full_screen = FALSE;
         if (vc->type == GD_VC_GFX) {
             vc->gfx.scale_x = 1.0;
@@ -2036,6 +2065,8 @@ static void gd_connect_signals(GtkDisplayState *s)
                      G_CALLBACK(gd_menu_show_tabs), s);
     g_signal_connect(s->untabify_item, "activate",
                      G_CALLBACK(gd_menu_untabify), s);
+    g_signal_connect(s->show_menubar_item, "activate",
+                     G_CALLBACK(gd_menu_show_menubar), s);
 
     g_signal_connect(s->window, "delete-event",
                      G_CALLBACK(gd_window_close), s);
@@ -2272,6 +2303,19 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
     s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);
 
+    s->show_menubar_item = gtk_check_menu_item_new_with_mnemonic(
+            _("Show Menubar"));
+    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->show_menubar_item),
+                                   TRUE);
+    gtk_accel_group_connect(s->accel_group, GDK_KEY_m, HOTKEY_MODIFIERS, 0,
+            g_cclosure_new_swap(G_CALLBACK(gd_accel_show_menubar), s, NULL));
+#if GTK_CHECK_VERSION(3, 8, 0)
+    gtk_accel_label_set_accel(
+            GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(s->show_menubar_item))),
+            GDK_KEY_m, HOTKEY_MODIFIERS);
+#endif
+    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_menubar_item);
+
     return view_menu;
 }
 
Signature · 1 line
2.17.0
author
#1 lore raw

PATCH 2/2 gtk: disable the F10 menubar key

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). Note that this property is deprecated and might be removed in the future (GTK+ commit b082fb598d).

---
 ui/gtk.c | 6 ++++++
 1 file changed, 6 insertions(+)
ui/gtk.c +6 -0
index 9e5390f2c2..6ad6cacfaa 100644
@@ -2321,6 +2321,8 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s)
 
 static void gd_create_menus(GtkDisplayState *s)
 {
+    GtkSettings *settings;
+
     s->accel_group = gtk_accel_group_new();
     s->machine_menu = gd_create_menu_machine(s);
     s->view_menu = gd_create_menu_view(s);
@@ -2336,6 +2338,10 @@ static void gd_create_menus(GtkDisplayState *s)
 
     g_object_set_data(G_OBJECT(s->window), "accel_group", s->accel_group);
     gtk_window_add_accel_group(GTK_WINDOW(s->window), s->accel_group);
+
+    /* Disable the default "F10" menu shortcut. */
+    settings = gtk_widget_get_settings(s->window);
+    g_object_set(G_OBJECT(settings), "gtk-menu-bar-accel", "", NULL);
 }
 
 
Signature · 1 line
2.17.0
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] gtk: disable the F10 menubar key

Quoted 3 lines · Peter Wu

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). Note that this property is deprecated and might be removed in the future (GTK+ commit b082fb598d).

Any replacement provided by gtk?

I don't feel like adding new code depending on already deprecated features ...

Also note that there is the "grab-on-hover" option in the view menu, which will make qemu grab the keyboard if the mouse pointer is inside the qemu window. Which is pretty convinient for hotkeys, they go to the guest if you work in the qemu window and to the host otherwise.

cheers,
Gerd

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] gtk: disable the F10 menubar key

Quoted 6 lines · Gerd Hoffmann
Quoted 3 lines · Peter Wu

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). Note that this property is deprecated and might be removed in the future (GTK+ commit b082fb598d).

Any replacement provided by gtk?

It doesn't look like it

Quoted 7 lines

I don't feel like adding new code depending on already deprecated features ...

Also note that there is the "grab-on-hover" option in the view menu, which will make qemu grab the keyboard if the mouse pointer is inside the qemu window. Which is pretty convinient for hotkeys, they go to the guest if you work in the qemu window and to the host otherwise.

IMHO the right thing todo long term is to kill the menu bar from the GTK UI entirely, and replace with the GTK HeaderBar. This allows the menu items to be provided via popups directly from the title bar of the windows, which is more vertically space efficient. This would probably have to wait for us to ditch GTK2, so I think this fix is reasonable until that time.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] gtk: disable the F10 menubar key

Quoted 18 lines · Daniel P. Berrangé
Quoted 6 lines · Gerd Hoffmann
Quoted 3 lines · Peter Wu

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). Note that this property is deprecated and might be removed in the future (GTK+ commit b082fb598d).

Any replacement provided by gtk?

It doesn't look like it

Quoted 7 lines

I don't feel like adding new code depending on already deprecated features ...

Also note that there is the "grab-on-hover" option in the view menu, which will make qemu grab the keyboard if the mouse pointer is inside the qemu window. Which is pretty convinient for hotkeys, they go to the guest if you work in the qemu window and to the host otherwise.

Quoted 6 lines

IMHO the right thing todo long term is to kill the menu bar from the GTK UI entirely, and replace with the GTK HeaderBar. This allows the menu items to be provided via popups directly from the title bar of the windows, which is more vertically space efficient. This would probably have to wait for us to ditch GTK2, so I think this fix is reasonable until that time.

Fair enough. Patch queued.

cheers,
Gerd

author
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] gtk: disable the F10 menubar key

Quoted 10 lines · Gerd Hoffmann
Quoted 9 lines · Daniel P. Berrangé
Quoted 6 lines · Gerd Hoffmann
Quoted 3 lines · Peter Wu

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). Note that this property is deprecated and might be removed in the future (GTK+ commit b082fb598d).

Any replacement provided by gtk?

It doesn't look like it

Would it still be possible to change the commit message? I asked Timm (the author of that commit) about it, and it appears that the feature was restored at some point again because gnome-terminal relied on it.

The new message (if possible):

The F10 key is used in various applications, disable it unconditionally (do not limit it to grab mode). This property will still work with GTK3, but as it is deprecated it might be removed in GTK4.

Signature · 3 lines
Kind regards,
Peter Wu
https://lekensteyn.nl