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 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
* Copyright (C) 2014, 2020 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollbarThemeAdwaita.h"
#if USE(THEME_ADWAITA)
#include "Color.h"
#include "FloatRoundedRect.h"
#include "GraphicsContext.h"
#include "PlatformMouseEvent.h"
#include "ScrollableArea.h"
#include "Scrollbar.h"
#include "ThemeAdwaita.h"
#if PLATFORM(GTK)
#include "GtkUtilities.h"
#include <gtk/gtk.h>
#endif
namespace WebCore {
static const unsigned scrollbarSize = 21;
static const unsigned scrollbarBorderSize = 1;
static const unsigned thumbBorderSize = 1;
static const unsigned overlayThumbSize = 3;
static const unsigned minimumThumbSize = 40;
static const unsigned horizThumbMargin = 6;
static const unsigned horizOverlayThumbMargin = 3;
static const unsigned vertThumbMargin = 7;
static constexpr auto scrollbarBackgroundColorLight = Color::white;
static constexpr auto scrollbarBorderColorLight = Color::black.colorWithAlphaByte(38);
static constexpr auto overlayThumbBorderColorLight = Color::white.colorWithAlphaByte(102);
static constexpr auto overlayTroughBorderColorLight = Color::white.colorWithAlphaByte(89);
static constexpr auto overlayTroughColorLight = Color::black.colorWithAlphaByte(25);
static constexpr auto thumbHoveredColorLight = Color::black.colorWithAlphaByte(102);
static constexpr auto thumbPressedColorLight = Color::black.colorWithAlphaByte(153);
static constexpr auto thumbColorLight = Color::black.colorWithAlphaByte(51);
static constexpr auto scrollbarBackgroundColorDark = SRGBA<uint8_t> { 30, 30, 30 };
static constexpr auto scrollbarBorderColorDark = Color::white.colorWithAlphaByte(38);
static constexpr auto overlayThumbBorderColorDark = Color::black.colorWithAlphaByte(51);
static constexpr auto overlayTroughBorderColorDark = Color::black.colorWithAlphaByte(44);
static constexpr auto overlayTroughColorDark = Color::white.colorWithAlphaByte(25);
static constexpr auto thumbHoveredColorDark = Color::white.colorWithAlphaByte(102);
static constexpr auto thumbPressedColorDark = Color::white.colorWithAlphaByte(153);
static constexpr auto thumbColorDark = Color::white.colorWithAlphaByte(51);
void ScrollbarThemeAdwaita::updateScrollbarOverlayStyle(Scrollbar& scrollbar)
{
scrollbar.invalidate();
}
bool ScrollbarThemeAdwaita::usesOverlayScrollbars() const
{
#if PLATFORM(GTK)
return shouldUseOverlayScrollbars();
#else
return true;
#endif
}
int ScrollbarThemeAdwaita::scrollbarThickness(ScrollbarWidth scrollbarWidth, ScrollbarExpansionState)
{
if (scrollbarWidth == ScrollbarWidth::None)
return 0;
return scrollbarSize;
}
int ScrollbarThemeAdwaita::minimumThumbLength(Scrollbar&)
{
return minimumThumbSize;
}
bool ScrollbarThemeAdwaita::hasButtons(Scrollbar&)
{
return false;
}
bool ScrollbarThemeAdwaita::hasThumb(Scrollbar& scrollbar)
{
return thumbLength(scrollbar) > 0;
}
IntRect ScrollbarThemeAdwaita::backButtonRect(Scrollbar&, ScrollbarPart, bool)
{
return { };
}
IntRect ScrollbarThemeAdwaita::forwardButtonRect(Scrollbar&, ScrollbarPart, bool)
{
return { };
}
IntRect ScrollbarThemeAdwaita::trackRect(Scrollbar& scrollbar, bool)
{
return scrollbar.frameRect();
}
bool ScrollbarThemeAdwaita::paint(Scrollbar& scrollbar, GraphicsContext& graphicsContext, const IntRect& damageRect)
{
if (graphicsContext.paintingDisabled())
return false;
if (!scrollbar.enabled() && usesOverlayScrollbars())
return true;
IntRect rect = scrollbar.frameRect();
if (!rect.intersects(damageRect))
return true;
double opacity;
if (usesOverlayScrollbars())
opacity = scrollbar.opacity();
else
opacity = 1;
if (!opacity)
return true;
SRGBA<uint8_t> scrollbarBackgroundColor;
SRGBA<uint8_t> scrollbarBorderColor;
SRGBA<uint8_t> overlayThumbBorderColor;
SRGBA<uint8_t> overlayTroughBorderColor;
SRGBA<uint8_t> overlayTroughColor;
SRGBA<uint8_t> thumbHoveredColor;
SRGBA<uint8_t> thumbPressedColor;
SRGBA<uint8_t> thumbColor;
if (scrollbar.scrollableArea().useDarkAppearanceForScrollbars()) {
scrollbarBackgroundColor = scrollbarBackgroundColorDark;
scrollbarBorderColor = scrollbarBorderColorDark;
overlayThumbBorderColor = overlayThumbBorderColorDark;
overlayTroughBorderColor = overlayThumbBorderColorDark;
overlayTroughColor = overlayTroughColorDark;
thumbHoveredColor = thumbHoveredColorDark;
thumbPressedColor = thumbPressedColorDark;
thumbColor = thumbColorDark;
} else {
scrollbarBackgroundColor = scrollbarBackgroundColorLight;
scrollbarBorderColor = scrollbarBorderColorLight;
overlayThumbBorderColor = overlayThumbBorderColorLight;
overlayTroughBorderColor = overlayThumbBorderColorLight;
overlayTroughColor = overlayTroughColorLight;
thumbHoveredColor = thumbHoveredColorLight;
thumbPressedColor = thumbPressedColorLight;
thumbColor = thumbColorLight;
}
GraphicsContextStateSaver stateSaver(graphicsContext);
if (opacity != 1) {
graphicsContext.clip(damageRect);
graphicsContext.beginTransparencyLayer(opacity);
}
int thumbSize = scrollbarSize - scrollbarBorderSize - horizThumbMargin * 2;
if (!usesOverlayScrollbars()) {
graphicsContext.fillRect(rect, scrollbarBackgroundColor);
IntRect frame = rect;
if (scrollbar.orientation() == ScrollbarOrientation::Vertical) {
if (scrollbar.scrollableArea().shouldPlaceVerticalScrollbarOnLeft())
frame.move(frame.width() - scrollbarBorderSize, 0);
frame.setWidth(scrollbarBorderSize);
} else
frame.setHeight(scrollbarBorderSize);
graphicsContext.fillRect(frame, scrollbarBorderColor);
} else if (scrollbar.hoveredPart() != NoPart) {
int thumbCornerSize = thumbSize / 2;
FloatSize corner(thumbCornerSize, thumbCornerSize);
FloatSize borderCorner(thumbCornerSize + thumbBorderSize, thumbCornerSize + thumbBorderSize);
IntRect trough = rect;
if (scrollbar.orientation() == ScrollbarOrientation::Vertical) {
if (scrollbar.scrollableArea().shouldPlaceVerticalScrollbarOnLeft())
trough.move(scrollbarSize - (scrollbarSize / 2 + thumbSize / 2) - scrollbarBorderSize, vertThumbMargin);
else
trough.move(scrollbarSize - (scrollbarSize / 2 + thumbSize / 2), vertThumbMargin);
trough.setWidth(thumbSize);
trough.setHeight(rect.height() - vertThumbMargin * 2);
} else {
trough.move(vertThumbMargin, scrollbarSize - (scrollbarSize / 2 + thumbSize / 2));
trough.setWidth(rect.width() - vertThumbMargin * 2);
trough.setHeight(thumbSize);
}
IntRect troughBorder(trough);
troughBorder.inflate(thumbBorderSize);
Path path;
path.addRoundedRect(trough, corner);
graphicsContext.setFillRule(WindRule::NonZero);
graphicsContext.setFillColor(overlayTroughColor);
graphicsContext.fillPath(path);
path.clear();
path.addRoundedRect(trough, corner);
path.addRoundedRect(troughBorder, borderCorner);
graphicsContext.setFillRule(WindRule::EvenOdd);
graphicsContext.setFillColor(overlayTroughBorderColor);
graphicsContext.fillPath(path);
}
int thumbCornerSize;
int thumbPos = thumbPosition(scrollbar);
int thumbLen = thumbLength(scrollbar);
IntRect thumb = rect;
if (scrollbar.hoveredPart() == NoPart && usesOverlayScrollbars()) {
thumbCornerSize = overlayThumbSize / 2;
if (scrollbar.orientation() == ScrollbarOrientation::Vertical) {
if (scrollbar.scrollableArea().shouldPlaceVerticalScrollbarOnLeft())
thumb.move(horizOverlayThumbMargin, thumbPos + vertThumbMargin);
else
thumb.move(scrollbarSize - overlayThumbSize - horizOverlayThumbMargin, thumbPos + vertThumbMargin);
thumb.setWidth(overlayThumbSize);
thumb.setHeight(thumbLen - vertThumbMargin * 2);
} else {
thumb.move(thumbPos + vertThumbMargin, scrollbarSize - overlayThumbSize - horizOverlayThumbMargin);
thumb.setWidth(thumbLen - vertThumbMargin * 2);
thumb.setHeight(overlayThumbSize);
}
} else {
thumbCornerSize = thumbSize / 2;
if (scrollbar.orientation() == ScrollbarOrientation::Vertical) {
if (scrollbar.scrollableArea().shouldPlaceVerticalScrollbarOnLeft())
thumb.move(scrollbarSize - (scrollbarSize / 2 + thumbSize / 2) - scrollbarBorderSize, thumbPos + vertThumbMargin);
else
thumb.move(scrollbarSize - (scrollbarSize / 2 + thumbSize / 2), thumbPos + vertThumbMargin);
thumb.setWidth(thumbSize);
thumb.setHeight(thumbLen - vertThumbMargin * 2);
} else {
thumb.move(thumbPos + vertThumbMargin, scrollbarSize - (scrollbarSize / 2 + thumbSize / 2));
thumb.setWidth(thumbLen - vertThumbMargin * 2);
thumb.setHeight(thumbSize);
}
}
FloatSize corner(thumbCornerSize, thumbCornerSize);
FloatSize borderCorner(thumbCornerSize + thumbBorderSize, thumbCornerSize + thumbBorderSize);
Path path;
path.addRoundedRect(thumb, corner);
graphicsContext.setFillRule(WindRule::NonZero);
if (scrollbar.pressedPart() == ThumbPart)
graphicsContext.setFillColor(thumbPressedColor);
else if (scrollbar.hoveredPart() == ThumbPart)
graphicsContext.setFillColor(thumbHoveredColor);
else
graphicsContext.setFillColor(thumbColor);
graphicsContext.fillPath(path);
path.clear();
if (usesOverlayScrollbars()) {
IntRect thumbBorder(thumb);
thumbBorder.inflate(thumbBorderSize);
path.addRoundedRect(thumb, corner);
path.addRoundedRect(thumbBorder, borderCorner);
graphicsContext.setFillRule(WindRule::EvenOdd);
graphicsContext.setFillColor(overlayThumbBorderColor);
graphicsContext.fillPath(path);
}
if (opacity != 1)
graphicsContext.endTransparencyLayer();
return true;
}
void ScrollbarThemeAdwaita::paintScrollCorner(ScrollableArea& scrollableArea, GraphicsContext& graphicsContext, const IntRect& cornerRect)
{
if (graphicsContext.paintingDisabled())
return;
SRGBA<uint8_t> scrollbarBackgroundColor;
SRGBA<uint8_t> scrollbarBorderColor;
if (scrollableArea.useDarkAppearanceForScrollbars()) {
scrollbarBackgroundColor = scrollbarBackgroundColorDark;
scrollbarBorderColor = scrollbarBorderColorDark;
} else {
scrollbarBackgroundColor = scrollbarBackgroundColorLight;
scrollbarBorderColor = scrollbarBorderColorLight;
}
IntRect borderRect = IntRect(cornerRect.location(), IntSize(scrollbarBorderSize, scrollbarBorderSize));
if (scrollableArea.shouldPlaceVerticalScrollbarOnLeft())
borderRect.move(cornerRect.width() - scrollbarBorderSize, 0);
graphicsContext.fillRect(cornerRect, scrollbarBackgroundColor);
graphicsContext.fillRect(borderRect, scrollbarBorderColor);
}
ScrollbarButtonPressAction ScrollbarThemeAdwaita::handleMousePressEvent(Scrollbar&, const PlatformMouseEvent& event, ScrollbarPart pressedPart)
{
bool warpSlider = false;
switch (pressedPart) {
case BackTrackPart:
case ForwardTrackPart:
#if PLATFORM(GTK)
warpSlider = [] {
gboolean gtkWarpsSlader = FALSE;
g_object_get(gtk_settings_get_default(),
"gtk-primary-button-warps-slider",
>kWarpsSlader, nullptr);
return gtkWarpsSlader;
}();
#endif
// The shift key or middle/right button reverses the sense.
if (event.shiftKey() || event.button() != LeftButton)
warpSlider = !warpSlider;
return warpSlider ?
ScrollbarButtonPressAction::CenterOnThumb:
ScrollbarButtonPressAction::Scroll;
case ThumbPart:
if (event.button() != RightButton)
return ScrollbarButtonPressAction::StartDrag;
break;
case BackButtonStartPart:
case ForwardButtonStartPart:
case BackButtonEndPart:
case ForwardButtonEndPart:
return ScrollbarButtonPressAction::Scroll;
default:
break;
}
return ScrollbarButtonPressAction::None;
}
#if !PLATFORM(GTK) || USE(GTK4)
ScrollbarTheme& ScrollbarTheme::nativeTheme()
{
static ScrollbarThemeAdwaita theme;
return theme;
}
#endif
} // namespace WebCore
#endif // USE(THEME_ADWAITA)
|