[gtk+/wip/matthiasc/icon-size: 13/15] iconhelper: Query size via CSS



commit 1aa1cbb05e96c170431352cc68e08fce864054ea
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Nov 14 22:29:31 2017 -0500

    iconhelper: Query size via CSS
    
    Instead of looking at the icon size, look at the CSS value for
    -gtk-icon-size. Set style classes depending on icon size instead.
    
    Trivially change Adwaita and HighContrast to report the same values as
    before.

 gtk/gtkcellrendererpixbuf.c         |    5 +--
 gtk/gtkiconhelper.c                 |   58 ++++++++++++++++------------------
 gtk/gtkiconhelperprivate.h          |    7 ++--
 gtk/gtkimage.c                      |   20 ++++++------
 gtk/theme/Adwaita/_common.scss      |    8 +++++
 gtk/theme/HighContrast/_common.scss |    8 +++++
 6 files changed, 58 insertions(+), 48 deletions(-)
---
diff --git a/gtk/gtkcellrendererpixbuf.c b/gtk/gtkcellrendererpixbuf.c
index 73276b3..54c3512 100644
--- a/gtk/gtkcellrendererpixbuf.c
+++ b/gtk/gtkcellrendererpixbuf.c
@@ -104,7 +104,6 @@ gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
   priv = cellpixbuf->priv;
 
   priv->image_def = gtk_image_definition_new_empty ();
-  priv->icon_size = GTK_ICON_SIZE_INHERIT;
 }
 
 static void
@@ -415,8 +414,6 @@ create_icon_helper (GtkIconHelper         *icon_helper,
                         widget);
   _gtk_icon_helper_set_force_scale_pixbuf (icon_helper, TRUE);
   _gtk_icon_helper_set_definition (icon_helper, priv->image_def);
-  if (gtk_image_definition_get_storage_type (priv->image_def) != GTK_IMAGE_SURFACE)
-    _gtk_icon_helper_set_icon_size (icon_helper, priv->icon_size);
 }
 
 static void
@@ -441,6 +438,7 @@ gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer    *cell,
   context = gtk_widget_get_style_context (widget);
   gtk_style_context_save (context);
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
+  gtk_icon_size_set_style_classes (gtk_style_context_get_node (context), priv->icon_size);
   create_icon_helper (&icon_helper, cellpixbuf, widget);
 
   if (!_gtk_icon_helper_get_is_empty (&icon_helper))
@@ -535,6 +533,7 @@ gtk_cell_renderer_pixbuf_snapshot (GtkCellRenderer      *cell,
   gtk_style_context_save (context);
 
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
+  gtk_icon_size_set_style_classes (gtk_style_context_get_node (context), priv->icon_size);
 
   g_object_get (cell, "is-expander", &is_expander, NULL);
   if (is_expander)
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index 543551e..902998b 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -26,6 +26,7 @@
 #include "gtkcssenumvalueprivate.h"
 #include "gtkcssiconthemevalueprivate.h"
 #include "gtkcssnodeprivate.h"
+#include "gtkcssnumbervalueprivate.h"
 #include "gtkcssstyleprivate.h"
 #include "gtkcssstylepropertyprivate.h"
 #include "gtkcsstransientnodeprivate.h"
@@ -113,7 +114,6 @@ gtk_icon_helper_init (GtkIconHelper *self,
   memset (self, 0, sizeof (GtkIconHelper));
   self->def = gtk_image_definition_new_empty ();
 
-  self->icon_size = GTK_ICON_SIZE_INHERIT;
   self->pixel_size = -1;
   self->texture_is_symbolic = FALSE;
 
@@ -134,17 +134,10 @@ ensure_icon_size (GtkIconHelper *self,
     {
       width = height = self->pixel_size;
     }
-  else if (!gtk_icon_size_lookup (self->icon_size, &width, &height))
+  else
     {
-      if (self->icon_size == GTK_ICON_SIZE_INHERIT)
-        {
-          width = height = 0;
-        }
-      else
-        {
-          g_warning ("Invalid icon size %d", self->icon_size);
-          width = height = 24;
-        }
+      GtkCssStyle *style = gtk_css_node_get_style (self->node);
+      width = height = _gtk_css_number_value_get (gtk_css_style_get_value (style, 
GTK_CSS_PROPERTY_ICON_SIZE), 100);
     }
 
   *width_out = width;
@@ -456,7 +449,7 @@ _gtk_icon_helper_get_size (GtkIconHelper *self,
           width = (gdk_texture_get_width (self->texture) + self->texture_scale - 1) / self->texture_scale;
           height = (gdk_texture_get_height (self->texture) + self->texture_scale - 1) / self->texture_scale;
         }
-      else if (self->icon_size != GTK_ICON_SIZE_INHERIT)
+      else
         {
           ensure_icon_size (self, &width, &height);
         }
@@ -507,19 +500,6 @@ _gtk_icon_helper_set_texture (GtkIconHelper *self,
 }
 
 gboolean
-_gtk_icon_helper_set_icon_size (GtkIconHelper *self,
-                                GtkIconSize    icon_size)
-{
-  if (self->icon_size != icon_size)
-    {
-      self->icon_size = icon_size;
-      gtk_icon_helper_invalidate (self);
-      return TRUE;
-    }
-  return FALSE;
-}
-
-gboolean
 _gtk_icon_helper_set_pixel_size (GtkIconHelper *self,
                                  gint           pixel_size)
 {
@@ -557,12 +537,6 @@ _gtk_icon_helper_get_use_fallback (GtkIconHelper *self)
   return self->use_fallback;
 }
 
-GtkIconSize
-_gtk_icon_helper_get_icon_size (GtkIconHelper *self)
-{
-  return self->icon_size;
-}
-
 gint
 _gtk_icon_helper_get_pixel_size (GtkIconHelper *self)
 {
@@ -640,3 +614,25 @@ _gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
       gtk_icon_helper_invalidate (self);
     }
 }
+
+void
+gtk_icon_size_set_style_classes (GtkCssNode  *cssnode,
+                                 GtkIconSize  icon_size)
+{
+  struct {
+    GtkIconSize icon_size;
+    const char *class_name;
+  } class_names[] = {
+    { GTK_ICON_SIZE_NORMAL, "normal-icons" },
+    { GTK_ICON_SIZE_LARGE, "large-icons" }
+  };
+  guint i;
+
+  for (i = 0; i < G_N_ELEMENTS (class_names); i++)
+    {
+      if (icon_size == class_names[i].icon_size)
+        gtk_css_node_add_class (cssnode, g_quark_from_string (class_names[i].class_name));
+      else
+        gtk_css_node_remove_class (cssnode, g_quark_from_string (class_names[i].class_name));
+    }
+}
diff --git a/gtk/gtkiconhelperprivate.h b/gtk/gtkiconhelperprivate.h
index 42ba490..1ba797a 100644
--- a/gtk/gtkiconhelperprivate.h
+++ b/gtk/gtkiconhelperprivate.h
@@ -36,7 +36,6 @@ struct _GtkIconHelper
 
   GtkImageDefinition *def;
 
-  GtkIconSize icon_size;
   gint pixel_size;
 
   guint use_fallback : 1;
@@ -71,15 +70,12 @@ void _gtk_icon_helper_set_surface (GtkIconHelper *self,
 void _gtk_icon_helper_set_texture (GtkIconHelper *self,
                                   GdkTexture *texture);
 
-gboolean _gtk_icon_helper_set_icon_size    (GtkIconHelper *self,
-                                            GtkIconSize    icon_size);
 gboolean _gtk_icon_helper_set_pixel_size   (GtkIconHelper *self,
                                             gint           pixel_size);
 gboolean _gtk_icon_helper_set_use_fallback (GtkIconHelper *self,
                                             gboolean       use_fallback);
 
 GtkImageType _gtk_icon_helper_get_storage_type (GtkIconHelper *self);
-GtkIconSize _gtk_icon_helper_get_icon_size (GtkIconHelper *self);
 gint _gtk_icon_helper_get_pixel_size (GtkIconHelper *self);
 gboolean _gtk_icon_helper_get_use_fallback (GtkIconHelper *self);
 
@@ -105,6 +101,9 @@ void      gtk_icon_helper_invalidate (GtkIconHelper *self);
 void      gtk_icon_helper_invalidate_for_change (GtkIconHelper     *self,
                                                  GtkCssStyleChange *change);
 
+void      gtk_icon_size_set_style_classes (GtkCssNode  *cssnode,
+                                           GtkIconSize  icon_size);
+
 G_END_DECLS
 
 #endif /* __GTK_ICON_HELPER_H__ */
diff --git a/gtk/gtkimage.c b/gtk/gtkimage.c
index a24ef9a..3c289b3 100644
--- a/gtk/gtkimage.c
+++ b/gtk/gtkimage.c
@@ -75,6 +75,7 @@
 struct _GtkImagePrivate
 {
   GtkIconHelper icon_helper;
+  GtkIconSize icon_size;
 
   float baseline_align;
 
@@ -83,7 +84,6 @@ struct _GtkImagePrivate
 };
 
 
-#define DEFAULT_ICON_SIZE GTK_ICON_SIZE_INHERIT
 static void gtk_image_snapshot             (GtkWidget    *widget,
                                             GtkSnapshot  *snapshot);
 static void gtk_image_size_allocate        (GtkWidget           *widget,
@@ -176,7 +176,7 @@ gtk_image_class_init (GtkImageClass *class)
                         P_("Icon size"),
                         P_("Symbolic size to use for icon set or named icon"),
                         0, G_MAXINT,
-                        DEFAULT_ICON_SIZE,
+                        GTK_ICON_SIZE_INHERIT,
                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
   /**
@@ -282,7 +282,6 @@ gtk_image_init (GtkImage *image)
   gtk_widget_set_has_window (GTK_WIDGET (image), FALSE);
 
   gtk_icon_helper_init (&priv->icon_helper, widget_node, GTK_WIDGET (image));
-  _gtk_icon_helper_set_icon_size (&priv->icon_helper, DEFAULT_ICON_SIZE);
 }
 
 static void
@@ -367,7 +366,7 @@ gtk_image_get_property (GObject     *object,
       g_value_set_string (value, priv->filename);
       break;
     case PROP_ICON_SIZE:
-      g_value_set_int (value, _gtk_icon_helper_get_icon_size (&priv->icon_helper));
+      g_value_set_int (value, priv->icon_size);
       break;
     case PROP_PIXEL_SIZE:
       g_value_set_int (value, _gtk_icon_helper_get_pixel_size (&priv->icon_helper));
@@ -1373,11 +1372,12 @@ gtk_image_set_icon_size (GtkImage    *image,
 
   g_return_if_fail (GTK_IS_IMAGE (image));
 
-  if (_gtk_icon_helper_set_icon_size (&priv->icon_helper, icon_size))
-    {
-      gtk_widget_queue_resize (GTK_WIDGET (image));
-      g_object_notify_by_pspec (G_OBJECT (image), image_props[PROP_ICON_SIZE]);
-    }
+  if (priv->icon_size == icon_size)
+    return;
+
+  priv->icon_size = icon_size;
+  gtk_icon_size_set_style_classes (gtk_widget_get_css_node (GTK_WIDGET (image)), icon_size);
+  g_object_notify_by_pspec (G_OBJECT (image), image_props[PROP_ICON_SIZE]);
 }
 
 /**
@@ -1397,5 +1397,5 @@ gtk_image_get_icon_size (GtkImage *image)
 
   g_return_val_if_fail (GTK_IS_IMAGE (image), GTK_ICON_SIZE_INHERIT);
 
-  return _gtk_icon_helper_get_icon_size (&priv->icon_helper);
+  return priv->icon_size;
 }
diff --git a/gtk/theme/Adwaita/_common.scss b/gtk/theme/Adwaita/_common.scss
index 05037eb..599631f 100644
--- a/gtk/theme/Adwaita/_common.scss
+++ b/gtk/theme/Adwaita/_common.scss
@@ -47,6 +47,14 @@ row:focus(visible) {
   }
 }
 
+.normal-icons {
+  -gtk-icon-size: 16px;
+}
+
+.large-icons {
+  -gtk-icon-size: 32px;
+}
+
 /*
    These wildcard seems unavoidable, need to investigate.
    Wildcards are bad and troublesome, use them with care,
diff --git a/gtk/theme/HighContrast/_common.scss b/gtk/theme/HighContrast/_common.scss
index c172362..e71b926 100644
--- a/gtk/theme/HighContrast/_common.scss
+++ b/gtk/theme/HighContrast/_common.scss
@@ -88,6 +88,14 @@ $ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
   }
 }
 
+.normal-icons {
+  -gtk-icon-size: 16px;
+}
+
+.large-icons {
+  -gtk-icon-size: 32px;
+}
+
 .view {
   color: $fg_color;
   background-color: $base_color;


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