From cdce5f957d57a41e116bca6abd4f76632f2cbcd2 Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Sat, 5 May 2012 11:23:42 +0300 Subject: [PATCH] Improve menu icon loading (Bug #8795) This code checks if the icon theme has the custom icon name and loads that directly into a gtk_image widget rather than a pixbuf first, which should provide a small speed improvement when many items are present in the menu. Should that fail, it will fallback to checking all other possible locations for the menu item's icon. --- src/xfdesktop-app-menu-item.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/xfdesktop-app-menu-item.c b/src/xfdesktop-app-menu-item.c index 3722c51..7946118 100644 --- a/src/xfdesktop-app-menu-item.c +++ b/src/xfdesktop-app-menu-item.c @@ -171,30 +171,31 @@ xfdesktop_app_menu_item_set_icon(XfdesktopAppMenuItem *app_menu_item) const gchar *icon_name; gint w, h, size; GdkPixbuf *pixbuf = NULL; - GtkWidget *image; + GtkWidget *image = NULL; GtkIconTheme *icon_theme; gchar *p, *name = NULL; gchar *filename; icon_name = garcon_menu_item_get_icon_name(app_menu_item->item); + icon_theme = gtk_icon_theme_get_default(); if(G_LIKELY(icon_name)) { gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &w, &h); size = MIN(w, h); - if (g_path_is_absolute (icon_name)) { - pixbuf = gdk_pixbuf_new_from_file_at_scale(icon_name, w, h, TRUE, NULL); - } else { - icon_theme = gtk_icon_theme_get_default(); - pixbuf = gtk_icon_theme_load_icon(icon_theme, icon_name, size, 0, NULL); - - if (G_UNLIKELY(pixbuf == NULL)) { + if(gtk_icon_theme_has_icon(icon_theme, icon_name)) + image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_MENU); + else { + if (g_path_is_absolute (icon_name)) { + pixbuf = gdk_pixbuf_new_from_file_at_scale(icon_name, w, h, TRUE, NULL); + } else { /* try to lookup names like application.png in the theme */ p = strrchr(icon_name, '.'); if (p) { name = g_strndup(icon_name, p - icon_name); pixbuf = gtk_icon_theme_load_icon(icon_theme, name, size, 0, NULL); g_free (name); + name = NULL; } /* maybe they point to a file in the pixbufs folder */ @@ -202,21 +203,24 @@ xfdesktop_app_menu_item_set_icon(XfdesktopAppMenuItem *app_menu_item) filename = g_build_filename("pixmaps", icon_name, NULL); name = xfce_resource_lookup(XFCE_RESOURCE_DATA, filename); g_free(filename); + } - if(name) - pixbuf = gdk_pixbuf_new_from_file(name, NULL); + if(name) { + pixbuf = gdk_pixbuf_new_from_file_at_scale(name, w, h, TRUE, NULL); g_free(name); } } + + /* Turn the pixbuf into a gtk_image */ + if(G_LIKELY(pixbuf)) { + image = gtk_image_new_from_pixbuf(pixbuf); + g_object_unref(G_OBJECT(pixbuf)); + } } } - if(G_LIKELY(pixbuf)) { - image = gtk_image_new_from_pixbuf(pixbuf); - g_object_unref(G_OBJECT(pixbuf)); - } else { + if(!GTK_IS_IMAGE(image)) image = gtk_image_new(); - } gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(app_menu_item), image); } -- 1.7.9.5