1 /* -*- Mode: C++; tab-width: 4; 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 __nsXPLookAndFeel
7 #define __nsXPLookAndFeel
9 #include "mozilla/LookAndFeel.h"
10 #include "mozilla/ServoStyleConsts.h"
11 #include "mozilla/widget/LookAndFeelTypes.h"
16 struct nsLookAndFeelIntPref
{
18 mozilla::LookAndFeel::IntID id
;
23 struct nsLookAndFeelFloatPref
{
25 mozilla::LookAndFeel::FloatID id
;
30 #define CACHE_BLOCK(x) (uint32_t(x) >> 5)
31 #define CACHE_BIT(x) (1 << (uint32_t(x) & 31))
33 #define COLOR_CACHE_SIZE (CACHE_BLOCK(uint32_t(LookAndFeel::ColorID::End)) + 1)
34 #define IS_COLOR_CACHED(x) \
35 (CACHE_BIT(x) & nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)])
36 #define CLEAR_COLOR_CACHE(x) \
37 nsXPLookAndFeel::sCachedColors[uint32_t(x)] = 0; \
38 nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] &= ~(CACHE_BIT(x));
39 #define CACHE_COLOR(x, y) \
40 nsXPLookAndFeel::sCachedColors[uint32_t(x)] = y; \
41 nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] |= CACHE_BIT(x);
43 class nsXPLookAndFeel
: public mozilla::LookAndFeel
{
45 virtual ~nsXPLookAndFeel();
47 static nsXPLookAndFeel
* GetInstance();
48 static void Shutdown();
52 // These functions will return a value specified by an override pref, if it
53 // exists, and otherwise will call into the NativeGetXxx function to get the
54 // platform-specific value.
56 // NS_ERROR_NOT_AVAILABLE is returned if there is neither an override pref or
57 // a platform-specific value.
58 nsresult
GetColorValue(ColorID aID
, bool aUseStandinsForNativeColors
,
60 nsresult
GetIntValue(IntID aID
, int32_t& aResult
);
61 nsresult
GetFloatValue(FloatID aID
, float& aResult
);
62 // Same, but returns false if there is no platform-specific value.
63 // (There are no override prefs for font values.)
64 bool GetFontValue(FontID aID
, nsString
& aName
, gfxFontStyle
& aStyle
) {
65 return NativeGetFont(aID
, aName
, aStyle
);
68 virtual nsresult
NativeGetInt(IntID aID
, int32_t& aResult
) = 0;
69 virtual nsresult
NativeGetFloat(FloatID aID
, float& aResult
) = 0;
70 virtual nsresult
NativeGetColor(ColorID aID
, nscolor
& aResult
) = 0;
71 virtual bool NativeGetFont(FontID aID
, nsString
& aName
,
72 gfxFontStyle
& aStyle
) = 0;
74 virtual void RefreshImpl();
76 virtual char16_t
GetPasswordCharacterImpl() { return char16_t('*'); }
78 virtual bool GetEchoPasswordImpl() { return false; }
80 virtual uint32_t GetPasswordMaskDelayImpl() { return 600; }
82 using FullLookAndFeel
= mozilla::widget::FullLookAndFeel
;
83 using LookAndFeelCache
= mozilla::widget::LookAndFeelCache
;
84 using LookAndFeelInt
= mozilla::widget::LookAndFeelInt
;
85 using LookAndFeelFont
= mozilla::widget::LookAndFeelFont
;
86 using LookAndFeelColor
= mozilla::widget::LookAndFeelColor
;
87 using LookAndFeelTheme
= mozilla::widget::LookAndFeelTheme
;
89 virtual LookAndFeelCache
GetCacheImpl();
90 virtual void SetCacheImpl(const LookAndFeelCache
& aCache
) {}
91 virtual void SetDataImpl(FullLookAndFeel
&& aTables
) {}
93 virtual void NativeInit() = 0;
95 virtual void WithThemeConfiguredForContent(
96 const std::function
<void(const LookAndFeelTheme
& aTheme
)>& aFn
) {
97 aFn(LookAndFeelTheme
{});
101 nsXPLookAndFeel() = default;
103 static void IntPrefChanged(nsLookAndFeelIntPref
* data
);
104 static void FloatPrefChanged(nsLookAndFeelFloatPref
* data
);
105 static void ColorPrefChanged(unsigned int index
, const char* prefName
);
106 static nscolor
GetStandinForNativeColor(ColorID
);
107 void InitFromPref(nsLookAndFeelIntPref
* aPref
);
108 void InitFromPref(nsLookAndFeelFloatPref
* aPref
);
109 void InitColorFromPref(int32_t aIndex
);
110 void RecordTelemetry();
111 virtual void RecordLookAndFeelSpecificTelemetry() {}
113 static void OnPrefChanged(const char* aPref
, void* aClosure
);
115 static bool sInitialized
;
116 static nsLookAndFeelIntPref sIntPrefs
[];
117 static nsLookAndFeelFloatPref sFloatPrefs
[];
118 /* this length must not be shorter than the length of the longest string in
119 * the array see nsXPLookAndFeel.cpp
121 static const char sColorPrefs
[][41];
122 static int32_t sCachedColors
[size_t(LookAndFeel::ColorID::End
)];
123 static int32_t sCachedColorBits
[COLOR_CACHE_SIZE
];
125 static nsXPLookAndFeel
* sInstance
;
126 static bool sShutdown
;