diff --git a/widget/gtk/nsLookAndFeel.cpp b/widget/gtk/nsLookAndFeel.cpp index 6fe5f0cbd5f0..d2e11a19672d 100644 --- a/widget/gtk/nsLookAndFeel.cpp +++ b/widget/gtk/nsLookAndFeel.cpp @@ -700,19 +700,9 @@ nsresult nsLookAndFeel::GetIntImpl(IntID aID, int32_t& aResult) { break; } case eIntID_SystemUsesDarkTheme: { - // It seems GTK doesn't have an API to query if the current theme is - // "light" or "dark", so we synthesize it from the CSS2 Window/WindowText - // colors instead, by comparing their luminosity. - nscolor fg, bg; - if (NS_SUCCEEDED(NativeGetColor(ColorID::Windowtext, fg)) && - NS_SUCCEEDED(NativeGetColor(ColorID::Window, bg))) { - aResult = (RelativeLuminanceUtils::Compute(bg) < - RelativeLuminanceUtils::Compute(fg)) - ? 1 - : 0; - break; - } - MOZ_FALLTHROUGH; + EnsureInit(); + aResult = mSystemUsesDarkTheme; + break; } default: aResult = 0; @@ -920,6 +910,17 @@ void nsLookAndFeel::EnsureInit() { GdkRGBA color; GtkStyleContext* style; + // It seems GTK doesn't have an API to query if the current theme is + // "light" or "dark", so we synthesize it from the CSS2 Window/WindowText + // colors instead, by comparing their luminosity. + GdkRGBA bg, fg; + style = GetStyleContext(MOZ_GTK_WINDOW); + gtk_style_context_get_background_color(style, GTK_STATE_FLAG_NORMAL, &bg); + gtk_style_context_get_color(style, GTK_STATE_FLAG_NORMAL, &fg); + mSystemUsesDarkTheme = + (RelativeLuminanceUtils::Compute(GDK_RGBA_TO_NS_RGBA(bg)) < + RelativeLuminanceUtils::Compute(GDK_RGBA_TO_NS_RGBA(fg))); + // Gtk manages a screen's CSS in the settings object so we // ask Gtk to create it explicitly. Otherwise we may end up // with wrong color theme, see Bug 972382 diff --git a/widget/gtk/nsLookAndFeel.h b/widget/gtk/nsLookAndFeel.h index 2943ae175282..eb73ff4cbae2 100644 --- a/widget/gtk/nsLookAndFeel.h +++ b/widget/gtk/nsLookAndFeel.h @@ -95,6 +95,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel { bool mCSDMinimizeButton = false; bool mCSDCloseButton = false; bool mCSDReversedPlacement = false; + bool mSystemUsesDarkTheme = false; bool mInitialized = false; void EnsureInit();