Bug 1779627 - Migrate toolkit/components/mozintl/mozIntl.jsm to esm; r=nordzilla
[gecko.git] / widget / LookAndFeel.h
blob7fd1a38e59ca366e2b163519a0143cda880fb0fe
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 ColorScheme SystemColorScheme() {
392 return GetInt(IntID::SystemUsesDarkTheme) ? ColorScheme::Dark
393 : ColorScheme::Light;
396 static bool IsDarkColor(nscolor);
398 enum class ChromeColorSchemeSetting { Light, Dark, System };
399 static ChromeColorSchemeSetting ColorSchemeSettingForChrome();
400 static ColorScheme ThemeDerivedColorSchemeForContent();
402 static ColorScheme ColorSchemeForChrome() {
403 MOZ_ASSERT(sColorSchemeInitialized);
404 return sChromeColorScheme;
406 static ColorScheme PreferredColorSchemeForContent() {
407 MOZ_ASSERT(sColorSchemeInitialized);
408 return sContentColorScheme;
411 static ColorScheme ColorSchemeForStyle(const dom::Document&,
412 const StyleColorSchemeFlags&);
413 static ColorScheme ColorSchemeForFrame(const nsIFrame*);
415 // Whether standins for native colors should be used (that is, colors faked,
416 // taken from win7, mostly). This forces light appearance, effectively.
417 enum class UseStandins : bool { No, Yes };
418 static UseStandins ShouldUseStandins(const dom::Document&, ColorID);
420 // Returns a native color value (might be overwritten by prefs) for a given
421 // color id.
423 // NOTE:
424 // ColorID::TextSelectForeground might return NS_SAME_AS_FOREGROUND_COLOR.
425 // ColorID::IME* might return NS_TRANSPARENT, NS_SAME_AS_FOREGROUND_COLOR or
426 // NS_40PERCENT_FOREGROUND_COLOR.
427 // These values have particular meaning. Then, they are not an actual
428 // color value.
429 static Maybe<nscolor> GetColor(ColorID, ColorScheme, UseStandins);
431 // Gets the color with appropriate defaults for UseStandins, ColorScheme etc
432 // for a given frame.
433 static Maybe<nscolor> GetColor(ColorID, const nsIFrame*);
435 // Versions of the above which returns the color if found, or a default (which
436 // defaults to opaque black) otherwise.
437 static nscolor Color(ColorID aId, ColorScheme aScheme,
438 UseStandins aUseStandins,
439 nscolor aDefault = NS_RGB(0, 0, 0)) {
440 return GetColor(aId, aScheme, aUseStandins).valueOr(aDefault);
443 static nscolor Color(ColorID aId, nsIFrame* aFrame,
444 nscolor aDefault = NS_RGB(0, 0, 0)) {
445 return GetColor(aId, aFrame).valueOr(aDefault);
448 static float GetTextScaleFactor() {
449 float f = GetFloat(FloatID::TextScaleFactor, 1.0f);
450 if (MOZ_UNLIKELY(f <= 0.0f)) {
451 return 1.0f;
453 return f;
456 struct ZoomSettings {
457 float mFullZoom = 1.0f;
458 float mTextZoom = 1.0f;
461 static ZoomSettings SystemZoomSettings();
464 * GetInt() and GetFloat() return a int or float value for aID. The result
465 * might be distance, time, some flags or a int value which has particular
466 * meaning. See each document at definition of each ID for the detail.
467 * The result is always 0 when they return error. Therefore, if you want to
468 * use a value for the default value, you should use the other method which
469 * returns int or float directly.
471 static nsresult GetInt(IntID, int32_t* aResult);
472 static nsresult GetFloat(FloatID aID, float* aResult);
474 static int32_t GetInt(IntID aID, int32_t aDefault = 0) {
475 int32_t result;
476 if (NS_FAILED(GetInt(aID, &result))) {
477 return aDefault;
479 return result;
482 static float GetFloat(FloatID aID, float aDefault = 0.0f) {
483 float result;
484 if (NS_FAILED(GetFloat(aID, &result))) {
485 return aDefault;
487 return result;
491 * Retrieve the name and style of a system-theme font. Returns true
492 * if the system theme specifies this font, false if a default should
493 * be used. In the latter case neither aName nor aStyle is modified.
495 * Size of the font should be in CSS pixels, not device pixels.
497 * @param aID Which system-theme font is wanted.
498 * @param aName The name of the font to use.
499 * @param aStyle Styling to apply to the font.
501 static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
504 * GetPasswordCharacter() returns a unicode character which should be used
505 * for a masked character in password editor. E.g., '*'.
507 static char16_t GetPasswordCharacter();
510 * If the latest character in password field shouldn't be hidden by the
511 * result of GetPasswordCharacter(), GetEchoPassword() returns TRUE.
512 * Otherwise, FALSE.
514 static bool GetEchoPassword();
517 * Whether we should be drawing in the titlebar by default.
519 static bool DrawInTitlebar();
522 * The millisecond to mask password value.
523 * This value is only valid when GetEchoPassword() returns true.
525 static uint32_t GetPasswordMaskDelay();
527 /** Gets theme information for about:support */
528 static void GetThemeInfo(nsACString&);
531 * When system look and feel is changed, Refresh() must be called. Then,
532 * cached data would be released.
534 static void Refresh();
537 * GTK's initialization code can't be run off main thread, call this
538 * if you plan on using LookAndFeel off main thread later.
540 * This initialized state may get reset due to theme changes, so it
541 * must be called prior to each potential off-main-thread LookAndFeel
542 * call, not just once.
544 static void NativeInit();
546 static void SetData(widget::FullLookAndFeel&& aTables);
547 static void NotifyChangedAllWindows(widget::ThemeChangeKind);
548 static bool HasPendingGlobalThemeChange() { return sGlobalThemeChanged; }
549 static void HandleGlobalThemeChange() {
550 if (MOZ_UNLIKELY(HasPendingGlobalThemeChange())) {
551 DoHandleGlobalThemeChange();
554 static void EnsureColorSchemesInitialized() {
555 if (!sColorSchemeInitialized) {
556 RecomputeColorSchemes();
558 MOZ_ASSERT(sColorSchemeInitialized);
561 static ColorScheme sChromeColorScheme;
562 static ColorScheme sContentColorScheme;
564 protected:
565 static void RecomputeColorSchemes();
566 static bool sColorSchemeInitialized;
568 static void DoHandleGlobalThemeChange();
569 // Set to true when ThemeChanged needs to be called on mTheme (and other
570 // global LookAndFeel. This is used because mTheme is a service, so there's
571 // no need to notify it from more than one prescontext.
572 static bool sGlobalThemeChanged;
575 } // namespace mozilla
577 // ---------------------------------------------------------------------
578 // Special colors for ColorID::IME* and ColorID::SpellCheckerUnderline
579 // ---------------------------------------------------------------------
581 // For background color only.
582 constexpr nscolor NS_TRANSPARENT = NS_RGBA(0x01, 0x00, 0x00, 0x00);
583 // For foreground color only.
584 constexpr nscolor NS_SAME_AS_FOREGROUND_COLOR = NS_RGBA(0x02, 0x00, 0x00, 0x00);
585 constexpr nscolor NS_40PERCENT_FOREGROUND_COLOR =
586 NS_RGBA(0x03, 0x00, 0x00, 0x00);
588 #define NS_IS_SELECTION_SPECIAL_COLOR(c) \
589 ((c) == NS_TRANSPARENT || (c) == NS_SAME_AS_FOREGROUND_COLOR || \
590 (c) == NS_40PERCENT_FOREGROUND_COLOR)
592 // ------------------------------------------
593 // Bits for IntID::AlertNotificationOrigin
594 // ------------------------------------------
596 #define NS_ALERT_HORIZONTAL 1
597 #define NS_ALERT_LEFT 2
598 #define NS_ALERT_TOP 4
600 #endif /* __LookAndFeel */