[gtksourceview] stylescheme: add work around for broken selections



commit 2d3d617fc0f8238182dcb45547efd0006e518da1
Author: Christian Hergert <chergert redhat com>
Date:   Thu Dec 9 02:15:30 2021 -0800

    stylescheme: add work around for broken selections
    
    Some selections in the wild have a background but no foreground set. For
    those we must override the background because some GTK themes no longer
    draw the foreground text (such as libadwaita).

 gtksourceview/gtksourcestylescheme.c | 43 +++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)
---
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index 7bcd0b72..aa2c7766 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -33,7 +33,7 @@
 
 /**
  * GtkSourceStyleScheme:
- * 
+ *
  * Controls the appearance of [class@View].
  *
  * #GtkSourceStyleScheme contains all the text styles to be used in
@@ -860,6 +860,43 @@ get_css_color_style (GtkSourceStyle *style,
        }
 }
 
+static void
+fix_broken_selection (char **bg_out,
+                     char **text_out)
+{
+       char *bg = *bg_out;
+       char *text = *text_out;
+       char *tmp = NULL;
+       const char *space;
+       const char *semi;
+       GdkRGBA rgba;
+
+       /* If there is no foreground and the background is solid, we must
+        * alter it to be transparent or some systems will not see anything
+        * (such as those in libadwaita).
+        */
+
+       if ((space = strchr (bg, ' ')) &&
+           (semi = strchr (space, ';')) &&
+           (tmp = g_strndup (space + 1, semi - space - 1)) &&
+           gdk_rgba_parse (&rgba, tmp) &&
+           rgba.alpha >= 1.0)
+       {
+               char *new_bg;
+               rgba.alpha = .3;
+               new_bg = gdk_rgba_to_string (&rgba);
+               g_free (bg);
+               text = g_strdup ("color: rgba(0,0,0,0);");
+               bg = g_strdup_printf ("background-color: %s;", new_bg);
+               g_free (new_bg);
+       }
+
+       g_free (tmp);
+
+       *bg_out = bg;
+       *text_out = text;
+}
+
 static void
 append_css_style (GString        *string,
                   GtkSourceStyle *style,
@@ -873,8 +910,12 @@ append_css_style (GString        *string,
                "}\n";
 
        get_css_color_style (style, &bg, &text);
+
        if (bg || text)
        {
+               if (bg && !text && g_str_has_suffix (selector, " selection"))
+                       fix_broken_selection (&bg, &text);
+
                g_string_append_printf (string, css_style, selector,
                                        bg != NULL ? bg : "",
                                        text != NULL ? text : "");


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