From 12db08f2c3bc6bdd0f2265a71a8f8033b3274f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 28 Feb 2014 06:52:57 +0100 Subject: IndicatorImageHelper: always try to use a GIcon or the file-name as source of the Gdk image We don't need to fallback to pure pixbuf, unless an indicator provided a custom icon that is not in the current theme path. This helps a lot in reducing the Unity7 workload as this decreases the cases where we need to encode the pixbuf contents, send them via dbus to unity, encode them back, reload to a new pixbuf... Also thanks to this, the library clients can load the actual icon, scaled at the value they want. --- libindicator/indicator-image-helper.c | 64 +++++++++++++++-------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c index 28a6c92..684fe8a 100644 --- a/libindicator/indicator-image-helper.c +++ b/libindicator/indicator-image-helper.c @@ -25,6 +25,7 @@ License along with this library. If not, see #include "indicator-image-helper.h" const gchar * INDICATOR_NAMES_DATA = "indicator-names-data"; +const gint ICON_SIZE = 22; static void refresh_image (GtkImage * image) @@ -32,21 +33,20 @@ refresh_image (GtkImage * image) g_return_if_fail(GTK_IS_IMAGE(image)); const gchar * icon_filename = NULL; GtkIconInfo * icon_info = NULL; - gint icon_size = 22; GIcon * icon_names = (GIcon *)g_object_get_data(G_OBJECT(image), INDICATOR_NAMES_DATA); - g_return_if_fail(icon_names != NULL); + g_return_if_fail(G_IS_ICON (icon_names)); /* Get the default theme */ GtkIconTheme * default_theme = gtk_icon_theme_get_default(); g_return_if_fail(default_theme != NULL); /* Look through the themes for that icon */ - icon_info = gtk_icon_theme_lookup_by_gicon(default_theme, icon_names, icon_size, 0); + icon_info = gtk_icon_theme_lookup_by_gicon(default_theme, icon_names, ICON_SIZE, 0); if (icon_info == NULL) { /* Maybe the icon was just added to the theme, see if a rescan helps */ gtk_icon_theme_rescan_if_needed(default_theme); - icon_info = gtk_icon_theme_lookup_by_gicon(default_theme, icon_names, icon_size, 0); + icon_info = gtk_icon_theme_lookup_by_gicon(default_theme, icon_names, ICON_SIZE, 0); } if (icon_info == NULL) { /* Try using the second item in the names, which should be the original filename supplied */ @@ -69,19 +69,36 @@ refresh_image (GtkImage * image) return; } - /* Build a pixbuf */ - GdkPixbuf * pixbuf = NULL; - - if (icon_filename == NULL) { + if (icon_info != NULL) { + gtk_image_set_from_gicon (image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); + } else if (icon_filename != NULL) { + gtk_image_set_from_file (image, icon_filename); + } else { + /* Build a pixbuf if needed */ + GdkPixbuf * pixbuf = NULL; GError * error = NULL; - GInputStream * stream = g_loadable_icon_load (G_LOADABLE_ICON (icon_names), icon_size, NULL, NULL, &error); + GInputStream * stream = g_loadable_icon_load (G_LOADABLE_ICON (icon_names), ICON_SIZE, NULL, NULL, &error); if (stream != NULL) { pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error); g_input_stream_close (stream, NULL, NULL); g_object_unref (stream); - if (pixbuf == NULL) { + if (pixbuf != NULL) { + /* Scale icon if all we get is something too big. */ + if (gdk_pixbuf_get_height(pixbuf) > ICON_SIZE) { + gfloat scale = (gfloat)ICON_SIZE / (gfloat)gdk_pixbuf_get_height(pixbuf); + gint width = round(gdk_pixbuf_get_width(pixbuf) * scale); + + GdkPixbuf * scaled = gdk_pixbuf_scale_simple(pixbuf, width, ICON_SIZE, GDK_INTERP_BILINEAR); + g_object_unref(G_OBJECT(pixbuf)); + pixbuf = scaled; + } + + /* Put the pixbuf on the image */ + gtk_image_set_from_pixbuf(image, pixbuf); + g_object_unref(G_OBJECT(pixbuf)); + } else { g_warning ("Unable to load icon from data: %s", error->message); g_error_free (error); } @@ -89,33 +106,6 @@ refresh_image (GtkImage * image) g_warning ("Unable to load icon from data: %s", error->message); g_error_free (error); } - } else { - GError * error = NULL; - - pixbuf = gdk_pixbuf_new_from_file (icon_filename, &error); - - if (pixbuf == NULL) { - g_warning ("Unable to load icon from file '%s': %s", icon_filename, error->message); - g_error_free (error); - } - } - - if (pixbuf != NULL) { - /* Scale icon if all we get is something too big. */ - if (gdk_pixbuf_get_height(pixbuf) > icon_size) { - gfloat scale = (gfloat)icon_size / (gfloat)gdk_pixbuf_get_height(pixbuf); - gint width = round(gdk_pixbuf_get_width(pixbuf) * scale); - - GdkPixbuf * scaled = gdk_pixbuf_scale_simple(pixbuf, width, icon_size, GDK_INTERP_BILINEAR); - g_object_unref(G_OBJECT(pixbuf)); - pixbuf = scaled; - } - - /* Put the pixbuf on the image */ - gtk_image_set_from_pixbuf(image, pixbuf); - g_object_unref(G_OBJECT(pixbuf)); - } else { - gtk_image_set_from_icon_name (image, "image-missing", GTK_ICON_SIZE_LARGE_TOOLBAR); } if (icon_info != NULL) { -- cgit v1.2.3 From acf1752ffbac11c5b0422adc7cc693cc78bac6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 28 Feb 2014 07:17:20 +0100 Subject: ImageHelper: set image from icon name if we have the filename --- libindicator/indicator-image-helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c index 684fe8a..a38015c 100644 --- a/libindicator/indicator-image-helper.c +++ b/libindicator/indicator-image-helper.c @@ -72,7 +72,7 @@ refresh_image (GtkImage * image) if (icon_info != NULL) { gtk_image_set_from_gicon (image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); } else if (icon_filename != NULL) { - gtk_image_set_from_file (image, icon_filename); + gtk_image_set_from_icon_name (image, icon_filename, GTK_ICON_SIZE_LARGE_TOOLBAR); } else { /* Build a pixbuf if needed */ GdkPixbuf * pixbuf = NULL; -- cgit v1.2.3 From 8a1bdd708c6bca61cc35589c132bf4bb98069a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 3 Mar 2014 10:44:10 +0100 Subject: IndicatorLoader: Make sure that we load the icon at its original size, if not higher than IMAGE_SIZE In this way icons such as the battery one won't be shrunk. --- libindicator/indicator-image-helper.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c index a38015c..da6a4e8 100644 --- a/libindicator/indicator-image-helper.c +++ b/libindicator/indicator-image-helper.c @@ -63,24 +63,24 @@ refresh_image (GtkImage * image) icon_filename = gtk_icon_info_get_filename(icon_info); } - if (icon_filename == NULL && !G_IS_BYTES_ICON (icon_names)) { + if (icon_filename == NULL && !G_IS_BYTES_ICON(icon_names)) { /* show a broken image if we don't have a filename or image data */ - gtk_image_set_from_icon_name (image, "image-missing", GTK_ICON_SIZE_LARGE_TOOLBAR); + gtk_image_set_from_icon_name(image, "image-missing", GTK_ICON_SIZE_LARGE_TOOLBAR); return; } if (icon_info != NULL) { - gtk_image_set_from_gicon (image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); + gtk_image_set_from_gicon(image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); } else if (icon_filename != NULL) { - gtk_image_set_from_icon_name (image, icon_filename, GTK_ICON_SIZE_LARGE_TOOLBAR); - } else { + gtk_image_set_from_file(image, icon_filename); + } else if (G_IS_LOADABLE_ICON(icon_names)) { /* Build a pixbuf if needed */ GdkPixbuf * pixbuf = NULL; GError * error = NULL; - GInputStream * stream = g_loadable_icon_load (G_LOADABLE_ICON (icon_names), ICON_SIZE, NULL, NULL, &error); + GInputStream * stream = g_loadable_icon_load(G_LOADABLE_ICON(icon_names), ICON_SIZE, NULL, NULL, &error); if (stream != NULL) { - pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error); + pixbuf = gdk_pixbuf_new_from_stream(stream, NULL, &error); g_input_stream_close (stream, NULL, NULL); g_object_unref (stream); @@ -108,6 +108,19 @@ refresh_image (GtkImage * image) } } + /* Make sure that we load the icon at its original size, if not higher than IMAGE_SIZE */ + gint pixel_size = 0; + + if (icon_filename) { + gint height; + gdk_pixbuf_get_file_info(icon_filename, NULL, &height); + + if (height > ICON_SIZE) + pixel_size = ICON_SIZE; + } + + gtk_image_set_pixel_size(image, pixel_size); + if (icon_info != NULL) { #if GTK_CHECK_VERSION(3, 8, 0) g_object_unref(icon_info); -- cgit v1.2.3 From 844e1b508be157342695aa852e81c1c3756511fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 3 Mar 2014 11:25:29 +0100 Subject: IndicatorImageHelper: let's use the actual icon file if its height is less than ICON_SIZE --- libindicator/indicator-image-helper.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c index da6a4e8..dd32cb2 100644 --- a/libindicator/indicator-image-helper.c +++ b/libindicator/indicator-image-helper.c @@ -70,9 +70,23 @@ refresh_image (GtkImage * image) } if (icon_info != NULL) { - gtk_image_set_from_gicon(image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); + GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon_info, NULL); + + if (gdk_pixbuf_get_height(pixbuf) < ICON_SIZE) { + gtk_image_set_from_file(image, icon_filename); + } else { + gtk_image_set_from_gicon(image, icon_names, GTK_ICON_SIZE_LARGE_TOOLBAR); + } + g_object_unref (pixbuf); } else if (icon_filename != NULL) { gtk_image_set_from_file(image, icon_filename); + + gint height; + gdk_pixbuf_get_file_info(icon_filename, NULL, &height); + + if (height > ICON_SIZE) { + gtk_image_set_pixel_size(image, ICON_SIZE); + } } else if (G_IS_LOADABLE_ICON(icon_names)) { /* Build a pixbuf if needed */ GdkPixbuf * pixbuf = NULL; @@ -108,19 +122,6 @@ refresh_image (GtkImage * image) } } - /* Make sure that we load the icon at its original size, if not higher than IMAGE_SIZE */ - gint pixel_size = 0; - - if (icon_filename) { - gint height; - gdk_pixbuf_get_file_info(icon_filename, NULL, &height); - - if (height > ICON_SIZE) - pixel_size = ICON_SIZE; - } - - gtk_image_set_pixel_size(image, pixel_size); - if (icon_info != NULL) { #if GTK_CHECK_VERSION(3, 8, 0) g_object_unref(icon_info); -- cgit v1.2.3