Bug 1717873 - Factor PreferenceSheet colors to its own struct. r=morgan
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sat, 24 Jul 2021 21:10:44 +0000
changeset 586582 dde0d94e689a04c12f0b386069465688f775caf0
parent 586581 e22605c20197ae2061b24a599f4c7ee6f405303b
child 586583 7b4b180bf39349c1b5b3da7a3c708f23b4740db2
push id38641
push userabutkovits@mozilla.com
push dateSun, 25 Jul 2021 21:23:34 +0000
treeherdermozilla-central@3881437c0949 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmorgan
bugs1717873
milestone92.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1717873 - Factor PreferenceSheet colors to its own struct. r=morgan This will come handy in the next patch. Depends on D120678 Differential Revision: https://phabricator.services.mozilla.com/D120679
layout/base/nsPresContext.h
layout/style/GlobalStyleSheetCache.cpp
layout/style/PreferenceSheet.cpp
layout/style/PreferenceSheet.h
layout/style/nsStyleStruct.cpp
servo/components/style/gecko/media_queries.rs
servo/components/style/values/specified/color.rs
--- a/layout/base/nsPresContext.h
+++ b/layout/base/nsPresContext.h
@@ -341,17 +341,17 @@ class nsPresContext : public nsISupports
 
     return false;
   }
 
   const mozilla::PreferenceSheet::Prefs& PrefSheetPrefs() const {
     return mozilla::PreferenceSheet::PrefsFor(*mDocument);
   }
   nscolor DefaultBackgroundColor() const {
-    return PrefSheetPrefs().mDefaultBackgroundColor;
+    return PrefSheetPrefs().mColors.mDefaultBackground;
   }
 
   nsISupports* GetContainerWeak() const;
 
   nsDocShell* GetDocShell() const;
 
   /**
    * Get the visible area associated with this presentation context.
--- a/layout/style/GlobalStyleSheetCache.cpp
+++ b/layout/style/GlobalStyleSheetCache.cpp
@@ -623,18 +623,19 @@ void GlobalStyleSheetCache::BuildPrefere
     sheetText.AppendPrintf(
         "%s { outline: %dpx %s !important; }\n",
         focusRingOnAnything ? ":focus" : "*|*:link:focus, *|*:visited:focus",
         focusRingWidth,
         focusRingStyle == 0 ? "solid -moz-mac-focusring" : "dotted WindowText");
   }
 
   if (StaticPrefs::browser_display_use_focus_colors()) {
-    nscolor focusText = aPrefs.mFocusTextColor;
-    nscolor focusBG = aPrefs.mFocusBackgroundColor;
+    const auto& colors = aPrefs.mColors;
+    nscolor focusText = colors.mFocusText;
+    nscolor focusBG = colors.mFocusBackground;
     sheetText.AppendPrintf(
         "*:focus, *:focus > font { color: #%02x%02x%02x !important; "
         "background-color: #%02x%02x%02x !important; }\n",
         NS_GET_R_G_B(focusText), NS_GET_R_G_B(focusBG));
   }
 
   NS_ASSERTION(sheetText.Length() <= kPreallocSize,
                "kPreallocSize should be big enough to build preference style "
--- a/layout/style/PreferenceSheet.cpp
+++ b/layout/style/PreferenceSheet.cpp
@@ -83,59 +83,59 @@ void PreferenceSheet::Prefs::Load(bool a
   mIsChrome = aIsChrome;
   mUseAccessibilityTheme = UseAccessibilityTheme(aIsChrome);
 
   const bool useStandins = nsContentUtils::UseStandinsForNativeColors();
   const bool usePrefColors = !useStandins && !aIsChrome &&
                              !mUseAccessibilityTheme &&
                              !StaticPrefs::browser_display_use_system_colors();
   if (usePrefColors) {
-    GetColor("browser.display.background_color", mDefaultBackgroundColor);
-    GetColor("browser.display.foreground_color", mDefaultColor);
-    GetColor("browser.anchor_color", mLinkColor);
+    GetColor("browser.display.background_color", mColors.mDefaultBackground);
+    GetColor("browser.display.foreground_color", mColors.mDefault);
+    GetColor("browser.anchor_color", mColors.mLink);
   } else {
     using ColorID = LookAndFeel::ColorID;
     const auto standins = LookAndFeel::UseStandins(useStandins);
     // TODO(emilio): In the future we probably want to keep both sets of colors
     // around or something.
     //
     // FIXME(emilio): Why do we look at a different set of colors when using
     // standins vs. not?
     const auto scheme = LookAndFeel::ColorScheme::Light;
-    mDefaultColor = LookAndFeel::Color(
+    mColors.mDefault = LookAndFeel::Color(
         useStandins ? ColorID::Windowtext : ColorID::WindowForeground, scheme,
-        standins, mDefaultColor);
-    mDefaultBackgroundColor = LookAndFeel::Color(
+        standins, mColors.mDefault);
+    mColors.mDefaultBackground = LookAndFeel::Color(
         useStandins ? ColorID::Window : ColorID::WindowBackground, scheme,
-        standins, mDefaultBackgroundColor);
-    mLinkColor = LookAndFeel::Color(ColorID::MozNativehyperlinktext, scheme,
-                                    standins, mLinkColor);
+        standins, mColors.mDefaultBackground);
+    mColors.mLink = LookAndFeel::Color(ColorID::MozNativehyperlinktext, scheme,
+                                    standins, mColors.mLink);
   }
 
   if (mUseAccessibilityTheme && !useStandins) {
-    mActiveLinkColor = mLinkColor;
+    mColors.mActiveLink = mColors.mLink;
     // Visited link color is produced by preserving the foreground's green
     // and averaging the foreground and background for the red and blue.
     // This is how IE and Edge do it too.
-    mVisitedLinkColor = NS_RGB(
-        AVG2(NS_GET_R(mDefaultColor), NS_GET_R(mDefaultBackgroundColor)),
-        NS_GET_G(mDefaultColor),
-        AVG2(NS_GET_B(mDefaultColor), NS_GET_B(mDefaultBackgroundColor)));
+    mColors.mVisitedLink = NS_RGB(
+        AVG2(NS_GET_R(mColors.mDefault), NS_GET_R(mColors.mDefaultBackground)),
+        NS_GET_G(mColors.mDefault),
+        AVG2(NS_GET_B(mColors.mDefault), NS_GET_B(mColors.mDefaultBackground)));
   } else {
-    GetColor("browser.active_color", mActiveLinkColor);
-    GetColor("browser.visited_color", mVisitedLinkColor);
+    GetColor("browser.active_color", mColors.mActiveLink);
+    GetColor("browser.visited_color", mColors.mVisitedLink);
   }
 
-  GetColor("browser.display.focus_text_color", mFocusTextColor);
-  GetColor("browser.display.focus_background_color", mFocusBackgroundColor);
+  GetColor("browser.display.focus_text_color", mColors.mFocusText);
+  GetColor("browser.display.focus_background_color", mColors.mFocusBackground);
 
   // Wherever we got the default background color from, ensure it is
   // opaque.
-  mDefaultBackgroundColor =
-      NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF), mDefaultBackgroundColor);
+  mColors.mDefaultBackground =
+      NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF), mColors.mDefaultBackground);
   mUseDocumentColors = UseDocumentColors(aIsChrome, mUseAccessibilityTheme);
 }
 
 void PreferenceSheet::Initialize() {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(!sInitialized);
 
   sInitialized = true;
@@ -159,19 +159,19 @@ void PreferenceSheet::Initialize() {
   Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_THEME, useDocumentColorPref,
                        sContentPrefs.mUseAccessibilityTheme);
   if (!sContentPrefs.mUseDocumentColors) {
     // If a user has chosen to override doc colors through OS HCM or our HCM,
     // we should log the user's current forground (text) color and background
     // color. Note, the document color use pref is the inverse of the HCM
     // dropdown option in preferences.
     Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_HCM_FOREGROUND,
-                         sContentPrefs.mDefaultColor);
+                         sContentPrefs.mColors.mDefault);
     Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_HCM_BACKGROUND,
-                         sContentPrefs.mDefaultBackgroundColor);
+                         sContentPrefs.mColors.mDefaultBackground);
   }
 
   Telemetry::ScalarSet(Telemetry::ScalarID::A11Y_BACKPLATE,
                        StaticPrefs::browser_display_permit_backplate());
 }
 
 }  // namespace mozilla
 
--- a/layout/style/PreferenceSheet.h
+++ b/layout/style/PreferenceSheet.h
@@ -14,27 +14,27 @@
 namespace mozilla {
 
 namespace dom {
 class Document;
 }
 
 struct PreferenceSheet {
   struct Prefs {
-    nscolor mLinkColor = NS_RGB(0x00, 0x00, 0xEE);
-    nscolor mActiveLinkColor = NS_RGB(0xEE, 0x00, 0x00);
-    nscolor mVisitedLinkColor = NS_RGB(0x55, 0x1A, 0x8B);
+    struct {
+      nscolor mLink = NS_RGB(0x00, 0x00, 0xEE);
+      nscolor mActiveLink = NS_RGB(0xEE, 0x00, 0x00);
+      nscolor mVisitedLink = NS_RGB(0x55, 0x1A, 0x8B);
 
-    nscolor mDefaultColor = NS_RGB(0, 0, 0);
-    nscolor mDefaultBackgroundColor = NS_RGB(0xFF, 0xFF, 0xFF);
+      nscolor mDefault = NS_RGB(0, 0, 0);
+      nscolor mDefaultBackground = NS_RGB(0xFF, 0xFF, 0xFF);
 
-    nscolor mLinkBackgroundColor = mDefaultBackgroundColor;
-
-    nscolor mFocusTextColor = mDefaultColor;
-    nscolor mFocusBackgroundColor = mDefaultBackgroundColor;
+      nscolor mFocusText = mDefault;
+      nscolor mFocusBackground = mDefaultBackground;
+    } mColors;
 
     bool mIsChrome = false;
     bool mUseAccessibilityTheme = false;
 
     bool mUseDocumentColors = true;
 
     // Whether the non-native theme should use system colors for widgets.
     // We only do that if we have a high-contrast theme _and_ we are overriding
--- a/layout/style/nsStyleStruct.cpp
+++ b/layout/style/nsStyleStruct.cpp
@@ -2878,17 +2878,17 @@ nsChangeHint nsStyleTextReset::CalcDiffe
 }
 
 // --------------------
 // nsStyleText
 //
 
 static StyleRGBA DefaultColor(const Document& aDocument) {
   return StyleRGBA::FromColor(
-      PreferenceSheet::PrefsFor(aDocument).mDefaultColor);
+      PreferenceSheet::PrefsFor(aDocument).mColors.mDefault);
 }
 
 nsStyleText::nsStyleText(const Document& aDocument)
     : mColor(DefaultColor(aDocument)),
       mTextTransform(StyleTextTransform::None()),
       mTextAlign(StyleTextAlign::Start),
       mTextAlignLast(StyleTextAlignLast::Auto),
       mTextJustify(StyleTextJustify::Auto),
--- a/servo/components/style/gecko/media_queries.rs
+++ b/servo/components/style/gecko/media_queries.rs
@@ -84,17 +84,17 @@ impl Device {
     pub fn new(document: *const structs::Document) -> Self {
         assert!(!document.is_null());
         let doc = unsafe { &*document };
         let prefs = unsafe { &*bindings::Gecko_GetPrefSheetPrefs(doc) };
         Device {
             document,
             default_values: ComputedValues::default_values(doc),
             root_font_size: AtomicU32::new(FONT_MEDIUM_PX.to_bits()),
-            body_text_color: AtomicUsize::new(prefs.mDefaultColor as usize),
+            body_text_color: AtomicUsize::new(prefs.mColors.mDefault as usize),
             used_root_font_size: AtomicBool::new(false),
             used_viewport_size: AtomicBool::new(false),
             environment: CssEnvironment,
         }
     }
 
     /// Get the relevant environment to resolve `env()` functions.
     #[inline]
@@ -310,22 +310,22 @@ impl Device {
         if doc.mIsBeingUsedAsImage() {
             return true;
         }
         self.pref_sheet_prefs().mUseDocumentColors
     }
 
     /// Returns the default background color.
     pub fn default_background_color(&self) -> RGBA {
-        convert_nscolor_to_rgba(self.pref_sheet_prefs().mDefaultBackgroundColor)
+        convert_nscolor_to_rgba(self.pref_sheet_prefs().mColors.mDefaultBackground)
     }
 
     /// Returns the default foreground color.
     pub fn default_color(&self) -> RGBA {
-        convert_nscolor_to_rgba(self.pref_sheet_prefs().mDefaultColor)
+        convert_nscolor_to_rgba(self.pref_sheet_prefs().mColors.mDefault)
     }
 
     /// Returns the current effective text zoom.
     #[inline]
     fn effective_text_zoom(&self) -> f32 {
         let pc = match self.pres_context() {
             Some(pc) => pc,
             None => return 1.,
--- a/servo/components/style/values/specified/color.rs
+++ b/servo/components/style/values/specified/color.rs
@@ -395,24 +395,24 @@ pub enum SystemColor {
 }
 
 #[cfg(feature = "gecko")]
 impl SystemColor {
     #[inline]
     fn compute(&self, cx: &Context, scheme: SystemColorScheme) -> ComputedColor {
         use crate::gecko_bindings::bindings;
 
-        let prefs = cx.device().pref_sheet_prefs();
+        let colors = &cx.device().pref_sheet_prefs().mColors;
 
         convert_nscolor_to_computedcolor(match *self {
-            SystemColor::Canvastext => prefs.mDefaultColor,
-            SystemColor::Canvas => prefs.mDefaultBackgroundColor,
-            SystemColor::Linktext => prefs.mLinkColor,
-            SystemColor::Activetext => prefs.mActiveLinkColor,
-            SystemColor::Visitedtext => prefs.mVisitedLinkColor,
+            SystemColor::Canvastext => colors.mDefault,
+            SystemColor::Canvas => colors.mDefaultBackground,
+            SystemColor::Linktext => colors.mLink,
+            SystemColor::Activetext => colors.mActiveLink,
+            SystemColor::Visitedtext => colors.mVisitedLink,
 
             _ => {
                 let color = unsafe {
                     bindings::Gecko_GetLookAndFeelSystemColor(*self as i32, cx.device().document(), scheme)
                 };
                 if color == bindings::NS_SAME_AS_FOREGROUND_COLOR {
                     return ComputedColor::currentcolor();
                 }