diff options
author | Marco Trevisan (TreviƱo) <mail@3v1n0.net> | 2014-03-04 10:58:28 +0000 |
---|---|---|
committer | CI bot <ps-jenkins@lists.canonical.com> | 2014-03-04 10:58:28 +0000 |
commit | a0a0b28ddd2022cd64ecc105abfe5441716afde8 (patch) | |
tree | 11971c44ee160d35e4b773cbe5c4cdfd5437d957 /libindicator | |
parent | 517d1b4d5901204c39f23c55aca8041470b162b6 (diff) | |
parent | 844e1b508be157342695aa852e81c1c3756511fd (diff) | |
download | libayatana-indicator-a0a0b28ddd2022cd64ecc105abfe5441716afde8.tar.gz libayatana-indicator-a0a0b28ddd2022cd64ecc105abfe5441716afde8.tar.bz2 libayatana-indicator-a0a0b28ddd2022cd64ecc105abfe5441716afde8.zip |
IndicatorImageHelper: always try to use a GIcon or the filename as source of the GdkImage
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. Fixes: 784055, 1000785, 1285989
Diffstat (limited to 'libindicator')
-rw-r--r-- | libindicator/indicator-image-helper.c | 82 |
1 files changed, 43 insertions, 39 deletions
diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c index 28a6c92..dd32cb2 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 */ @@ -63,25 +63,56 @@ 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; } - /* Build a pixbuf */ - GdkPixbuf * pixbuf = NULL; + if (icon_info != NULL) { + 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 (icon_filename == NULL) { + 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; 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); - 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 +120,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) { |