Bug 1793579 [wpt PR 36253] - Set empty string for reflection of IDREF attributes...
[gecko.git] / widget / LookAndFeel.h
blobc96306a9843856ab044cad7cc509ab47a9757eda
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 struct StyleColorSchemeFlags;
29 namespace dom {
30 class Document;
33 namespace widget {
34 class FullLookAndFeel;
35 } // namespace widget
37 enum class StyleSystemColor : uint8_t;
38 enum class StyleSystemColorScheme : uint8_t;
39 enum class StyleSystemFont : uint8_t;
41 class LookAndFeel {
42 public:
43 using ColorID = StyleSystemColor;
44 using ColorScheme = mozilla::ColorScheme;
46 // When modifying this list, also modify nsXPLookAndFeel::sIntPrefs
47 // in widget/xpwidgts/nsXPLookAndFeel.cpp.
48 enum class IntID {
49 // default, may be overriden by OS
50 CaretBlinkTime,
51 // Amount of blinks that happen before the caret stops blinking.
52 CaretBlinkCount,
53 // pixel width of caret
54 CaretWidth,
55 // show the caret when text is selected?
56 ShowCaretDuringSelection,
57 // select textfields when focused via tab/accesskey?
58 SelectTextfieldsOnKeyFocus,
59 // delay before submenus open
60 SubmenuDelay,
61 // can popups overlap menu/task bar?
62 MenusCanOverlapOSBar,
63 // should overlay scrollbars be used?
64 UseOverlayScrollbars,
65 // allow H and V overlay scrollbars to overlap?
66 AllowOverlayScrollbarsOverlap,
67 // skip navigating to disabled menu item?
68 SkipNavigatingDisabledMenuItem,
69 // begin a drag if the mouse is moved further than the threshold while the
70 // button is down
71 DragThresholdX,
72 DragThresholdY,
73 // Accessibility theme being used?
74 UseAccessibilityTheme,
76 // position of scroll arrows in a scrollbar
77 ScrollArrowStyle,
79 // each button can take one of four values:
80 ScrollButtonLeftMouseButtonAction,
81 // 0 - scrolls one line, 1 - scrolls one page
82 ScrollButtonMiddleMouseButtonAction,
83 // 2 - scrolls to end, 3 - button ignored
84 ScrollButtonRightMouseButtonAction,
86 // delay for opening spring loaded folders
87 TreeOpenDelay,
88 // delay for closing spring loaded folders
89 TreeCloseDelay,
90 // delay for triggering the tree scrolling
91 TreeLazyScrollDelay,
92 // delay for scrolling the tree
93 TreeScrollDelay,
94 // the maximum number of lines to be scrolled at ones
95 TreeScrollLinesMax,
96 // What type of tab-order to use
97 TabFocusModel,
98 // Should menu items blink when they're chosen?
99 ChosenMenuItemsShouldBlink,
102 * A Boolean value to determine whether the Windows accent color
103 * should be applied to the title bar.
105 * The value of this metric is not used on other platforms. These platforms
106 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
108 WindowsAccentColorInTitlebar,
111 * A Boolean value to determine whether the Windows default theme is
112 * being used.
114 * The value of this metric is not used on other platforms. These platforms
115 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
117 WindowsDefaultTheme,
120 * A Boolean value to determine whether the DWM compositor is being used
122 * This metric is not used on non-Windows platforms. These platforms
123 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
125 DWMCompositor,
128 * A Boolean value to determine whether Windows is themed (Classic vs.
129 * uxtheme)
131 * This is Windows-specific and is not implemented on other platforms
132 * (will return the default of NS_ERROR_FAILURE).
134 WindowsClassic,
137 * A Boolean value to determine whether the current Windows desktop theme
138 * supports Aero Glass.
140 * This is Windows-specific and is not implemented on other platforms
141 * (will return the default of NS_ERROR_FAILURE).
143 WindowsGlass,
146 * A Boolean value to determine whether the Mac graphite theme is
147 * being used.
149 MacGraphiteTheme,
152 * A Boolean value to determine whether the macOS Big Sur-specific
153 * theming should be used.
155 MacBigSurTheme,
158 * A Boolean value to determine whether macOS is in RTL mode or not.
160 MacRTL,
163 * AlertNotificationOrigin indicates from which corner of the
164 * screen alerts slide in, and from which direction (horizontal/vertical).
165 * 0, the default, represents bottom right, sliding vertically.
166 * Use any bitwise combination of the following constants:
167 * NS_ALERT_HORIZONTAL (1), NS_ALERT_LEFT (2), NS_ALERT_TOP (4).
169 * 6 4
170 * +-----------+
171 * 7| |5
172 * | |
173 * 3| |1
174 * +-----------+
175 * 2 0
177 AlertNotificationOrigin,
180 * If true, clicking on a scrollbar (not as in dragging the thumb) defaults
181 * to scrolling the view corresponding to the clicked point. Otherwise, we
182 * only do so if the scrollbar is clicked using the middle mouse button or
183 * if shift is pressed when the scrollbar is clicked.
185 ScrollToClick,
188 * IME and spell checker underline styles, the values should be
189 * NS_DECORATION_LINE_STYLE_*. They are defined below.
191 IMERawInputUnderlineStyle,
192 IMESelectedRawTextUnderlineStyle,
193 IMEConvertedTextUnderlineStyle,
194 IMESelectedConvertedTextUnderline,
195 SpellCheckerUnderlineStyle,
198 * If this metric != 0, support window dragging on the menubar.
200 MenuBarDrag,
202 * 0: scrollbar button repeats to scroll only when cursor is on the button.
203 * 1: scrollbar button repeats to scroll even if cursor is outside of it.
205 ScrollbarButtonAutoRepeatBehavior,
207 * Delay before showing a tooltip.
209 TooltipDelay,
211 * A Boolean value to determine whether swipe animations should be used.
213 SwipeAnimationEnabled,
216 * Controls whether overlay scrollbars display when the user moves
217 * the mouse in a scrollable frame.
219 ScrollbarDisplayOnMouseMove,
222 * Overlay scrollbar animation constants.
224 ScrollbarFadeBeginDelay,
225 ScrollbarFadeDuration,
228 * Distance in pixels to offset the context menu from the cursor
229 * on open.
231 ContextMenuOffsetVertical,
232 ContextMenuOffsetHorizontal,
235 * A boolean value indicating whether client-side decorations are
236 * supported by the user's GTK version.
238 GTKCSDAvailable,
241 * A boolean value indicating whether client-side decorations should
242 * contain a minimize button.
244 GTKCSDMinimizeButton,
247 * A boolean value indicating whether client-side decorations should
248 * contain a maximize button.
250 GTKCSDMaximizeButton,
253 * A boolean value indicating whether client-side decorations should
254 * contain a close button.
256 GTKCSDCloseButton,
259 * An Integer value that will represent the position of the Minimize button
260 * in GTK Client side decoration header.
262 GTKCSDMinimizeButtonPosition,
265 * An Integer value that will represent the position of the Maximize button
266 * in GTK Client side decoration header.
268 GTKCSDMaximizeButtonPosition,
271 * An Integer value that will represent the position of the Close button
272 * in GTK Client side decoration header.
274 GTKCSDCloseButtonPosition,
277 * A boolean value indicating whether titlebar buttons are located
278 * in left titlebar corner.
280 GTKCSDReversedPlacement,
283 * A boolean value indicating whether or not the OS is using a dark theme,
284 * which we may want to switch to as well if not overridden by the user.
286 SystemUsesDarkTheme,
289 * Corresponding to prefers-reduced-motion.
290 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-motion
291 * 0: no-preference
292 * 1: reduce
295 PrefersReducedMotion,
297 * Corresponding to PointerCapabilities in ServoTypes.h
298 * 0: None
299 * 1: Coarse
300 * 2: Fine
301 * 4: Hover
303 PrimaryPointerCapabilities,
305 * Corresponding to union of PointerCapabilities values in ServoTypes.h
306 * E.g. if there is a mouse and a digitizer, the value will be
307 * 'Coarse | Fine | Hover'.
309 AllPointerCapabilities,
310 /** The vertical scrollbar width, in CSS pixels. */
311 SystemVerticalScrollbarWidth,
313 /** The horizontal scrollbar height, in CSS pixels. */
314 SystemHorizontalScrollbarHeight,
316 /** A boolean value to determine whether a touch device is present */
317 TouchDeviceSupportPresent,
319 /** GTK titlebar radius */
320 TitlebarRadius,
322 /** GTK menu radius */
323 GtkMenuRadius,
326 * Corresponding to dynamic-range.
327 * https://drafts.csswg.org/mediaqueries-5/#dynamic-range
328 * 0: Standard
329 * 1: High
331 DynamicRange,
332 VideoDynamicRange,
335 * Not an ID; used to define the range of valid IDs. Must be last.
337 End,
340 // This is a common enough integer that seems worth the shortcut.
341 static bool UseOverlayScrollbars() {
342 return GetInt(IntID::UseOverlayScrollbars);
345 enum {
346 eScrollArrow_None = 0,
347 eScrollArrow_StartBackward = 0x1000,
348 eScrollArrow_StartForward = 0x0100,
349 eScrollArrow_EndBackward = 0x0010,
350 eScrollArrow_EndForward = 0x0001
353 enum {
354 // single arrow at each end
355 eScrollArrowStyle_Single =
356 eScrollArrow_StartBackward | eScrollArrow_EndForward,
357 // both arrows at bottom/right, none at top/left
358 eScrollArrowStyle_BothAtBottom =
359 eScrollArrow_EndBackward | eScrollArrow_EndForward,
360 // both arrows at both ends
361 eScrollArrowStyle_BothAtEachEnd =
362 eScrollArrow_EndBackward | eScrollArrow_EndForward |
363 eScrollArrow_StartBackward | eScrollArrow_StartForward,
364 // both arrows at top/left, none at bottom/right
365 eScrollArrowStyle_BothAtTop =
366 eScrollArrow_StartBackward | eScrollArrow_StartForward
369 // When modifying this list, also modify nsXPLookAndFeel::sFloatPrefs
370 // in widget/nsXPLookAndFeel.cpp.
371 enum class FloatID {
372 IMEUnderlineRelativeSize,
373 SpellCheckerUnderlineRelativeSize,
375 // The width/height ratio of the cursor. If used, the CaretWidth int metric
376 // should be added to the calculated caret width.
377 CaretAspectRatio,
379 // GTK text scale factor.
380 TextScaleFactor,
382 // Mouse pointer scaling factor.
383 CursorScale,
385 // Not an ID; used to define the range of valid IDs. Must be last.
386 End,
389 using FontID = mozilla::StyleSystemFont;
391 static bool WindowsNonNativeMenusEnabled();
393 static ColorScheme SystemColorScheme() {
394 return GetInt(IntID::SystemUsesDarkTheme) ? ColorScheme::Dark
395 : ColorScheme::Light;
398 static bool IsDarkColor(nscolor);
400 enum class ChromeColorSchemeSetting { Light, Dark, System };
401 static ChromeColorSchemeSetting ColorSchemeSettingForChrome();
402 static ColorScheme ThemeDerivedColorSchemeForContent();
404 static ColorScheme ColorSchemeForChrome() {
405 MOZ_ASSERT(sColorSchemeInitialized);
406 return sChromeColorScheme;
408 static ColorScheme PreferredColorSchemeForContent() {
409 MOZ_ASSERT(sColorSchemeInitialized);
410 return sContentColorScheme;
413 static ColorScheme ColorSchemeForStyle(
414 const dom::Document&, const StyleColorSchemeFlags&,
415 ColorSchemeMode = ColorSchemeMode::Used);
416 static ColorScheme ColorSchemeForFrame(
417 const nsIFrame*, ColorSchemeMode = ColorSchemeMode::Used);
419 // Whether standins for native colors should be used (that is, colors faked,
420 // taken from win7, mostly). This forces light appearance, effectively.
421 enum class UseStandins : bool { No, Yes };
422 static UseStandins ShouldUseStandins(const dom::Document&, ColorID);
424 // Returns a native color value (might be overwritten by prefs) for a given
425 // color id.
427 // NOTE:
428 // ColorID::TextSelectForeground might return NS_SAME_AS_FOREGROUND_COLOR.
429 // ColorID::IME* might return NS_TRANSPARENT, NS_SAME_AS_FOREGROUND_COLOR or
430 // NS_40PERCENT_FOREGROUND_COLOR.
431 // These values have particular meaning. Then, they are not an actual
432 // color value.
433 static Maybe<nscolor> GetColor(ColorID, ColorScheme, UseStandins);
435 // Gets the color with appropriate defaults for UseStandins, ColorScheme etc
436 // for a given frame.
437 static Maybe<nscolor> GetColor(ColorID, const nsIFrame*);
439 // Versions of the above which returns the color if found, or a default (which
440 // defaults to opaque black) otherwise.
441 static nscolor Color(ColorID aId, ColorScheme aScheme,
442 UseStandins aUseStandins,
443 nscolor aDefault = NS_RGB(0, 0, 0)) {
444 return GetColor(aId, aScheme, aUseStandins).valueOr(aDefault);
447 static nscolor Color(ColorID aId, nsIFrame* aFrame,
448 nscolor aDefault = NS_RGB(0, 0, 0)) {
449 return GetColor(aId, aFrame).valueOr(aDefault);
452 static float GetTextScaleFactor() {
453 float f = GetFloat(FloatID::TextScaleFactor, 1.0f);
454 if (MOZ_UNLIKELY(f <= 0.0f)) {
455 return 1.0f;
457 return f;
460 struct ZoomSettings {
461 float mFullZoom = 1.0f;
462 float mTextZoom = 1.0f;
465 static ZoomSettings SystemZoomSettings();
468 * GetInt() and GetFloat() return a int or float value for aID. The result
469 * might be distance, time, some flags or a int value which has particular
470 * meaning. See each document at definition of each ID for the detail.
471 * The result is always 0 when they return error. Therefore, if you want to
472 * use a value for the default value, you should use the other method which
473 * returns int or float directly.
475 static nsresult GetInt(IntID, int32_t* aResult);
476 static nsresult GetFloat(FloatID aID, float* aResult);
478 static int32_t GetInt(IntID aID, int32_t aDefault = 0) {
479 int32_t result;
480 if (NS_FAILED(GetInt(aID, &result))) {
481 return aDefault;
483 return result;
486 static float GetFloat(FloatID aID, float aDefault = 0.0f) {
487 float result;
488 if (NS_FAILED(GetFloat(aID, &result))) {
489 return aDefault;
491 return result;
495 * Retrieve the name and style of a system-theme font. Returns true
496 * if the system theme specifies this font, false if a default should
497 * be used. In the latter case neither aName nor aStyle is modified.
499 * Size of the font should be in CSS pixels, not device pixels.
501 * @param aID Which system-theme font is wanted.
502 * @param aName The name of the font to use.
503 * @param aStyle Styling to apply to the font.
505 static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
508 * GetPasswordCharacter() returns a unicode character which should be used
509 * for a masked character in password editor. E.g., '*'.
511 static char16_t GetPasswordCharacter();
514 * If the latest character in password field shouldn't be hidden by the
515 * result of GetPasswordCharacter(), GetEchoPassword() returns TRUE.
516 * Otherwise, FALSE.
518 static bool GetEchoPassword();
521 * Whether we should be drawing in the titlebar by default.
523 static bool DrawInTitlebar();
526 * The millisecond to mask password value.
527 * This value is only valid when GetEchoPassword() returns true.
529 static uint32_t GetPasswordMaskDelay();
531 /** Gets theme information for about:support */
532 static void GetThemeInfo(nsACString&);
535 * When system look and feel is changed, Refresh() must be called. Then,
536 * cached data would be released.
538 static void Refresh();
541 * GTK's initialization code can't be run off main thread, call this
542 * if you plan on using LookAndFeel off main thread later.
544 * This initialized state may get reset due to theme changes, so it
545 * must be called prior to each potential off-main-thread LookAndFeel
546 * call, not just once.
548 static void NativeInit();
550 static void SetData(widget::FullLookAndFeel&& aTables);
551 static void NotifyChangedAllWindows(widget::ThemeChangeKind);
552 static bool HasPendingGlobalThemeChange() { return sGlobalThemeChanged; }
553 static void HandleGlobalThemeChange() {
554 if (MOZ_UNLIKELY(HasPendingGlobalThemeChange())) {
555 DoHandleGlobalThemeChange();
558 static void EnsureColorSchemesInitialized() {
559 if (!sColorSchemeInitialized) {
560 RecomputeColorSchemes();
562 MOZ_ASSERT(sColorSchemeInitialized);
565 static ColorScheme sChromeColorScheme;
566 static ColorScheme sContentColorScheme;
568 protected:
569 static void RecomputeColorSchemes();
570 static bool sColorSchemeInitialized;
572 static void DoHandleGlobalThemeChange();
573 // Set to true when ThemeChanged needs to be called on mTheme (and other
574 // global LookAndFeel. This is used because mTheme is a service, so there's
575 // no need to notify it from more than one prescontext.
576 static bool sGlobalThemeChanged;
579 } // namespace mozilla
581 // ---------------------------------------------------------------------
582 // Special colors for ColorID::IME* and ColorID::SpellCheckerUnderline
583 // ---------------------------------------------------------------------
585 // For background color only.
586 constexpr nscolor NS_TRANSPARENT = NS_RGBA(0x01, 0x00, 0x00, 0x00);
587 // For foreground color only.
588 constexpr nscolor NS_SAME_AS_FOREGROUND_COLOR = NS_RGBA(0x02, 0x00, 0x00, 0x00);
589 constexpr nscolor NS_40PERCENT_FOREGROUND_COLOR =
590 NS_RGBA(0x03, 0x00, 0x00, 0x00);
592 #define NS_IS_SELECTION_SPECIAL_COLOR(c) \
593 ((c) == NS_TRANSPARENT || (c) == NS_SAME_AS_FOREGROUND_COLOR || \
594 (c) == NS_40PERCENT_FOREGROUND_COLOR)
596 // ------------------------------------------
597 // Bits for IntID::AlertNotificationOrigin
598 // ------------------------------------------
600 #define NS_ALERT_HORIZONTAL 1
601 #define NS_ALERT_LEFT 2
602 #define NS_ALERT_TOP 4
604 #endif /* __LookAndFeel */