[gtk: 1/2] macos: fix weird menubar rendering.




commit 75868dfee4fad6b03b9f4e16b956380525eaff2a
Author: Zhi <yfwz100 yeah net>
Date:   Sun May 30 08:02:18 2021 +0800

    macos: fix weird menubar rendering.
    
    As app menu has been deprecated, the function of app menu should be
    merged into the menubar, which behaves like the original API of NSApp.
    
    This also brings back the default app menu, which looks like native
    macOS apps.
    
    For future refactoring, please note that the menubar must contain at
    least one menu before being set to NSApp to avoid weird menubar
    rendering.
    
    Fixes #3967.

 gtk/gtkapplication-quartz.c | 68 ++++++++++++++-------------------------------
 1 file changed, 21 insertions(+), 47 deletions(-)
---
diff --git a/gtk/gtkapplication-quartz.c b/gtk/gtkapplication-quartz.c
index 5472c20641..f4506ec441 100644
--- a/gtk/gtkapplication-quartz.c
+++ b/gtk/gtkapplication-quartz.c
@@ -147,36 +147,12 @@ static GActionEntry gtk_application_impl_quartz_actions[] = {
   { "show-all",         gtk_application_impl_quartz_show_all    }
 };
 
-static void
-gtk_application_impl_quartz_set_app_menu (GtkApplicationImpl *impl,
-                                          GMenuModel         *app_menu)
-{
-  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
-
-  /* If there are any items at all, then the first one is the app menu */
-  if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)))
-    g_menu_remove (quartz->combined, 0);
-
-  if (app_menu)
-    g_menu_prepend_submenu (quartz->combined, "Application", app_menu);
-  else
-    {
-      GMenu *empty;
-
-      /* We must preserve the rule that index 0 is the app menu */
-      empty = g_menu_new ();
-      g_menu_prepend_submenu (quartz->combined, "Application", G_MENU_MODEL (empty));
-      g_object_unref (empty);
-    }
-}
-
 static void
 gtk_application_impl_quartz_startup (GtkApplicationImpl *impl,
                                      gboolean            register_session)
 {
   GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
   GSimpleActionGroup *gtkinternal;
-  GMenuModel *app_menu;
   const char *pref_accel[] = {"<Control>comma", NULL};
   const char *hide_others_accel[] = {"<Control><Alt>h", NULL};
   const char *hide_accel[] = {"<Control>h", NULL};
@@ -205,25 +181,6 @@ gtk_application_impl_quartz_startup (GtkApplicationImpl *impl,
   g_object_unref (gtkinternal);
 
   /* now setup the menu */
-  app_menu = g_object_get_data (G_OBJECT (impl), "APP_MENU");
-  if (app_menu == NULL)
-    {
-      GtkBuilder *builder;
-
-      /* If the user didn't fill in their own menu yet, add ours.
-       *
-       * The fact that we do this here ensures that we will always have the
-       * app menu at index 0 in 'combined'.
-       */
-      builder = gtk_builder_new_from_resource ("/org/gtk/libgtk/ui/gtkapplication-quartz.ui");
-      app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
-      g_object_set_data_full (G_OBJECT (impl), "APP_DATA", g_object_ref (app_menu), g_object_unref);
-      g_object_unref (builder);
-    }
-  else
-    gtk_application_impl_quartz_set_app_menu (impl, app_menu);
-
-  /* This may or may not add an item to 'combined' */
   gtk_application_impl_set_menubar (impl, gtk_application_get_menubar (impl->application));
 
   /* OK.  Now put it in the menu. */
@@ -294,12 +251,30 @@ gtk_application_impl_quartz_set_menubar (GtkApplicationImpl *impl,
 {
   GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
 
-  /* If we have the menubar, it is a section at index '1' */
-  if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)) > 1)
-    g_menu_remove (quartz->combined, 1);
+  /* If we have the menubar, it is a section at index '0' */
+  if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)))
+    g_menu_remove (quartz->combined, 0);
 
   if (menubar)
     g_menu_append_section (quartz->combined, NULL, menubar);
+  else
+    {
+      // Ensure that we will always have one menu.
+      char app_menu_key[] = "APP_MENU";
+      GMenuModel *app_menu = g_object_get_data (G_OBJECT (impl), app_menu_key);
+      if (app_menu == NULL)
+        {
+          GtkBuilder *builder;
+
+          // If the user didn't fill in their own menu yet, add ours.
+          builder = gtk_builder_new_from_resource ("/org/gtk/libgtk/ui/gtkapplication-quartz.ui");
+          app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
+          g_object_set_data_full (G_OBJECT (impl), app_menu_key, g_object_ref (app_menu), g_object_unref);
+          g_object_unref (builder);
+        }
+
+      g_menu_append_submenu (quartz->combined, "Application", app_menu);
+    }
 }
 
 static guint
@@ -380,7 +355,6 @@ gtk_application_impl_quartz_class_init (GtkApplicationImplClass *class)
   class->startup = gtk_application_impl_quartz_startup;
   class->shutdown = gtk_application_impl_quartz_shutdown;
   class->active_window_changed = gtk_application_impl_quartz_active_window_changed;
-  class->set_app_menu = gtk_application_impl_quartz_set_app_menu;
   class->set_menubar = gtk_application_impl_quartz_set_menubar;
   class->inhibit = gtk_application_impl_quartz_inhibit;
   class->uninhibit = gtk_application_impl_quartz_uninhibit;


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