Merge branch 'macos-application-demo' into 'main'

Revert "macos: fix weird menubar rendering."

Closes #6524

See merge request GNOME/gtk!7052
This commit is contained in:
Matthias Clasen 2024-03-18 14:40:45 +00:00
commit b007597c58
2 changed files with 48 additions and 23 deletions

View File

@ -2,7 +2,7 @@
<interface>
<menu id="menubar">
<submenu>
<attribute name="label" translatable="yes">_Application</attribute>
<attribute name="label" translatable="yes">_File</attribute>
<section>
<item>
<attribute name="label" translatable="yes">_New</attribute>
@ -33,7 +33,7 @@
</section>
</submenu>
<submenu>
<attribute name="label" translatable="yes">_File</attribute>
<attribute name="label" translatable="yes">_Preferences</attribute>
<section>
<item>
<attribute name="label" translatable="yes">_Prefer Dark Theme</attribute>

View File

@ -150,12 +150,36 @@ 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};
@ -184,6 +208,24 @@ 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);
}
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. */
@ -254,30 +296,12 @@ gtk_application_impl_quartz_set_menubar (GtkApplicationImpl *impl,
{
GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
/* 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 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 (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
@ -358,6 +382,7 @@ 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;