Bug 1754698 - add environment variables to interactive tasks. r=releng-reviewers...
[gecko.git] / widget / ScrollbarDrawing.cpp
bloba082b58eb39dacc536e3079630d3524c2d00b5fb
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
2 /* vim: set sw=2 ts=8 et 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 #include "ScrollbarDrawing.h"
9 #include "mozilla/RelativeLuminanceUtils.h"
10 #include "mozilla/StaticPrefs_widget.h"
11 #include "nsContainerFrame.h"
12 #include "nsDeviceContext.h"
13 #include "nsIFrame.h"
14 #include "nsLayoutUtils.h"
15 #include "nsLookAndFeel.h"
16 #include "nsNativeTheme.h"
18 using namespace mozilla::gfx;
20 namespace mozilla::widget {
22 using mozilla::RelativeLuminanceUtils;
24 /* static */
25 auto ScrollbarDrawing::GetDPIRatioForScrollbarPart(nsPresContext* aPc)
26 -> DPIRatio {
27 if (auto* rootPc = aPc->GetRootPresContext()) {
28 if (nsCOMPtr<nsIWidget> widget = rootPc->GetRootWidget()) {
29 return widget->GetDefaultScale();
32 return DPIRatio(
33 float(AppUnitsPerCSSPixel()) /
34 float(aPc->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom()));
37 /*static*/
38 nsIFrame* ScrollbarDrawing::GetParentScrollbarFrame(nsIFrame* aFrame) {
39 // Walk our parents to find a scrollbar frame
40 nsIFrame* scrollbarFrame = aFrame;
41 do {
42 if (scrollbarFrame->IsScrollbarFrame()) {
43 break;
45 } while ((scrollbarFrame = scrollbarFrame->GetParent()));
47 // We return null if we can't find a parent scrollbar frame
48 return scrollbarFrame;
51 /*static*/
52 bool ScrollbarDrawing::IsParentScrollbarRolledOver(nsIFrame* aFrame) {
53 nsIFrame* scrollbarFrame = GetParentScrollbarFrame(aFrame);
54 return nsLookAndFeel::GetInt(LookAndFeel::IntID::UseOverlayScrollbars) != 0
55 ? nsNativeTheme::CheckBooleanAttr(scrollbarFrame, nsGkAtoms::hover)
56 : nsNativeTheme::GetContentState(scrollbarFrame,
57 StyleAppearance::None)
58 .HasState(NS_EVENT_STATE_HOVER);
61 /*static*/
62 bool ScrollbarDrawing::IsParentScrollbarHoveredOrActive(nsIFrame* aFrame) {
63 nsIFrame* scrollbarFrame = GetParentScrollbarFrame(aFrame);
64 return scrollbarFrame && scrollbarFrame->GetContent()
65 ->AsElement()
66 ->State()
67 .HasAtLeastOneOfStates(NS_EVENT_STATE_HOVER |
68 NS_EVENT_STATE_ACTIVE);
71 /*static*/
72 bool ScrollbarDrawing::IsScrollbarWidthThin(const ComputedStyle& aStyle) {
73 auto scrollbarWidth = aStyle.StyleUIReset()->ScrollbarWidth();
74 return scrollbarWidth == StyleScrollbarWidth::Thin;
77 /*static*/
78 bool ScrollbarDrawing::IsScrollbarWidthThin(nsIFrame* aFrame) {
79 ComputedStyle* style = nsLayoutUtils::StyleForScrollbar(aFrame);
80 return IsScrollbarWidthThin(*style);
83 auto ScrollbarDrawing::GetScrollbarSizes(nsPresContext* aPresContext,
84 StyleScrollbarWidth aWidth, Overlay)
85 -> ScrollbarSizes {
86 uint32_t h = GetHorizontalScrollbarHeight();
87 uint32_t w = GetVerticalScrollbarWidth();
88 if (aWidth == StyleScrollbarWidth::Thin) {
89 h /= 2;
90 w /= 2;
92 auto dpi = GetDPIRatioForScrollbarPart(aPresContext);
93 return {(CSSCoord(w) * dpi).Rounded(), (CSSCoord(h) * dpi).Rounded()};
96 auto ScrollbarDrawing::GetScrollbarSizes(nsPresContext* aPresContext,
97 nsIFrame* aFrame) -> ScrollbarSizes {
98 auto* style = nsLayoutUtils::StyleForScrollbar(aFrame);
99 auto width = style->StyleUIReset()->ScrollbarWidth();
100 auto overlay =
101 aPresContext->UseOverlayScrollbars() ? Overlay::Yes : Overlay::No;
102 return GetScrollbarSizes(aPresContext, width, overlay);
105 bool ScrollbarDrawing::IsScrollbarTrackOpaque(nsIFrame* aFrame) {
106 auto trackColor = ComputeScrollbarTrackColor(
107 aFrame, *nsLayoutUtils::StyleForScrollbar(aFrame),
108 aFrame->PresContext()->Document()->GetDocumentState(),
109 Colors(aFrame, StyleAppearance::ScrollbartrackVertical));
110 return trackColor.a == 1.0f;
113 sRGBColor ScrollbarDrawing::ComputeScrollbarTrackColor(
114 nsIFrame* aFrame, const ComputedStyle& aStyle,
115 const EventStates& aDocumentState, const Colors& aColors) {
116 if (aColors.HighContrast()) {
117 return aColors.System(StyleSystemColor::Window);
119 const nsStyleUI* ui = aStyle.StyleUI();
120 if (ui->mScrollbarColor.IsColors()) {
121 return sRGBColor::FromABGR(
122 ui->mScrollbarColor.AsColors().track.CalcColor(aStyle));
124 static constexpr sRGBColor sDefaultDarkTrackColor =
125 sRGBColor::FromU8(20, 20, 25, 77);
126 static constexpr sRGBColor sDefaultTrackColor(
127 gfx::sRGBColor::UnusualFromARGB(0xfff0f0f0));
129 auto systemColor =
130 aDocumentState.HasAllStates(NS_DOCUMENT_STATE_WINDOW_INACTIVE)
131 ? StyleSystemColor::ThemedScrollbarInactive
132 : StyleSystemColor::ThemedScrollbar;
133 return aColors.SystemOrElse(systemColor, [&] {
134 return aColors.IsDark() ? sDefaultDarkTrackColor : sDefaultTrackColor;
138 // Don't use the theme color for dark scrollbars if it's not a color (if it's
139 // grey-ish), as that'd either lack enough contrast, or be close to what we'd do
140 // by default anyways.
141 sRGBColor ScrollbarDrawing::ComputeScrollbarThumbColor(
142 nsIFrame* aFrame, const ComputedStyle& aStyle,
143 const EventStates& aElementState, const EventStates& aDocumentState,
144 const Colors& aColors) {
145 const nsStyleUI* ui = aStyle.StyleUI();
146 if (ui->mScrollbarColor.IsColors()) {
147 return sRGBColor::FromABGR(ThemeColors::AdjustUnthemedScrollbarThumbColor(
148 ui->mScrollbarColor.AsColors().thumb.CalcColor(aStyle), aElementState));
151 auto systemColor = [&] {
152 if (aDocumentState.HasState(NS_DOCUMENT_STATE_WINDOW_INACTIVE)) {
153 return StyleSystemColor::ThemedScrollbarThumbInactive;
155 if (aElementState.HasState(NS_EVENT_STATE_ACTIVE)) {
156 if (aColors.HighContrast()) {
157 return StyleSystemColor::Selecteditem;
159 return StyleSystemColor::ThemedScrollbarThumbActive;
161 if (aElementState.HasState(NS_EVENT_STATE_HOVER)) {
162 if (aColors.HighContrast()) {
163 return StyleSystemColor::Selecteditem;
165 return StyleSystemColor::ThemedScrollbarThumbHover;
167 if (aColors.HighContrast()) {
168 return StyleSystemColor::Windowtext;
170 return StyleSystemColor::ThemedScrollbarThumb;
171 }();
173 return aColors.SystemOrElse(systemColor, [&] {
174 const nscolor unthemedColor = aColors.IsDark() ? NS_RGBA(249, 249, 250, 102)
175 : NS_RGB(0xcd, 0xcd, 0xcd);
177 return sRGBColor::FromABGR(ThemeColors::AdjustUnthemedScrollbarThumbColor(
178 unthemedColor, aElementState));
182 template <typename PaintBackendData>
183 bool ScrollbarDrawing::DoPaintDefaultScrollbar(
184 PaintBackendData& aPaintData, const LayoutDeviceRect& aRect,
185 bool aHorizontal, nsIFrame* aFrame, const ComputedStyle& aStyle,
186 const EventStates& aElementState, const EventStates& aDocumentState,
187 const Colors& aColors, const DPIRatio& aDpiRatio) {
188 if (aFrame->PresContext()->UseOverlayScrollbars() &&
189 !aElementState.HasAtLeastOneOfStates(NS_EVENT_STATE_HOVER |
190 NS_EVENT_STATE_ACTIVE)) {
191 return true;
193 auto scrollbarColor =
194 ComputeScrollbarTrackColor(aFrame, aStyle, aDocumentState, aColors);
195 ThemeDrawing::FillRect(aPaintData, aRect, scrollbarColor);
196 return true;
199 bool ScrollbarDrawing::PaintScrollbar(
200 DrawTarget& aDrawTarget, const LayoutDeviceRect& aRect, bool aHorizontal,
201 nsIFrame* aFrame, const ComputedStyle& aStyle,
202 const EventStates& aElementState, const EventStates& aDocumentState,
203 const Colors& aColors, const DPIRatio& aDpiRatio) {
204 return DoPaintDefaultScrollbar(aDrawTarget, aRect, aHorizontal, aFrame,
205 aStyle, aElementState, aDocumentState, aColors,
206 aDpiRatio);
209 bool ScrollbarDrawing::PaintScrollbar(
210 WebRenderBackendData& aWrData, const LayoutDeviceRect& aRect,
211 bool aHorizontal, nsIFrame* aFrame, const ComputedStyle& aStyle,
212 const EventStates& aElementState, const EventStates& aDocumentState,
213 const Colors& aColors, const DPIRatio& aDpiRatio) {
214 return DoPaintDefaultScrollbar(aWrData, aRect, aHorizontal, aFrame, aStyle,
215 aElementState, aDocumentState, aColors,
216 aDpiRatio);
219 template <typename PaintBackendData>
220 bool ScrollbarDrawing::DoPaintDefaultScrollCorner(
221 PaintBackendData& aPaintData, const LayoutDeviceRect& aRect,
222 nsIFrame* aFrame, const ComputedStyle& aStyle,
223 const EventStates& aDocumentState, const Colors& aColors,
224 const DPIRatio& aDpiRatio) {
225 auto scrollbarColor =
226 ComputeScrollbarTrackColor(aFrame, aStyle, aDocumentState, aColors);
227 ThemeDrawing::FillRect(aPaintData, aRect, scrollbarColor);
228 return true;
231 bool ScrollbarDrawing::PaintScrollCorner(
232 DrawTarget& aDrawTarget, const LayoutDeviceRect& aRect, nsIFrame* aFrame,
233 const ComputedStyle& aStyle, const EventStates& aDocumentState,
234 const Colors& aColors, const DPIRatio& aDpiRatio) {
235 return DoPaintDefaultScrollCorner(aDrawTarget, aRect, aFrame, aStyle,
236 aDocumentState, aColors, aDpiRatio);
239 bool ScrollbarDrawing::PaintScrollCorner(WebRenderBackendData& aWrData,
240 const LayoutDeviceRect& aRect,
241 nsIFrame* aFrame,
242 const ComputedStyle& aStyle,
243 const EventStates& aDocumentState,
244 const Colors& aColors,
245 const DPIRatio& aDpiRatio) {
246 return DoPaintDefaultScrollCorner(aWrData, aRect, aFrame, aStyle,
247 aDocumentState, aColors, aDpiRatio);
250 nscolor ScrollbarDrawing::GetScrollbarButtonColor(nscolor aTrackColor,
251 EventStates aStates) {
252 // See numbers in GetScrollbarArrowColor.
253 // This function is written based on ratios between values listed there.
255 bool isActive = aStates.HasState(NS_EVENT_STATE_ACTIVE);
256 bool isHover = aStates.HasState(NS_EVENT_STATE_HOVER);
257 if (!isActive && !isHover) {
258 return aTrackColor;
260 float luminance = RelativeLuminanceUtils::Compute(aTrackColor);
261 if (isActive) {
262 if (luminance >= 0.18f) {
263 luminance *= 0.134f;
264 } else {
265 luminance /= 0.134f;
266 luminance = std::min(luminance, 1.0f);
268 } else {
269 if (luminance >= 0.18f) {
270 luminance *= 0.805f;
271 } else {
272 luminance /= 0.805f;
275 return RelativeLuminanceUtils::Adjust(aTrackColor, luminance);
278 Maybe<nscolor> ScrollbarDrawing::GetScrollbarArrowColor(nscolor aButtonColor) {
279 // In Windows 10 scrollbar, there are several gray colors used:
281 // State | Background (lum) | Arrow | Contrast
282 // -------+------------------+---------+---------
283 // Normal | Gray 240 (87.1%) | Gray 96 | 5.5
284 // Hover | Gray 218 (70.1%) | Black | 15.0
285 // Active | Gray 96 (11.7%) | White | 6.3
287 // Contrast value is computed based on the definition in
288 // https://www.w3.org/TR/WCAG20/#contrast-ratiodef
290 // This function is written based on these values.
292 if (NS_GET_A(aButtonColor) == 0) {
293 // If the button color is transparent, because of e.g.
294 // scrollbar-color: <something> transparent, then use
295 // the thumb color, which is expected to have enough
296 // contrast.
297 return Nothing();
300 float luminance = RelativeLuminanceUtils::Compute(aButtonColor);
301 // Color with luminance larger than 0.72 has contrast ratio over 4.6
302 // to color with luminance of gray 96, so this value is chosen for
303 // this range. It is the luminance of gray 221.
304 if (luminance >= 0.72) {
305 // ComputeRelativeLuminanceFromComponents(96). That function cannot
306 // be constexpr because of std::pow.
307 const float GRAY96_LUMINANCE = 0.117f;
308 return Some(RelativeLuminanceUtils::Adjust(aButtonColor, GRAY96_LUMINANCE));
310 // The contrast ratio of a color to black equals that to white when its
311 // luminance is around 0.18, with a contrast ratio ~4.6 to both sides,
312 // thus the value below. It's the lumanince of gray 118.
314 // TODO(emilio): Maybe the button alpha is not the best thing to use here and
315 // we should use the thumb alpha? It seems weird that the color of the arrow
316 // depends on the opacity of the scrollbar thumb...
317 if (luminance >= 0.18) {
318 return Some(NS_RGBA(0, 0, 0, NS_GET_A(aButtonColor)));
320 return Some(NS_RGBA(255, 255, 255, NS_GET_A(aButtonColor)));
323 std::pair<sRGBColor, sRGBColor> ScrollbarDrawing::ComputeScrollbarButtonColors(
324 nsIFrame* aFrame, StyleAppearance aAppearance, const ComputedStyle& aStyle,
325 const EventStates& aElementState, const EventStates& aDocumentState,
326 const Colors& aColors) {
327 if (aColors.HighContrast()) {
328 if (aElementState.HasAtLeastOneOfStates(NS_EVENT_STATE_ACTIVE |
329 NS_EVENT_STATE_HOVER)) {
330 return aColors.SystemPair(StyleSystemColor::Selecteditem,
331 StyleSystemColor::Buttonface);
333 return aColors.SystemPair(StyleSystemColor::Window,
334 StyleSystemColor::Windowtext);
337 auto trackColor =
338 ComputeScrollbarTrackColor(aFrame, aStyle, aDocumentState, aColors);
339 nscolor buttonColor =
340 GetScrollbarButtonColor(trackColor.ToABGR(), aElementState);
341 auto arrowColor =
342 GetScrollbarArrowColor(buttonColor)
343 .map(sRGBColor::FromABGR)
344 .valueOrFrom([&] {
345 return ComputeScrollbarThumbColor(aFrame, aStyle, aElementState,
346 aDocumentState, aColors);
348 return {sRGBColor::FromABGR(buttonColor), arrowColor};
351 bool ScrollbarDrawing::PaintScrollbarButton(
352 DrawTarget& aDrawTarget, StyleAppearance aAppearance,
353 const LayoutDeviceRect& aRect, nsIFrame* aFrame,
354 const ComputedStyle& aStyle, const EventStates& aElementState,
355 const EventStates& aDocumentState, const Colors& aColors, const DPIRatio&) {
356 auto [buttonColor, arrowColor] = ComputeScrollbarButtonColors(
357 aFrame, aAppearance, aStyle, aElementState, aDocumentState, aColors);
358 aDrawTarget.FillRect(aRect.ToUnknownRect(),
359 ColorPattern(ToDeviceColor(buttonColor)));
361 // Start with Up arrow.
362 float arrowPolygonX[] = {-4.0f, 0.0f, 4.0f, 4.0f, 0.0f, -4.0f};
363 float arrowPolygonY[] = {0.0f, -4.0f, 0.0f, 3.0f, -1.0f, 3.0f};
365 const float kPolygonSize = 17;
367 const int32_t arrowNumPoints = ArrayLength(arrowPolygonX);
368 switch (aAppearance) {
369 case StyleAppearance::ScrollbarbuttonUp:
370 break;
371 case StyleAppearance::ScrollbarbuttonDown:
372 for (int32_t i = 0; i < arrowNumPoints; i++) {
373 arrowPolygonY[i] *= -1;
375 break;
376 case StyleAppearance::ScrollbarbuttonLeft:
377 for (int32_t i = 0; i < arrowNumPoints; i++) {
378 float temp = arrowPolygonX[i];
379 arrowPolygonX[i] = arrowPolygonY[i];
380 arrowPolygonY[i] = temp;
382 break;
383 case StyleAppearance::ScrollbarbuttonRight:
384 for (int32_t i = 0; i < arrowNumPoints; i++) {
385 float temp = arrowPolygonX[i];
386 arrowPolygonX[i] = arrowPolygonY[i] * -1;
387 arrowPolygonY[i] = temp;
389 break;
390 default:
391 return false;
393 ThemeDrawing::PaintArrow(aDrawTarget, aRect, arrowPolygonX, arrowPolygonY,
394 kPolygonSize, arrowNumPoints, arrowColor);
395 return true;
398 } // namespace mozilla::widget