From cefa63783a99136f18272e8dd7bcd27c9c1907a3 Mon Sep 17 00:00:00 2001 From: Henry Wong Date: Sun, 28 Sep 2025 22:26:49 -0700 Subject: [PATCH] Fix CSS tweaking when theme contrast is not default (< 10) sp_tweak_background_colors: background-image rules were rewritten with an extra image() in the value. Removing the extra image() fixes the red background on affected UI elements (mainly buttons) when contrast is set to < 10. ThemeContext::add_gtk_css: Replace some nullptrs with empty strings to avoid assertion failures. The above fixes https://gitlab.com/inkscape/inkscape/-/issues/5434 ThemeContext::add_gtk_css: When loading the CSS for a theme, the variant should be set by whether the current theme is actually dark, not whether it's preferred dark. Fixes broken colours when contrast < 10 and the actual darkness of the theme does not match the preferDarkTheme setting (for example, when the theme only has a dark option regardless of what the user prefers). --- src/ui/themes.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/ui/themes.cpp b/src/ui/themes.cpp index ecb2efb3a4..76bc4890c9 100644 --- a/src/ui/themes.cpp +++ b/src/ui/themes.cpp @@ -204,10 +204,10 @@ std::string sp_tweak_background_colors(std::string cssstring, double crossfade, } else if (cssstring.find("background-image") != std::string::npos) { if (dark) { contrast = std::clamp((int)((contrast) * 27), 0, 100); - sub = "background-image:cross-fade(" + Inkscape::ustring::format_classic(contrast) + "% image(rgb(255,255,255)), image($2));"; + sub = "background-image:cross-fade(" + Inkscape::ustring::format_classic(contrast) + "% image(rgb(255,255,255)), $2);"; } else { contrast = std::clamp((int)((contrast) * 90), 0 , 100); - sub = "background-image:cross-fade(" + Inkscape::ustring::format_classic(contrast) + "% image(rgb(0,0,0)), image($2));"; + sub = "background-image:cross-fade(" + Inkscape::ustring::format_classic(contrast) + "% image(rgb(0,0,0)), $2);"; } cssstring = std::regex_replace(cssstring, re_image, sub); } @@ -253,18 +253,19 @@ void ThemeContext::add_gtk_css(bool only_providers, bool cached) // Add style sheet (GTK3) auto const display = Gdk::Display::get_default(); Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - gchar *gtkThemeName = nullptr; - gchar *gtkIconThemeName = nullptr; Glib::ustring themeiconname; - gboolean gtkApplicationPreferDarkTheme; GtkSettings *settings = gtk_settings_get_default(); if (settings && !only_providers) { + gchar *gtkThemeName = nullptr; + gchar *gtkIconThemeName = nullptr; + gboolean gtkApplicationPreferDarkTheme = false; + g_object_get(settings, "gtk-icon-theme-name", >kIconThemeName, nullptr); g_object_get(settings, "gtk-theme-name", >kThemeName, nullptr); g_object_get(settings, "gtk-application-prefer-dark-theme", >kApplicationPreferDarkTheme, nullptr); prefs->setBool("/theme/defaultPreferDarkTheme", gtkApplicationPreferDarkTheme); - prefs->setString("/theme/defaultGtkTheme", Glib::ustring(gtkThemeName)); - prefs->setString("/theme/defaultIconTheme", Glib::ustring(gtkIconThemeName)); + prefs->setString("/theme/defaultGtkTheme", Glib::ustring(gtkThemeName ? gtkThemeName : "")); + prefs->setString("/theme/defaultIconTheme", Glib::ustring(gtkIconThemeName ? gtkIconThemeName : "")); Glib::ustring gtkthemename = prefs->getString("/theme/gtkTheme"); if (gtkthemename != "") { g_object_set(settings, "gtk-theme-name", gtkthemename.c_str(), nullptr); @@ -275,10 +276,11 @@ void ThemeContext::add_gtk_css(bool only_providers, bool cached) if (themeiconname != "") { g_object_set(settings, "gtk-icon-theme-name", themeiconname.c_str(), nullptr); } + + g_free(gtkThemeName); + g_free(gtkIconThemeName); } - g_free(gtkThemeName); - g_free(gtkIconThemeName); int themecontrast = prefs->getInt("/theme/contrast", 10); if (!_contrastthemeprovider) { @@ -292,11 +294,8 @@ void ThemeContext::add_gtk_css(bool only_providers, bool cached) Glib::ustring css_contrast = ""; double contrast = (10 - themecontrast) / 30.0; double shade = 1 - contrast; - const gchar *variant = nullptr; - if (prefs->getBool("/theme/preferDarkTheme", false)) { - variant = "dark"; - } bool dark = prefs->getBool("/theme/darkTheme", false); + const gchar *variant = dark ? "dark" : ""; if (dark) { contrast *= 2.5; shade = 1 + contrast; -- GitLab