cssnode: Automatically recreate style on get_style() call

When the style is invalid, redo it.

Make this a vfunc, so the widget nodes can opt out.
This commit is contained in:
Benjamin Otte 2015-02-04 04:43:55 +01:00
parent 71813671ef
commit 219e52f7e1
6 changed files with 44 additions and 36 deletions

View File

@ -222,9 +222,16 @@ gtk_css_node_create_style (GtkCssNode *cssnode)
return style;
}
static GtkCssStyle *
gtk_css_node_real_update_style (GtkCssNode *cssnode,
GtkCssChange pending_change,
GtkCssStyle *old_style)
{
return gtk_css_node_create_style (cssnode);
}
static void
gtk_css_node_real_invalidate (GtkCssNode *cssnode,
GtkCssChange change)
gtk_css_node_real_invalidate (GtkCssNode *node)
{
}
@ -276,6 +283,7 @@ gtk_css_node_class_init (GtkCssNodeClass *klass)
object_class->dispose = gtk_css_node_dispose;
object_class->finalize = gtk_css_node_finalize;
klass->update_style = gtk_css_node_real_update_style;
klass->invalidate = gtk_css_node_real_invalidate;
klass->validate = gtk_css_node_real_validate;
klass->set_invalid = gtk_css_node_real_set_invalid;
@ -288,6 +296,8 @@ static void
gtk_css_node_init (GtkCssNode *cssnode)
{
cssnode->decl = gtk_css_node_declaration_new ();
cssnode->style = g_object_ref (gtk_css_static_style_get_default ());
}
void
@ -402,6 +412,21 @@ gtk_css_node_set_style (GtkCssNode *cssnode,
GtkCssStyle *
gtk_css_node_get_style (GtkCssNode *cssnode)
{
GtkCssStyle *new_style;
if (cssnode->pending_changes)
{
new_style = GTK_CSS_NODE_GET_CLASS (cssnode)->update_style (cssnode,
cssnode->pending_changes,
cssnode->style);
if (new_style)
{
gtk_css_node_set_style (cssnode, new_style);
g_object_unref (new_style);
cssnode->pending_changes = 0;
}
}
return cssnode->style;
}
@ -533,7 +558,7 @@ gtk_css_node_invalidate (GtkCssNode *cssnode,
{
cssnode->pending_changes |= change;
GTK_CSS_NODE_GET_CLASS (cssnode)->invalidate (cssnode, change);
GTK_CSS_NODE_GET_CLASS (cssnode)->invalidate (cssnode);
gtk_css_node_set_invalid (cssnode, TRUE);
}

View File

@ -59,8 +59,10 @@ struct _GtkCssNodeClass
GtkWidgetPath * (* create_widget_path) (GtkCssNode *cssnode);
const GtkWidgetPath * (* get_widget_path) (GtkCssNode *cssnode);
GtkStyleProviderPrivate *(* get_style_provider) (GtkCssNode *cssnode);
void (* invalidate) (GtkCssNode *cssnode,
GtkCssChange change);
GtkCssStyle * (* update_style) (GtkCssNode *cssnode,
GtkCssChange pending_changes,
GtkCssStyle *old_style);
void (* invalidate) (GtkCssNode *node);
void (* set_invalid) (GtkCssNode *node,
gboolean invalid);
GtkBitmask * (* validate) (GtkCssNode *cssnode,

View File

@ -24,13 +24,10 @@
G_DEFINE_TYPE (GtkCssPathNode, gtk_css_path_node, GTK_TYPE_CSS_NODE)
static void
gtk_css_path_node_invalidate (GtkCssNode *node,
GtkCssChange change)
gtk_css_path_node_invalidate (GtkCssNode *node)
{
GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
gtk_css_node_set_style (node, NULL);
if (path_node->context)
{
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

View File

@ -22,13 +22,6 @@
G_DEFINE_TYPE (GtkCssTransientNode, gtk_css_transient_node, GTK_TYPE_CSS_NODE)
static void
gtk_css_transient_node_invalidate (GtkCssNode *node,
GtkCssChange change)
{
gtk_css_node_set_style (node, NULL);
}
static void
gtk_css_transient_node_set_invalid (GtkCssNode *node,
gboolean invalid)
@ -71,7 +64,6 @@ gtk_css_transient_node_class_init (GtkCssTransientNodeClass *klass)
{
GtkCssNodeClass *node_class = GTK_CSS_NODE_CLASS (klass);
node_class->invalidate = gtk_css_transient_node_invalidate;
node_class->set_invalid = gtk_css_transient_node_set_invalid;
node_class->create_widget_path = gtk_css_transient_node_create_widget_path;
node_class->get_widget_path = gtk_css_transient_node_get_widget_path;

View File

@ -31,6 +31,14 @@
G_DEFINE_TYPE (GtkCssWidgetNode, gtk_css_widget_node, GTK_TYPE_CSS_NODE)
static GtkCssStyle *
gtk_css_widget_node_update_style (GtkCssNode *cssnode,
GtkCssChange pending_change,
GtkCssStyle *old_style)
{
return NULL;
}
static void
gtk_css_widget_node_set_invalid (GtkCssNode *node,
gboolean invalid)
@ -206,6 +214,7 @@ gtk_css_widget_node_class_init (GtkCssWidgetNodeClass *klass)
{
GtkCssNodeClass *node_class = GTK_CSS_NODE_CLASS (klass);
node_class->update_style = gtk_css_widget_node_update_style;
node_class->validate = gtk_css_widget_node_validate;
node_class->set_invalid = gtk_css_widget_node_set_invalid;
node_class->create_widget_path = gtk_css_widget_node_create_widget_path;

View File

@ -608,24 +608,8 @@ gtk_style_context_has_custom_cascade (GtkStyleContext *context)
GtkCssStyle *
gtk_style_context_lookup_style (GtkStyleContext *context)
{
GtkStyleContextPrivate *priv;
GtkCssStyle *values;
GtkCssNode *cssnode;
priv = context->priv;
cssnode = priv->cssnode;
/* Current data in use is cached, just return it */
values = gtk_css_node_get_style (cssnode);
if (values)
return values;
values = gtk_css_node_create_style (cssnode);
gtk_css_node_set_style (cssnode, values);
g_object_unref (values);
return values;
/* Code will recreate style if it was changed */
return gtk_css_node_get_style (context->priv->cssnode);
}
static GtkCssStyle *
@ -702,7 +686,6 @@ _gtk_style_context_set_widget (GtkStyleContext *context,
g_object_unref (priv->cssnode);
priv->cssnode = gtk_css_widget_node_new (widget);
gtk_css_node_set_state (priv->cssnode, GTK_STATE_FLAG_DIR_LTR);
gtk_css_node_set_style (priv->cssnode, gtk_css_static_style_get_default ());
}
if (widget)