1 | /* |
2 | * Copyright (C) 2004, 2006, 2008, 2015 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "Scrollbar.h" |
28 | |
29 | #include "FrameView.h" |
30 | #include "GraphicsContext.h" |
31 | #include "PlatformMouseEvent.h" |
32 | #include "ScrollAnimator.h" |
33 | #include "ScrollView.h" |
34 | #include "ScrollableArea.h" |
35 | #include "ScrollbarTheme.h" |
36 | #include <algorithm> |
37 | |
38 | #if PLATFORM(GTK) |
39 | // The position of the scrollbar thumb affects the appearance of the steppers, so |
40 | // when the thumb moves, we have to invalidate them for painting. |
41 | #define THUMB_POSITION_AFFECTS_BUTTONS |
42 | #endif |
43 | |
44 | namespace WebCore { |
45 | |
46 | Ref<Scrollbar> Scrollbar::createNativeScrollbar(ScrollableArea& scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size) |
47 | { |
48 | return adoptRef(*new Scrollbar(scrollableArea, orientation, size)); |
49 | } |
50 | |
51 | int Scrollbar::maxOverlapBetweenPages() |
52 | { |
53 | static int maxOverlapBetweenPages = ScrollbarTheme::theme().maxOverlapBetweenPages(); |
54 | return maxOverlapBetweenPages; |
55 | } |
56 | |
57 | Scrollbar::Scrollbar(ScrollableArea& scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* customTheme) |
58 | : m_scrollableArea(scrollableArea) |
59 | , m_orientation(orientation) |
60 | , m_controlSize(controlSize) |
61 | , m_theme(customTheme ? *customTheme : ScrollbarTheme::theme()) |
62 | , m_scrollTimer(*this, &Scrollbar::autoscrollTimerFired) |
63 | { |
64 | theme().registerScrollbar(*this); |
65 | |
66 | // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for |
67 | // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar |
68 | // alone when sizing). |
69 | int thickness = theme().scrollbarThickness(controlSize); |
70 | Widget::setFrameRect(IntRect(0, 0, thickness, thickness)); |
71 | |
72 | m_currentPos = static_cast<float>(m_scrollableArea.scrollOffset(m_orientation)); |
73 | } |
74 | |
75 | Scrollbar::~Scrollbar() |
76 | { |
77 | stopTimerIfNeeded(); |
78 | |
79 | theme().unregisterScrollbar(*this); |
80 | } |
81 | |
82 | int Scrollbar::occupiedWidth() const |
83 | { |
84 | return isOverlayScrollbar() ? 0 : width(); |
85 | } |
86 | |
87 | int Scrollbar::occupiedHeight() const |
88 | { |
89 | return isOverlayScrollbar() ? 0 : height(); |
90 | } |
91 | |
92 | void Scrollbar::offsetDidChange() |
93 | { |
94 | float position = static_cast<float>(m_scrollableArea.scrollOffset(m_orientation)); |
95 | if (position == m_currentPos) |
96 | return; |
97 | |
98 | int oldThumbPosition = theme().thumbPosition(*this); |
99 | m_currentPos = position; |
100 | updateThumbPosition(); |
101 | if (m_pressedPart == ThumbPart) |
102 | setPressedPos(m_pressedPos + theme().thumbPosition(*this) - oldThumbPosition); |
103 | } |
104 | |
105 | void Scrollbar::setProportion(int visibleSize, int totalSize) |
106 | { |
107 | if (visibleSize == m_visibleSize && totalSize == m_totalSize) |
108 | return; |
109 | |
110 | m_visibleSize = visibleSize; |
111 | m_totalSize = totalSize; |
112 | |
113 | updateThumbProportion(); |
114 | } |
115 | |
116 | void Scrollbar::setSteps(int lineStep, int pageStep, int pixelsPerStep) |
117 | { |
118 | m_lineStep = lineStep; |
119 | m_pageStep = pageStep; |
120 | m_pixelStep = 1.0f / pixelsPerStep; |
121 | } |
122 | |
123 | void Scrollbar::updateThumb() |
124 | { |
125 | #ifdef THUMB_POSITION_AFFECTS_BUTTONS |
126 | invalidate(); |
127 | #else |
128 | theme().invalidateParts(*this, ForwardTrackPart | BackTrackPart | ThumbPart); |
129 | #endif |
130 | } |
131 | |
132 | void Scrollbar::updateThumbPosition() |
133 | { |
134 | updateThumb(); |
135 | } |
136 | |
137 | void Scrollbar::updateThumbProportion() |
138 | { |
139 | updateThumb(); |
140 | } |
141 | |
142 | void Scrollbar::paint(GraphicsContext& context, const IntRect& damageRect, Widget::SecurityOriginPaintPolicy) |
143 | { |
144 | if (context.invalidatingControlTints() && theme().supportsControlTints()) { |
145 | invalidate(); |
146 | return; |
147 | } |
148 | |
149 | if (context.paintingDisabled() || !frameRect().intersects(damageRect)) |
150 | return; |
151 | |
152 | if (!theme().paint(*this, context, damageRect)) |
153 | Widget::paint(context, damageRect); |
154 | } |
155 | |
156 | void Scrollbar::autoscrollTimerFired() |
157 | { |
158 | autoscrollPressedPart(theme().autoscrollTimerDelay()); |
159 | } |
160 | |
161 | static bool thumbUnderMouse(Scrollbar* scrollbar) |
162 | { |
163 | int thumbPos = scrollbar->theme().trackPosition(*scrollbar) + scrollbar->theme().thumbPosition(*scrollbar); |
164 | int thumbLength = scrollbar->theme().thumbLength(*scrollbar); |
165 | return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength; |
166 | } |
167 | |
168 | void Scrollbar::autoscrollPressedPart(Seconds delay) |
169 | { |
170 | // Don't do anything for the thumb or if nothing was pressed. |
171 | if (m_pressedPart == ThumbPart || m_pressedPart == NoPart) |
172 | return; |
173 | |
174 | // Handle the track. |
175 | if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) { |
176 | theme().invalidatePart(*this, m_pressedPart); |
177 | setHoveredPart(ThumbPart); |
178 | return; |
179 | } |
180 | |
181 | // Handle the arrows and track. |
182 | if (m_scrollableArea.scroll(pressedPartScrollDirection(), pressedPartScrollGranularity())) |
183 | startTimerIfNeeded(delay); |
184 | } |
185 | |
186 | void Scrollbar::startTimerIfNeeded(Seconds delay) |
187 | { |
188 | // Don't do anything for the thumb. |
189 | if (m_pressedPart == ThumbPart) |
190 | return; |
191 | |
192 | // Handle the track. We halt track scrolling once the thumb is level |
193 | // with us. |
194 | if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) { |
195 | theme().invalidatePart(*this, m_pressedPart); |
196 | setHoveredPart(ThumbPart); |
197 | return; |
198 | } |
199 | |
200 | // We can't scroll if we've hit the beginning or end. |
201 | ScrollDirection dir = pressedPartScrollDirection(); |
202 | if (dir == ScrollUp || dir == ScrollLeft) { |
203 | if (m_currentPos == 0) |
204 | return; |
205 | } else { |
206 | if (m_currentPos == maximum()) |
207 | return; |
208 | } |
209 | |
210 | m_scrollTimer.startOneShot(delay); |
211 | } |
212 | |
213 | void Scrollbar::stopTimerIfNeeded() |
214 | { |
215 | if (m_scrollTimer.isActive()) |
216 | m_scrollTimer.stop(); |
217 | } |
218 | |
219 | ScrollDirection Scrollbar::pressedPartScrollDirection() |
220 | { |
221 | if (m_orientation == HorizontalScrollbar) { |
222 | if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart) |
223 | return ScrollLeft; |
224 | return ScrollRight; |
225 | } else { |
226 | if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart) |
227 | return ScrollUp; |
228 | return ScrollDown; |
229 | } |
230 | } |
231 | |
232 | ScrollGranularity Scrollbar::pressedPartScrollGranularity() |
233 | { |
234 | if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart) |
235 | return ScrollByLine; |
236 | return ScrollByPage; |
237 | } |
238 | |
239 | void Scrollbar::moveThumb(int pos, bool draggingDocument) |
240 | { |
241 | int delta = pos - m_pressedPos; |
242 | |
243 | if (draggingDocument) { |
244 | if (m_draggingDocument) |
245 | delta = pos - m_documentDragPos; |
246 | m_draggingDocument = true; |
247 | FloatPoint currentPosition = m_scrollableArea.scrollAnimator().currentPosition(); |
248 | int destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta; |
249 | if (delta > 0) |
250 | destinationPosition = std::min(destinationPosition + delta, maximum()); |
251 | else if (delta < 0) |
252 | destinationPosition = std::max(destinationPosition + delta, 0); |
253 | m_scrollableArea.scrollToOffsetWithoutAnimation(m_orientation, destinationPosition); |
254 | m_documentDragPos = pos; |
255 | return; |
256 | } |
257 | |
258 | if (m_draggingDocument) { |
259 | delta += m_pressedPos - m_documentDragPos; |
260 | m_draggingDocument = false; |
261 | } |
262 | |
263 | // Drag the thumb. |
264 | int thumbPos = theme().thumbPosition(*this); |
265 | int thumbLen = theme().thumbLength(*this); |
266 | int trackLen = theme().trackLength(*this); |
267 | int maxPos = trackLen - thumbLen; |
268 | if (delta > 0) |
269 | delta = std::min(maxPos - thumbPos, delta); |
270 | else if (delta < 0) |
271 | delta = std::max(-thumbPos, delta); |
272 | |
273 | if (delta) { |
274 | float newPosition = static_cast<float>(thumbPos + delta) * maximum() / (trackLen - thumbLen); |
275 | m_scrollableArea.scrollToOffsetWithoutAnimation(m_orientation, newPosition); |
276 | } |
277 | } |
278 | |
279 | void Scrollbar::setHoveredPart(ScrollbarPart part) |
280 | { |
281 | if (part == m_hoveredPart) |
282 | return; |
283 | |
284 | if ((m_hoveredPart == NoPart || part == NoPart) && theme().invalidateOnMouseEnterExit()) |
285 | invalidate(); // Just invalidate the whole scrollbar, since the buttons at either end change anyway. |
286 | else if (m_pressedPart == NoPart) { // When there's a pressed part, we don't draw a hovered state, so there's no reason to invalidate. |
287 | theme().invalidatePart(*this, part); |
288 | theme().invalidatePart(*this, m_hoveredPart); |
289 | } |
290 | m_hoveredPart = part; |
291 | } |
292 | |
293 | void Scrollbar::setPressedPart(ScrollbarPart part) |
294 | { |
295 | if (m_pressedPart != NoPart) |
296 | theme().invalidatePart(*this, m_pressedPart); |
297 | m_pressedPart = part; |
298 | if (m_pressedPart != NoPart) |
299 | theme().invalidatePart(*this, m_pressedPart); |
300 | else if (m_hoveredPart != NoPart) // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part. |
301 | theme().invalidatePart(*this, m_hoveredPart); |
302 | } |
303 | |
304 | #if !PLATFORM(IOS_FAMILY) |
305 | bool Scrollbar::mouseMoved(const PlatformMouseEvent& evt) |
306 | { |
307 | if (m_pressedPart == ThumbPart) { |
308 | if (theme().shouldSnapBackToDragOrigin(*this, evt)) |
309 | m_scrollableArea.scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin); |
310 | else { |
311 | moveThumb(m_orientation == HorizontalScrollbar ? |
312 | convertFromContainingWindow(evt.position()).x() : |
313 | convertFromContainingWindow(evt.position()).y(), theme().shouldDragDocumentInsteadOfThumb(*this, evt)); |
314 | } |
315 | return true; |
316 | } |
317 | |
318 | if (m_pressedPart != NoPart) |
319 | m_pressedPos = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y()); |
320 | |
321 | ScrollbarPart part = theme().hitTest(*this, evt.position()); |
322 | if (part != m_hoveredPart) { |
323 | if (m_pressedPart != NoPart) { |
324 | if (part == m_pressedPart) { |
325 | // The mouse is moving back over the pressed part. We |
326 | // need to start up the timer action again. |
327 | startTimerIfNeeded(theme().autoscrollTimerDelay()); |
328 | theme().invalidatePart(*this, m_pressedPart); |
329 | } else if (m_hoveredPart == m_pressedPart) { |
330 | // The mouse is leaving the pressed part. Kill our timer |
331 | // if needed. |
332 | stopTimerIfNeeded(); |
333 | theme().invalidatePart(*this, m_pressedPart); |
334 | } |
335 | } |
336 | |
337 | setHoveredPart(part); |
338 | } |
339 | |
340 | return true; |
341 | } |
342 | #endif |
343 | |
344 | void Scrollbar::mouseEntered() |
345 | { |
346 | m_scrollableArea.mouseEnteredScrollbar(this); |
347 | } |
348 | |
349 | bool Scrollbar::mouseExited() |
350 | { |
351 | m_scrollableArea.mouseExitedScrollbar(this); |
352 | setHoveredPart(NoPart); |
353 | return true; |
354 | } |
355 | |
356 | bool Scrollbar::mouseUp(const PlatformMouseEvent& mouseEvent) |
357 | { |
358 | setPressedPart(NoPart); |
359 | m_pressedPos = 0; |
360 | m_draggingDocument = false; |
361 | stopTimerIfNeeded(); |
362 | |
363 | m_scrollableArea.mouseIsDownInScrollbar(this, false); |
364 | |
365 | // m_hoveredPart won't be updated until the next mouseMoved or mouseDown, so we have to hit test |
366 | // to really know if the mouse has exited the scrollbar on a mouseUp. |
367 | ScrollbarPart part = theme().hitTest(*this, mouseEvent.position()); |
368 | if (part == NoPart) |
369 | m_scrollableArea.mouseExitedScrollbar(this); |
370 | |
371 | return true; |
372 | } |
373 | |
374 | bool Scrollbar::mouseDown(const PlatformMouseEvent& evt) |
375 | { |
376 | ScrollbarPart pressedPart = theme().hitTest(*this, evt.position()); |
377 | auto action = theme().handleMousePressEvent(*this, evt, pressedPart); |
378 | if (action == ScrollbarButtonPressAction::None) |
379 | return true; |
380 | |
381 | m_scrollableArea.mouseIsDownInScrollbar(this, true); |
382 | setPressedPart(pressedPart); |
383 | |
384 | int pressedPosition = (orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y()); |
385 | if (action == ScrollbarButtonPressAction::CenterOnThumb) { |
386 | setHoveredPart(ThumbPart); |
387 | setPressedPart(ThumbPart); |
388 | m_dragOrigin = m_currentPos; |
389 | // Set the pressed position to the middle of the thumb so that when we do the move, the delta |
390 | // will be from the current pixel position of the thumb to the new desired position for the thumb. |
391 | m_pressedPos = theme().trackPosition(*this) + theme().thumbPosition(*this) + theme().thumbLength(*this) / 2; |
392 | moveThumb(pressedPosition); |
393 | return true; |
394 | } |
395 | |
396 | m_pressedPos = pressedPosition; |
397 | |
398 | if (action == ScrollbarButtonPressAction::StartDrag) |
399 | m_dragOrigin = m_currentPos; |
400 | |
401 | if (action == ScrollbarButtonPressAction::Scroll) |
402 | autoscrollPressedPart(theme().initialAutoscrollTimerDelay()); |
403 | |
404 | return true; |
405 | } |
406 | |
407 | void Scrollbar::setEnabled(bool e) |
408 | { |
409 | if (m_enabled == e) |
410 | return; |
411 | m_enabled = e; |
412 | theme().updateEnabledState(*this); |
413 | invalidate(); |
414 | } |
415 | |
416 | bool Scrollbar::isOverlayScrollbar() const |
417 | { |
418 | return theme().usesOverlayScrollbars(); |
419 | } |
420 | |
421 | bool Scrollbar::shouldParticipateInHitTesting() |
422 | { |
423 | // Non-overlay scrollbars should always participate in hit testing. |
424 | if (!isOverlayScrollbar()) |
425 | return true; |
426 | return m_scrollableArea.scrollAnimator().shouldScrollbarParticipateInHitTesting(this); |
427 | } |
428 | |
429 | bool Scrollbar::isWindowActive() const |
430 | { |
431 | return m_scrollableArea.isActive(); |
432 | } |
433 | |
434 | void Scrollbar::invalidateRect(const IntRect& rect) |
435 | { |
436 | if (suppressInvalidation()) |
437 | return; |
438 | |
439 | m_scrollableArea.invalidateScrollbar(*this, rect); |
440 | } |
441 | |
442 | IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const |
443 | { |
444 | return m_scrollableArea.convertFromScrollbarToContainingView(*this, localRect); |
445 | } |
446 | |
447 | IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const |
448 | { |
449 | return m_scrollableArea.convertFromContainingViewToScrollbar(*this, parentRect); |
450 | } |
451 | |
452 | IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const |
453 | { |
454 | return m_scrollableArea.convertFromScrollbarToContainingView(*this, localPoint); |
455 | } |
456 | |
457 | IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const |
458 | { |
459 | return m_scrollableArea.convertFromContainingViewToScrollbar(*this, parentPoint); |
460 | } |
461 | |
462 | bool Scrollbar::supportsUpdateOnSecondaryThread() const |
463 | { |
464 | // It's unfortunate that this needs to be done with an ifdef. Ideally there would be a way to feature-detect |
465 | // the necessary support within AppKit. |
466 | #if ENABLE(ASYNC_SCROLLING) && PLATFORM(MAC) |
467 | return !m_scrollableArea.forceUpdateScrollbarsOnMainThreadForPerformanceTesting() |
468 | && (m_scrollableArea.hasLayerForVerticalScrollbar() || m_scrollableArea.hasLayerForHorizontalScrollbar()) |
469 | && m_scrollableArea.usesAsyncScrolling(); |
470 | #else |
471 | return false; |
472 | #endif |
473 | } |
474 | |
475 | } // namespace WebCore |
476 | |