Backed out changeset e9d46c179688 (bug 1855759) for causing build bustages CLOSED...
[gecko.git] / widget / LookAndFeel.h
blob4a8466089886c414452dcd8b72d3e0a4bdd03055
1 /* -*- Mode: C++; tab-width: 2; 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 __LookAndFeel
7 #define __LookAndFeel
9 #ifndef MOZILLA_INTERNAL_API
10 # error "This header is only usable from within libxul (MOZILLA_INTERNAL_API)."
11 #endif
13 #include "nsDebug.h"
14 #include "nsColor.h"
15 #include "nsString.h"
16 #include "nsTArray.h"
17 #include "mozilla/Maybe.h"
18 #include "mozilla/widget/ThemeChangeKind.h"
19 #include "mozilla/ColorScheme.h"
21 struct gfxFontStyle;
23 class nsIFrame;
25 namespace mozilla {
27 using Modifiers = uint16_t;
28 struct StyleColorSchemeFlags;
30 namespace dom {
31 class Document;
34 namespace widget {
35 class FullLookAndFeel;
36 } // namespace widget
38 enum class StyleSystemColor : uint8_t;
39 enum class StyleSystemColorScheme : uint8_t;
40 enum class StyleSystemFont : uint8_t;
42 class LookAndFeel {
43 public:
44 using ColorID = StyleSystemColor;
45 using ColorScheme = mozilla::ColorScheme;
47 // When modifying this list, also modify nsXPLookAndFeel::sIntPrefs
48 // in widget/xpwidgts/nsXPLookAndFeel.cpp.
49 enum class IntID {
50 // default, may be overriden by OS
51 CaretBlinkTime,
52 // Amount of blinks that happen before the caret stops blinking.
53 CaretBlinkCount,
54 // pixel width of caret
55 CaretWidth,
56 // show the caret when text is selected?
57 ShowCaretDuringSelection,
58 // select textfields when focused via tab/accesskey?
59 SelectTextfieldsOnKeyFocus,
60 // delay before submenus open
61 SubmenuDelay,
62 // can popups overlap menu/task bar?
63 MenusCanOverlapOSBar,
64 // should overlay scrollbars be used?
65 UseOverlayScrollbars,
66 // allow H and V overlay scrollbars to overlap?
67 AllowOverlayScrollbarsOverlap,
68 // skip navigating to disabled menu item?
69 SkipNavigatingDisabledMenuItem,
70 // begin a drag if the mouse is moved further than the threshold while the
71 // button is down
72 DragThresholdX,
73 DragThresholdY,
74 // Accessibility theme being used?
75 UseAccessibilityTheme,
77 // position of scroll arrows in a scrollbar
78 ScrollArrowStyle,
80 // each button can take one of four values:
81 ScrollButtonLeftMouseButtonAction,
82 // 0 - scrolls one line, 1 - scrolls one page
83 ScrollButtonMiddleMouseButtonAction,
84 // 2 - scrolls to end, 3 - button ignored
85 ScrollButtonRightMouseButtonAction,
87 // delay for opening spring loaded folders
88 TreeOpenDelay,
89 // delay for closing spring loaded folders
90 TreeCloseDelay,
91 // delay for triggering the tree scrolling
92 TreeLazyScrollDelay,
93 // delay for scrolling the tree
94 TreeScrollDelay,
95 // the maximum number of lines to be scrolled at ones
96 TreeScrollLinesMax,
97 // What type of tab-order to use
98 TabFocusModel,
99 // Should menu items blink when they're chosen?
100 ChosenMenuItemsShouldBlink,
103 * A Boolean value to determine whether the Windows accent color
104 * should be applied to the title bar.
106 * The value of this metric is not used on other platforms. These platforms
107 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
109 WindowsAccentColorInTitlebar,
112 * A Boolean value to determine whether the macOS Big Sur-specific
113 * theming should be used.
115 MacBigSurTheme,
118 * A Boolean value to determine whether macOS is in RTL mode or not.
120 MacRTL,
123 * AlertNotificationOrigin indicates from which corner of the
124 * screen alerts slide in, and from which direction (horizontal/vertical).
125 * 0, the default, represents bottom right, sliding vertically.
126 * Use any bitwise combination of the following constants:
127 * NS_ALERT_HORIZONTAL (1), NS_ALERT_LEFT (2), NS_ALERT_TOP (4).
129 * 6 4
130 * +-----------+
131 * 7| |5
132 * | |
133 * 3| |1
134 * +-----------+
135 * 2 0
137 AlertNotificationOrigin,
140 * If true, clicking on a scrollbar (not as in dragging the thumb) defaults
141 * to scrolling the view corresponding to the clicked point. Otherwise, we
142 * only do so if the scrollbar is clicked using the middle mouse button or
143 * if shift is pressed when the scrollbar is clicked.
145 ScrollToClick,
148 * IME and spell checker underline styles, the values should be
149 * NS_DECORATION_LINE_STYLE_*. They are defined below.
151 IMERawInputUnderlineStyle,
152 IMESelectedRawTextUnderlineStyle,
153 IMEConvertedTextUnderlineStyle,
154 IMESelectedConvertedTextUnderline,
155 SpellCheckerUnderlineStyle,
158 * If this metric != 0, support window dragging on the menubar.
160 MenuBarDrag,
162 * 0: scrollbar button repeats to scroll only when cursor is on the button.
163 * 1: scrollbar button repeats to scroll even if cursor is outside of it.
165 ScrollbarButtonAutoRepeatBehavior,
167 * Delay before showing a tooltip.
169 TooltipDelay,
171 * A Boolean value to determine whether swipe animations should be used.
173 SwipeAnimationEnabled,
176 * Controls whether overlay scrollbars display when the user moves
177 * the mouse in a scrollable frame.
179 ScrollbarDisplayOnMouseMove,
182 * Overlay scrollbar animation constants.
184 ScrollbarFadeBeginDelay,
185 ScrollbarFadeDuration,
188 * Distance in pixels to offset the context menu from the cursor
189 * on open.
191 ContextMenuOffsetVertical,
192 ContextMenuOffsetHorizontal,
195 * A boolean value indicating whether client-side decorations are
196 * supported by the user's GTK version.
198 GTKCSDAvailable,
201 * A boolean value indicating whether client-side decorations should
202 * contain a minimize button.
204 GTKCSDMinimizeButton,
207 * A boolean value indicating whether client-side decorations should
208 * contain a maximize button.
210 GTKCSDMaximizeButton,
213 * A boolean value indicating whether client-side decorations should
214 * contain a close button.
216 GTKCSDCloseButton,
219 * An Integer value that will represent the position of the Minimize button
220 * in GTK Client side decoration header.
222 GTKCSDMinimizeButtonPosition,
225 * An Integer value that will represent the position of the Maximize button
226 * in GTK Client side decoration header.
228 GTKCSDMaximizeButtonPosition,
231 * An Integer value that will represent the position of the Close button
232 * in GTK Client side decoration header.
234 GTKCSDCloseButtonPosition,
237 * A boolean value indicating whether titlebar buttons are located
238 * in left titlebar corner.
240 GTKCSDReversedPlacement,
243 * A boolean value indicating whether or not the OS is using a dark theme,
244 * which we may want to switch to as well if not overridden by the user.
246 SystemUsesDarkTheme,
249 * Corresponding to prefers-reduced-motion.
250 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-motion
251 * 0: no-preference
252 * 1: reduce
254 PrefersReducedMotion,
257 * Corresponding to prefers-reduced-transparency.
258 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-transparency
259 * 0: no-preference
260 * 1: reduce
262 PrefersReducedTransparency,
265 * Corresponding to inverted-colors.
266 * https://drafts.csswg.org/mediaqueries-5/#inverted
267 * 0: none
268 * 1: inverted
270 InvertedColors,
273 * Corresponding to PointerCapabilities in ServoTypes.h
274 * 0: None
275 * 1: Coarse
276 * 2: Fine
277 * 4: Hover
279 PrimaryPointerCapabilities,
281 * Corresponding to union of PointerCapabilities values in ServoTypes.h
282 * E.g. if there is a mouse and a digitizer, the value will be
283 * 'Coarse | Fine | Hover'.
285 AllPointerCapabilities,
287 /** The scrollbar size, in CSS pixels. */
288 SystemScrollbarSize,
290 /** A boolean value to determine whether a touch device is present */
291 TouchDeviceSupportPresent,
293 /** GTK titlebar radius */
294 TitlebarRadius,
297 * Corresponding to dynamic-range.
298 * https://drafts.csswg.org/mediaqueries-5/#dynamic-range
299 * 0: Standard
300 * 1: High
302 DynamicRange,
303 VideoDynamicRange,
305 /** Whether XUL panel animations are enabled. */
306 PanelAnimations,
308 /* Whether we should hide the cursor while typing */
309 HideCursorWhileTyping,
312 * Not an ID; used to define the range of valid IDs. Must be last.
314 End,
317 // This is a common enough integer that seems worth the shortcut.
318 static bool UseOverlayScrollbars() {
319 return GetInt(IntID::UseOverlayScrollbars);
322 // Returns keyCode value of a modifier key which is used for accesskey.
323 // Returns 0 if the platform doesn't support access key.
324 static uint32_t GetMenuAccessKey();
325 // Modifier mask for the menu accesskey.
326 static Modifiers GetMenuAccessKeyModifiers();
328 enum {
329 eScrollArrow_None = 0,
330 eScrollArrow_StartBackward = 0x1000,
331 eScrollArrow_StartForward = 0x0100,
332 eScrollArrow_EndBackward = 0x0010,
333 eScrollArrow_EndForward = 0x0001
336 enum {
337 // single arrow at each end
338 eScrollArrowStyle_Single =
339 eScrollArrow_StartBackward | eScrollArrow_EndForward,
340 // both arrows at bottom/right, none at top/left
341 eScrollArrowStyle_BothAtBottom =
342 eScrollArrow_EndBackward | eScrollArrow_EndForward,
343 // both arrows at both ends
344 eScrollArrowStyle_BothAtEachEnd =
345 eScrollArrow_EndBackward | eScrollArrow_EndForward |
346 eScrollArrow_StartBackward | eScrollArrow_StartForward,
347 // both arrows at top/left, none at bottom/right
348 eScrollArrowStyle_BothAtTop =
349 eScrollArrow_StartBackward | eScrollArrow_StartForward
352 // When modifying this list, also modify nsXPLookAndFeel::sFloatPrefs
353 // in widget/nsXPLookAndFeel.cpp.
354 enum class FloatID {
355 IMEUnderlineRelativeSize,
356 SpellCheckerUnderlineRelativeSize,
358 // The width/height ratio of the cursor. If used, the CaretWidth int metric
359 // should be added to the calculated caret width.
360 CaretAspectRatio,
362 // GTK text scale factor.
363 TextScaleFactor,
365 // Mouse pointer scaling factor.
366 CursorScale,
368 // Not an ID; used to define the range of valid IDs. Must be last.
369 End,
372 using FontID = mozilla::StyleSystemFont;
374 static ColorScheme SystemColorScheme() {
375 return GetInt(IntID::SystemUsesDarkTheme) ? ColorScheme::Dark
376 : ColorScheme::Light;
379 static bool IsDarkColor(nscolor);
381 enum class ChromeColorSchemeSetting { Light, Dark, System };
382 static ChromeColorSchemeSetting ColorSchemeSettingForChrome();
383 static ColorScheme ThemeDerivedColorSchemeForContent();
385 static ColorScheme ColorSchemeForChrome() {
386 MOZ_ASSERT(sColorSchemeInitialized);
387 return sChromeColorScheme;
389 static ColorScheme PreferredColorSchemeForContent() {
390 MOZ_ASSERT(sColorSchemeInitialized);
391 return sContentColorScheme;
394 static ColorScheme ColorSchemeForStyle(
395 const dom::Document&, const StyleColorSchemeFlags&,
396 ColorSchemeMode = ColorSchemeMode::Used);
397 static ColorScheme ColorSchemeForFrame(
398 const nsIFrame*, ColorSchemeMode = ColorSchemeMode::Used);
400 // Whether standins for native colors should be used (that is, colors faked,
401 // taken from win7, mostly). This forces light appearance, effectively.
402 enum class UseStandins : bool { No, Yes };
403 static UseStandins ShouldUseStandins(const dom::Document&, ColorID);
405 // Returns a native color value (might be overwritten by prefs) for a given
406 // color id.
408 // NOTE:
409 // ColorID::TextSelectForeground might return NS_SAME_AS_FOREGROUND_COLOR.
410 // ColorID::IME* might return NS_TRANSPARENT, NS_SAME_AS_FOREGROUND_COLOR or
411 // NS_40PERCENT_FOREGROUND_COLOR.
412 // These values have particular meaning. Then, they are not an actual
413 // color value.
414 static Maybe<nscolor> GetColor(ColorID, ColorScheme, UseStandins);
416 // Gets the color with appropriate defaults for UseStandins, ColorScheme etc
417 // for a given frame.
418 static Maybe<nscolor> GetColor(ColorID, const nsIFrame*);
420 // Versions of the above which returns the color if found, or a default (which
421 // defaults to opaque black) otherwise.
422 static nscolor Color(ColorID aId, ColorScheme aScheme,
423 UseStandins aUseStandins,
424 nscolor aDefault = NS_RGB(0, 0, 0)) {
425 return GetColor(aId, aScheme, aUseStandins).valueOr(aDefault);
428 static nscolor Color(ColorID aId, nsIFrame* aFrame,
429 nscolor aDefault = NS_RGB(0, 0, 0)) {
430 return GetColor(aId, aFrame).valueOr(aDefault);
433 static float GetTextScaleFactor() {
434 float f = GetFloat(FloatID::TextScaleFactor, 1.0f);
435 if (MOZ_UNLIKELY(f <= 0.0f)) {
436 return 1.0f;
438 return f;
441 struct ZoomSettings {
442 float mFullZoom = 1.0f;
443 float mTextZoom = 1.0f;
446 static ZoomSettings SystemZoomSettings();
449 * GetInt() and GetFloat() return a int or float value for aID. The result
450 * might be distance, time, some flags or a int value which has particular
451 * meaning. See each document at definition of each ID for the detail.
452 * The result is always 0 when they return error. Therefore, if you want to
453 * use a value for the default value, you should use the other method which
454 * returns int or float directly.
456 static nsresult GetInt(IntID, int32_t* aResult);
457 static nsresult GetFloat(FloatID aID, float* aResult);
459 static int32_t GetInt(IntID aID, int32_t aDefault = 0) {
460 int32_t result;
461 if (NS_FAILED(GetInt(aID, &result))) {
462 return aDefault;
464 return result;
467 static float GetFloat(FloatID aID, float aDefault = 0.0f) {
468 float result;
469 if (NS_FAILED(GetFloat(aID, &result))) {
470 return aDefault;
472 return result;
476 * Retrieve the name and style of a system-theme font. Returns true
477 * if the system theme specifies this font, false if a default should
478 * be used. In the latter case neither aName nor aStyle is modified.
480 * Size of the font should be in CSS pixels, not device pixels.
482 * @param aID Which system-theme font is wanted.
483 * @param aName The name of the font to use.
484 * @param aStyle Styling to apply to the font.
486 static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
489 * GetPasswordCharacter() returns a unicode character which should be used
490 * for a masked character in password editor. E.g., '*'.
492 static char16_t GetPasswordCharacter();
495 * If the latest character in password field shouldn't be hidden by the
496 * result of GetPasswordCharacter(), GetEchoPassword() returns TRUE.
497 * Otherwise, FALSE.
499 static bool GetEchoPassword();
502 * Whether we should be drawing in the titlebar by default.
504 static bool DrawInTitlebar();
507 * The millisecond to mask password value.
508 * This value is only valid when GetEchoPassword() returns true.
510 static uint32_t GetPasswordMaskDelay();
512 /** Gets theme information for about:support */
513 static void GetThemeInfo(nsACString&);
516 * When system look and feel is changed, Refresh() must be called. Then,
517 * cached data would be released.
519 static void Refresh();
522 * GTK's initialization code can't be run off main thread, call this
523 * if you plan on using LookAndFeel off main thread later.
525 * This initialized state may get reset due to theme changes, so it
526 * must be called prior to each potential off-main-thread LookAndFeel
527 * call, not just once.
529 static void NativeInit();
531 static void SetData(widget::FullLookAndFeel&& aTables);
532 static void NotifyChangedAllWindows(widget::ThemeChangeKind);
533 static bool HasPendingGlobalThemeChange() { return sGlobalThemeChanged; }
534 static void HandleGlobalThemeChange() {
535 if (MOZ_UNLIKELY(HasPendingGlobalThemeChange())) {
536 DoHandleGlobalThemeChange();
539 static void EnsureColorSchemesInitialized() {
540 if (!sColorSchemeInitialized) {
541 RecomputeColorSchemes();
543 MOZ_ASSERT(sColorSchemeInitialized);
546 static ColorScheme sChromeColorScheme;
547 static ColorScheme sContentColorScheme;
549 protected:
550 static void RecomputeColorSchemes();
551 static bool sColorSchemeInitialized;
553 static void DoHandleGlobalThemeChange();
554 // Set to true when ThemeChanged needs to be called on mTheme (and other
555 // global LookAndFeel. This is used because mTheme is a service, so there's
556 // no need to notify it from more than one prescontext.
557 static bool sGlobalThemeChanged;
560 } // namespace mozilla
562 // ---------------------------------------------------------------------
563 // Special colors for ColorID::IME* and ColorID::SpellCheckerUnderline
564 // ---------------------------------------------------------------------
566 // For background color only.
567 constexpr nscolor NS_TRANSPARENT = NS_RGBA(0x01, 0x00, 0x00, 0x00);
568 // For foreground color only.
569 constexpr nscolor NS_SAME_AS_FOREGROUND_COLOR = NS_RGBA(0x02, 0x00, 0x00, 0x00);
570 constexpr nscolor NS_40PERCENT_FOREGROUND_COLOR =
571 NS_RGBA(0x03, 0x00, 0x00, 0x00);
573 #define NS_IS_SELECTION_SPECIAL_COLOR(c) \
574 ((c) == NS_TRANSPARENT || (c) == NS_SAME_AS_FOREGROUND_COLOR || \
575 (c) == NS_40PERCENT_FOREGROUND_COLOR)
577 // ------------------------------------------
578 // Bits for IntID::AlertNotificationOrigin
579 // ------------------------------------------
581 #define NS_ALERT_HORIZONTAL 1
582 #define NS_ALERT_LEFT 2
583 #define NS_ALERT_TOP 4
585 #endif /* __LookAndFeel */