File: native_theme_mac.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (187 lines) | stat: -rw-r--r-- 7,417 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_NATIVE_THEME_NATIVE_THEME_MAC_H_
#define UI_NATIVE_THEME_NATIVE_THEME_MAC_H_

#include "base/component_export.h"
#include "base/no_destructor.h"
#include "ui/gfx/geometry/size.h"
#include "ui/native_theme/native_theme_aura.h"
#include "ui/native_theme/native_theme_base.h"

@class NativeThemeEffectiveAppearanceObserver;

namespace ui {

// Mac implementation of native theme support.
class COMPONENT_EXPORT(NATIVE_THEME) NativeThemeMac : public NativeThemeBase {
 public:
  static const int kButtonCornerRadius = 3;

  // Type of gradient to use on a button background. Use HIGHLIGHTED for the
  // default button of a window and all combobox controls, but only when the
  // window is active.
  enum class ButtonBackgroundType {
    DISABLED,
    HIGHLIGHTED,
    NORMAL,
    PRESSED,
    COUNT
  };

  NativeThemeMac(const NativeThemeMac&) = delete;
  NativeThemeMac& operator=(const NativeThemeMac&) = delete;

  // NativeTheme:
  SkColor GetSystemButtonPressedColor(SkColor base_color) const override;
  PreferredContrast CalculatePreferredContrast() const override;

  // NativeThemeBase:
  void Paint(cc::PaintCanvas* canvas,
             const ColorProvider* color_provider,
             Part part,
             State state,
             const gfx::Rect& rect,
             const ExtraParams& extra,
             ColorScheme color_scheme,
             bool in_forced_colors,
             const std::optional<SkColor>& accent_color) const override;
  void PaintMenuPopupBackground(
      cc::PaintCanvas* canvas,
      const ColorProvider* color_provider,
      const gfx::Size& size,
      const MenuBackgroundExtraParams& menu_background,
      ColorScheme color_scheme) const override;
  void PaintMenuItemBackground(cc::PaintCanvas* canvas,
                               const ColorProvider* color_provider,
                               State state,
                               const gfx::Rect& rect,
                               const MenuItemExtraParams& menu_item,
                               ColorScheme color_scheme) const override;
  void PaintMacScrollbarThumb(cc::PaintCanvas* canvas,
                              Part part,
                              State state,
                              const gfx::Rect& rect,
                              const ScrollbarExtraParams& scroll_thumb,
                              ColorScheme color_scheme) const;
  // Paint the track. |track_bounds| is the bounds for the track.
  void PaintMacScrollBarTrackOrCorner(cc::PaintCanvas* canvas,
                                      Part part,
                                      State state,
                                      const ScrollbarExtraParams& extra_params,
                                      const gfx::Rect& rect,
                                      ColorScheme color_scheme,
                                      bool is_corner) const;

  // Paints the styled button shape used for default controls on Mac. The basic
  // style is used for dialog buttons, comboboxes, and tabbed pane tabs.
  // Depending on the control part being drawn, the left or the right side can
  // be given rounded corners.
  static void PaintStyledGradientButton(cc::PaintCanvas* canvas,
                                        const gfx::Rect& bounds,
                                        ButtonBackgroundType type,
                                        bool round_left,
                                        bool round_right,
                                        bool focus);

  // Returns the minimum size for the thumb. We will not inset the thumb if it
  // will be smaller than this size. The scale parameter should be the device
  // scale factor.
  static gfx::Size GetThumbMinSize(bool vertical, float scale);

  // This is used in tests to simulate enabling a non-blinking cursor.
  void SetPrefersNonBlinkingCursorForTesting(bool enabled) {
    prefers_non_blinking_cursor_for_testing_ = enabled;
  }

 protected:
  friend class NativeTheme;
  friend class base::NoDestructor<NativeThemeMac>;
  static NativeThemeMac* instance();

  NativeThemeMac(bool configure_web_instance, bool should_only_use_dark_colors);
  ~NativeThemeMac() override;

  // NativeTheme:
  std::optional<base::TimeDelta> GetPlatformCaretBlinkInterval() const override;

 private:
  // Returns true if the user prefers a non blinking cursor.
  bool PrefersNonBlinkingCursor() const;

  // Paint the selected menu item background, and a border for emphasis when in
  // high contrast.
  void PaintSelectedMenuItem(cc::PaintCanvas* canvas,
                             const ColorProvider* color_provider,
                             const gfx::Rect& rect,
                             const MenuItemExtraParams& extra_params) const;

  void PaintScrollBarTrackGradient(cc::PaintCanvas* canvas,
                                   const gfx::Rect& rect,
                                   const ScrollbarExtraParams& extra_params,
                                   bool is_corner,
                                   ColorScheme color_scheme) const;
  void PaintScrollbarTrackInnerBorder(cc::PaintCanvas* canvas,
                                      const gfx::Rect& rect,
                                      const ScrollbarExtraParams& extra_params,
                                      bool is_corner,
                                      ColorScheme color_scheme) const;
  void PaintScrollbarTrackOuterBorder(cc::PaintCanvas* canvas,
                                      const gfx::Rect& rect,
                                      const ScrollbarExtraParams& extra_params,
                                      bool is_corner,
                                      ColorScheme color_scheme) const;

  void InitializeDarkModeStateAndObserver();

  void ConfigureWebInstance() override;

  enum ScrollbarPart {
    kThumb,
    kTrack,
    kTrackInnerBorder,
    kTrackOuterBorder,
  };

  std::optional<SkColor> GetScrollbarColor(
      ScrollbarPart part,
      ColorScheme color_scheme,
      const ScrollbarExtraParams& extra_params) const;

  int ScrollbarTrackBorderWidth(float scale_from_dip) const {
    constexpr float border_width = 1.0f;
    return scale_from_dip * border_width;
  }

  // The amount the thumb is inset from the ends and the inside edge of track
  // border.
  int GetScrollbarThumbInset(bool is_overlay, float scale_from_dip) const {
    return scale_from_dip * (is_overlay ? 2.0f : 3.0f);
  }

  NativeThemeEffectiveAppearanceObserver* __strong appearance_observer_;
  id __strong display_accessibility_notification_token_;
  id __strong non_blinking_cursor_token_;
  bool prefers_non_blinking_cursor_for_testing_ = false;

  // Used to notify the web native theme of changes to dark mode and high
  // contrast.
  std::unique_ptr<NativeTheme::ColorSchemeNativeThemeObserver>
      color_scheme_observer_;
};

// Mac implementation of native theme support for web controls.
// For consistency with older versions of Chrome for Mac, we do multiply
// the border width and radius by the zoom, unlike the generic impl.
class NativeThemeMacWeb : public NativeThemeAura {
 public:
  NativeThemeMacWeb();

  static NativeThemeMacWeb* instance();
};

}  // namespace ui

#endif  // UI_NATIVE_THEME_NATIVE_THEME_MAC_H_