Bug 1555565 - Export correct user's preference for dark theme by prefers-color-scheme media query, r=mats a=jcristau
authorMartin Stransky <stransky@redhat.com>
Mon, 10 Jun 2019 09:43:27 +0000
changeset 537006 2edcae33cd6a9755aee477a868ccd4dff299eb35
parent 537005 080ef15620cf31461faf8ad853561c49c3dfe373
child 537007 ca5607bbcad3ed768cd576cebcc6b1f69b3e624c
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmats, jcristau
bugs1555565
milestone68.0
Bug 1555565 - Export correct user's preference for dark theme by prefers-color-scheme media query, r=mats a=jcristau Differential Revision: https://phabricator.services.mozilla.com/D33133
widget/gtk/nsLookAndFeel.cpp
widget/gtk/nsLookAndFeel.h
--- a/widget/gtk/nsLookAndFeel.cpp
+++ b/widget/gtk/nsLookAndFeel.cpp
@@ -695,29 +695,19 @@ nsresult nsLookAndFeel::GetIntImpl(IntID
 
       settings = gtk_settings_get_default();
       g_object_get(settings, "gtk-enable-animations", &enableAnimations,
                    nullptr);
       aResult = enableAnimations ? 0 : 1;
       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;
       res = NS_ERROR_FAILURE;
   }
 
   return res;
 }
@@ -833,34 +823,52 @@ bool nsLookAndFeel::GetFontImpl(FontID a
 
   return true;
 }
 
 void nsLookAndFeel::EnsureInit() {
   GdkColor colorValue;
   GdkColor* colorValuePtr;
 
-  if (mInitialized) return;
+  if (mInitialized) {
+    return;
+  }
+
+  // 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
+  GdkScreen* screen = gdk_screen_get_default();
+  if (MOZ_UNLIKELY(!screen)) {
+    NS_WARNING("EnsureInit: No screen");
+    return;
+  }
+  GtkSettings* settings = gtk_settings_get_for_screen(screen);
+  if (MOZ_UNLIKELY(!settings)) {
+    NS_WARNING("EnsureInit: No settings");
+    return;
+  }
+
   mInitialized = true;
 
   // gtk does non threadsafe refcounting
   MOZ_ASSERT(NS_IsMainThread());
 
   GdkRGBA color;
   GtkStyleContext* style;
 
-  // 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
-  GtkSettings* settings = gtk_settings_get_for_screen(gdk_screen_get_default());
-
-  if (MOZ_UNLIKELY(!settings)) {
-    NS_WARNING("EnsureInit: No settings");
-    return;
-  }
+  // 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)));
 
   // Dark themes interacts poorly with widget styling (see bug 1216658).
   // We disable dark themes by default for all processes (chrome, web content)
   // but allow user to overide it by prefs.
   const gchar* dark_setting = "gtk-application-prefer-dark-theme";
   gboolean darkThemeDefault;
   g_object_get(settings, dark_setting, &darkThemeDefault, nullptr);
 
--- a/widget/gtk/nsLookAndFeel.h
+++ b/widget/gtk/nsLookAndFeel.h
@@ -90,16 +90,17 @@ class nsLookAndFeel final : public nsXPL
   int32_t mCaretBlinkTime = 0;
   bool mMenuSupportsDrag = false;
   bool mCSDAvailable = false;
   bool mCSDHideTitlebarByDefault = false;
   bool mCSDMaximizeButton = false;
   bool mCSDMinimizeButton = false;
   bool mCSDCloseButton = false;
   bool mCSDReversedPlacement = false;
+  bool mSystemUsesDarkTheme = false;
   bool mInitialized = false;
 
   void EnsureInit();
 
  private:
   nsresult InitCellHighlightColors();
 };