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 370 371 372 373 374 375 376 377 378 379
|
/*
* Copyright (C) 2011 Apple Inc. All Rights Reserved.
*
* 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. ``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
* 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 "platform/scroll/ScrollbarTheme.h"
#include "platform/PlatformMouseEvent.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/graphics/Color.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/paint/DisplayItemList.h"
#include "platform/graphics/paint/DrawingDisplayItem.h"
#include "platform/graphics/paint/DrawingRecorder.h"
#include "platform/scroll/ScrollbarThemeClient.h"
#include "platform/scroll/ScrollbarThemeMock.h"
#include "platform/scroll/ScrollbarThemeOverlayMock.h"
#include "public/platform/Platform.h"
#include "public/platform/WebPoint.h"
#include "public/platform/WebRect.h"
#include "public/platform/WebScrollbarBehavior.h"
#if !OS(MACOSX)
#include "public/platform/WebRect.h"
#include "public/platform/WebThemeEngine.h"
#endif
namespace blink {
bool ScrollbarTheme::gMockScrollbarsEnabled = false;
bool ScrollbarTheme::paint(ScrollbarThemeClient* scrollbar, GraphicsContext* graphicsContext, const IntRect& damageRect)
{
DrawingRecorder recorder(graphicsContext, displayItemClient(), DisplayItem::Scrollbar, damageRect);
return paintInternal(scrollbar, graphicsContext, damageRect);
}
bool ScrollbarTheme::paintInternal(ScrollbarThemeClient* scrollbar, GraphicsContext* graphicsContext, const IntRect& damageRect)
{
// Create the ScrollbarControlPartMask based on the damageRect
ScrollbarControlPartMask scrollMask = NoPart;
IntRect backButtonStartPaintRect;
IntRect backButtonEndPaintRect;
IntRect forwardButtonStartPaintRect;
IntRect forwardButtonEndPaintRect;
if (hasButtons(scrollbar)) {
backButtonStartPaintRect = backButtonRect(scrollbar, BackButtonStartPart, true);
if (damageRect.intersects(backButtonStartPaintRect))
scrollMask |= BackButtonStartPart;
backButtonEndPaintRect = backButtonRect(scrollbar, BackButtonEndPart, true);
if (damageRect.intersects(backButtonEndPaintRect))
scrollMask |= BackButtonEndPart;
forwardButtonStartPaintRect = forwardButtonRect(scrollbar, ForwardButtonStartPart, true);
if (damageRect.intersects(forwardButtonStartPaintRect))
scrollMask |= ForwardButtonStartPart;
forwardButtonEndPaintRect = forwardButtonRect(scrollbar, ForwardButtonEndPart, true);
if (damageRect.intersects(forwardButtonEndPaintRect))
scrollMask |= ForwardButtonEndPart;
}
IntRect startTrackRect;
IntRect thumbRect;
IntRect endTrackRect;
IntRect trackPaintRect = trackRect(scrollbar, true);
if (damageRect.intersects(trackPaintRect))
scrollMask |= TrackBGPart;
bool thumbPresent = hasThumb(scrollbar);
if (thumbPresent) {
IntRect track = trackRect(scrollbar);
splitTrack(scrollbar, track, startTrackRect, thumbRect, endTrackRect);
if (damageRect.intersects(thumbRect))
scrollMask |= ThumbPart;
if (damageRect.intersects(startTrackRect))
scrollMask |= BackTrackPart;
if (damageRect.intersects(endTrackRect))
scrollMask |= ForwardTrackPart;
}
// Paint the scrollbar background (only used by custom CSS scrollbars).
paintScrollbarBackground(graphicsContext, scrollbar);
// Paint the back and forward buttons.
if (scrollMask & BackButtonStartPart)
paintButton(graphicsContext, scrollbar, backButtonStartPaintRect, BackButtonStartPart);
if (scrollMask & BackButtonEndPart)
paintButton(graphicsContext, scrollbar, backButtonEndPaintRect, BackButtonEndPart);
if (scrollMask & ForwardButtonStartPart)
paintButton(graphicsContext, scrollbar, forwardButtonStartPaintRect, ForwardButtonStartPart);
if (scrollMask & ForwardButtonEndPart)
paintButton(graphicsContext, scrollbar, forwardButtonEndPaintRect, ForwardButtonEndPart);
if (scrollMask & TrackBGPart)
paintTrackBackground(graphicsContext, scrollbar, trackPaintRect);
if ((scrollMask & ForwardTrackPart) || (scrollMask & BackTrackPart)) {
// Paint the track pieces above and below the thumb.
if (scrollMask & BackTrackPart)
paintTrackPiece(graphicsContext, scrollbar, startTrackRect, BackTrackPart);
if (scrollMask & ForwardTrackPart)
paintTrackPiece(graphicsContext, scrollbar, endTrackRect, ForwardTrackPart);
paintTickmarks(graphicsContext, scrollbar, trackPaintRect);
}
// Paint the thumb.
if (scrollMask & ThumbPart)
paintThumb(graphicsContext, scrollbar, thumbRect);
return true;
}
ScrollbarPart ScrollbarTheme::hitTest(ScrollbarThemeClient* scrollbar, const IntPoint& position)
{
ScrollbarPart result = NoPart;
if (!scrollbar->enabled())
return result;
IntPoint testPosition = scrollbar->convertFromContainingWindow(position);
testPosition.move(scrollbar->x(), scrollbar->y());
if (!scrollbar->frameRect().contains(testPosition))
return NoPart;
result = ScrollbarBGPart;
IntRect track = trackRect(scrollbar);
if (track.contains(testPosition)) {
IntRect beforeThumbRect;
IntRect thumbRect;
IntRect afterThumbRect;
splitTrack(scrollbar, track, beforeThumbRect, thumbRect, afterThumbRect);
if (thumbRect.contains(testPosition))
result = ThumbPart;
else if (beforeThumbRect.contains(testPosition))
result = BackTrackPart;
else if (afterThumbRect.contains(testPosition))
result = ForwardTrackPart;
else
result = TrackBGPart;
} else if (backButtonRect(scrollbar, BackButtonStartPart).contains(testPosition)) {
result = BackButtonStartPart;
} else if (backButtonRect(scrollbar, BackButtonEndPart).contains(testPosition)) {
result = BackButtonEndPart;
} else if (forwardButtonRect(scrollbar, ForwardButtonStartPart).contains(testPosition)) {
result = ForwardButtonStartPart;
} else if (forwardButtonRect(scrollbar, ForwardButtonEndPart).contains(testPosition)) {
result = ForwardButtonEndPart;
}
return result;
}
void ScrollbarTheme::invalidatePart(ScrollbarThemeClient* scrollbar, ScrollbarPart part)
{
if (part == NoPart)
return;
IntRect result;
switch (part) {
case BackButtonStartPart:
result = backButtonRect(scrollbar, BackButtonStartPart, true);
break;
case BackButtonEndPart:
result = backButtonRect(scrollbar, BackButtonEndPart, true);
break;
case ForwardButtonStartPart:
result = forwardButtonRect(scrollbar, ForwardButtonStartPart, true);
break;
case ForwardButtonEndPart:
result = forwardButtonRect(scrollbar, ForwardButtonEndPart, true);
break;
case TrackBGPart:
result = trackRect(scrollbar, true);
break;
case ScrollbarBGPart:
result = scrollbar->frameRect();
break;
default: {
IntRect beforeThumbRect, thumbRect, afterThumbRect;
splitTrack(scrollbar, trackRect(scrollbar), beforeThumbRect, thumbRect, afterThumbRect);
if (part == BackTrackPart)
result = beforeThumbRect;
else if (part == ForwardTrackPart)
result = afterThumbRect;
else
result = thumbRect;
}
}
result.moveBy(-scrollbar->location());
scrollbar->invalidateRect(result);
}
void ScrollbarTheme::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
{
if (cornerRect.isEmpty())
return;
DrawingRecorder recorder(context, displayItemClient(), DisplayItem::ScrollbarCorner, cornerRect);
#if OS(MACOSX)
context->fillRect(cornerRect, Color::white);
#else
Platform::current()->themeEngine()->paint(context->canvas(), WebThemeEngine::PartScrollbarCorner, WebThemeEngine::StateNormal, WebRect(cornerRect), 0);
#endif
}
void ScrollbarTheme::paintOverhangBackground(GraphicsContext* context, const IntRect& horizontalOverhangRect, const IntRect& verticalOverhangRect, const IntRect& dirtyRect)
{
context->setFillColor(Color::white);
if (!horizontalOverhangRect.isEmpty())
context->fillRect(intersection(horizontalOverhangRect, dirtyRect));
if (!verticalOverhangRect.isEmpty())
context->fillRect(intersection(verticalOverhangRect, dirtyRect));
}
bool ScrollbarTheme::shouldCenterOnThumb(ScrollbarThemeClient* scrollbar, const PlatformMouseEvent& evt)
{
return Platform::current()->scrollbarBehavior()->shouldCenterOnThumb(static_cast<WebScrollbarBehavior::Button>(evt.button()), evt.shiftKey(), evt.altKey());
}
bool ScrollbarTheme::shouldSnapBackToDragOrigin(ScrollbarThemeClient* scrollbar, const PlatformMouseEvent& evt)
{
IntPoint mousePosition = scrollbar->convertFromContainingWindow(evt.position());
mousePosition.move(scrollbar->x(), scrollbar->y());
return Platform::current()->scrollbarBehavior()->shouldSnapBackToDragOrigin(mousePosition, trackRect(scrollbar), scrollbar->orientation() == HorizontalScrollbar);
}
// Returns the size represented by track taking into account scrolling past
// the end of the document.
static float usedTotalSize(ScrollbarThemeClient* scrollbar)
{
float overhangAtStart = -scrollbar->currentPos();
float overhangAtEnd = scrollbar->currentPos() + scrollbar->visibleSize() - scrollbar->totalSize();
float overhang = std::max(0.0f, std::max(overhangAtStart, overhangAtEnd));
return scrollbar->totalSize() + overhang;
}
int ScrollbarTheme::thumbPosition(ScrollbarThemeClient* scrollbar)
{
if (scrollbar->enabled()) {
float size = usedTotalSize(scrollbar) - scrollbar->visibleSize();
// Avoid doing a floating point divide by zero and return 1 when usedTotalSize == visibleSize.
if (!size)
return 1;
float pos = std::max(0.0f, scrollbar->currentPos()) * (trackLength(scrollbar) - thumbLength(scrollbar)) / size;
return (pos < 1 && pos > 0) ? 1 : pos;
}
return 0;
}
int ScrollbarTheme::thumbLength(ScrollbarThemeClient* scrollbar)
{
if (!scrollbar->enabled())
return 0;
// It is safe to compute the overhang by adding the result from the main
// thread overscroll mode (first) and then adding the result from compositor
// thread overscroll (second) because the modes are mutually exclusive (and
// determining which mode is in use here will require lots of temporary
// plumbing, and the main thread mode is to be deleted).
float overhang = 0;
if (scrollbar->currentPos() < 0)
overhang = -scrollbar->currentPos();
else if (scrollbar->visibleSize() + scrollbar->currentPos() > scrollbar->totalSize())
overhang = scrollbar->currentPos() + scrollbar->visibleSize() - scrollbar->totalSize();
overhang += fabsf(scrollbar->elasticOverscroll());
float proportion = 0.0f;
float totalSize = usedTotalSize(scrollbar);
if (totalSize > 0.0f) {
proportion = (scrollbar->visibleSize() - overhang) / totalSize;
}
int trackLen = trackLength(scrollbar);
int length = round(proportion * trackLen);
length = std::max(length, minimumThumbLength(scrollbar));
if (length > trackLen)
length = 0; // Once the thumb is below the track length, it just goes away (to make more room for the track).
return length;
}
int ScrollbarTheme::trackPosition(ScrollbarThemeClient* scrollbar)
{
IntRect constrainedTrackRect = constrainTrackRectToTrackPieces(scrollbar, trackRect(scrollbar));
return (scrollbar->orientation() == HorizontalScrollbar) ? constrainedTrackRect.x() - scrollbar->x() : constrainedTrackRect.y() - scrollbar->y();
}
int ScrollbarTheme::trackLength(ScrollbarThemeClient* scrollbar)
{
IntRect constrainedTrackRect = constrainTrackRectToTrackPieces(scrollbar, trackRect(scrollbar));
return (scrollbar->orientation() == HorizontalScrollbar) ? constrainedTrackRect.width() : constrainedTrackRect.height();
}
IntRect ScrollbarTheme::thumbRect(ScrollbarThemeClient* scrollbar)
{
if (!hasThumb(scrollbar))
return IntRect();
IntRect track = trackRect(scrollbar);
IntRect startTrackRect;
IntRect thumbRect;
IntRect endTrackRect;
splitTrack(scrollbar, track, startTrackRect, thumbRect, endTrackRect);
return thumbRect;
}
int ScrollbarTheme::thumbThickness(ScrollbarThemeClient* scrollbar)
{
IntRect track = trackRect(scrollbar);
return scrollbar->orientation() == HorizontalScrollbar ? track.height() : track.width();
}
int ScrollbarTheme::minimumThumbLength(ScrollbarThemeClient* scrollbar)
{
return scrollbarThickness(scrollbar->controlSize());
}
void ScrollbarTheme::splitTrack(ScrollbarThemeClient* scrollbar, const IntRect& unconstrainedTrackRect, IntRect& beforeThumbRect, IntRect& thumbRect, IntRect& afterThumbRect)
{
// This function won't even get called unless we're big enough to have some combination of these three rects where at least
// one of them is non-empty.
IntRect trackRect = constrainTrackRectToTrackPieces(scrollbar, unconstrainedTrackRect);
int thumbPos = thumbPosition(scrollbar);
if (scrollbar->orientation() == HorizontalScrollbar) {
thumbRect = IntRect(trackRect.x() + thumbPos, trackRect.y(), thumbLength(scrollbar), scrollbar->height());
beforeThumbRect = IntRect(trackRect.x(), trackRect.y(), thumbPos + thumbRect.width() / 2, trackRect.height());
afterThumbRect = IntRect(trackRect.x() + beforeThumbRect.width(), trackRect.y(), trackRect.maxX() - beforeThumbRect.maxX(), trackRect.height());
} else {
thumbRect = IntRect(trackRect.x(), trackRect.y() + thumbPos, scrollbar->width(), thumbLength(scrollbar));
beforeThumbRect = IntRect(trackRect.x(), trackRect.y(), trackRect.width(), thumbPos + thumbRect.height() / 2);
afterThumbRect = IntRect(trackRect.x(), trackRect.y() + beforeThumbRect.height(), trackRect.width(), trackRect.maxY() - beforeThumbRect.maxY());
}
}
ScrollbarTheme* ScrollbarTheme::theme()
{
if (ScrollbarTheme::mockScrollbarsEnabled()) {
if (RuntimeEnabledFeatures::overlayScrollbarsEnabled()) {
DEFINE_STATIC_LOCAL(ScrollbarThemeOverlayMock, overlayMockTheme, ());
return &overlayMockTheme;
}
DEFINE_STATIC_LOCAL(ScrollbarThemeMock, mockTheme, ());
return &mockTheme;
}
return nativeTheme();
}
void ScrollbarTheme::setMockScrollbarsEnabled(bool flag)
{
gMockScrollbarsEnabled = flag;
}
bool ScrollbarTheme::mockScrollbarsEnabled()
{
return gMockScrollbarsEnabled;
}
} // namespace blink
|