shithub: puzzles

Download patch

ref: 3d0c734e433972d6a9421ebd58763ca5bf1f1355
parent: 4c1e47b272274972556d7790384998e593d0ae57
author: Ben Hutchings <benh@debian.org>
date: Sun Feb 13 17:23:31 EST 2022

gtk: Do not override window background colour when using a dark theme

When the user chooses a global dark theme, the window background will
be dark and the default text colour light.  Overriding the window
background to be light grey can make the menu and status bars
unreadable.  In case a dark theme is used, only set the background
colour for the content area.

References: https://bugs.debian.org/944237

--- a/gtk.c
+++ b/gtk.c
@@ -2,6 +2,10 @@
  * gtk.c: GTK front end for my puzzle collection.
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1 /* for strcasestr */
+#endif
+
 #include <stdio.h>
 #include <assert.h>
 #include <stdlib.h>
@@ -389,6 +393,21 @@
 
 static void set_window_background(frontend *fe, int colour)
 {
+#if GTK_CHECK_VERSION(3,0,0)
+    /* In case the user's chosen theme is dark, we should not override
+     * the background colour for the whole window as this makes the
+     * menu and status bars unreadable. This might be visible through
+     * the gtk-application-prefer-dark-theme flag or else we have to
+     * work it out from the name. */
+    gboolean dark_theme = FALSE;
+    char *theme_name = NULL;
+    g_object_get(gtk_settings_get_default(),
+		 "gtk-application-prefer-dark-theme", &dark_theme,
+		 "gtk-theme-name", &theme_name,
+		 NULL);
+    if (theme_name && strcasestr(theme_name, "-dark"))
+	dark_theme = TRUE;
+    g_free(theme_name);
 #if GTK_CHECK_VERSION(3,20,0)
     char css_buf[512];
     sprintf(css_buf, ".background { "
@@ -401,15 +420,17 @@
     if (!gtk_css_provider_load_from_data(
             GTK_CSS_PROVIDER(fe->css_provider), css_buf, -1, NULL))
         assert(0 && "Couldn't load CSS");
+    if (!dark_theme) {
+	gtk_style_context_add_provider(
+	    gtk_widget_get_style_context(fe->window),
+	    GTK_STYLE_PROVIDER(fe->css_provider),
+	    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
     gtk_style_context_add_provider(
-        gtk_widget_get_style_context(fe->window),
-        GTK_STYLE_PROVIDER(fe->css_provider),
-        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-    gtk_style_context_add_provider(
         gtk_widget_get_style_context(fe->area),
         GTK_STYLE_PROVIDER(fe->css_provider),
         GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-#elif GTK_CHECK_VERSION(3,0,0)
+#else
     GdkRGBA rgba;
     rgba.red = fe->colours[3*colour + 0];
     rgba.green = fe->colours[3*colour + 1];
@@ -416,7 +437,10 @@
     rgba.blue = fe->colours[3*colour + 2];
     rgba.alpha = 1.0;
     gdk_window_set_background_rgba(gtk_widget_get_window(fe->area), &rgba);
-    gdk_window_set_background_rgba(gtk_widget_get_window(fe->window), &rgba);
+    if (!dark_theme)
+	gdk_window_set_background_rgba(gtk_widget_get_window(fe->window),
+				       &rgba);
+#endif
 #else
     GdkColormap *colmap;