[gtk/wip/matthiasc/css-values] css: Handle initial values more efficiently



commit 326f73e14bc00c19c2b1217ae4753bef44b36030
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jan 11 21:13:27 2020 -0500

    css: Handle initial values more efficiently
    
    Instead of creating a new values every time we
    resolve the initial values for dpi and font family,
    keep a css value around for the settings.

 gtk/gtkcssinitialvalue.c | 13 +++----------
 gtk/gtksettings.c        | 37 +++++++++++++++++++++++++++++++++++++
 gtk/gtksettingsprivate.h |  3 +++
 3 files changed, 43 insertions(+), 10 deletions(-)
---
diff --git a/gtk/gtkcssinitialvalue.c b/gtk/gtkcssinitialvalue.c
index f01b49c612..9650b276d6 100644
--- a/gtk/gtkcssinitialvalue.c
+++ b/gtk/gtkcssinitialvalue.c
@@ -51,20 +51,13 @@ gtk_css_value_initial_compute (GtkCssValue      *value,
     case GTK_CSS_PROPERTY_DPI:
       settings = gtk_style_provider_get_settings (provider);
       if (settings)
-        {
-          int dpi_int;
-
-          g_object_get (settings, "gtk-xft-dpi", &dpi_int, NULL);
-
-          if (dpi_int > 0.0)
-            return _gtk_css_number_value_new (dpi_int / 1024., GTK_CSS_NUMBER);
-        }
+        return gtk_css_value_ref (gtk_settings_get_dpi_css_value (settings));
       break;
 
     case GTK_CSS_PROPERTY_FONT_FAMILY:
       settings = gtk_style_provider_get_settings (provider);
-      if (settings && gtk_settings_get_font_family (settings) != NULL)
-        return _gtk_css_string_value_new (gtk_settings_get_font_family (settings));
+      if (settings)
+        return gtk_css_value_ref (gtk_settings_get_font_family_css_value (settings));
       break;
 
     default:
diff --git a/gtk/gtksettings.c b/gtk/gtksettings.c
index 7895f414e2..38e3f1797d 100644
--- a/gtk/gtksettings.c
+++ b/gtk/gtksettings.c
@@ -30,6 +30,8 @@
 #include "gtktypebuiltins.h"
 #include "gtkversion.h"
 #include "gtkwidget.h"
+#include "gtkcssnumbervalueprivate.h"
+#include "gtkcssstringvalueprivate.h"
 
 #include "gdk/gdk-private.h"
 
@@ -129,6 +131,8 @@ struct _GtkSettingsPrivate
   gboolean font_size_absolute;
   gchar *font_family;
   cairo_font_options_t *font_options;
+  GtkCssValue *css_dpi_value;
+  GtkCssValue *css_font_family_value;
 };
 
 struct _GtkSettingsValuePrivate
@@ -1002,6 +1006,9 @@ gtk_settings_finalize (GObject *object)
 
   g_free (priv->font_family);
 
+  g_clear_pointer (&priv->css_dpi_value, gtk_css_value_unref);
+  g_clear_pointer (&priv->css_font_family_value, gtk_css_value_unref);
+
   G_OBJECT_CLASS (gtk_settings_parent_class)->finalize (object);
 }
 
@@ -1234,6 +1241,8 @@ settings_update_font_values (GtkSettings *settings)
 
   if (desc)
     pango_font_description_free (desc);
+
+  g_clear_pointer (&priv->css_font_family_value, gtk_css_value_unref);
 }
 
 static void
@@ -1267,6 +1276,7 @@ gtk_settings_notify (GObject    *object,
        * widgets with gtk_widget_style_set(), and also causes more
        * recomputation than necessary.
        */
+      g_clear_pointer (&priv->css_dpi_value, gtk_css_value_unref);
       gtk_style_context_reset_widgets (priv->display);
       break;
     case PROP_XFT_ANTIALIAS:
@@ -2079,3 +2089,30 @@ gtk_settings_get_font_size_is_absolute (GtkSettings *settings)
 
   return priv->font_size_absolute;
 }
+
+GtkCssValue *
+gtk_settings_get_dpi_css_value (GtkSettings *settings)
+{
+  GtkSettingsPrivate *priv = gtk_settings_get_instance_private (settings);
+
+  if (!priv->css_dpi_value)
+    {
+      int dpi;
+
+      g_object_get (settings, "gtk-xft-dpi", &dpi, NULL);
+      priv->css_dpi_value = _gtk_css_number_value_new (dpi / 1024., GTK_CSS_NUMBER);
+    }
+
+  return priv->css_dpi_value;
+}
+
+GtkCssValue *
+gtk_settings_get_font_family_css_value (GtkSettings *settings)
+{
+  GtkSettingsPrivate *priv = gtk_settings_get_instance_private (settings);
+
+  if (!priv->css_font_family_value)
+    priv->css_font_family_value = _gtk_css_string_value_new (priv->font_family);
+
+  return priv->css_font_family_value;
+}
diff --git a/gtk/gtksettingsprivate.h b/gtk/gtksettingsprivate.h
index 73a362a1be..e9632940b3 100644
--- a/gtk/gtksettingsprivate.h
+++ b/gtk/gtksettingsprivate.h
@@ -20,6 +20,7 @@
 
 #include <gtk/gtksettings.h>
 #include "gtkstylecascadeprivate.h"
+#include "gtkcssvalueprivate.h"
 
 G_BEGIN_DECLS
 
@@ -48,6 +49,8 @@ gint     gtk_settings_get_dnd_drag_threshold (GtkSettings *settings);
 const gchar *gtk_settings_get_font_family    (GtkSettings *settings);
 gint         gtk_settings_get_font_size      (GtkSettings *settings);
 gboolean     gtk_settings_get_font_size_is_absolute (GtkSettings *settings);
+GtkCssValue *gtk_settings_get_dpi_css_value (GtkSettings *settings);
+GtkCssValue *gtk_settings_get_font_family_css_value (GtkSettings *settings);
 
 G_END_DECLS
 


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