application: Set default window icon to appid by default

Lots of newer apps that use their appid as their icon name don't set
window icons, since they aren't used in GNOME. Instead of setting it
manually in every app, just default to it.

Only set the icon if it exists in the icon theme.

Remove manually set default icons in the demo.

No tests as GtkApplication doesn't have any in the first place.

Fixes https://gitlab.gnome.org/GNOME/gtk/-/issues/7120
This commit is contained in:
Alice Mikhaylenko
2024-10-30 18:05:18 +04:00
parent c8cc8c4842
commit 3274a286cc
5 changed files with 22 additions and 6 deletions

View File

@@ -1056,8 +1056,6 @@ command_line (GApplication *app,
window = gtk_application_get_windows (GTK_APPLICATION (app))->data;
gtk_window_set_icon_name (GTK_WINDOW (window), "org.gtk.Demo4");
if (name == NULL)
goto out;

View File

@@ -223,8 +223,6 @@ node_editor_application_activate (GApplication *app)
if (g_strcmp0 (PROFILE, "devel") == 0)
gtk_widget_add_css_class (GTK_WIDGET (win), "devel");
gtk_window_set_icon_name (GTK_WINDOW (win), "org.gtk.gtk4.NodeEditor");
gtk_window_present (GTK_WINDOW (win));
}

View File

@@ -788,7 +788,6 @@ activate (GApplication *app)
if (g_strcmp0 (PROFILE, "devel") == 0)
gtk_widget_add_css_class (GTK_WIDGET (main_window), "devel");
gtk_window_set_icon_name (GTK_WINDOW (main_window), "org.gtk.PrintEditor4");
gtk_window_set_default_size (GTK_WINDOW (main_window), 400, 600);
gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (main_window), TRUE);
update_title (GTK_WINDOW (main_window));

View File

@@ -2232,7 +2232,6 @@ activate (GApplication *app)
if (g_strcmp0 (PROFILE, "devel") == 0)
gtk_widget_add_css_class (GTK_WIDGET (window), "devel");
gtk_window_set_icon_name (window, "org.gtk.WidgetFactory4");
gtk_application_add_window (GTK_APPLICATION (app), window);
g_action_map_add_action_entries (G_ACTION_MAP (window),
win_entries, G_N_ELEMENTS (win_entries),

View File

@@ -88,6 +88,10 @@
* displays the shortcuts window, associate the item with the action
* `win.show-help-overlay`.
*
* `GtkApplication` will also automatically set the application id as the
* default window icon. Use [func@Gtk.Window.set_default_icon_name] or
* [property@Gtk.Window:icon-name] to override that behavior.
*
* ## A simple application
*
* [A simple example](https://gitlab.gnome.org/GNOME/gtk/tree/main/examples/bp/bloatpad.c)
@@ -244,6 +248,23 @@ gtk_application_load_resources (GtkApplication *application)
}
}
static void
gtk_application_set_window_icon (GtkApplication *application)
{
GtkIconTheme *default_theme;
const char *appid;
if (gtk_window_get_default_icon_name () != NULL)
return;
default_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
appid = g_application_get_application_id (G_APPLICATION (application));
if (!gtk_icon_theme_has_icon (default_theme, appid))
return;
gtk_window_set_default_icon_name (appid);
}
static void
gtk_application_startup (GApplication *g_application)
@@ -267,6 +288,7 @@ gtk_application_startup (GApplication *g_application)
gtk_application_impl_startup (priv->impl, priv->register_session);
gtk_application_load_resources (application);
gtk_application_set_window_icon (application);
gdk_profiler_end_mark (before, "Application startup", NULL);
}