Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / cocoa / nsNativeThemeCocoa.h
blobfca7672979ce97630795dc42a94f4d71d262ed0b
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 nsNativeThemeCocoa_h_
7 #define nsNativeThemeCocoa_h_
9 #import <Carbon/Carbon.h>
10 #import <Cocoa/Cocoa.h>
12 #include "mozilla/Variant.h"
14 #include "nsITheme.h"
15 #include "ThemeCocoa.h"
16 #include "mozilla/dom/RustTypes.h"
18 @class MOZCellDrawWindow;
19 @class MOZCellDrawView;
20 @class MOZSearchFieldCell;
21 @class NSProgressBarCell;
22 class nsDeviceContext;
23 struct SegmentedControlRenderSettings;
25 namespace mozilla {
26 namespace gfx {
27 class DrawTarget;
28 } // namespace gfx
29 } // namespace mozilla
31 class nsNativeThemeCocoa : public mozilla::widget::ThemeCocoa {
32 using ThemeCocoa = mozilla::widget::ThemeCocoa;
34 public:
35 enum class CheckboxOrRadioState : uint8_t { eOff, eOn, eIndeterminate };
37 enum class ButtonType : uint8_t {
38 eRegularPushButton,
39 eDefaultPushButton,
40 eSquareBezelPushButton,
41 eArrowButton,
42 eHelpButton,
43 eDisclosureButtonClosed,
44 eDisclosureButtonOpen
47 enum class SpinButton : uint8_t { eUp, eDown };
49 enum class SegmentType : uint8_t { eToolbarButton, eTab };
51 enum class OptimumState : uint8_t { eOptimum, eSubOptimum, eSubSubOptimum };
53 struct ControlParams {
54 ControlParams()
55 : disabled(false),
56 insideActiveWindow(false),
57 pressed(false),
58 focused(false),
59 rtl(false) {}
61 bool disabled : 1;
62 bool insideActiveWindow : 1;
63 bool pressed : 1;
64 bool focused : 1;
65 bool rtl : 1;
68 struct CheckboxOrRadioParams {
69 ControlParams controlParams;
70 CheckboxOrRadioState state = CheckboxOrRadioState::eOff;
71 float verticalAlignFactor = 0.5f;
74 struct ButtonParams {
75 ControlParams controlParams;
76 ButtonType button = ButtonType::eRegularPushButton;
79 struct DropdownParams {
80 ControlParams controlParams;
81 bool pullsDown = false;
82 bool editable = false;
85 struct SpinButtonParams {
86 mozilla::Maybe<SpinButton> pressedButton;
87 bool disabled = false;
88 bool insideActiveWindow = false;
91 struct SegmentParams {
92 SegmentType segmentType = SegmentType::eToolbarButton;
93 bool insideActiveWindow = false;
94 bool pressed = false;
95 bool selected = false;
96 bool focused = false;
97 bool atLeftEnd = false;
98 bool atRightEnd = false;
99 bool drawsLeftSeparator = false;
100 bool drawsRightSeparator = false;
101 bool rtl = false;
104 struct TextFieldParams {
105 float verticalAlignFactor = 0.5f;
106 bool insideToolbar = false;
107 bool disabled = false;
108 bool focused = false;
109 bool rtl = false;
112 struct ProgressParams {
113 double value = 0.0;
114 double max = 0.0;
115 float verticalAlignFactor = 0.5f;
116 bool insideActiveWindow = false;
117 bool indeterminate = false;
118 bool horizontal = false;
119 bool rtl = false;
122 struct MeterParams {
123 double value = 0;
124 double min = 0;
125 double max = 0;
126 OptimumState optimumState = OptimumState::eOptimum;
127 float verticalAlignFactor = 0.5f;
128 bool horizontal = true;
129 bool rtl = false;
132 struct ScaleParams {
133 int32_t value = 0;
134 int32_t min = 0;
135 int32_t max = 0;
136 bool insideActiveWindow = false;
137 bool disabled = false;
138 bool focused = false;
139 bool horizontal = true;
140 bool reverse = false;
143 enum Widget : uint8_t {
144 eColorFill, // mozilla::gfx::sRGBColor
145 eCheckbox, // CheckboxOrRadioParams
146 eRadio, // CheckboxOrRadioParams
147 eButton, // ButtonParams
148 eDropdown, // DropdownParams
149 eSpinButtons, // SpinButtonParams
150 eSpinButtonUp, // SpinButtonParams
151 eSpinButtonDown, // SpinButtonParams
152 eSegment, // SegmentParams
153 eSeparator,
154 eStatusBar, // bool
155 eGroupBox,
156 eTextField, // TextFieldParams
157 eSearchField, // TextFieldParams
158 eProgressBar, // ProgressParams
159 eMeter, // MeterParams
160 eScale, // ScaleParams
161 eMultilineTextField, // bool
162 eListBox,
163 eTabPanel,
166 struct WidgetInfo {
167 static WidgetInfo ColorFill(const mozilla::gfx::sRGBColor& aParams) {
168 return WidgetInfo(Widget::eColorFill, aParams);
170 static WidgetInfo Checkbox(const CheckboxOrRadioParams& aParams) {
171 return WidgetInfo(Widget::eCheckbox, aParams);
173 static WidgetInfo Radio(const CheckboxOrRadioParams& aParams) {
174 return WidgetInfo(Widget::eRadio, aParams);
176 static WidgetInfo Button(const ButtonParams& aParams) {
177 return WidgetInfo(Widget::eButton, aParams);
179 static WidgetInfo Dropdown(const DropdownParams& aParams) {
180 return WidgetInfo(Widget::eDropdown, aParams);
182 static WidgetInfo SpinButtons(const SpinButtonParams& aParams) {
183 return WidgetInfo(Widget::eSpinButtons, aParams);
185 static WidgetInfo SpinButtonUp(const SpinButtonParams& aParams) {
186 return WidgetInfo(Widget::eSpinButtonUp, aParams);
188 static WidgetInfo SpinButtonDown(const SpinButtonParams& aParams) {
189 return WidgetInfo(Widget::eSpinButtonDown, aParams);
191 static WidgetInfo Segment(const SegmentParams& aParams) {
192 return WidgetInfo(Widget::eSegment, aParams);
194 static WidgetInfo Separator() {
195 return WidgetInfo(Widget::eSeparator, false);
197 static WidgetInfo StatusBar(bool aParams) {
198 return WidgetInfo(Widget::eStatusBar, aParams);
200 static WidgetInfo GroupBox() {
201 return WidgetInfo(Widget::eGroupBox, false);
203 static WidgetInfo TextField(const TextFieldParams& aParams) {
204 return WidgetInfo(Widget::eTextField, aParams);
206 static WidgetInfo SearchField(const TextFieldParams& aParams) {
207 return WidgetInfo(Widget::eSearchField, aParams);
209 static WidgetInfo ProgressBar(const ProgressParams& aParams) {
210 return WidgetInfo(Widget::eProgressBar, aParams);
212 static WidgetInfo Meter(const MeterParams& aParams) {
213 return WidgetInfo(Widget::eMeter, aParams);
215 static WidgetInfo Scale(const ScaleParams& aParams) {
216 return WidgetInfo(Widget::eScale, aParams);
218 static WidgetInfo MultilineTextField(bool aParams) {
219 return WidgetInfo(Widget::eMultilineTextField, aParams);
221 static WidgetInfo ListBox() { return WidgetInfo(Widget::eListBox, false); }
222 static WidgetInfo TabPanel(bool aParams) {
223 return WidgetInfo(Widget::eTabPanel, aParams);
226 template <typename T>
227 T Params() const {
228 MOZ_RELEASE_ASSERT(mVariant.is<T>());
229 return mVariant.as<T>();
232 enum Widget Widget() const { return mWidget; }
234 private:
235 template <typename T>
236 WidgetInfo(enum Widget aWidget, const T& aParams)
237 : mVariant(aParams), mWidget(aWidget) {}
239 mozilla::Variant<mozilla::gfx::sRGBColor, CheckboxOrRadioParams,
240 ButtonParams, DropdownParams, SpinButtonParams,
241 SegmentParams, TextFieldParams, ProgressParams,
242 MeterParams, ScaleParams, bool>
243 mVariant;
245 enum Widget mWidget;
248 explicit nsNativeThemeCocoa();
250 NS_DECL_ISUPPORTS_INHERITED
252 // The nsITheme interface.
253 NS_IMETHOD DrawWidgetBackground(gfxContext* aContext, nsIFrame*,
254 StyleAppearance, const nsRect& aRect,
255 const nsRect& aDirtyRect,
256 DrawOverflow) override;
257 bool CreateWebRenderCommandsForWidget(
258 mozilla::wr::DisplayListBuilder& aBuilder,
259 mozilla::wr::IpcResourceUpdateQueue& aResources,
260 const mozilla::layers::StackingContextHelper& aSc,
261 mozilla::layers::RenderRootStateManager* aManager, nsIFrame*,
262 StyleAppearance, const nsRect& aRect) override;
263 [[nodiscard]] LayoutDeviceIntMargin GetWidgetBorder(nsDeviceContext* aContext,
264 nsIFrame*,
265 StyleAppearance) override;
267 bool GetWidgetPadding(nsDeviceContext* aContext, nsIFrame*, StyleAppearance,
268 LayoutDeviceIntMargin* aResult) override;
270 bool GetWidgetOverflow(nsDeviceContext* aContext, nsIFrame*, StyleAppearance,
271 nsRect* aOverflowRect) override;
273 LayoutDeviceIntSize GetMinimumWidgetSize(nsPresContext*, nsIFrame*,
274 StyleAppearance) override;
275 bool WidgetAttributeChangeRequiresRepaint(StyleAppearance,
276 nsAtom* aAttribute) override;
277 NS_IMETHOD ThemeChanged() override;
278 bool ThemeSupportsWidget(nsPresContext* aPresContext, nsIFrame*,
279 StyleAppearance) override;
280 bool WidgetIsContainer(StyleAppearance) override;
281 bool ThemeDrawsFocusForWidget(nsIFrame*, StyleAppearance) override;
282 bool ThemeNeedsComboboxDropmarker() override;
283 bool WidgetAppearanceDependsOnWindowFocus(StyleAppearance) override;
284 ThemeGeometryType ThemeGeometryTypeForWidget(nsIFrame*,
285 StyleAppearance) override;
286 Transparency GetWidgetTransparency(nsIFrame*, StyleAppearance) override;
287 mozilla::Maybe<WidgetInfo> ComputeWidgetInfo(nsIFrame*, StyleAppearance,
288 const nsRect& aRect);
289 void DrawProgress(CGContextRef context, const HIRect& inBoxRect,
290 const ProgressParams& aParams);
292 protected:
293 virtual ~nsNativeThemeCocoa();
295 LayoutDeviceIntMargin DirectionAwareMargin(const LayoutDeviceIntMargin&,
296 nsIFrame*);
297 nsIFrame* SeparatorResponsibility(nsIFrame* aBefore, nsIFrame* aAfter);
298 ControlParams ComputeControlParams(nsIFrame*, mozilla::dom::ElementState);
299 SegmentParams ComputeSegmentParams(nsIFrame*, mozilla::dom::ElementState,
300 SegmentType);
301 TextFieldParams ComputeTextFieldParams(nsIFrame*, mozilla::dom::ElementState);
302 ProgressParams ComputeProgressParams(nsIFrame*, mozilla::dom::ElementState,
303 bool aIsHorizontal);
304 MeterParams ComputeMeterParams(nsIFrame*);
305 mozilla::Maybe<ScaleParams> ComputeHTMLScaleParams(
306 nsIFrame*, mozilla::dom::ElementState);
308 // HITheme drawing routines
309 void DrawMeter(CGContextRef context, const HIRect& inBoxRect,
310 const MeterParams& aParams);
311 void DrawSegment(CGContextRef cgContext, const HIRect& inBoxRect,
312 const SegmentParams& aParams);
313 void DrawSegmentBackground(CGContextRef cgContext, const HIRect& inBoxRect,
314 const SegmentParams& aParams);
315 void DrawTabPanel(CGContextRef context, const HIRect& inBoxRect,
316 bool aIsInsideActiveWindow);
317 void DrawScale(CGContextRef context, const HIRect& inBoxRect,
318 const ScaleParams& aParams);
319 void DrawCheckboxOrRadio(CGContextRef cgContext, bool inCheckbox,
320 const HIRect& inBoxRect,
321 const CheckboxOrRadioParams& aParams);
322 void DrawSearchField(CGContextRef cgContext, const HIRect& inBoxRect,
323 const TextFieldParams& aParams);
324 void DrawTextField(CGContextRef cgContext, const HIRect& inBoxRect,
325 const TextFieldParams& aParams);
326 void DrawPushButton(CGContextRef cgContext, const HIRect& inBoxRect,
327 ButtonType aButtonType, ControlParams aControlParams);
328 void DrawSquareBezelPushButton(CGContextRef cgContext,
329 const HIRect& inBoxRect,
330 ControlParams aControlParams);
331 void DrawHelpButton(CGContextRef cgContext, const HIRect& inBoxRect,
332 ControlParams aControlParams);
333 void DrawDisclosureButton(CGContextRef cgContext, const HIRect& inBoxRect,
334 ControlParams aControlParams,
335 NSControlStateValue aState);
336 void DrawHIThemeButton(CGContextRef cgContext, const HIRect& aRect,
337 ThemeButtonKind aKind, ThemeButtonValue aValue,
338 ThemeDrawState aState, ThemeButtonAdornment aAdornment,
339 const ControlParams& aParams);
340 void DrawButton(CGContextRef context, const HIRect& inBoxRect,
341 const ButtonParams& aParams);
342 void DrawDropdown(CGContextRef context, const HIRect& inBoxRect,
343 const DropdownParams& aParams);
344 HIThemeButtonDrawInfo SpinButtonDrawInfo(ThemeButtonKind aKind,
345 const SpinButtonParams& aParams);
346 void DrawSpinButtons(CGContextRef context, const HIRect& inBoxRect,
347 const SpinButtonParams& aParams);
348 void DrawSpinButton(CGContextRef context, const HIRect& inBoxRect,
349 SpinButton aDrawnButton, const SpinButtonParams& aParams);
350 void DrawToolbar(CGContextRef cgContext, const CGRect& inBoxRect,
351 bool aIsMain);
352 void DrawStatusBar(CGContextRef cgContext, const HIRect& inBoxRect,
353 bool aIsMain);
354 void DrawMultilineTextField(CGContextRef cgContext, const CGRect& inBoxRect,
355 bool aIsFocused);
356 void RenderWidget(const WidgetInfo& aWidgetInfo, mozilla::ColorScheme,
357 mozilla::gfx::DrawTarget& aDrawTarget,
358 const mozilla::gfx::Rect& aWidgetRect,
359 const mozilla::gfx::Rect& aDirtyRect, float aScale);
361 private:
362 NSButtonCell* mDisclosureButtonCell;
363 NSButtonCell* mHelpButtonCell;
364 NSButtonCell* mPushButtonCell;
365 NSButtonCell* mRadioButtonCell;
366 NSButtonCell* mCheckboxCell;
367 NSTextFieldCell* mTextFieldCell;
368 MOZSearchFieldCell* mSearchFieldCell;
369 NSPopUpButtonCell* mDropdownCell;
370 NSComboBoxCell* mComboBoxCell;
371 NSProgressBarCell* mProgressBarCell;
372 NSLevelIndicatorCell* mMeterBarCell;
373 MOZCellDrawWindow* mCellDrawWindow = nil;
374 MOZCellDrawView* mCellDrawView;
377 #endif // nsNativeThemeCocoa_h_