[gtksourceview] map: improve fallback map overlay styling



commit 53b421105ebb46f4ba7a2962ae4f7394f6de4a38
Author: Christian Hergert <chergert redhat com>
Date:   Sat Apr 9 13:22:05 2022 -0700

    map: improve fallback map overlay styling
    
    Rather than hardcoding specialized values in fallback schemes, we should
    generate better values automatically as some schemes in the whild will be
    missing these values anyway.
    
    Fixes #260

 data/styles/classic-dark.xml |  1 -
 gtksourceview/gtksourcemap.c | 64 ++++++++++++++++++++++++++++----------------
 2 files changed, 41 insertions(+), 24 deletions(-)
---
diff --git a/data/styles/classic-dark.xml b/data/styles/classic-dark.xml
index de891506..03decded 100644
--- a/data/styles/classic-dark.xml
+++ b/data/styles/classic-dark.xml
@@ -53,7 +53,6 @@
   <style name="draw-spaces"                 foreground="text2"/>
   <style name="background-pattern"          background="#rgba(73,74,71,0.1)"/>
   <style name="snippet-focus"               background="gray"/>
-  <style name="map-overlay"                 background="gray"/>
 
   <!-- Bracket Matching -->
   <style name="bracket-match"               foreground="text2" background="gray"/>
diff --git a/gtksourceview/gtksourcemap.c b/gtksourceview/gtksourcemap.c
index 998ba843..d3c10ce9 100644
--- a/gtksourceview/gtksourcemap.c
+++ b/gtksourceview/gtksourcemap.c
@@ -312,6 +312,8 @@ gtk_source_map_rebuild_css (GtkSourceMap *map)
        GtkSourceStyleScheme *style_scheme;
        GtkSourceStyle *style = NULL;
        GtkTextBuffer *buffer;
+       const char *color;
+       gboolean use_fg = FALSE;
        GString *gstr;
        char *background = NULL;
        char *foreground = NULL;
@@ -366,33 +368,40 @@ gtk_source_map_rebuild_css (GtkSourceMap *map)
        if (style_scheme != NULL)
        {
                if (!(style = gtk_source_style_scheme_get_style (style_scheme, "map-overlay")) &&
-                   !(style = gtk_source_style_scheme_get_style (style_scheme, "selection")) &&
-                   !(style = gtk_source_style_scheme_get_style (style_scheme, "current-line")))
+                   !(style = gtk_source_style_scheme_get_style (style_scheme, "selection")))
                {
-                       /* Do Nothing */
+                       /* Use the foreground color if we can as that will get lightened to
+                        * .25 alpha below so that we have *something* rather dark compared
+                        * to the background color. Otherwise it will get washed out like it
+                        * does with classic.xml.
+                        */
+                       if ((style = gtk_source_style_scheme_get_style (style_scheme, "text")))
+                       {
+                               use_fg = TRUE;
+                       }
                }
        }
 
        if (style != NULL)
        {
-               gboolean foreground_set;
                gboolean background_set;
+               gboolean foreground_set;
 
                g_object_get (style,
-                             "foreground", &foreground,
-                             "foreground-set", &foreground_set,
                              "background", &background,
                              "background-set", &background_set,
+                             "foreground", &foreground,
+                             "foreground-set", &foreground_set,
                              NULL);
 
-               if (!foreground_set)
+               if (!background_set)
                {
-                       g_clear_pointer (&foreground, g_free);
+                       g_clear_pointer (&background, g_free);
                }
 
-               if (!background_set)
+               if (!foreground_set)
                {
-                       g_clear_pointer (&background, g_free);
+                       g_clear_pointer (&foreground, g_free);
                }
        }
        else
@@ -406,22 +415,10 @@ gtk_source_map_rebuild_css (GtkSourceMap *map)
                }
        }
 
-       priv->had_color = background != NULL;
-
        if (background != NULL)
        {
                GdkRGBA parsed;
 
-               if (foreground == NULL)
-               {
-                       GtkStyleContext *style_context;
-                       GdkRGBA color;
-
-                       style_context = gtk_widget_get_style_context (GTK_WIDGET (map));
-                       gtk_style_context_get_color (style_context, &color);
-                       foreground = gdk_rgba_to_string (&color);
-               }
-
                if (gdk_rgba_parse (&parsed, background))
                {
                        if (parsed.alpha < 1.0)
@@ -431,7 +428,28 @@ gtk_source_map_rebuild_css (GtkSourceMap *map)
                                background = gdk_rgba_to_string (&parsed);
                        }
                }
+       }
+
+       if (foreground != NULL)
+       {
+               GdkRGBA parsed;
+
+               if (gdk_rgba_parse (&parsed, foreground))
+               {
+                       if (parsed.alpha < 1.0)
+                       {
+                               parsed.alpha = 1.0;
+                               g_free (foreground);
+                               foreground = gdk_rgba_to_string (&parsed);
+                       }
+               }
+       }
 
+       color = use_fg ? foreground : background;
+       priv->had_color = color != NULL;
+
+       if (color != NULL)
+       {
                g_string_append_printf (gstr,
                                        "slider {"
                                        " background-color: alpha(%s,.25);"
@@ -443,7 +461,7 @@ gtk_source_map_rebuild_css (GtkSourceMap *map)
                                        "slider.dragging:hover {"
                                        " background-color: alpha(%s,.5);"
                                        "}\n",
-                                       background, background, background);
+                                       color, color, color);
        }
 
        g_free (background);


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