[gtk: 6/40] Add gtk_icon_info_snapshot_with_colors() and use instead of custom code



commit 53c542765f9c57519e7d74d69bb564ca82e24d60
Author: Alexander Larsson <alexl redhat com>
Date:   Mon Jan 27 16:42:38 2020 +0100

    Add gtk_icon_info_snapshot_with_colors() and use instead of custom code

 gtk/gtkcssimageicontheme.c        | 76 +++++++++++++------------------------
 gtk/gtkcssimageiconthemeprivate.h |  2 +-
 gtk/gtkicontheme.c                | 80 +++++++++++++++++++++++++++++++++++----
 gtk/gtkicontheme.h                |  8 ++++
 4 files changed, 108 insertions(+), 58 deletions(-)
---
diff --git a/gtk/gtkcssimageicontheme.c b/gtk/gtkcssimageicontheme.c
index bf4dc47415..d6432bee36 100644
--- a/gtk/gtkcssimageicontheme.c
+++ b/gtk/gtkcssimageicontheme.c
@@ -45,25 +45,22 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
                                    double       height)
 {
   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
-  GdkTexture *texture;
-  double texture_width, texture_height;
+  GtkIconInfo *icon_info;
+  double icon_width, icon_height;
   gint size;
-  gboolean symbolic;
+  double x, y;
 
   size = floor (MIN (width, height));
   if (size <= 0)
     return;
 
   if (size == icon_theme->cached_size &&
-      icon_theme->cached_texture != NULL)
+      icon_theme->cached_icon != NULL)
     {
-      texture = icon_theme->cached_texture;
-      symbolic = icon_theme->cached_symbolic;
+      icon_info = icon_theme->cached_icon;
     }
   else
     {
-      GtkIconInfo *icon_info;
-
       icon_info = gtk_icon_theme_lookup_icon_for_scale (icon_theme->icon_theme,
                                                         icon_theme->name,
                                                         size,
@@ -77,53 +74,32 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
 
       g_assert (icon_info != NULL);
 
-      symbolic = gtk_icon_info_is_symbolic (icon_info);
-      texture = GDK_TEXTURE (gtk_icon_info_load_icon (icon_info, NULL));
-
-      g_clear_object (&icon_theme->cached_texture);
+      g_clear_object (&icon_theme->cached_icon);
 
       icon_theme->cached_size = size;
-      icon_theme->cached_texture = texture;
-      icon_theme->cached_symbolic = symbolic;
-
-      g_object_unref (icon_info);
+      icon_theme->cached_icon = icon_info;
     }
 
-  texture_width = (double) gdk_texture_get_width (texture) / icon_theme->scale;
-  texture_height = (double) gdk_texture_get_height (texture) / icon_theme->scale;
+  icon_width = (double) MIN (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon_info)), width);
+  icon_height = (double) MIN (gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon_info)), height);
 
-  if (symbolic)
+  x = (width - icon_width) / 2;
+  y = (height - icon_height) / 2;
+
+  if (x != 0 || y != 0)
     {
-      const GdkRGBA *fg = &icon_theme->color;
-      const GdkRGBA *sc = &icon_theme->success;
-      const GdkRGBA *wc = &icon_theme->warning;
-      const GdkRGBA *ec = &icon_theme->error;
-      graphene_matrix_t matrix;
-      graphene_vec4_t offset;
-
-
-      graphene_matrix_init_from_float (&matrix,
-          (float[16]) {
-                       sc->red - fg->red, sc->green - fg->green, sc->blue - fg->blue, 0,
-                       wc->red - fg->red, wc->green - fg->green, wc->blue - fg->blue, 0,
-                       ec->red - fg->red, ec->green - fg->green, ec->blue - fg->blue, 0,
-                       0, 0, 0, fg->alpha
-                      });
-      graphene_vec4_init (&offset, fg->red, fg->green, fg->blue, 0);
-
-      gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
+      gtk_snapshot_save (snapshot);
+      gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x, y));
     }
-
-  gtk_snapshot_append_texture (snapshot,
-                               texture,
-                               &GRAPHENE_RECT_INIT(
-                                   (width - texture_width) / 2.0,
-                                   (height - texture_height) / 2.0,
-                                   texture_width,
-                                   texture_height
-                               ));
-  if (symbolic)
-    gtk_snapshot_pop (snapshot);
+  gtk_icon_info_snapshot_with_colors (icon_info, snapshot,
+                                      icon_width,
+                                      icon_height,
+                                      &icon_theme->color,
+                                      &icon_theme->success,
+                                      &icon_theme->warning,
+                                      &icon_theme->error);
+  if (x != 0 || y != 0)
+    gtk_snapshot_restore (snapshot);
 }
 
 static guint
@@ -201,7 +177,7 @@ gtk_css_image_icon_theme_dispose (GObject *object)
   g_free (icon_theme->name);
   icon_theme->name = NULL;
 
-  g_clear_object (&icon_theme->cached_texture);
+  g_clear_object (&icon_theme->cached_icon);
 
   G_OBJECT_CLASS (_gtk_css_image_icon_theme_parent_class)->dispose (object);
 }
@@ -228,6 +204,6 @@ _gtk_css_image_icon_theme_init (GtkCssImageIconTheme *icon_theme)
   icon_theme->icon_theme = gtk_icon_theme_get_default ();
   icon_theme->scale = 1;
   icon_theme->cached_size = -1;
-  icon_theme->cached_texture = NULL;
+  icon_theme->cached_icon = NULL;
 }
 
diff --git a/gtk/gtkcssimageiconthemeprivate.h b/gtk/gtkcssimageiconthemeprivate.h
index f583b35230..6a80ad0758 100644
--- a/gtk/gtkcssimageiconthemeprivate.h
+++ b/gtk/gtkcssimageiconthemeprivate.h
@@ -49,7 +49,7 @@ struct _GtkCssImageIconTheme
 
   int cached_size;
   gboolean cached_symbolic;
-  GdkTexture *cached_texture;
+  GtkIconInfo *cached_icon;
 };
 
 struct _GtkCssImageIconThemeClass
diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
index c8c4cbf40f..01da64cc8f 100644
--- a/gtk/gtkicontheme.c
+++ b/gtk/gtkicontheme.c
@@ -3832,14 +3832,9 @@ icon_info_ensure_scale_and_texture__locked (GtkIconInfo *icon_info)
   return TRUE;
 }
 
-
-static void
-icon_info_paintable_snapshot (GdkPaintable *paintable,
-                              GdkSnapshot  *snapshot,
-                              double        width,
-                              double        height)
+static GdkTexture *
+icon_info_get_texture (GtkIconInfo *icon_info)
 {
-  GtkIconInfo *icon_info = GTK_ICON_INFO (paintable);
   GdkTexture *texture = NULL;
 
   g_mutex_lock (&icon_info->cache_lock);
@@ -3852,17 +3847,87 @@ icon_info_paintable_snapshot (GdkPaintable *paintable,
 
   g_mutex_unlock (&icon_info->cache_lock);
 
+  return texture;
+}
+
+static void
+icon_info_paintable_snapshot (GdkPaintable *paintable,
+                              GdkSnapshot  *snapshot,
+                              double        width,
+                              double        height)
+{
+  GtkIconInfo *icon_info = GTK_ICON_INFO (paintable);
+  GdkTexture *texture;
+
+  texture = icon_info_get_texture (icon_info);
+  if (texture)
+    {
+      if (icon_info->desired_scale != 1)
+        {
+          gtk_snapshot_save (snapshot);
+          gtk_snapshot_scale (snapshot, 1.0 / icon_info->desired_scale, 1.0 / icon_info->desired_scale);
+        }
+
+      gtk_snapshot_append_texture (snapshot, texture,
+                                   &GRAPHENE_RECT_INIT (0, 0, width * icon_info->desired_scale, height * 
icon_info->desired_scale));
+
+      if (icon_info->desired_scale != 1)
+        gtk_snapshot_restore (snapshot);
+
+      g_object_unref (texture);
+    }
+}
+
+void
+gtk_icon_info_snapshot_with_colors (GtkIconInfo *icon_info,
+                                    GdkSnapshot  *snapshot,
+                                    double        width,
+                                    double        height,
+                                    const GdkRGBA *foreground_color,
+                                    const GdkRGBA *success_color,
+                                    const GdkRGBA *warning_color,
+                                    const GdkRGBA *error_color)
+{
+  GdkTexture *texture;
+
+  texture = icon_info_get_texture (icon_info);
   if (texture)
     {
+      gboolean symbolic = gtk_icon_info_is_symbolic (icon_info);
+
       if (icon_info->desired_scale != 1)
         {
           gtk_snapshot_save (snapshot);
           gtk_snapshot_scale (snapshot, 1.0 / icon_info->desired_scale, 1.0 / icon_info->desired_scale);
         }
 
+      if (symbolic)
+        {
+          const GdkRGBA *fg = foreground_color;
+          const GdkRGBA *sc = success_color;
+          const GdkRGBA *wc = warning_color;
+          const GdkRGBA *ec = error_color;
+          graphene_matrix_t matrix;
+          graphene_vec4_t offset;
+
+          graphene_matrix_init_from_float (&matrix,
+                                           (float[16]) {
+                                             sc->red - fg->red, sc->green - fg->green, sc->blue - fg->blue, 
0,
+                                               wc->red - fg->red, wc->green - fg->green, wc->blue - 
fg->blue, 0,
+                                               ec->red - fg->red, ec->green - fg->green, ec->blue - 
fg->blue, 0,
+                                               0, 0, 0, fg->alpha
+                                               });
+          graphene_vec4_init (&offset, fg->red, fg->green, fg->blue, 0);
+
+          gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
+        }
+
       gtk_snapshot_append_texture (snapshot, texture,
                                    &GRAPHENE_RECT_INIT (0, 0, width * icon_info->desired_scale, height * 
icon_info->desired_scale));
 
+      if (symbolic)
+        gtk_snapshot_pop (snapshot);
+
       if (icon_info->desired_scale != 1)
         gtk_snapshot_restore (snapshot);
 
@@ -3870,6 +3935,7 @@ icon_info_paintable_snapshot (GdkPaintable *paintable,
     }
 }
 
+
 static GdkPaintableFlags
 icon_info_paintable_get_flags (GdkPaintable *paintable)
 {
diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h
index 6f96f3e246..484d329439 100644
--- a/gtk/gtkicontheme.h
+++ b/gtk/gtkicontheme.h
@@ -213,6 +213,14 @@ GDK_AVAILABLE_IN_ALL
 const gchar *         gtk_icon_info_get_filename       (GtkIconInfo   *self);
 GDK_AVAILABLE_IN_ALL
 gboolean              gtk_icon_info_is_symbolic        (GtkIconInfo   *self);
+void                  gtk_icon_info_snapshot_with_colors (GtkIconInfo *icon_info,
+                                                          GdkSnapshot  *snapshot,
+                                                          double        width,
+                                                          double        height,
+                                                          const GdkRGBA *foreground_color,
+                                                          const GdkRGBA *success_color,
+                                                          const GdkRGBA *warning_color,
+                                                          const GdkRGBA *error_color);
 GDK_AVAILABLE_IN_ALL
 GdkPaintable *        gtk_icon_info_load_icon          (GtkIconInfo   *self,
                                                         GError       **error);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]