Bug 1732409 make getUserMedia() permission prompt requirement independent of "media...
[gecko.git] / widget / ThemeColors.h
blobced6f2856c072824f62cd2a23e8523944cba819b
1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_widget_ThemeColors_h
7 #define mozilla_widget_ThemeColors_h
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/gfx/Types.h"
11 #include "mozilla/LookAndFeel.h"
12 #include "nsIFrame.h"
14 namespace mozilla::widget {
16 static constexpr gfx::sRGBColor sDefaultAccent(
17 gfx::sRGBColor::UnusualFromARGB(0xff0060df)); // Luminance: 13.69346%
18 static constexpr gfx::sRGBColor sDefaultAccentForeground(
19 gfx::sRGBColor::OpaqueWhite());
21 class ThemeAccentColor {
22 protected:
23 using sRGBColor = mozilla::gfx::sRGBColor;
24 using ComputedStyle = mozilla::ComputedStyle;
26 Maybe<nscolor> mAccentColor;
28 public:
29 explicit ThemeAccentColor(const ComputedStyle& aStyle);
30 virtual ~ThemeAccentColor() = default;
32 sRGBColor Get() const;
33 sRGBColor GetForeground() const;
34 sRGBColor GetLight() const;
35 sRGBColor GetDark() const;
36 sRGBColor GetDarker() const;
39 // Widget color information associated to a particular frame.
40 class ThemeColors {
41 protected:
42 using Document = mozilla::dom::Document;
43 using sRGBColor = mozilla::gfx::sRGBColor;
44 using LookAndFeel = mozilla::LookAndFeel;
45 using StyleSystemColor = mozilla::StyleSystemColor;
46 using AccentColor = ThemeAccentColor;
48 const AccentColor mAccentColor;
49 const Document& mDoc;
50 const bool mHighContrast;
51 const LookAndFeel::ColorScheme mColorScheme;
53 public:
54 explicit ThemeColors(const nsIFrame* aFrame)
55 : mAccentColor(*aFrame->Style()),
56 mDoc(*aFrame->PresContext()->Document()),
57 mHighContrast(ShouldBeHighContrast(*aFrame->PresContext())),
58 mColorScheme(LookAndFeel::ColorSchemeForFrame(aFrame)) {}
59 virtual ~ThemeColors() = default;
61 [[nodiscard]] static float ScaleLuminanceBy(float aLuminance, float aFactor) {
62 return aLuminance >= 0.18f ? aLuminance * aFactor : aLuminance / aFactor;
65 const AccentColor& Accent() const { return mAccentColor; }
66 bool HighContrast() const { return mHighContrast; }
67 bool IsDark() const { return mColorScheme == LookAndFeel::ColorScheme::Dark; }
69 nscolor SystemNs(StyleSystemColor aColor) const {
70 return LookAndFeel::Color(aColor, mColorScheme,
71 LookAndFeel::ShouldUseStandins(mDoc, aColor));
74 sRGBColor System(StyleSystemColor aColor) const {
75 return sRGBColor::FromABGR(SystemNs(aColor));
78 template <typename Compute>
79 sRGBColor SystemOrElse(StyleSystemColor aColor, Compute aCompute) const {
80 if (auto color = LookAndFeel::GetColor(
81 aColor, mColorScheme,
82 LookAndFeel::ShouldUseStandins(mDoc, aColor))) {
83 return sRGBColor::FromABGR(*color);
85 return aCompute();
88 std::pair<sRGBColor, sRGBColor> SystemPair(StyleSystemColor aFirst,
89 StyleSystemColor aSecond) const {
90 return std::make_pair(System(aFirst), System(aSecond));
93 // Whether we should use system colors (for high contrast mode).
94 static bool ShouldBeHighContrast(const nsPresContext&);
96 static void RecomputeAccentColors();
98 static nscolor ComputeCustomAccentForeground(nscolor aColor);
100 static nscolor AdjustUnthemedScrollbarThumbColor(nscolor aFaceColor,
101 EventStates aStates);
104 } // namespace mozilla::widget
106 #endif