diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index 1da8ae973..11308083c 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -103,8 +103,22 @@ struct _GtkIconThemePrivate GList *dir_mtimes; gulong reset_styles_idle; + + /* the icons loaded last with gtk_icon_theme_load_icon(), most recent + * first, as tree views and menus load the same few again and again */ + struct { + gchar *name; + gint size; + GtkIconLookupFlags flags; + GdkPixbuf *pixbuf; + } loaded[32]; + guint n_loaded; + guint loaded_builtin_serial; }; +/* icons small enough to be kept: 48 × 48 */ +#define MAX_LOADED_PIXELS (48 * 48) + struct _GtkIconInfo { /* Information about the source @@ -247,6 +261,7 @@ static BuiltinIcon *find_builtin_icon (const gchar *icon_name, static guint signal_changed = 0; static GHashTable *icon_theme_builtin_icons; +static guint icon_theme_builtin_serial; /* changes as builtin icons are added */ /* also used in gtkiconfactory.c */ GtkIconCache *_builtin_cache = NULL; @@ -639,11 +654,24 @@ do_theme_change (GtkIconTheme *icon_theme) reset_styles_idle, icon_theme, NULL); } +static void +forget_loaded_icons (GtkIconThemePrivate *priv) +{ + while (priv->n_loaded > 0) + { + priv->n_loaded--; + g_free (priv->loaded[priv->n_loaded].name); + g_object_unref (priv->loaded[priv->n_loaded].pixbuf); + } +} + static void blow_themes (GtkIconTheme *icon_theme) { GtkIconThemePrivate *priv = icon_theme->priv; + forget_loaded_icons (priv); + if (priv->themes_valid) { g_hash_table_destroy (priv->all_icons); @@ -1536,8 +1564,10 @@ gtk_icon_theme_load_icon (GtkIconTheme *icon_theme, GtkIconLookupFlags flags, GError **error) { + GtkIconThemePrivate *priv; GtkIconInfo *icon_info; GdkPixbuf *pixbuf = NULL; + guint i; g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL); g_return_val_if_fail (icon_name != NULL, NULL); @@ -1545,6 +1575,35 @@ gtk_icon_theme_load_icon (GtkIconTheme *icon_theme, (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); + priv = icon_theme->priv; + + /* notices changed themes, which forget the loaded icons */ + ensure_valid_themes (icon_theme); + + if (priv->loaded_builtin_serial != icon_theme_builtin_serial) + { + forget_loaded_icons (priv); + priv->loaded_builtin_serial = icon_theme_builtin_serial; + } + + /* A copy of the one loaded last time, instead of reading and + * scaling the file again; a copy, as programs may change it, which + * the documentation forbids, but which worked before. */ + for (i = 0; i < priv->n_loaded; i++) + if (priv->loaded[i].size == size && priv->loaded[i].flags == flags && + strcmp (priv->loaded[i].name, icon_name) == 0) + { + if (i > 0) + { + __typeof__ (priv->loaded[0]) hit = priv->loaded[i]; + + memmove (priv->loaded + 1, priv->loaded, i * sizeof (priv->loaded[0])); + priv->loaded[0] = hit; + } + + return gdk_pixbuf_copy (priv->loaded[0].pixbuf); + } + icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, flags | GTK_ICON_LOOKUP_USE_BUILTIN); if (!icon_info) @@ -1557,6 +1616,24 @@ gtk_icon_theme_load_icon (GtkIconTheme *icon_theme, pixbuf = gtk_icon_info_load_icon (icon_info, error); gtk_icon_info_free (icon_info); + if (pixbuf && + gdk_pixbuf_get_width (pixbuf) * gdk_pixbuf_get_height (pixbuf) <= MAX_LOADED_PIXELS) + { + if (priv->n_loaded == G_N_ELEMENTS (priv->loaded)) + { + priv->n_loaded--; + g_free (priv->loaded[priv->n_loaded].name); + g_object_unref (priv->loaded[priv->n_loaded].pixbuf); + } + + memmove (priv->loaded + 1, priv->loaded, priv->n_loaded * sizeof (priv->loaded[0])); + priv->loaded[0].name = g_strdup (icon_name); + priv->loaded[0].size = size; + priv->loaded[0].flags = flags; + priv->loaded[0].pixbuf = gdk_pixbuf_copy (pixbuf); + priv->n_loaded++; + } + return pixbuf; } @@ -3428,6 +3505,7 @@ gtk_icon_theme_add_builtin_icon (const gchar *icon_name, /* Replaces value, leaves key untouched */ g_hash_table_insert (icon_theme_builtin_icons, key, icons); + icon_theme_builtin_serial++; } /* Look up a builtin icon; the min_difference_p and