Commit f04f89bc authored by Christian Hergert's avatar Christian Hergert

stylescheme: add support for fallback style-ids

As we try to make styles more declarative, we need a fallback to ensure
that we don't break existing style-schemes in the wild.

Related to !77
parent adc85255
......@@ -493,6 +493,47 @@ fix_style_colors (GtkSourceStyleScheme *scheme,
return style;
}
static GtkSourceStyle *
gtk_source_style_scheme_get_style_internal (GtkSourceStyleScheme *scheme,
const gchar *style_id)
{
GtkSourceStyle *style = NULL;
GtkSourceStyle *real_style;
if (g_hash_table_lookup_extended (scheme->priv->style_cache,
style_id,
NULL,
(gpointer)&style))
{
return style;
}
real_style = g_hash_table_lookup (scheme->priv->defined_styles, style_id);
if (real_style == NULL)
{
if (scheme->priv->parent != NULL)
{
style = gtk_source_style_scheme_get_style (scheme->priv->parent,
style_id);
}
if (style != NULL)
{
g_object_ref (style);
}
}
else
{
style = fix_style_colors (scheme, real_style);
}
g_hash_table_insert (scheme->priv->style_cache,
g_strdup (style_id),
style);
return style;
}
/**
* gtk_source_style_scheme_get_style:
* @scheme: a #GtkSourceStyleScheme.
......@@ -519,42 +560,35 @@ GtkSourceStyle *
gtk_source_style_scheme_get_style (GtkSourceStyleScheme *scheme,
const gchar *style_id)
{
GtkSourceStyle *style = NULL;
GtkSourceStyle *real_style;
GtkSourceStyle *style;
g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
g_return_val_if_fail (style_id != NULL, NULL);
if (g_hash_table_lookup_extended (scheme->priv->style_cache,
style_id,
NULL,
(gpointer)&style))
{
return style;
}
real_style = g_hash_table_lookup (scheme->priv->defined_styles, style_id);
style = gtk_source_style_scheme_get_style_internal (scheme, style_id);
if (real_style == NULL)
if (style == NULL)
{
if (scheme->priv->parent != NULL)
{
style = gtk_source_style_scheme_get_style (scheme->priv->parent,
style_id);
}
if (style != NULL)
/* Long ago, "underlined" was added as a style. The problem with
* this is that it defines how something should look rather than
* classifying what it is.
*
* In general, this was used for URLs.
*
* However, going forward we want to change this but do our best
* to not break existing style-schemes. Should "net-address" be
* requested, but only "underlined" existed, we will fallback to
* the "underlined" style.
*
* If in the future, we need to support more fallbacks, this should
* be changed to a GHashTable to map from src->dst style id.
*/
if (g_str_equal (style_id, "def:net-address"))
{
g_object_ref (style);
style = gtk_source_style_scheme_get_style_internal (scheme, "def:underlined");
}
}
else
{
style = fix_style_colors (scheme, real_style);
}
g_hash_table_insert (scheme->priv->style_cache,
g_strdup (style_id),
style);
return style;
}
......
......@@ -10,4 +10,5 @@
<style name="background-pattern" background="#rgba(100, 0, 0, 0.5)"/>
<style name="def:comment" foreground="#FF0000"/>
<style name="def:underlined" background="#FFFFFF"/>
</style-scheme>
......@@ -104,7 +104,8 @@ check_scheme (GtkSourceStyleScheme *scheme,
const gchar *expected_name,
const gchar *expected_description,
const gchar **expected_authors,
const gchar *style_id)
const gchar *style_id,
const gchar *background_rgba)
{
GtkSourceStyle *style;
......@@ -115,6 +116,18 @@ check_scheme (GtkSourceStyleScheme *scheme,
style = gtk_source_style_scheme_get_style (scheme, style_id);
g_assert_true (GTK_SOURCE_IS_STYLE (style));
if (background_rgba != NULL)
{
gchar *background = NULL;
g_object_get (style,
"background", &background,
NULL);
g_assert_cmpstr (background_rgba, ==, background);
g_free (background);
}
}
static void
......@@ -125,7 +138,11 @@ test_scheme_properties (TestFixture *fixture,
const gchar *authors[] = { "Paolo Borelli", "John Doe", NULL};
scheme = gtk_source_style_scheme_manager_get_scheme (fixture->manager, "test");
check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:comment");
check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:comment", NULL);
/* Check that net-address remapped correctly to "underlined" */
check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:net-address", "#FFFFFF");
}
static void
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment