From 6b26664c414a44e26553c0546632c9d5fff6a7b1 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Tue, 16 Dec 2014 12:14:23 -0500 Subject: [PATCH] GtkMenuTracker: add hidden-when='macos-menubar' Provide a mechanism for hiding the "Quit", "About" and "Preferences" menu items from the normal places in a traditional menubar layout (in the File and Edit menus) when the menu is being rendered in the Mac OS menubar. These items can already be found in the application menu. With this feature, applications can now define a single menu to use in all 'traditional' scenarios. Use this new attribute in Bloatpad. https://bugzilla.gnome.org/show_bug.cgi?id=741610 --- examples/bp/gtk/menus.ui | 2 ++ gtk/gtkapplication-quartz-menu.c | 2 ++ gtk/gtkmenusectionbox.c | 6 +++--- gtk/gtkmenushell.c | 6 +++--- gtk/gtkmenutracker.c | 13 ++++++++++--- gtk/gtkmenutracker.h | 2 ++ gtk/gtkmenutrackeritem.c | 8 ++++++++ gtk/gtkmenutrackeritem.h | 1 + 8 files changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/bp/gtk/menus.ui b/examples/bp/gtk/menus.ui index a05986fdb5..60abf4adb8 100644 --- a/examples/bp/gtk/menus.ui +++ b/examples/bp/gtk/menus.ui @@ -11,12 +11,14 @@
_About Bloatpad + macos-menubar app.about
_Quit + macos-menubar app.quit
diff --git a/gtk/gtkapplication-quartz-menu.c b/gtk/gtkapplication-quartz-menu.c index 7c49d78fc4..caf0154a4f 100644 --- a/gtk/gtkapplication-quartz-menu.c +++ b/gtk/gtkapplication-quartz-menu.c @@ -379,6 +379,7 @@ menu_item_removed (gint position, model, NO, YES, + YES, NULL, menu_item_inserted, menu_item_removed, @@ -397,6 +398,7 @@ menu_item_removed (gint position, tracker = gtk_menu_tracker_new_for_item_link (trackerItem, G_MENU_LINK_SUBMENU, YES, + YES, menu_item_inserted, menu_item_removed, self); diff --git a/gtk/gtkmenusectionbox.c b/gtk/gtkmenusectionbox.c index e6b93748eb..359d6341d5 100644 --- a/gtk/gtkmenusectionbox.c +++ b/gtk/gtkmenusectionbox.c @@ -380,7 +380,7 @@ gtk_menu_section_box_new_toplevel (GtkStack *stack, gtk_stack_add_named (stack, GTK_WIDGET (box), "main"); box->tracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (_gtk_widget_get_action_muxer (GTK_WIDGET (box), TRUE)), - model, TRUE, FALSE, action_namespace, + model, TRUE, FALSE, FALSE, action_namespace, gtk_menu_section_box_insert_func, gtk_menu_section_box_remove_func, box); @@ -420,7 +420,7 @@ gtk_menu_section_box_new_submenu (GtkMenuTrackerItem *item, GTK_WIDGET (box), gtk_menu_tracker_item_get_label (item)); gtk_widget_show (GTK_WIDGET (box)); - box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SUBMENU, FALSE, + box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SUBMENU, FALSE, FALSE, gtk_menu_section_box_insert_func, gtk_menu_section_box_remove_func, box); @@ -486,7 +486,7 @@ gtk_menu_section_box_new_section (GtkMenuTrackerItem *item, gtk_widget_show (box->separator); } - box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SECTION, FALSE, + box->tracker = gtk_menu_tracker_new_for_item_link (item, G_MENU_LINK_SECTION, FALSE, FALSE, gtk_menu_section_box_insert_func, gtk_menu_section_box_remove_func, box); diff --git a/gtk/gtkmenushell.c b/gtk/gtkmenushell.c index 939d5f4462..e1ba138b65 100644 --- a/gtk/gtkmenushell.c +++ b/gtk/gtkmenushell.c @@ -2087,7 +2087,7 @@ gtk_menu_shell_tracker_insert_func (GtkMenuTrackerItem *item, * lazy... */ submenu->priv->tracker = gtk_menu_tracker_new_for_item_link (item, - G_MENU_LINK_SUBMENU, TRUE, + G_MENU_LINK_SUBMENU, TRUE, FALSE, gtk_menu_shell_tracker_insert_func, gtk_menu_shell_tracker_remove_func, submenu); @@ -2202,8 +2202,8 @@ gtk_menu_shell_bind_model (GtkMenuShell *menu_shell, gtk_container_remove (GTK_CONTAINER (menu_shell), menu_shell->priv->children->data); if (model) - menu_shell->priv->tracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (muxer), - model, with_separators, TRUE, action_namespace, + menu_shell->priv->tracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (muxer), model, + with_separators, TRUE, FALSE, action_namespace, gtk_menu_shell_tracker_insert_func, gtk_menu_shell_tracker_remove_func, menu_shell); diff --git a/gtk/gtkmenutracker.c b/gtk/gtkmenutracker.c index 1b89fe3c7a..ae2e7f8e8c 100644 --- a/gtk/gtkmenutracker.c +++ b/gtk/gtkmenutracker.c @@ -60,7 +60,8 @@ typedef struct _GtkMenuTrackerSection GtkMenuTrackerSection; struct _GtkMenuTracker { GtkActionObservable *observable; - gboolean merge_sections; + guint merge_sections : 1; + guint mac_os_mode : 1; GtkMenuTrackerInsertFunc insert_func; GtkMenuTrackerRemoveFunc remove_func; gpointer user_data; @@ -200,7 +201,7 @@ gtk_menu_tracker_section_sync_separators (GtkMenuTrackerSection *section, /* Add a separator */ GtkMenuTrackerItem *item; - item = _gtk_menu_tracker_item_new (tracker->observable, parent_model, parent_index, NULL, TRUE); + item = _gtk_menu_tracker_item_new (tracker->observable, parent_model, parent_index, FALSE, NULL, TRUE); (* tracker->insert_func) (item, offset, tracker->user_data); g_object_unref (item); @@ -345,6 +346,7 @@ gtk_menu_tracker_add_items (GtkMenuTracker *tracker, GtkMenuTrackerItem *item; item = _gtk_menu_tracker_item_new (tracker->observable, model, position + n_items, + tracker->mac_os_mode, section->action_namespace, submenu != NULL); /* In the case that the item may disappear we handle that by @@ -498,6 +500,7 @@ gtk_menu_tracker_section_new (GtkMenuTracker *tracker, * @merge_sections: if sections should have their items merged in the * usual way or reported only as separators (which can be queried to * manually handle the items) + * @mac_os_mode: if this is on behalf of the Mac OS menubar * @action_namespace: the passed-in action namespace * @insert_func: insert callback * @remove_func: remove callback @@ -549,6 +552,7 @@ gtk_menu_tracker_new (GtkActionObservable *observable, GMenuModel *model, gboolean with_separators, gboolean merge_sections, + gboolean mac_os_mode, const gchar *action_namespace, GtkMenuTrackerInsertFunc insert_func, GtkMenuTrackerRemoveFunc remove_func, @@ -558,6 +562,7 @@ gtk_menu_tracker_new (GtkActionObservable *observable, tracker = g_slice_new (GtkMenuTracker); tracker->merge_sections = merge_sections; + tracker->mac_os_mode = mac_os_mode; tracker->observable = g_object_ref (observable); tracker->insert_func = insert_func; tracker->remove_func = remove_func; @@ -573,6 +578,7 @@ GtkMenuTracker * gtk_menu_tracker_new_for_item_link (GtkMenuTrackerItem *item, const gchar *link_name, gboolean merge_sections, + gboolean mac_os_mode, GtkMenuTrackerInsertFunc insert_func, GtkMenuTrackerRemoveFunc remove_func, gpointer user_data) @@ -585,7 +591,8 @@ gtk_menu_tracker_new_for_item_link (GtkMenuTrackerItem *item, namespace = _gtk_menu_tracker_item_get_link_namespace (item); tracker = gtk_menu_tracker_new (_gtk_menu_tracker_item_get_observable (item), submenu, - TRUE, merge_sections, namespace, insert_func, remove_func, user_data); + TRUE, merge_sections, mac_os_mode, + namespace, insert_func, remove_func, user_data); g_object_unref (submenu); g_free (namespace); diff --git a/gtk/gtkmenutracker.h b/gtk/gtkmenutracker.h index 5d4b945af9..35f4a09f7b 100644 --- a/gtk/gtkmenutracker.h +++ b/gtk/gtkmenutracker.h @@ -36,6 +36,7 @@ GtkMenuTracker * gtk_menu_tracker_new (GtkActi GMenuModel *model, gboolean with_separators, gboolean merge_sections, + gboolean mac_os_mode, const gchar *action_namespace, GtkMenuTrackerInsertFunc insert_func, GtkMenuTrackerRemoveFunc remove_func, @@ -44,6 +45,7 @@ GtkMenuTracker * gtk_menu_tracker_new (GtkActi GtkMenuTracker * gtk_menu_tracker_new_for_item_link (GtkMenuTrackerItem *item, const gchar *link_name, gboolean merge_sections, + gboolean mac_os_mode, GtkMenuTrackerInsertFunc insert_func, GtkMenuTrackerRemoveFunc remove_func, gpointer user_data); diff --git a/gtk/gtkmenutrackeritem.c b/gtk/gtkmenutrackeritem.c index b9185def48..c304b66414 100644 --- a/gtk/gtkmenutrackeritem.c +++ b/gtk/gtkmenutrackeritem.c @@ -103,6 +103,7 @@ struct _GtkMenuTrackerItem #define HIDDEN_NEVER 0 #define HIDDEN_WHEN_MISSING 1 #define HIDDEN_WHEN_DISABLED 2 +#define HIDDEN_WHEN_ALWAYS 3 enum { PROP_0, @@ -271,6 +272,10 @@ gtk_menu_tracker_item_update_visibility (GtkMenuTrackerItem *self) visible = self->sensitive; break; + case HIDDEN_WHEN_ALWAYS: + visible = FALSE; + break; + default: g_assert_not_reached (); } @@ -479,6 +484,7 @@ GtkMenuTrackerItem * _gtk_menu_tracker_item_new (GtkActionObservable *observable, GMenuModel *model, gint item_index, + gboolean mac_os_mode, const gchar *action_namespace, gboolean is_separator) { @@ -501,6 +507,8 @@ _gtk_menu_tracker_item_new (GtkActionObservable *observable, self->hidden_when = HIDDEN_WHEN_DISABLED; else if (g_str_equal (hidden_when, "action-missing")) self->hidden_when = HIDDEN_WHEN_MISSING; + else if (mac_os_mode && g_str_equal (hidden_when, "macos-menubar")) + self->hidden_when = HIDDEN_WHEN_ALWAYS; /* Ignore other values -- this code may be running in context of a * desktop shell or the like and should not spew criticals due to diff --git a/gtk/gtkmenutrackeritem.h b/gtk/gtkmenutrackeritem.h index 48c6dd4891..6b4fcb576a 100644 --- a/gtk/gtkmenutrackeritem.h +++ b/gtk/gtkmenutrackeritem.h @@ -45,6 +45,7 @@ GType gtk_menu_tracker_item_role_get_type (void) G GtkMenuTrackerItem * _gtk_menu_tracker_item_new (GtkActionObservable *observable, GMenuModel *model, gint item_index, + gboolean mac_os_mode, const gchar *action_namespace, gboolean is_separator);