Backed out changeset a6e4191fa2ce (bug 1919287) for causing 1923870. a=backout
[gecko.git] / widget / LookAndFeel.h
blob1b269714a4a3c02fd958479b1ecc9483287c87ea
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 // select textfields when focused via tab/accesskey?
57 SelectTextfieldsOnKeyFocus,
58 // delay before submenus open
59 SubmenuDelay,
60 // can popups overlap menu/task bar?
61 MenusCanOverlapOSBar,
62 // should overlay scrollbars be used?
63 UseOverlayScrollbars,
64 // allow H and V overlay scrollbars to overlap?
65 AllowOverlayScrollbarsOverlap,
66 // skip navigating to disabled menu item?
67 SkipNavigatingDisabledMenuItem,
68 // begin a drag if the mouse is moved further than the threshold while the
69 // button is down
70 DragThresholdX,
71 DragThresholdY,
72 // Accessibility theme being used?
73 UseAccessibilityTheme,
75 // position of scroll arrows in a scrollbar
76 ScrollArrowStyle,
78 // each button can take one of four values:
79 ScrollButtonLeftMouseButtonAction,
80 // 0 - scrolls one line, 1 - scrolls one page
81 ScrollButtonMiddleMouseButtonAction,
82 // 2 - scrolls to end, 3 - button ignored
83 ScrollButtonRightMouseButtonAction,
85 // delay for opening spring loaded folders
86 TreeOpenDelay,
87 // delay for closing spring loaded folders
88 TreeCloseDelay,
89 // delay for triggering the tree scrolling
90 TreeLazyScrollDelay,
91 // delay for scrolling the tree
92 TreeScrollDelay,
93 // the maximum number of lines to be scrolled at ones
94 TreeScrollLinesMax,
95 // Should menu items blink when they're chosen?
96 ChosenMenuItemsShouldBlink,
99 * A Boolean value to determine whether the Windows accent color
100 * should be applied to the title bar.
102 * The value of this metric is not used on other platforms. These platforms
103 * should return NS_ERROR_NOT_IMPLEMENTED when queried for this metric.
105 WindowsAccentColorInTitlebar,
108 * A Boolean value to determine whether the macOS Big Sur-specific
109 * theming should be used.
111 MacBigSurTheme,
114 * A Boolean value to determine whether macOS is in RTL mode or not.
116 MacRTL,
118 /* Native macOS titlebar height. */
119 MacTitlebarHeight,
122 * AlertNotificationOrigin indicates from which corner of the
123 * screen alerts slide in, and from which direction (horizontal/vertical).
124 * 0, the default, represents bottom right, sliding vertically.
125 * Use any bitwise combination of the following constants:
126 * NS_ALERT_HORIZONTAL (1), NS_ALERT_LEFT (2), NS_ALERT_TOP (4).
128 * 6 4
129 * +-----------+
130 * 7| |5
131 * | |
132 * 3| |1
133 * +-----------+
134 * 2 0
136 AlertNotificationOrigin,
139 * If true, clicking on a scrollbar (not as in dragging the thumb) defaults
140 * to scrolling the view corresponding to the clicked point. Otherwise, we
141 * only do so if the scrollbar is clicked using the middle mouse button or
142 * if shift is pressed when the scrollbar is clicked.
144 ScrollToClick,
147 * IME and spell checker underline styles, the values should be
148 * NS_DECORATION_LINE_STYLE_*. They are defined below.
150 IMERawInputUnderlineStyle,
151 IMESelectedRawTextUnderlineStyle,
152 IMEConvertedTextUnderlineStyle,
153 IMESelectedConvertedTextUnderline,
154 SpellCheckerUnderlineStyle,
157 * If this metric != 0, support window dragging on the menubar.
159 MenuBarDrag,
161 * 0: scrollbar button repeats to scroll only when cursor is on the button.
162 * 1: scrollbar button repeats to scroll even if cursor is outside of it.
164 ScrollbarButtonAutoRepeatBehavior,
166 * A Boolean value to determine whether swipe animations should be used.
168 SwipeAnimationEnabled,
171 * Controls whether overlay scrollbars display when the user moves
172 * the mouse in a scrollable frame.
174 ScrollbarDisplayOnMouseMove,
177 * Overlay scrollbar animation constants.
179 ScrollbarFadeBeginDelay,
180 ScrollbarFadeDuration,
183 * Distance in pixels to offset the context menu from the cursor
184 * on open.
186 ContextMenuOffsetVertical,
187 ContextMenuOffsetHorizontal,
188 TooltipOffsetVertical,
191 * A boolean value indicating whether client-side decorations are
192 * supported by the user's GTK version.
194 GTKCSDAvailable,
197 * A boolean value indicating whether semi-transparent
198 * windows are available.
200 GTKCSDTransparencyAvailable,
203 * A boolean value indicating whether client-side decorations should
204 * contain a minimize button.
206 GTKCSDMinimizeButton,
209 * A boolean value indicating whether client-side decorations should
210 * contain a maximize button.
212 GTKCSDMaximizeButton,
215 * A boolean value indicating whether client-side decorations should
216 * contain a close button.
218 GTKCSDCloseButton,
221 * An Integer value that will represent the position of the Minimize button
222 * in GTK Client side decoration header.
224 GTKCSDMinimizeButtonPosition,
227 * An Integer value that will represent the position of the Maximize button
228 * in GTK Client side decoration header.
230 GTKCSDMaximizeButtonPosition,
233 * An Integer value that will represent the position of the Close button
234 * in GTK Client side decoration header.
236 GTKCSDCloseButtonPosition,
239 * A boolean value indicating whether titlebar buttons are located
240 * in left titlebar corner.
242 GTKCSDReversedPlacement,
245 * A boolean value indicating whether or not the OS is using a dark theme,
246 * which we may want to switch to as well if not overridden by the user.
248 SystemUsesDarkTheme,
251 * Corresponding to prefers-reduced-motion.
252 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-motion
253 * 0: no-preference
254 * 1: reduce
256 PrefersReducedMotion,
259 * Corresponding to prefers-reduced-transparency.
260 * https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-transparency
261 * 0: no-preference
262 * 1: reduce
264 PrefersReducedTransparency,
267 * Corresponding to inverted-colors.
268 * https://drafts.csswg.org/mediaqueries-5/#inverted
269 * 0: none
270 * 1: inverted
272 InvertedColors,
275 * Corresponding to PointerCapabilities in ServoTypes.h
276 * 0: None
277 * 1: Coarse
278 * 2: Fine
279 * 4: Hover
281 PrimaryPointerCapabilities,
283 * Corresponding to union of PointerCapabilities values in ServoTypes.h
284 * E.g. if there is a mouse and a digitizer, the value will be
285 * 'Coarse | Fine | Hover'.
287 AllPointerCapabilities,
289 /** The scrollbar size, in CSS pixels. */
290 SystemScrollbarSize,
292 /** A boolean value to determine whether a touch device is present */
293 TouchDeviceSupportPresent,
295 /** GTK titlebar radius */
296 TitlebarRadius,
298 /** GTK button-to-button spacing in the inline axis */
299 TitlebarButtonSpacing,
302 * Corresponding to dynamic-range.
303 * https://drafts.csswg.org/mediaqueries-5/#dynamic-range
304 * 0: Standard
305 * 1: High
307 DynamicRange,
309 /** Whether XUL panel animations are enabled. */
310 PanelAnimations,
312 /* Whether we should hide the cursor while typing */
313 HideCursorWhileTyping,
315 /* The StyleGtkThemeFamily of the current GTK theme. */
316 GTKThemeFamily,
318 /* Whether macOS' full keyboard access is enabled */
319 FullKeyboardAccess,
322 * Not an ID; used to define the range of valid IDs. Must be last.
324 End,
327 // This is a common enough integer that seems worth the shortcut.
328 static bool UseOverlayScrollbars() {
329 return GetInt(IntID::UseOverlayScrollbars);
332 static constexpr int32_t kDefaultTooltipOffset = 21;
333 static int32_t TooltipOffsetVertical() {
334 return GetInt(IntID::TooltipOffsetVertical, kDefaultTooltipOffset);
337 // Returns keyCode value of a modifier key which is used for accesskey.
338 // Returns 0 if the platform doesn't support access key.
339 static uint32_t GetMenuAccessKey();
340 // Modifier mask for the menu accesskey.
341 static Modifiers GetMenuAccessKeyModifiers();
343 enum {
344 eScrollArrow_None = 0,
345 eScrollArrow_StartBackward = 0x1000,
346 eScrollArrow_StartForward = 0x0100,
347 eScrollArrow_EndBackward = 0x0010,
348 eScrollArrow_EndForward = 0x0001
351 enum {
352 // single arrow at each end
353 eScrollArrowStyle_Single =
354 eScrollArrow_StartBackward | eScrollArrow_EndForward,
355 // both arrows at bottom/right, none at top/left
356 eScrollArrowStyle_BothAtBottom =
357 eScrollArrow_EndBackward | eScrollArrow_EndForward,
358 // both arrows at both ends
359 eScrollArrowStyle_BothAtEachEnd =
360 eScrollArrow_EndBackward | eScrollArrow_EndForward |
361 eScrollArrow_StartBackward | eScrollArrow_StartForward,
362 // both arrows at top/left, none at bottom/right
363 eScrollArrowStyle_BothAtTop =
364 eScrollArrow_StartBackward | eScrollArrow_StartForward
367 // When modifying this list, also modify nsXPLookAndFeel::sFloatPrefs
368 // in widget/nsXPLookAndFeel.cpp.
369 enum class FloatID {
370 IMEUnderlineRelativeSize,
371 SpellCheckerUnderlineRelativeSize,
373 // The width/height ratio of the cursor. If used, the CaretWidth int metric
374 // should be added to the calculated caret width.
375 CaretAspectRatio,
377 // GTK text scale factor.
378 TextScaleFactor,
380 // Mouse pointer scaling factor.
381 CursorScale,
383 // Not an ID; used to define the range of valid IDs. Must be last.
384 End,
387 using FontID = mozilla::StyleSystemFont;
389 static ColorScheme SystemColorScheme() {
390 return GetInt(IntID::SystemUsesDarkTheme) ? ColorScheme::Dark
391 : ColorScheme::Light;
394 static bool IsDarkColor(nscolor);
396 static ColorScheme ColorSchemeForStyle(
397 const dom::Document&, const StyleColorSchemeFlags&,
398 ColorSchemeMode = ColorSchemeMode::Used);
399 static ColorScheme ColorSchemeForFrame(
400 const nsIFrame*, ColorSchemeMode = ColorSchemeMode::Used);
402 // Whether standins for native colors should be used (that is, colors faked,
403 // taken from win7, mostly). This forces light appearance, effectively.
404 enum class UseStandins : bool { No, Yes };
405 static UseStandins ShouldUseStandins(const dom::Document&, ColorID);
407 // Returns a native color value (might be overwritten by prefs) for a given
408 // color id.
410 // NOTE:
411 // ColorID::TextSelectForeground might return NS_SAME_AS_FOREGROUND_COLOR.
412 // ColorID::IME* might return NS_TRANSPARENT, NS_SAME_AS_FOREGROUND_COLOR or
413 // NS_40PERCENT_FOREGROUND_COLOR.
414 // These values have particular meaning. Then, they are not an actual
415 // color value.
416 static Maybe<nscolor> GetColor(ColorID, ColorScheme, UseStandins);
418 // Gets the color with appropriate defaults for UseStandins, ColorScheme etc
419 // for a given frame.
420 static Maybe<nscolor> GetColor(ColorID, const nsIFrame*);
422 // Versions of the above which returns the color if found, or a default (which
423 // defaults to opaque black) otherwise.
424 static nscolor Color(ColorID aId, ColorScheme aScheme,
425 UseStandins aUseStandins,
426 nscolor aDefault = NS_RGB(0, 0, 0)) {
427 return GetColor(aId, aScheme, aUseStandins).valueOr(aDefault);
430 static nscolor Color(ColorID aId, nsIFrame* aFrame,
431 nscolor aDefault = NS_RGB(0, 0, 0)) {
432 return GetColor(aId, aFrame).valueOr(aDefault);
435 static float GetTextScaleFactor() {
436 float f = GetFloat(FloatID::TextScaleFactor, 1.0f);
437 if (MOZ_UNLIKELY(f <= 0.0f)) {
438 return 1.0f;
440 return f;
443 struct ZoomSettings {
444 float mFullZoom = 1.0f;
445 float mTextZoom = 1.0f;
448 static ZoomSettings SystemZoomSettings();
451 * GetInt() and GetFloat() return a int or float value for aID. The result
452 * might be distance, time, some flags or a int value which has particular
453 * meaning. See each document at definition of each ID for the detail.
454 * The result is always 0 when they return error. Therefore, if you want to
455 * use a value for the default value, you should use the other method which
456 * returns int or float directly.
458 static nsresult GetInt(IntID, int32_t* aResult);
459 static nsresult GetFloat(FloatID aID, float* aResult);
461 static int32_t GetInt(IntID aID, int32_t aDefault = 0) {
462 int32_t result;
463 if (NS_FAILED(GetInt(aID, &result))) {
464 return aDefault;
466 return result;
469 static float GetFloat(FloatID aID, float aDefault = 0.0f) {
470 float result;
471 if (NS_FAILED(GetFloat(aID, &result))) {
472 return aDefault;
474 return result;
478 * Retrieve the name and style of a system-theme font. Returns true
479 * if the system theme specifies this font, false if a default should
480 * be used. In the latter case neither aName nor aStyle is modified.
482 * Size of the font should be in CSS pixels, not device pixels.
484 * @param aID Which system-theme font is wanted.
485 * @param aName The name of the font to use.
486 * @param aStyle Styling to apply to the font.
488 static bool GetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle);
491 * GetPasswordCharacter() returns a unicode character which should be used
492 * for a masked character in password editor. E.g., '*'.
494 static char16_t GetPasswordCharacter();
497 * If the latest character in password field shouldn't be hidden by the
498 * result of GetPasswordCharacter(), GetEchoPassword() returns TRUE.
499 * Otherwise, FALSE.
501 static bool GetEchoPassword();
504 * Whether we should be drawing in the titlebar by default.
506 static bool DrawInTitlebar();
508 enum class TitlebarAction {
509 None,
510 WindowLower,
511 WindowMenu,
512 WindowMinimize,
513 WindowMaximize,
514 WindowMaximizeToggle,
515 // We don't support more actions (maximize-horizontal, maximize-vertical,..)
516 // as they're implemented as part of Wayland gtk_surface1 protocol
517 // which is not accessible to us.
520 enum class TitlebarEvent {
521 Double_Click,
522 Middle_Click,
526 * Get system defined action for titlebar events.
528 static TitlebarAction GetTitlebarAction(TitlebarEvent aEvent);
531 * The millisecond to mask password value.
532 * This value is only valid when GetEchoPassword() returns true.
534 static uint32_t GetPasswordMaskDelay();
536 /** Gets theme information for about:support */
537 static void GetThemeInfo(nsACString&);
540 * When system look and feel is changed, Refresh() must be called. Then,
541 * cached data would be released.
543 static void Refresh();
546 * GTK's initialization code can't be run off main thread, call this
547 * if you plan on using LookAndFeel off main thread later.
549 * This initialized state may get reset due to theme changes, so it
550 * must be called prior to each potential off-main-thread LookAndFeel
551 * call, not just once.
553 static void NativeInit();
555 static void SetData(widget::FullLookAndFeel&& aTables);
556 static void NotifyChangedAllWindows(widget::ThemeChangeKind);
557 static bool HasPendingGlobalThemeChange() { return sGlobalThemeChanged; }
558 static void HandleGlobalThemeChange() {
559 if (MOZ_UNLIKELY(HasPendingGlobalThemeChange())) {
560 DoHandleGlobalThemeChange();
564 static nsresult GetKeyboardLayout(nsACString& aLayout);
566 protected:
567 static void DoHandleGlobalThemeChange();
568 // Set to true when ThemeChanged needs to be called on mTheme (and other
569 // global LookAndFeel. This is used because mTheme is a service, so there's
570 // no need to notify it from more than one prescontext.
571 static bool sGlobalThemeChanged;
574 } // namespace mozilla
576 // ---------------------------------------------------------------------
577 // Special colors for ColorID::IME* and ColorID::SpellCheckerUnderline
578 // ---------------------------------------------------------------------
580 // For background color only.
581 constexpr nscolor NS_TRANSPARENT = NS_RGBA(0x01, 0x00, 0x00, 0x00);
582 // For foreground color only.
583 constexpr nscolor NS_SAME_AS_FOREGROUND_COLOR = NS_RGBA(0x02, 0x00, 0x00, 0x00);
584 constexpr nscolor NS_40PERCENT_FOREGROUND_COLOR =
585 NS_RGBA(0x03, 0x00, 0x00, 0x00);
587 #define NS_IS_SELECTION_SPECIAL_COLOR(c) \
588 ((c) == NS_TRANSPARENT || (c) == NS_SAME_AS_FOREGROUND_COLOR || \
589 (c) == NS_40PERCENT_FOREGROUND_COLOR)
591 // ------------------------------------------
592 // Bits for IntID::AlertNotificationOrigin
593 // ------------------------------------------
595 #define NS_ALERT_HORIZONTAL 1
596 #define NS_ALERT_LEFT 2
597 #define NS_ALERT_TOP 4
599 #endif /* __LookAndFeel */