inspector: Parse custom css with a delay

The CSS editor was feeling a little sluggish, because it was
reparsing and reapplying the CSS on every keystroke. Add a small
delay, to make this feel smoother.
This commit is contained in:
Matthias Clasen 2014-06-03 09:44:28 -04:00
parent 45dbd84ff3
commit f6cf7fee73

View File

@ -55,6 +55,7 @@ struct _GtkInspectorCssEditorPrivate
gboolean global; gboolean global;
GtkStyleContext *context; GtkStyleContext *context;
GtkToggleToolButton *disable_button; GtkToggleToolButton *disable_button;
guint timeout;
}; };
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorCssEditor, gtk_inspector_css_editor, GTK_TYPE_BOX) G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorCssEditor, gtk_inspector_css_editor, GTK_TYPE_BOX)
@ -207,8 +208,7 @@ apply_system_font (GtkInspectorCssEditor *ce)
} }
static void static void
text_changed (GtkTextBuffer *buffer, update_style (GtkInspectorCssEditor *ce)
GtkInspectorCssEditor *ce)
{ {
GtkCssProvider *provider; GtkCssProvider *provider;
gchar *text; gchar *text;
@ -220,13 +220,35 @@ text_changed (GtkTextBuffer *buffer,
else else
return; return;
text = get_current_text (buffer); text = get_current_text (ce->priv->text);
gtk_css_provider_load_from_data (provider, text, -1, NULL); gtk_css_provider_load_from_data (provider, text, -1, NULL);
g_free (text); g_free (text);
gtk_style_context_reset_widgets (gdk_screen_get_default ()); gtk_style_context_reset_widgets (gdk_screen_get_default ());
} }
static gboolean
update_timeout (gpointer data)
{
GtkInspectorCssEditor *ce = data;
ce->priv->timeout = 0;
update_style (ce);
return G_SOURCE_REMOVE;
}
static void
text_changed (GtkTextBuffer *buffer,
GtkInspectorCssEditor *ce)
{
if (ce->priv->timeout != 0)
g_source_remove (ce->priv->timeout);
ce->priv->timeout = g_timeout_add (100, update_timeout, ce);
}
static void static void
show_parsing_error (GtkCssProvider *provider, show_parsing_error (GtkCssProvider *provider,
GtkCssSection *section, GtkCssSection *section,
@ -337,6 +359,17 @@ set_property (GObject *object,
} }
} }
static void
finalize (GObject *object)
{
GtkInspectorCssEditor *ce = GTK_INSPECTOR_CSS_EDITOR (object);
if (ce->priv->timeout != 0)
g_source_remove (ce->priv->timeout);
G_OBJECT_CLASS (gtk_inspector_css_editor_parent_class)->finalize (object);
}
static void static void
gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass) gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
{ {
@ -346,6 +379,7 @@ gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
object_class->get_property = get_property; object_class->get_property = get_property;
object_class->set_property = set_property; object_class->set_property = set_property;
object_class->constructed = constructed; object_class->constructed = constructed;
object_class->finalize = finalize;
g_object_class_install_property (object_class, PROP_GLOBAL, g_object_class_install_property (object_class, PROP_GLOBAL,
g_param_spec_boolean ("global", "Global", "Whether this editor changes the whole application or just the selected widget", g_param_spec_boolean ("global", "Global", "Whether this editor changes the whole application or just the selected widget",