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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ScrollbarDrawingWin11.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPrefs_widget.h"
#include "nsLayoutUtils.h"
#include "Theme.h"
#include "nsNativeTheme.h"
using mozilla::gfx::sRGBColor;
namespace mozilla::widget {
// There are effectively three kinds of scrollbars in Windows 11:
//
// * Overlay scrollbars (the ones where the scrollbar disappears automatically
// and doesn't take space)
// * Non-overlay scrollbar with thin (overlay-like) thumb.
// * Non-overlay scrollbar with thick thumb.
//
// See bug 1755193 for some discussion on non-overlay scrollbar styles.
enum class Style {
Overlay,
ThinThumb,
ThickThumb,
};
static Style ScrollbarStyle(nsPresContext* aPresContext) {
if (aPresContext->UseOverlayScrollbars()) {
return Style::Overlay;
}
if (StaticPrefs::
widget_non_native_theme_win11_scrollbar_force_overlay_style()) {
return Style::ThinThumb;
}
return Style::ThickThumb;
}
static constexpr CSSIntCoord kDefaultWinOverlayScrollbarSize = CSSIntCoord(12);
static constexpr CSSIntCoord kDefaultWinOverlayThinScrollbarSize =
CSSIntCoord(10);
LayoutDeviceIntSize ScrollbarDrawingWin11::GetMinimumWidgetSize(
nsPresContext* aPresContext, StyleAppearance aAppearance,
nsIFrame* aFrame) {
MOZ_ASSERT(nsNativeTheme::IsWidgetScrollbarPart(aAppearance));
if (ScrollbarStyle(aPresContext) != Style::ThinThumb) {
return ScrollbarDrawingWin::GetMinimumWidgetSize(aPresContext, aAppearance,
aFrame);
}
constexpr float kArrowRatio = 14.0f / kDefaultWinScrollbarSize;
switch (aAppearance) {
case StyleAppearance::ScrollbarbuttonUp:
case StyleAppearance::ScrollbarbuttonDown:
case StyleAppearance::ScrollbarbuttonLeft:
case StyleAppearance::ScrollbarbuttonRight: {
const LayoutDeviceIntCoord size =
ScrollbarDrawing::GetScrollbarSize(aPresContext, aFrame);
return LayoutDeviceIntSize{
size, (kArrowRatio * LayoutDeviceCoord(size)).Rounded()};
}
default:
return ScrollbarDrawingWin::GetMinimumWidgetSize(aPresContext,
aAppearance, aFrame);
}
}
sRGBColor ScrollbarDrawingWin11::ComputeScrollbarTrackColor(
nsIFrame* aFrame, const ComputedStyle& aStyle, const Colors& aColors) {
if (aColors.HighContrast()) {
return ScrollbarDrawingWin::ComputeScrollbarTrackColor(aFrame, aStyle,
aColors);
}
const nsStyleUI* ui = aStyle.StyleUI();
if (ui->mScrollbarColor.IsColors()) {
return sRGBColor::FromABGR(
ui->mScrollbarColor.AsColors().track.CalcColor(aStyle));
}
return aColors.IsDark() ? sRGBColor::FromU8(23, 23, 23, 255)
: sRGBColor::FromU8(240, 240, 240, 255);
}
sRGBColor ScrollbarDrawingWin11::ComputeScrollbarThumbColor(
nsIFrame* aFrame, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors) {
if (aColors.HighContrast()) {
return ScrollbarDrawingWin::ComputeScrollbarThumbColor(
aFrame, aStyle, aElementState, aColors);
}
const nscolor baseColor = [&] {
const nsStyleUI* ui = aStyle.StyleUI();
if (ui->mScrollbarColor.IsColors()) {
return ui->mScrollbarColor.AsColors().thumb.CalcColor(aStyle);
}
return aColors.IsDark() ? NS_RGBA(149, 149, 149, 255)
: NS_RGBA(133, 133, 133, 255);
}();
ElementState state = aElementState;
if (!IsScrollbarWidthThin(aStyle)) {
// non-thin scrollbars get hover feedback by changing thumb shape, so we
// only provide active feedback (and we use the hover state for that as it's
// more subtle).
state &= ~ElementState::HOVER;
if (state.HasState(ElementState::ACTIVE)) {
state &= ~ElementState::ACTIVE;
state |= ElementState::HOVER;
}
}
return sRGBColor::FromABGR(
ThemeColors::AdjustUnthemedScrollbarThumbColor(baseColor, state));
}
std::pair<sRGBColor, sRGBColor>
ScrollbarDrawingWin11::ComputeScrollbarButtonColors(
nsIFrame* aFrame, StyleAppearance aAppearance, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors) {
if (aColors.HighContrast()) {
return ScrollbarDrawingWin::ComputeScrollbarButtonColors(
aFrame, aAppearance, aStyle, aElementState, aColors);
}
// The button always looks transparent (the track behind it is visible), so we
// can hardcode it.
sRGBColor arrowColor =
ComputeScrollbarThumbColor(aFrame, aStyle, aElementState, aColors);
return {sRGBColor::White(0.0f), arrowColor};
}
bool ScrollbarDrawingWin11::PaintScrollbarButton(
DrawTarget& aDrawTarget, StyleAppearance aAppearance,
const LayoutDeviceRect& aRect, ScrollbarKind aScrollbarKind,
nsIFrame* aFrame, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors,
const DPIRatio& aDpiRatio) {
if (!ScrollbarDrawing::IsParentScrollbarHoveredOrActive(aFrame)) {
return true;
}
const auto style = ScrollbarStyle(aFrame->PresContext());
auto [buttonColor, arrowColor] = ComputeScrollbarButtonColors(
aFrame, aAppearance, aStyle, aElementState, aColors);
if (style != Style::Overlay) {
aDrawTarget.FillRect(aRect.ToUnknownRect(),
gfx::ColorPattern(ToDeviceColor(buttonColor)));
}
// Start with Up arrow.
float arrowPolygonX[] = {-4.5f, 4.5f, 4.5f, 0.5f, -0.5f, -4.5f, -4.5f};
float arrowPolygonXActive[] = {-4.0f, 4.0f, 4.0f, -0.25f,
-0.25f, -4.0f, -4.0f};
float arrowPolygonXHover[] = {-5.0f, 5.0f, 5.0f, 0.75f, -0.75f, -5.0f, -5.0f};
float arrowPolygonY[] = {2.5f, 2.5f, 1.0f, -4.0f, -4.0f, 1.0f, 2.5f};
float arrowPolygonYActive[] = {2.0f, 2.0f, 0.5f, -3.5f, -3.5f, 0.5f, 2.0f};
float arrowPolygonYHover[] = {3.0f, 3.0f, 1.5f, -4.5f, -4.5f, 1.5f, 3.0f};
float* arrowX = arrowPolygonX;
float* arrowY = arrowPolygonY;
const bool horizontal = aScrollbarKind == ScrollbarKind::Horizontal;
const float verticalOffset = [&] {
if (style != Style::Overlay) {
return 0.0f;
}
// To compensate for the scrollbar track radius we shift stuff vertically a
// bit. This 1px is arbitrary, but enough for the triangle not to overflow.
return 1.0f;
}();
const float horizontalOffset = [&] {
if (style != Style::ThinThumb) {
return 0.0f; // Always center it in the rect.
}
// Compensate for the displacement we do of the thumb position by displacing
// the arrow as well, see comment in DoPaintScrollbarThumb.
if (horizontal) {
return -0.5f;
}
return aScrollbarKind == ScrollbarKind::VerticalRight ? 0.5f : -0.5f;
}();
const float polygonSize = style == Style::Overlay
? float(kDefaultWinOverlayScrollbarSize)
: float(kDefaultWinScrollbarSize);
const int32_t arrowNumPoints = std::size(arrowPolygonX);
if (aElementState.HasState(ElementState::ACTIVE)) {
arrowX = arrowPolygonXActive;
arrowY = arrowPolygonYActive;
} else if (aElementState.HasState(ElementState::HOVER)) {
arrowX = arrowPolygonXHover;
arrowY = arrowPolygonYHover;
}
switch (aAppearance) {
case StyleAppearance::ScrollbarbuttonDown:
case StyleAppearance::ScrollbarbuttonRight:
for (int32_t i = 0; i < arrowNumPoints; i++) {
arrowY[i] += verticalOffset;
arrowY[i] *= -1;
}
[[fallthrough]];
case StyleAppearance::ScrollbarbuttonUp:
case StyleAppearance::ScrollbarbuttonLeft:
if (horizontalOffset != 0.0f) {
for (int32_t i = 0; i < arrowNumPoints; i++) {
arrowX[i] += horizontalOffset;
}
}
break;
default:
return false;
}
if (horizontal) {
std::swap(arrowX, arrowY);
}
LayoutDeviceRect arrowRect(aRect);
if (style != Style::ThinThumb) {
auto margin = CSSCoord(style == Style::Overlay ? 1 : 2) * aDpiRatio;
arrowRect.Deflate(margin, margin);
}
ThemeDrawing::PaintArrow(aDrawTarget, arrowRect, arrowX, arrowY, polygonSize,
arrowNumPoints, arrowColor);
return true;
}
template <typename PaintBackendData>
bool ScrollbarDrawingWin11::DoPaintScrollbarThumb(
PaintBackendData& aPaintData, const LayoutDeviceRect& aRect,
ScrollbarKind aScrollbarKind, nsIFrame* aFrame, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors,
const DPIRatio& aDpiRatio) {
sRGBColor thumbColor =
ComputeScrollbarThumbColor(aFrame, aStyle, aElementState, aColors);
LayoutDeviceRect thumbRect(aRect);
const auto style = ScrollbarStyle(aFrame->PresContext());
const bool hovered =
ScrollbarDrawing::IsParentScrollbarHoveredOrActive(aFrame) ||
(style != Style::Overlay && IsScrollbarWidthThin(aStyle));
const bool horizontal = aScrollbarKind == ScrollbarKind::Horizontal;
if (style == Style::ThickThumb) {
constexpr float kHoveredThumbRatio =
(1.0f - (11.0f / kDefaultWinScrollbarSize)) / 2.0f;
constexpr float kUnhoveredThumbRatio =
(1.0f - (9.0f / kDefaultWinScrollbarSize)) / 2.0f;
const float ratio = hovered ? kHoveredThumbRatio : kUnhoveredThumbRatio;
if (horizontal) {
thumbRect.Deflate(0, thumbRect.height * ratio);
} else {
thumbRect.Deflate(thumbRect.width * ratio, 0);
}
auto radius = CSSCoord(hovered ? 2 : 0);
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, thumbRect, thumbColor,
sRGBColor(), 0, radius, aDpiRatio);
return true;
}
const float defaultTrackSize = style == Style::Overlay
? float(kDefaultWinOverlayScrollbarSize)
: float(kDefaultWinScrollbarSize);
const float trackSize = horizontal ? thumbRect.height : thumbRect.width;
const float thumbSizeInPixels = hovered ? 6.0f : 2.0f;
// The thumb might be a bit off-center, depending on our scrollbar styles.
//
// Hovered shifts, if any, need to be accounted for in PaintScrollbarButton.
// For example, for the hovered horizontal thin scrollbar shift:
//
// Scrollbar is 17px high by default. We make the thumb 6px tall and move
// it 5px towards the bottom, so the center (8.5 initially) is displaced
// by:
// (5px + 6px / 2) - 8.5px = -0.5px
//
// Same calculations apply to other shifts.
const float shiftInPixels = [&] {
if (style == Style::Overlay) {
if (hovered) {
// Keep the center intact.
return (defaultTrackSize - thumbSizeInPixels) / 2.0f;
}
// We want logical pixels from the thumb to the edge. For LTR and
// horizontal scrollbars that means shifting down the scrollbar size minus
// the thumb.
constexpr float kSpaceToEdge = 3.0f;
if (horizontal || aScrollbarKind == ScrollbarKind::VerticalRight) {
return defaultTrackSize - thumbSizeInPixels - kSpaceToEdge;
}
// For rtl is simpler.
return kSpaceToEdge;
}
if (horizontal) {
return hovered ? 5.0f : 7.0f;
}
const bool ltr = aScrollbarKind == ScrollbarKind::VerticalRight;
return ltr ? (hovered ? 6.0f : 8.0f) : (hovered ? 5.0f : 7.0f);
}();
if (horizontal) {
thumbRect.y += shiftInPixels * trackSize / defaultTrackSize;
thumbRect.height *= thumbSizeInPixels / defaultTrackSize;
} else {
thumbRect.x += shiftInPixels * trackSize / defaultTrackSize;
thumbRect.width *= thumbSizeInPixels / defaultTrackSize;
}
if (style == Style::Overlay || hovered) {
LayoutDeviceCoord radius =
(horizontal ? thumbRect.height : thumbRect.width) / 2.0f;
MOZ_ASSERT(aRect.Contains(thumbRect));
ThemeDrawing::PaintRoundedRectWithRadius(aPaintData, thumbRect, thumbColor,
sRGBColor(), 0, radius / aDpiRatio,
aDpiRatio);
return true;
}
ThemeDrawing::FillRect(aPaintData, thumbRect, thumbColor);
return true;
}
bool ScrollbarDrawingWin11::PaintScrollbarThumb(
DrawTarget& aDrawTarget, const LayoutDeviceRect& aRect,
ScrollbarKind aScrollbarKind, nsIFrame* aFrame, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors,
const DPIRatio& aDpiRatio) {
return DoPaintScrollbarThumb(aDrawTarget, aRect, aScrollbarKind, aFrame,
aStyle, aElementState, aColors, aDpiRatio);
}
bool ScrollbarDrawingWin11::PaintScrollbarThumb(
WebRenderBackendData& aWrData, const LayoutDeviceRect& aRect,
ScrollbarKind aScrollbarKind, nsIFrame* aFrame, const ComputedStyle& aStyle,
const ElementState& aElementState, const Colors& aColors,
const DPIRatio& aDpiRatio) {
return DoPaintScrollbarThumb(aWrData, aRect, aScrollbarKind, aFrame, aStyle,
aElementState, aColors, aDpiRatio);
}
void ScrollbarDrawingWin11::RecomputeScrollbarParams() {
ScrollbarDrawingWin::RecomputeScrollbarParams();
// TODO(emilio): Maybe make this configurable? Though this doesn't respect
// classic Windows registry settings, and cocoa overlay scrollbars also don't
// respect the override it seems, so this should be fine.
ConfigureScrollbarSize(StyleScrollbarWidth::Thin, Overlay::Yes,
kDefaultWinOverlayThinScrollbarSize);
ConfigureScrollbarSize(StyleScrollbarWidth::Auto, Overlay::Yes,
kDefaultWinOverlayScrollbarSize);
}
} // namespace mozilla::widget
|