From 0f6179ec9b12fe416e7adbb5bec41267dbc58213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Wilmet?= Date: Sat, 31 Aug 2024 19:10:18 +0200 Subject: [PATCH] StyleSchemeCss: more robust code for cursors CSS Retrieving the background_color can fail. --- gtksourceview/gtksourcestyleschemecss.c | 63 ++++++++++++++----------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/gtksourceview/gtksourcestyleschemecss.c b/gtksourceview/gtksourcestyleschemecss.c index 8e2e734cb..355e31b52 100644 --- a/gtksourceview/gtksourcestyleschemecss.c +++ b/gtksourceview/gtksourcestyleschemecss.c @@ -247,7 +247,6 @@ get_cursors_css (GtkSourceStyleScheme *scheme, gboolean secondary_color_set; GdkRGBA primary_color; GdkRGBA secondary_color; - gchar *secondary_color_str; GString *css; primary_color_set = get_style_foreground_color (scheme, "cursor", &primary_color); @@ -258,23 +257,10 @@ get_cursors_css (GtkSourceStyleScheme *scheme, return NULL; } - css = g_string_new ("textview text {\n"); - - if (primary_color_set) - { - gchar *primary_color_str; - - primary_color_str = gdk_rgba_to_string (&primary_color); - g_string_append_printf (css, - "\tcaret-color: %s;\n", - primary_color_str); - g_free (primary_color_str); - } - if (!secondary_color_set) { GtkStyleContext *context; - GdkRGBA *background_color; + GdkRGBA *background_color = NULL; g_assert (primary_color_set); @@ -290,22 +276,45 @@ get_cursors_css (GtkSourceStyleScheme *scheme, gtk_style_context_restore (context); - /* Blend primary cursor color with background color. */ - secondary_color.red = (primary_color.red + background_color->red) * 0.5; - secondary_color.green = (primary_color.green + background_color->green) * 0.5; - secondary_color.blue = (primary_color.blue + background_color->blue) * 0.5; - secondary_color.alpha = (primary_color.alpha + background_color->alpha) * 0.5; + if (background_color != NULL) + { + /* Blend primary cursor color with background color. */ + secondary_color.red = (primary_color.red + background_color->red) * 0.5; + secondary_color.green = (primary_color.green + background_color->green) * 0.5; + secondary_color.blue = (primary_color.blue + background_color->blue) * 0.5; + secondary_color.alpha = (primary_color.alpha + background_color->alpha) * 0.5; + + secondary_color_set = TRUE; - gdk_rgba_free (background_color); + gdk_rgba_free (background_color); + } } - secondary_color_str = gdk_rgba_to_string (&secondary_color); - g_string_append_printf (css, - "\t-gtk-secondary-caret-color: %s;\n", - secondary_color_str); - g_free (secondary_color_str); + css = g_string_new ("textview text {\n"); + + if (primary_color_set) + { + gchar *primary_color_str; + + primary_color_str = gdk_rgba_to_string (&primary_color); + g_string_append_printf (css, + "\tcaret-color: %s;\n", + primary_color_str); + g_free (primary_color_str); + } + + if (secondary_color_set) + { + gchar *secondary_color_str; + + secondary_color_str = gdk_rgba_to_string (&secondary_color); + g_string_append_printf (css, + "\t-gtk-secondary-caret-color: %s;\n", + secondary_color_str); + g_free (secondary_color_str); + } - g_string_append_printf (css, "}\n"); + g_string_append (css, "}\n"); return g_string_free (css, FALSE); } -- GitLab