Bug 1631735 Part 1: Make nsCocoaWindow animated transitions asynchronous and atomic...
[gecko.git] / widget / LookAndFeel.h
blobedace53cf4156d61d1f6ed74101b6bb30bef9928
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 Windows default theme is
113 * being used.
115 * The value of this metric is not used on other platforms. These platforms
116 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
118 WindowsDefaultTheme,
121 * A Boolean value to determine whether the DWM compositor is being used
123 * This metric is not used on non-Windows platforms. These platforms
124 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
126 DWMCompositor,
129 * A Boolean value to determine whether Windows is themed (Classic vs.
130 * uxtheme)
132 * This is Windows-specific and is not implemented on other platforms
133 * (will return the default of NS_ERROR_FAILURE).
135 WindowsClassic,
138 * A Boolean value to determine whether the current Windows desktop theme
139 * supports Aero Glass.
141 * This is Windows-specific and is not implemented on other platforms
142 * (will return the default of NS_ERROR_FAILURE).
144 WindowsGlass,
147 * A Boolean value to determine whether the Mac graphite theme is
148 * being used.
150 MacGraphiteTheme,
153 * A Boolean value to determine whether the macOS Big Sur-specific
154 * theming should be used.
156 MacBigSurTheme,
159 * A Boolean value to determine whether macOS is in RTL mode or not.
161 MacRTL,
164 * AlertNotificationOrigin indicates from which corner of the
165 * screen alerts slide in, and from which direction (horizontal/vertical).
166 * 0, the default, represents bottom right, sliding vertically.
167 * Use any bitwise combination of the following constants:
168 * NS_ALERT_HORIZONTAL (1), NS_ALERT_LEFT (2), NS_ALERT_TOP (4).
170 * 6 4
171 * +-----------+
172 * 7| |5
173 * | |
174 * 3| |1
175 * +-----------+
176 * 2 0
178 AlertNotificationOrigin,
181 * If true, clicking on a scrollbar (not as in dragging the thumb) defaults
182 * to scrolling the view corresponding to the clicked point. Otherwise, we
183 * only do so if the scrollbar is clicked using the middle mouse button or
184 * if shift is pressed when the scrollbar is clicked.
186 ScrollToClick,
189 * IME and spell checker underline styles, the values should be
190 * NS_DECORATION_LINE_STYLE_*. They are defined below.
192 IMERawInputUnderlineStyle,
193 IMESelectedRawTextUnderlineStyle,
194 IMEConvertedTextUnderlineStyle,
195 IMESelectedConvertedTextUnderline,
196 SpellCheckerUnderlineStyle,
199 * If this metric != 0, support window dragging on the menubar.
201 MenuBarDrag,
203 * 0: scrollbar button repeats to scroll only when cursor is on the button.
204 * 1: scrollbar button repeats to scroll even if cursor is outside of it.
206 ScrollbarButtonAutoRepeatBehavior,
208 * Delay before showing a tooltip.
210 TooltipDelay,
212 * A Boolean value to determine whether swipe animations should be used.
214 SwipeAnimationEnabled,
217 * Controls whether overlay scrollbars display when the user moves
218 * the mouse in a scrollable frame.
220 ScrollbarDisplayOnMouseMove,
223 * Overlay scrollbar animation constants.
225 ScrollbarFadeBeginDelay,
226 ScrollbarFadeDuration,
229 * Distance in pixels to offset the context menu from the cursor
230 * on open.
232 ContextMenuOffsetVertical,
233 ContextMenuOffsetHorizontal,
236 * A boolean value indicating whether client-side decorations are
237 * supported by the user's GTK version.
239 GTKCSDAvailable,
242 * A boolean value indicating whether client-side decorations should
243 * contain a minimize button.
245 GTKCSDMinimizeButton,
248 * A boolean value indicating whether client-side decorations should
249 * contain a maximize button.
251 GTKCSDMaximizeButton,
254 * A boolean value indicating whether client-side decorations should
255 * contain a close button.
257 GTKCSDCloseButton,
260 * An Integer value that will represent the position of the Minimize button
261 * in GTK Client side decoration header.
263 GTKCSDMinimizeButtonPosition,
266 * An Integer value that will represent the position of the Maximize button
267 * in GTK Client side decoration header.
269 GTKCSDMaximizeButtonPosition,
272 * An Integer value that will represent the position of the Close button
273 * in GTK Client side decoration header.
275 GTKCSDCloseButtonPosition,
278 * A boolean value indicating whether titlebar buttons are located
279 * in left titlebar corner.
281 GTKCSDReversedPlacement,
284 * A boolean value indicating whether or not the OS is using a dark theme,
285 * which we may want to switch to as well if not overridden by the user.
287 SystemUsesDarkTheme,
290 * Corresponding to prefers-reduced-motion.
291 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-motion
292 * 0: no-preference
293 * 1: reduce
296 PrefersReducedMotion,
298 * Corresponding to PointerCapabilities in ServoTypes.h
299 * 0: None
300 * 1: Coarse
301 * 2: Fine
302 * 4: Hover
304 PrimaryPointerCapabilities,
306 * Corresponding to union of PointerCapabilities values in ServoTypes.h
307 * E.g. if there is a mouse and a digitizer, the value will be
308 * 'Coarse | Fine | Hover'.
310 AllPointerCapabilities,
312 /** The scrollbar size, in CSS pixels. */
313 SystemScrollbarSize,
315 /** A boolean value to determine whether a touch device is present */
316 TouchDeviceSupportPresent,
318 /** GTK titlebar radius */
319 TitlebarRadius,
321 /** GTK menu radius */
322 GtkMenuRadius,
325 * Corresponding to dynamic-range.
326 * https://drafts.csswg.org/mediaqueries-5/#dynamic-range
327 * 0: Standard
328 * 1: High
330 DynamicRange,
331 VideoDynamicRange,
333 /** Whether XUL panel animations are enabled. */
334 PanelAnimations,
337 * Not an ID; used to define the range of valid IDs. Must be last.
339 End,
342 // This is a common enough integer that seems worth the shortcut.
343 static bool UseOverlayScrollbars() {
344 return GetInt(IntID::UseOverlayScrollbars);
347 // Returns keyCode value of a modifier key which is used for accesskey.
348 // Returns 0 if the platform doesn't support access key.
349 static uint32_t GetMenuAccessKey();
350 // Modifier mask for the menu accesskey.
351 static Modifiers GetMenuAccessKeyModifiers();
353 enum {
354 eScrollArrow_None = 0,
355 eScrollArrow_StartBackward = 0x1000,
356 eScrollArrow_StartForward = 0x0100,
357 eScrollArrow_EndBackward = 0x0010,
358 eScrollArrow_EndForward = 0x0001
361 enum {
362 // single arrow at each end
363 eScrollArrowStyle_Single =
364 eScrollArrow_StartBackward | eScrollArrow_EndForward,
365 // both arrows at bottom/right, none at top/left
366 eScrollArrowStyle_BothAtBottom =
367 eScrollArrow_EndBackward | eScrollArrow_EndForward,
368 // both arrows at both ends
369 eScrollArrowStyle_BothAtEachEnd =
370 eScrollArrow_EndBackward | eScrollArrow_EndForward |
371 eScrollArrow_StartBackward | eScrollArrow_StartForward,
372 // both arrows at top/left, none at bottom/right
373 eScrollArrowStyle_BothAtTop =
374 eScrollArrow_StartBackward | eScrollArrow_StartForward
377 // When modifying this list, also modify nsXPLookAndFeel::sFloatPrefs
378 // in widget/nsXPLookAndFeel.cpp.
379 enum class FloatID {
380 IMEUnderlineRelativeSize,
381 SpellCheckerUnderlineRelativeSize,
383 // The width/height ratio of the cursor. If used, the CaretWidth int metric
384 // should be added to the calculated caret width.
385 CaretAspectRatio,
387 // GTK text scale factor.
388 TextScaleFactor,
390 // Mouse pointer scaling factor.
391 CursorScale,
393 // Not an ID; used to define the range of valid IDs. Must be last.
394 End,
397 using FontID = mozilla::StyleSystemFont;
399 static bool WindowsNonNativeMenusEnabled();
401 static ColorScheme SystemColorScheme() {
402 return GetInt(IntID::SystemUsesDarkTheme) ? ColorScheme::Dark
403 : ColorScheme::Light;
406 static bool IsDarkColor(nscolor);
408 enum class ChromeColorSchemeSetting { Light, Dark, System };
409 static ChromeColorSchemeSetting ColorSchemeSettingForChrome();
410 static ColorScheme ThemeDerivedColorSchemeForContent();
412 static ColorScheme ColorSchemeForChrome() {
413 MOZ_ASSERT(sColorSchemeInitialized);
414 return sChromeColorScheme;
416 static ColorScheme PreferredColorSchemeForContent() {
417 MOZ_ASSERT(sColorSchemeInitialized);
418 return sContentColorScheme;
421 static ColorScheme ColorSchemeForStyle(
422 const dom::Document&, const StyleColorSchemeFlags&,
423 ColorSchemeMode = ColorSchemeMode::Used);
424 static ColorScheme ColorSchemeForFrame(
425 const nsIFrame*, ColorSchemeMode = ColorSchemeMode::Used);
427 // Whether standins for native colors should be used (that is, colors faked,
428 // taken from win7, mostly). This forces light appearance, effectively.
429 enum class UseStandins : bool { No, Yes };
430 static UseStandins ShouldUseStandins(const dom::Document&, ColorID);
432 // Returns a native color value (might be overwritten by prefs) for a given
433 // color id.
435 // NOTE:
436 // ColorID::TextSelectForeground might return NS_SAME_AS_FOREGROUND_COLOR.
437 // ColorID::IME* might return NS_TRANSPARENT, NS_SAME_AS_FOREGROUND_COLOR or
438 // NS_40PERCENT_FOREGROUND_COLOR.
439 // These values have particular meaning. Then, they are not an actual
440 // color value.
441 static Maybe<nscolor> GetColor(ColorID, ColorScheme, UseStandins);
443 // Gets the color with appropriate defaults for UseStandins, ColorScheme etc
444 // for a given frame.
445 static Maybe<nscolor> GetColor(ColorID, const nsIFrame*);
447 // Versions of the above which returns the color if found, or a default (which
448 // defaults to opaque black) otherwise.
449 static nscolor Color(ColorID aId, ColorScheme aScheme,
450 UseStandins aUseStandins,
451 nscolor aDefault = NS_RGB(0, 0, 0)) {
452 return GetColor(aId, aScheme, aUseStandins).valueOr(aDefault);
455 static nscolor Color(ColorID aId, nsIFrame* aFrame,
456 nscolor aDefault = NS_RGB(0, 0, 0)) {
457 return GetColor(aId, aFrame).valueOr(aDefault);
460 static float GetTextScaleFactor() {
461 float f = GetFloat(FloatID::TextScaleFactor, 1.0f);
462 if (MOZ_UNLIKELY(f <= 0.0f)) {
463 return 1.0f;
465 return f;
468 struct ZoomSettings {
469 float mFullZoom = 1.0f;
470 float mTextZoom = 1.0f;
473 static ZoomSettings SystemZoomSettings();
476 * GetInt() and GetFloat() return a int or float value for aID. The result
477 * might be distance, time, some flags or a int value which has particular
478 * meaning. See each document at definition of each ID for the detail.
479 * The result is always 0 when they return error. Therefore, if you want to
480 * use a value for the default value, you should use the other method which
481 * returns int or float directly.
483 static nsresult GetInt(IntID, int32_t* aResult);
484 static nsresult GetFloat(FloatID aID, float* aResult);
486 static int32_t GetInt(IntID aID, int32_t aDefault = 0) {
487 int32_t result;
488 if (NS_FAILED(GetInt(aID, &result))) {
489 return aDefault;
491 return result;
494 static float GetFloat(FloatID aID, float aDefault = 0.0f) {
495 float result;
496 if (NS_FAILED(GetFloat(aID, &result))) {
497 return aDefault;
499 return result;
503 * Retrieve the name and style of a system-theme font. Returns true
504 * if the system theme specifies this font, false if a default should
505 * be used. In the latter case neither aName nor aStyle is modified.
507 * Size of the font should be in CSS pixels, not device pixels.
509 * @param aID Which system-theme font is wanted.
510 * @param aName The name of the font to use.
511 * @param aStyle Styling to apply to the font.
513 static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
516 * GetPasswordCharacter() returns a unicode character which should be used
517 * for a masked character in password editor. E.g., '*'.
519 static char16_t GetPasswordCharacter();
522 * If the latest character in password field shouldn't be hidden by the
523 * result of GetPasswordCharacter(), GetEchoPassword() returns TRUE.
524 * Otherwise, FALSE.
526 static bool GetEchoPassword();
529 * Whether we should be drawing in the titlebar by default.
531 static bool DrawInTitlebar();
534 * The millisecond to mask password value.
535 * This value is only valid when GetEchoPassword() returns true.
537 static uint32_t GetPasswordMaskDelay();
539 /** Gets theme information for about:support */
540 static void GetThemeInfo(nsACString&);
543 * When system look and feel is changed, Refresh() must be called. Then,
544 * cached data would be released.
546 static void Refresh();
549 * GTK's initialization code can't be run off main thread, call this
550 * if you plan on using LookAndFeel off main thread later.
552 * This initialized state may get reset due to theme changes, so it
553 * must be called prior to each potential off-main-thread LookAndFeel
554 * call, not just once.
556 static void NativeInit();
558 static void SetData(widget::FullLookAndFeel&& aTables);
559 static void NotifyChangedAllWindows(widget::ThemeChangeKind);
560 static bool HasPendingGlobalThemeChange() { return sGlobalThemeChanged; }
561 static void HandleGlobalThemeChange() {
562 if (MOZ_UNLIKELY(HasPendingGlobalThemeChange())) {
563 DoHandleGlobalThemeChange();
566 static void EnsureColorSchemesInitialized() {
567 if (!sColorSchemeInitialized) {
568 RecomputeColorSchemes();
570 MOZ_ASSERT(sColorSchemeInitialized);
573 static ColorScheme sChromeColorScheme;
574 static ColorScheme sContentColorScheme;
576 protected:
577 static void RecomputeColorSchemes();
578 static bool sColorSchemeInitialized;
580 static void DoHandleGlobalThemeChange();
581 // Set to true when ThemeChanged needs to be called on mTheme (and other
582 // global LookAndFeel. This is used because mTheme is a service, so there's
583 // no need to notify it from more than one prescontext.
584 static bool sGlobalThemeChanged;
587 } // namespace mozilla
589 // ---------------------------------------------------------------------
590 // Special colors for ColorID::IME* and ColorID::SpellCheckerUnderline
591 // ---------------------------------------------------------------------
593 // For background color only.
594 constexpr nscolor NS_TRANSPARENT = NS_RGBA(0x01, 0x00, 0x00, 0x00);
595 // For foreground color only.
596 constexpr nscolor NS_SAME_AS_FOREGROUND_COLOR = NS_RGBA(0x02, 0x00, 0x00, 0x00);
597 constexpr nscolor NS_40PERCENT_FOREGROUND_COLOR =
598 NS_RGBA(0x03, 0x00, 0x00, 0x00);
600 #define NS_IS_SELECTION_SPECIAL_COLOR(c) \
601 ((c) == NS_TRANSPARENT || (c) == NS_SAME_AS_FOREGROUND_COLOR || \
602 (c) == NS_40PERCENT_FOREGROUND_COLOR)
604 // ------------------------------------------
605 // Bits for IntID::AlertNotificationOrigin
606 // ------------------------------------------
608 #define NS_ALERT_HORIZONTAL 1
609 #define NS_ALERT_LEFT 2
610 #define NS_ALERT_TOP 4
612 #endif /* __LookAndFeel */