gtk-demo/dnd: Fix, generalise detecting dark theme
authorDaniel Boles <dboles.src@gmail.com>
Wed, 28 Jun 2023 13:40:06 +0000 (14:40 +0100)
committerDaniel Boles <dboles.src@gmail.com>
Wed, 28 Jun 2023 13:40:06 +0000 (14:40 +0100)
Our default theme is now Default, not Adwaita, & HighContrastInverse was
renamed to Default-hc. So these checks did not work anymore. Rather than
hard-coding the new names, & possibly running into the same issue again,
we can just look for the convention of appending -dark to the theme name
and/or the Settings:prefer-dark-theme prop. The latter, we can & likely
SHOULD also apply to all themes - not just ours as before. We also check
for the :dark suffix as that means the theme variant - & before checking
GtkSettings check the GTK_THEME env var, just as GtkSettings itself does

demos/gtk-demo/dnd.c

index 16c4091c0c6e7aebab4979f642ccd2dfb2bdfa02..91ffd7f925e9fdcd86f8a698ee1c94f568eadbda 100644 (file)
@@ -162,27 +162,39 @@ click_done (GtkGesture *gesture)
     gtk_widget_insert_after (item, canvas, last_child);
 }
 
+/* GtkSettings treats `GTK_THEME=foo:dark` as theme name `foo`, variant `dark`,
+ * and our embedded CSS files let `foo-dark` work as an alias for `foo:dark`. */
+static gboolean
+has_dark_suffix (const char *theme)
+{
+  return g_str_has_suffix (theme, ":dark") ||
+         g_str_has_suffix (theme, "-dark");
+}
+
+/* So we can make a good guess whether the current theme is dark by checking for
+ * either: it is suffixed `[:-]dark`, or Settings:…prefer-dark-theme is TRUE. */
 static gboolean
 theme_is_dark (void)
 {
+  const char *env_theme;
   GtkSettings *settings;
   char *theme;
   gboolean prefer_dark;
   gboolean dark;
 
+  /* Like GtkSettings, 1st see if theme is overridden by environment variable */
+  env_theme = g_getenv ("GTK_THEME");
+  if (env_theme != NULL)
+    return has_dark_suffix (env_theme);
+
+  /* If not, test Settings:…theme-name in the same way OR :…prefer-dark-theme */
   settings = gtk_settings_get_default ();
   g_object_get (settings,
                 "gtk-theme-name", &theme,
                 "gtk-application-prefer-dark-theme", &prefer_dark,
                 NULL);
-
-  if ((strcmp (theme, "Adwaita") == 0 && prefer_dark) || strcmp (theme, "HighContrastInverse") == 0)
-    dark = TRUE;
-  else
-    dark = FALSE;
-
+  dark = prefer_dark || has_dark_suffix (theme);
   g_free (theme);
-
   return dark;
 }