1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3  */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
8 #ifndef __nsLookAndFeel
9 #define __nsLookAndFeel
10 
11 #include "X11UndefineNone.h"
12 #include "nsXPLookAndFeel.h"
13 #include "nsCOMPtr.h"
14 #include "gfxFont.h"
15 
16 enum WidgetNodeType : int;
17 struct _GtkStyle;
18 typedef struct _GDBusProxy GDBusProxy;
19 
20 class nsLookAndFeel final : public nsXPLookAndFeel {
21  public:
22   nsLookAndFeel();
23   virtual ~nsLookAndFeel();
24 
25   void NativeInit() final;
26   void RefreshImpl() override;
27   nsresult NativeGetInt(IntID aID, int32_t& aResult) override;
28   nsresult NativeGetFloat(FloatID aID, float& aResult) override;
29   nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override;
30   bool NativeGetFont(FontID aID, nsString& aFontName,
31                      gfxFontStyle& aFontStyle) override;
32 
33   char16_t GetPasswordCharacterImpl() override;
34   bool GetEchoPasswordImpl() override;
35 
36   bool GetDefaultDrawInTitlebar() override;
37 
38   void GetGtkContentTheme(LookAndFeelTheme&) override;
39   void GetThemeInfo(nsACString&) override;
40 
41   static void ConfigureTheme(const LookAndFeelTheme& aTheme);
42 
43   static const nscolor kBlack = NS_RGB(0, 0, 0);
44   static const nscolor kWhite = NS_RGB(255, 255, 255);
45 
46  protected:
47   static bool WidgetUsesImage(WidgetNodeType aNodeType);
48   void RecordLookAndFeelSpecificTelemetry() override;
49   static bool ShouldHonorThemeScrollbarColors();
50   mozilla::Maybe<ColorScheme> ComputeColorSchemeSetting();
51 
52   // We use up to two themes (one light, one dark), which might have different
53   // sets of fonts and colors.
54   struct PerThemeData {
55     nsCString mName;
56 
57     bool mIsDark = false;
58     bool mHighContrast = false;
59     bool mPreferDarkTheme = false;
60 
61     // NOTE(emilio): This is unused, but if we need to we can use it to override
62     // system colors with standins like we do for the non-native theme.
63     bool mCompatibleWithHTMLLightColors = false;
64 
65     // Cached fonts
66     nsString mDefaultFontName;
67     nsString mButtonFontName;
68     nsString mFieldFontName;
69     nsString mMenuFontName;
70     gfxFontStyle mDefaultFontStyle;
71     gfxFontStyle mButtonFontStyle;
72     gfxFontStyle mFieldFontStyle;
73     gfxFontStyle mMenuFontStyle;
74 
75     // Cached colors
76     nscolor mInfoBackground = kWhite;
77     nscolor mInfoText = kBlack;
78     nscolor mMenuBackground = kWhite;
79     nscolor mMenuBarText = kBlack;
80     nscolor mMenuBarHoverText = kBlack;
81     nscolor mMenuText = kBlack;
82     nscolor mMenuTextInactive = kWhite;
83     nscolor mMenuHover = kWhite;
84     nscolor mMenuHoverText = kBlack;
85     nscolor mButtonDefault = kWhite;
86     nscolor mButtonText = kBlack;
87     nscolor mButtonHoverText = kBlack;
88     nscolor mButtonHoverFace = kWhite;
89     nscolor mButtonActiveText = kBlack;
90     nscolor mFrameOuterLightBorder = kBlack;
91     nscolor mFrameInnerDarkBorder = kBlack;
92     nscolor mOddCellBackground = kWhite;
93     nscolor mNativeHyperLinkText = kBlack;
94     nscolor mNativeVisitedHyperLinkText = kBlack;
95     nscolor mComboBoxText = kBlack;
96     nscolor mComboBoxBackground = kWhite;
97     nscolor mFieldText = kBlack;
98     nscolor mFieldBackground = kWhite;
99     nscolor mMozWindowText = kBlack;
100     nscolor mMozWindowBackground = kWhite;
101     nscolor mMozWindowActiveBorder = kBlack;
102     nscolor mMozWindowInactiveBorder = kBlack;
103     nscolor mMozCellHighlightBackground = kWhite;
104     nscolor mMozCellHighlightText = kBlack;
105     nscolor mTextSelectedText = kBlack;
106     nscolor mTextSelectedBackground = kWhite;
107     nscolor mAccentColor = kWhite;
108     nscolor mAccentColorForeground = kWhite;
109     nscolor mMozColHeaderText = kBlack;
110     nscolor mMozColHeaderHoverText = kBlack;
111     nscolor mTitlebarText = kBlack;
112     nscolor mTitlebarBackground = kWhite;
113     nscolor mTitlebarInactiveText = kBlack;
114     nscolor mTitlebarInactiveBackground = kWhite;
115     nscolor mThemedScrollbar = kWhite;
116     nscolor mThemedScrollbarInactive = kWhite;
117     nscolor mThemedScrollbarThumb = kBlack;
118     nscolor mThemedScrollbarThumbHover = kBlack;
119     nscolor mThemedScrollbarThumbActive = kBlack;
120     nscolor mThemedScrollbarThumbInactive = kBlack;
121 
122     float mCaretRatio = 0.0f;
123     int32_t mTitlebarRadius = 0;
124     int32_t mMenuRadius = 0;
125     char16_t mInvisibleCharacter = 0;
126     bool mMenuSupportsDrag = false;
127 
128     void Init();
129     nsresult GetColor(ColorID, nscolor&) const;
130     bool GetFont(FontID, nsString& aFontName, gfxFontStyle&) const;
131     void InitCellHighlightColors();
132   };
133 
134   PerThemeData mSystemTheme;
135 
136   // If the system theme is light, a dark theme. Otherwise, a light theme. The
137   // alternative theme to the current one is preferred, but otherwise we fall
138   // back to Adwaita / Adwaita Dark, respectively.
139   PerThemeData mAltTheme;
140 
LightTheme()141   const PerThemeData& LightTheme() const {
142     return mSystemTheme.mIsDark ? mAltTheme : mSystemTheme;
143   }
144 
DarkTheme()145   const PerThemeData& DarkTheme() const {
146     return mSystemTheme.mIsDark ? mSystemTheme : mAltTheme;
147   }
148 
EffectiveTheme()149   const PerThemeData& EffectiveTheme() const {
150     return mSystemThemeOverridden ? mAltTheme : mSystemTheme;
151   }
152 
153   GDBusProxy* mDBusSettingsProxy = nullptr;
154   mozilla::Maybe<ColorScheme> mColorSchemePreference;
155   int32_t mCaretBlinkTime = 0;
156   int32_t mCaretBlinkCount = -1;
157   bool mCSDMaximizeButton = false;
158   bool mCSDMinimizeButton = false;
159   bool mCSDCloseButton = false;
160   bool mCSDReversedPlacement = false;
161   bool mPrefersReducedMotion = false;
162   bool mInitialized = false;
163   bool mSystemThemeOverridden = false;
164   int32_t mCSDMaximizeButtonPosition = 0;
165   int32_t mCSDMinimizeButtonPosition = 0;
166   int32_t mCSDCloseButtonPosition = 0;
167 
EnsureInit()168   void EnsureInit() {
169     if (mInitialized) {
170       return;
171     }
172     Initialize();
173   }
174 
175   void Initialize();
176 
177   void RestoreSystemTheme();
178   void InitializeGlobalSettings();
179   void ConfigureAndInitializeAltTheme();
180   void ConfigureFinalEffectiveTheme();
181 };
182 
183 #endif
184