Bug 1832284 - Fix rooting hazard in JSObject::swap r=sfink
[gecko.git] / widget / ScrollbarDrawing.h
blob656822aeed949bab76b4314aeaacd53b65a2f2ae
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_widget_ScrollbarDrawing_h
8 #define mozilla_widget_ScrollbarDrawing_h
10 #include "mozilla/dom/RustTypes.h"
11 #include "mozilla/gfx/2D.h"
12 #include "nsColor.h"
13 #include "nsITheme.h"
14 #include "ThemeColors.h"
15 #include "ThemeDrawing.h"
16 #include "Units.h"
18 namespace mozilla::widget {
20 class ScrollbarDrawing {
21 protected:
22 using DPIRatio = mozilla::CSSToLayoutDeviceScale;
23 using ElementState = dom::ElementState;
24 using DocumentState = dom::DocumentState;
25 using DrawTarget = mozilla::gfx::DrawTarget;
26 using sRGBColor = mozilla::gfx::sRGBColor;
27 using Colors = ThemeColors;
28 using Overlay = nsITheme::Overlay;
29 using WebRenderBackendData = mozilla::widget::WebRenderBackendData;
31 enum class Kind : uint8_t {
32 Android,
33 Cocoa,
34 Gtk,
35 Win10,
36 Win11,
39 explicit ScrollbarDrawing(Kind aKind) : mKind(aKind) {}
41 public:
42 virtual ~ScrollbarDrawing() = default;
44 enum class ScrollbarKind : uint8_t {
45 Horizontal,
46 VerticalLeft,
47 VerticalRight,
50 DPIRatio GetDPIRatioForScrollbarPart(const nsPresContext*);
52 static nsIFrame* GetParentScrollbarFrame(nsIFrame* aFrame);
53 static bool IsParentScrollbarRolledOver(nsIFrame* aFrame);
54 static bool IsParentScrollbarHoveredOrActive(nsIFrame* aFrame);
56 static bool IsScrollbarWidthThin(const ComputedStyle& aStyle);
57 static bool IsScrollbarWidthThin(nsIFrame* aFrame);
59 CSSIntCoord GetCSSScrollbarSize(StyleScrollbarWidth, Overlay) const;
60 LayoutDeviceIntCoord GetScrollbarSize(const nsPresContext*,
61 StyleScrollbarWidth, Overlay);
62 LayoutDeviceIntCoord GetScrollbarSize(const nsPresContext*, nsIFrame*);
64 virtual LayoutDeviceIntSize GetMinimumWidgetSize(nsPresContext*,
65 StyleAppearance aAppearance,
66 nsIFrame* aFrame) = 0;
67 virtual Maybe<nsITheme::Transparency> GetScrollbarPartTransparency(
68 nsIFrame* aFrame, StyleAppearance aAppearance) {
69 return Nothing();
72 bool IsScrollbarTrackOpaque(nsIFrame*);
73 virtual sRGBColor ComputeScrollbarTrackColor(nsIFrame*, const ComputedStyle&,
74 const DocumentState&,
75 const Colors&);
76 virtual sRGBColor ComputeScrollbarThumbColor(nsIFrame*, const ComputedStyle&,
77 const ElementState&,
78 const DocumentState&,
79 const Colors&);
81 nscolor GetScrollbarButtonColor(nscolor aTrackColor, ElementState);
82 Maybe<nscolor> GetScrollbarArrowColor(nscolor aButtonColor);
84 // Returned colors are button, arrow.
85 virtual std::pair<sRGBColor, sRGBColor> ComputeScrollbarButtonColors(
86 nsIFrame*, StyleAppearance, const ComputedStyle&, const ElementState&,
87 const DocumentState&, const Colors&);
89 virtual bool PaintScrollbarButton(DrawTarget&, StyleAppearance,
90 const LayoutDeviceRect&, ScrollbarKind,
91 nsIFrame*, const ComputedStyle&,
92 const ElementState&, const DocumentState&,
93 const Colors&, const DPIRatio&);
95 virtual bool PaintScrollbarThumb(DrawTarget&, const LayoutDeviceRect&,
96 ScrollbarKind, nsIFrame*,
97 const ComputedStyle&, const ElementState&,
98 const DocumentState&, const Colors&,
99 const DPIRatio&) = 0;
100 virtual bool PaintScrollbarThumb(WebRenderBackendData&,
101 const LayoutDeviceRect&, ScrollbarKind,
102 nsIFrame*, const ComputedStyle&,
103 const ElementState&, const DocumentState&,
104 const Colors&, const DPIRatio&) = 0;
106 template <typename PaintBackendData>
107 bool DoPaintDefaultScrollbar(PaintBackendData&, const LayoutDeviceRect&,
108 ScrollbarKind, nsIFrame*, const ComputedStyle&,
109 const ElementState&, const DocumentState&,
110 const Colors&, const DPIRatio&);
111 bool PaintScrollbar(DrawTarget&, const LayoutDeviceRect&, ScrollbarKind,
112 nsIFrame*, const ComputedStyle&, const ElementState&,
113 const DocumentState&, const Colors&, const DPIRatio&);
114 bool PaintScrollbar(WebRenderBackendData&, const LayoutDeviceRect&,
115 ScrollbarKind, nsIFrame*, const ComputedStyle&,
116 const ElementState&, const DocumentState&, const Colors&,
117 const DPIRatio&);
119 virtual bool PaintScrollbarTrack(DrawTarget&, const LayoutDeviceRect&,
120 ScrollbarKind, nsIFrame*,
121 const ComputedStyle&, const DocumentState&,
122 const Colors&, const DPIRatio&) {
123 // Draw nothing by default. Subclasses can override this.
124 return true;
126 virtual bool PaintScrollbarTrack(WebRenderBackendData&,
127 const LayoutDeviceRect&, ScrollbarKind,
128 nsIFrame*, const ComputedStyle&,
129 const DocumentState&, const Colors&,
130 const DPIRatio&) {
131 // Draw nothing by default. Subclasses can override this.
132 return true;
135 template <typename PaintBackendData>
136 bool DoPaintDefaultScrollCorner(PaintBackendData&, const LayoutDeviceRect&,
137 ScrollbarKind, nsIFrame*,
138 const ComputedStyle&, const DocumentState&,
139 const Colors&, const DPIRatio&);
140 virtual bool PaintScrollCorner(DrawTarget&, const LayoutDeviceRect&,
141 ScrollbarKind, nsIFrame*, const ComputedStyle&,
142 const DocumentState&, const Colors&,
143 const DPIRatio&);
144 virtual bool PaintScrollCorner(WebRenderBackendData&, const LayoutDeviceRect&,
145 ScrollbarKind, nsIFrame*, const ComputedStyle&,
146 const DocumentState&, const Colors&,
147 const DPIRatio&);
149 virtual void RecomputeScrollbarParams() = 0;
151 virtual bool ShouldDrawScrollbarButtons() { return true; }
153 private:
154 // The scrollbar sizes for all our scrollbars. Indices are overlay or not,
155 // then thin or not. Should be configured via ConfigureScrollbarSize.
156 CSSIntCoord mScrollbarSize[2][2]{};
158 protected:
159 // For some kind of style differences a full virtual method is overkill, so we
160 // store the kind here so we can branch on it if necessary.
161 Kind mKind;
163 // Configures the scrollbar sizes based on a single size.
164 void ConfigureScrollbarSize(CSSIntCoord);
166 // Configures a particular scrollbar size.
167 void ConfigureScrollbarSize(StyleScrollbarWidth, Overlay, CSSIntCoord);
170 } // namespace mozilla::widget
172 #endif