From 15458b5af3343e23f147cb2ace9cd6aa6f7dcffc Mon Sep 17 00:00:00 2001 From: Daniel Boles Date: Wed, 28 Jun 2023 14:40:06 +0100 Subject: [PATCH] gtk-demo/dnd: Fix, generalise detecting dark theme 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 | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/demos/gtk-demo/dnd.c b/demos/gtk-demo/dnd.c index 16c4091c0c..91ffd7f925 100644 --- a/demos/gtk-demo/dnd.c +++ b/demos/gtk-demo/dnd.c @@ -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; } -- 2.30.2