1 | /* |
2 | * Copyright (C) 2008-2016 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 | #pragma once |
27 | |
28 | #include "ScrollSnapOffsetsInfo.h" |
29 | #include "ScrollTypes.h" |
30 | #include "Scrollbar.h" |
31 | #include <wtf/Forward.h> |
32 | #include <wtf/WeakPtr.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class FloatPoint; |
37 | class GraphicsContext; |
38 | class LayoutPoint; |
39 | class LayoutSize; |
40 | class PlatformTouchEvent; |
41 | class PlatformWheelEvent; |
42 | class ScrollAnimator; |
43 | class GraphicsLayer; |
44 | class TiledBacking; |
45 | |
46 | // scrollPosition is in content coordinates (0,0 is at scrollOrigin), so may have negative components. |
47 | typedef IntPoint ScrollPosition; |
48 | // scrollOffset() is the value used by scrollbars (min is 0,0), and should never have negative components. |
49 | typedef IntPoint ScrollOffset; |
50 | |
51 | |
52 | inline int offsetForOrientation(ScrollOffset offset, ScrollbarOrientation orientation) |
53 | { |
54 | switch (orientation) { |
55 | case HorizontalScrollbar: return offset.x(); |
56 | case VerticalScrollbar: return offset.y(); |
57 | } |
58 | ASSERT_NOT_REACHED(); |
59 | return 0; |
60 | } |
61 | |
62 | |
63 | class ScrollableArea : public CanMakeWeakPtr<ScrollableArea> { |
64 | public: |
65 | WEBCORE_EXPORT bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1); |
66 | WEBCORE_EXPORT void scrollToOffsetWithoutAnimation(const FloatPoint&, ScrollClamping = ScrollClamping::Clamped); |
67 | void scrollToOffsetWithoutAnimation(ScrollbarOrientation, float offset); |
68 | |
69 | // Should be called when the scroll position changes externally, for example if the scroll layer position |
70 | // is updated on the scrolling thread and we need to notify the main thread. |
71 | WEBCORE_EXPORT void notifyScrollPositionChanged(const ScrollPosition&); |
72 | |
73 | // Allows subclasses to handle scroll position updates themselves. If this member function |
74 | // returns true, the scrollable area won't actually update the scroll position and instead |
75 | // expect it to happen sometime in the future. |
76 | virtual bool requestScrollPositionUpdate(const ScrollPosition&) { return false; } |
77 | |
78 | WEBCORE_EXPORT bool handleWheelEvent(const PlatformWheelEvent&); |
79 | |
80 | #if ENABLE(CSS_SCROLL_SNAP) |
81 | WEBCORE_EXPORT const Vector<LayoutUnit>* horizontalSnapOffsets() const; |
82 | WEBCORE_EXPORT const Vector<LayoutUnit>* verticalSnapOffsets() const; |
83 | WEBCORE_EXPORT const Vector<ScrollOffsetRange<LayoutUnit>>* horizontalSnapOffsetRanges() const; |
84 | WEBCORE_EXPORT const Vector<ScrollOffsetRange<LayoutUnit>>* verticalSnapOffsetRanges() const; |
85 | virtual void updateSnapOffsets() { }; |
86 | void setHorizontalSnapOffsets(const Vector<LayoutUnit>&); |
87 | void setVerticalSnapOffsets(const Vector<LayoutUnit>&); |
88 | void setHorizontalSnapOffsetRanges(const Vector<ScrollOffsetRange<LayoutUnit>>&); |
89 | void setVerticalSnapOffsetRanges(const Vector<ScrollOffsetRange<LayoutUnit>>&); |
90 | void clearHorizontalSnapOffsets(); |
91 | void clearVerticalSnapOffsets(); |
92 | unsigned currentHorizontalSnapPointIndex() const { return m_currentHorizontalSnapPointIndex; } |
93 | void setCurrentHorizontalSnapPointIndex(unsigned index) { m_currentHorizontalSnapPointIndex = index; } |
94 | unsigned currentVerticalSnapPointIndex() const { return m_currentVerticalSnapPointIndex; } |
95 | void setCurrentVerticalSnapPointIndex(unsigned index) { m_currentVerticalSnapPointIndex = index; } |
96 | IntPoint nearestActiveSnapPoint(const IntPoint&); |
97 | #endif |
98 | |
99 | void updateScrollSnapState(); |
100 | |
101 | #if ENABLE(TOUCH_EVENTS) |
102 | virtual bool handleTouchEvent(const PlatformTouchEvent&); |
103 | #endif |
104 | |
105 | #if PLATFORM(IOS_FAMILY) |
106 | virtual void didStartScroll() { } |
107 | virtual void didEndScroll() { } |
108 | virtual void didUpdateScroll() { } |
109 | #endif |
110 | |
111 | // Functions for controlling if you can scroll past the end of the document. |
112 | bool constrainsScrollingToContentEdge() const { return m_constrainsScrollingToContentEdge; } |
113 | void setConstrainsScrollingToContentEdge(bool constrainsScrollingToContentEdge) { m_constrainsScrollingToContentEdge = constrainsScrollingToContentEdge; } |
114 | |
115 | void setVerticalScrollElasticity(ScrollElasticity scrollElasticity) { m_verticalScrollElasticity = scrollElasticity; } |
116 | ScrollElasticity verticalScrollElasticity() const { return static_cast<ScrollElasticity>(m_verticalScrollElasticity); } |
117 | |
118 | void setHorizontalScrollElasticity(ScrollElasticity scrollElasticity) { m_horizontalScrollElasticity = scrollElasticity; } |
119 | ScrollElasticity horizontalScrollElasticity() const { return static_cast<ScrollElasticity>(m_horizontalScrollElasticity); } |
120 | |
121 | virtual ScrollbarMode horizontalScrollbarMode() const { return ScrollbarAuto; } |
122 | virtual ScrollbarMode verticalScrollbarMode() const { return ScrollbarAuto; } |
123 | |
124 | virtual bool horizontalScrollbarHiddenByStyle() const { return false; } |
125 | virtual bool verticalScrollbarHiddenByStyle() const { return false; } |
126 | |
127 | bool inLiveResize() const { return m_inLiveResize; } |
128 | WEBCORE_EXPORT virtual void willStartLiveResize(); |
129 | WEBCORE_EXPORT virtual void willEndLiveResize(); |
130 | |
131 | WEBCORE_EXPORT void contentAreaWillPaint() const; |
132 | WEBCORE_EXPORT void mouseEnteredContentArea() const; |
133 | WEBCORE_EXPORT void mouseExitedContentArea() const; |
134 | WEBCORE_EXPORT void mouseMovedInContentArea() const; |
135 | WEBCORE_EXPORT void mouseEnteredScrollbar(Scrollbar*) const; |
136 | void mouseExitedScrollbar(Scrollbar*) const; |
137 | void mouseIsDownInScrollbar(Scrollbar*, bool) const; |
138 | void contentAreaDidShow() const; |
139 | void contentAreaDidHide() const; |
140 | |
141 | void lockOverlayScrollbarStateToHidden(bool shouldLockState) const; |
142 | WEBCORE_EXPORT bool scrollbarsCanBeActive() const; |
143 | |
144 | WEBCORE_EXPORT virtual void didAddScrollbar(Scrollbar*, ScrollbarOrientation); |
145 | WEBCORE_EXPORT virtual void willRemoveScrollbar(Scrollbar*, ScrollbarOrientation); |
146 | |
147 | WEBCORE_EXPORT virtual void contentsResized(); |
148 | |
149 | // Force the contents to recompute their size (i.e. do layout). |
150 | virtual void updateContentsSize() { } |
151 | |
152 | enum class AvailableSizeChangeReason { |
153 | ScrollbarsChanged, |
154 | AreaSizeChanged |
155 | }; |
156 | WEBCORE_EXPORT virtual void availableContentSizeChanged(AvailableSizeChangeReason); |
157 | |
158 | bool hasOverlayScrollbars() const; |
159 | WEBCORE_EXPORT virtual void setScrollbarOverlayStyle(ScrollbarOverlayStyle); |
160 | ScrollbarOverlayStyle scrollbarOverlayStyle() const { return static_cast<ScrollbarOverlayStyle>(m_scrollbarOverlayStyle); } |
161 | bool useDarkAppearanceForScrollbars() const; |
162 | |
163 | virtual ScrollingNodeID scrollingNodeID() const { return 0; } |
164 | |
165 | // This getter will create a ScrollAnimator if it doesn't already exist. |
166 | WEBCORE_EXPORT ScrollAnimator& scrollAnimator() const; |
167 | |
168 | // This getter will return null if the ScrollAnimator hasn't been created yet. |
169 | ScrollAnimator* existingScrollAnimator() const { return m_scrollAnimator.get(); } |
170 | |
171 | virtual bool isActive() const = 0; |
172 | WEBCORE_EXPORT virtual void invalidateScrollbar(Scrollbar&, const IntRect&); |
173 | virtual bool isScrollCornerVisible() const = 0; |
174 | virtual IntRect scrollCornerRect() const = 0; |
175 | WEBCORE_EXPORT virtual void invalidateScrollCorner(const IntRect&); |
176 | |
177 | virtual bool forceUpdateScrollbarsOnMainThreadForPerformanceTesting() const = 0; |
178 | |
179 | // Convert points and rects between the scrollbar and its containing view. |
180 | // The client needs to implement these in order to be aware of layout effects |
181 | // like CSS transforms. |
182 | virtual IntRect convertFromScrollbarToContainingView(const Scrollbar& scrollbar, const IntRect& scrollbarRect) const |
183 | { |
184 | return scrollbar.Widget::convertToContainingView(scrollbarRect); |
185 | } |
186 | virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar& scrollbar, const IntRect& parentRect) const |
187 | { |
188 | return scrollbar.Widget::convertFromContainingView(parentRect); |
189 | } |
190 | virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar& scrollbar, const IntPoint& scrollbarPoint) const |
191 | { |
192 | return scrollbar.Widget::convertToContainingView(scrollbarPoint); |
193 | } |
194 | virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar& scrollbar, const IntPoint& parentPoint) const |
195 | { |
196 | return scrollbar.Widget::convertFromContainingView(parentPoint); |
197 | } |
198 | |
199 | int horizontalScrollbarIntrusion() const; |
200 | int verticalScrollbarIntrusion() const; |
201 | WEBCORE_EXPORT IntSize scrollbarIntrusion() const; |
202 | |
203 | virtual Scrollbar* horizontalScrollbar() const { return nullptr; } |
204 | virtual Scrollbar* verticalScrollbar() const { return nullptr; } |
205 | |
206 | const IntPoint& scrollOrigin() const { return m_scrollOrigin; } |
207 | bool scrollOriginChanged() const { return m_scrollOriginChanged; } |
208 | |
209 | virtual ScrollPosition scrollPosition() const = 0; |
210 | virtual ScrollPosition minimumScrollPosition() const; |
211 | virtual ScrollPosition maximumScrollPosition() const; |
212 | |
213 | ScrollPosition constrainScrollPosition(const ScrollPosition& position) const |
214 | { |
215 | return position.constrainedBetween(minimumScrollPosition(), maximumScrollPosition()); |
216 | } |
217 | |
218 | WEBCORE_EXPORT ScrollOffset scrollOffset() const; |
219 | |
220 | ScrollOffset maximumScrollOffset() const; |
221 | |
222 | WEBCORE_EXPORT ScrollPosition scrollPositionFromOffset(ScrollOffset) const; |
223 | WEBCORE_EXPORT ScrollOffset scrollOffsetFromPosition(ScrollPosition) const; |
224 | |
225 | template<typename PositionType, typename SizeType> |
226 | static PositionType scrollPositionFromOffset(PositionType offset, SizeType scrollOrigin) |
227 | { |
228 | return offset - scrollOrigin; |
229 | } |
230 | |
231 | template<typename PositionType, typename SizeType> |
232 | static PositionType scrollOffsetFromPosition(PositionType position, SizeType scrollOrigin) |
233 | { |
234 | return position + scrollOrigin; |
235 | } |
236 | |
237 | WEBCORE_EXPORT virtual bool scrolledToTop() const; |
238 | WEBCORE_EXPORT virtual bool scrolledToBottom() const; |
239 | WEBCORE_EXPORT virtual bool scrolledToLeft() const; |
240 | WEBCORE_EXPORT virtual bool scrolledToRight() const; |
241 | |
242 | ScrollType currentScrollType() const { return static_cast<ScrollType>(m_currentScrollType); } |
243 | void setCurrentScrollType(ScrollType scrollType) { m_currentScrollType = static_cast<unsigned>(scrollType); } |
244 | |
245 | bool scrollShouldClearLatchedState() const { return m_scrollShouldClearLatchedState; } |
246 | void setScrollShouldClearLatchedState(bool shouldClear) { m_scrollShouldClearLatchedState = shouldClear; } |
247 | |
248 | enum VisibleContentRectIncludesScrollbars { ExcludeScrollbars, IncludeScrollbars }; |
249 | enum VisibleContentRectBehavior { |
250 | ContentsVisibleRect, |
251 | #if PLATFORM(IOS_FAMILY) |
252 | LegacyIOSDocumentViewRect, |
253 | LegacyIOSDocumentVisibleRect = LegacyIOSDocumentViewRect |
254 | #else |
255 | LegacyIOSDocumentVisibleRect = ContentsVisibleRect |
256 | #endif |
257 | }; |
258 | |
259 | WEBCORE_EXPORT IntRect visibleContentRect(VisibleContentRectBehavior = ContentsVisibleRect) const; |
260 | WEBCORE_EXPORT IntRect visibleContentRectIncludingScrollbars(VisibleContentRectBehavior = ContentsVisibleRect) const; |
261 | |
262 | int visibleWidth() const { return visibleSize().width(); } |
263 | int visibleHeight() const { return visibleSize().height(); } |
264 | virtual IntSize visibleSize() const = 0; |
265 | |
266 | virtual IntSize contentsSize() const = 0; |
267 | virtual IntSize overhangAmount() const { return IntSize(); } |
268 | virtual IntPoint lastKnownMousePosition() const { return IntPoint(); } |
269 | virtual bool isHandlingWheelEvent() const { return false; } |
270 | |
271 | virtual int () const { return 0; } |
272 | virtual int () const { return 0; } |
273 | |
274 | // The totalContentsSize() is equivalent to the contentsSize() plus the header and footer heights. |
275 | WEBCORE_EXPORT IntSize totalContentsSize() const; |
276 | WEBCORE_EXPORT virtual IntSize reachableTotalContentsSize() const; |
277 | |
278 | virtual bool useDarkAppearance() const { return false; } |
279 | |
280 | virtual bool shouldSuspendScrollAnimations() const { return true; } |
281 | WEBCORE_EXPORT virtual void scrollbarStyleChanged(ScrollbarStyle /*newStyle*/, bool /*forceUpdate*/); |
282 | virtual void setVisibleScrollerThumbRect(const IntRect&) { } |
283 | |
284 | // Note that this only returns scrollable areas that can actually be scrolled. |
285 | virtual ScrollableArea* enclosingScrollableArea() const = 0; |
286 | |
287 | virtual bool isScrollableOrRubberbandable() = 0; |
288 | virtual bool hasScrollableOrRubberbandableAncestor() = 0; |
289 | |
290 | // Returns the bounding box of this scrollable area, in the coordinate system of the enclosing scroll view. |
291 | virtual IntRect scrollableAreaBoundingBox(bool* = nullptr) const = 0; |
292 | |
293 | virtual bool isRubberBandInProgress() const { return false; } |
294 | virtual bool isScrollSnapInProgress() const { return false; } |
295 | |
296 | virtual bool scrollAnimatorEnabled() const { return false; } |
297 | |
298 | // NOTE: Only called from Internals for testing. |
299 | WEBCORE_EXPORT void setScrollOffsetFromInternals(const ScrollOffset&); |
300 | |
301 | WEBCORE_EXPORT static LayoutPoint constrainScrollPositionForOverhang(const LayoutRect& visibleContentRect, const LayoutSize& totalContentsSize, const LayoutPoint& scrollPosition, const LayoutPoint& scrollOrigin, int , int ); |
302 | LayoutPoint constrainScrollPositionForOverhang(const LayoutPoint& scrollPosition); |
303 | |
304 | // Computes the double value for the scrollbar's current position and the current overhang amount. |
305 | // This function is static so that it can be called from the main thread or the scrolling thread. |
306 | WEBCORE_EXPORT static void computeScrollbarValueAndOverhang(float currentPosition, float totalSize, float visibleSize, float& doubleValue, float& overhangAmount); |
307 | |
308 | // Let subclasses provide a way of asking for and servicing scroll |
309 | // animations. |
310 | virtual bool scheduleAnimation() { return false; } |
311 | void serviceScrollAnimations(); |
312 | |
313 | bool isHorizontalScrollerPinnedToMinimumPosition() const { return !horizontalScrollbar() || scrollOffset().x() <= 0; } |
314 | bool isHorizontalScrollerPinnedToMaximumPosition() const { return !horizontalScrollbar() || scrollOffset().x() >= maximumScrollOffset().x(); } |
315 | bool isVerticalScrollerPinnedToMinimumPosition() const { return !verticalScrollbar() || scrollOffset().y() <= 0; } |
316 | bool isVerticalScrollerPinnedToMaximumPosition() const { return !verticalScrollbar() || scrollOffset().y() >= maximumScrollOffset().y(); } |
317 | |
318 | bool isPinnedInBothDirections(const IntSize&) const; |
319 | bool isPinnedHorizontallyInDirection(int horizontalScrollDelta) const; |
320 | bool isPinnedVerticallyInDirection(int verticalScrollDelta) const; |
321 | |
322 | // True if scrolling happens by moving compositing layers. |
323 | virtual bool usesCompositedScrolling() const { return false; } |
324 | // True if the contents can be scrolled asynchronously (i.e. by a ScrollingCoordinator). |
325 | virtual bool usesAsyncScrolling() const { return false; } |
326 | |
327 | virtual GraphicsLayer* layerForHorizontalScrollbar() const { return nullptr; } |
328 | virtual GraphicsLayer* layerForVerticalScrollbar() const { return nullptr; } |
329 | |
330 | bool hasLayerForHorizontalScrollbar() const; |
331 | bool hasLayerForVerticalScrollbar() const; |
332 | |
333 | void verticalScrollbarLayerDidChange(); |
334 | void horizontalScrollbarLayerDidChange(); |
335 | |
336 | virtual bool usesMockScrollAnimator() const { return false; } |
337 | virtual void logMockScrollAnimatorMessage(const String&) const { }; |
338 | |
339 | virtual bool shouldPlaceBlockDirectionScrollbarOnLeft() const = 0; |
340 | |
341 | protected: |
342 | WEBCORE_EXPORT ScrollableArea(); |
343 | WEBCORE_EXPORT virtual ~ScrollableArea(); |
344 | |
345 | void setScrollOrigin(const IntPoint&); |
346 | void resetScrollOriginChanged() { m_scrollOriginChanged = false; } |
347 | |
348 | WEBCORE_EXPORT virtual float adjustScrollStepForFixedContent(float step, ScrollbarOrientation, ScrollGranularity); |
349 | virtual void invalidateScrollbarRect(Scrollbar&, const IntRect&) = 0; |
350 | virtual void invalidateScrollCornerRect(const IntRect&) = 0; |
351 | |
352 | friend class ScrollingCoordinator; |
353 | virtual GraphicsLayer* layerForScrollCorner() const { return nullptr; } |
354 | #if ENABLE(RUBBER_BANDING) |
355 | virtual GraphicsLayer* layerForOverhangAreas() const { return nullptr; } |
356 | #endif |
357 | |
358 | bool hasLayerForScrollCorner() const; |
359 | |
360 | private: |
361 | WEBCORE_EXPORT virtual IntRect visibleContentRectInternal(VisibleContentRectIncludesScrollbars, VisibleContentRectBehavior) const; |
362 | void scrollPositionChanged(const ScrollPosition&); |
363 | |
364 | // NOTE: Only called from the ScrollAnimator. |
365 | friend class ScrollAnimator; |
366 | void setScrollOffsetFromAnimation(const ScrollOffset&); |
367 | |
368 | // This function should be overridden by subclasses to perform the actual |
369 | // scroll of the content. |
370 | virtual void setScrollOffset(const ScrollOffset&) = 0; |
371 | ScrollSnapOffsetsInfo<LayoutUnit>& ensureSnapOffsetsInfo(); |
372 | |
373 | mutable std::unique_ptr<ScrollAnimator> m_scrollAnimator; |
374 | |
375 | #if ENABLE(CSS_SCROLL_SNAP) |
376 | std::unique_ptr<ScrollSnapOffsetsInfo<LayoutUnit>> m_snapOffsetsInfo; |
377 | unsigned m_currentHorizontalSnapPointIndex { 0 }; |
378 | unsigned m_currentVerticalSnapPointIndex { 0 }; |
379 | #endif |
380 | |
381 | // There are 8 possible combinations of writing mode and direction. Scroll origin will be non-zero in the x or y axis |
382 | // if there is any reversed direction or writing-mode. The combinations are: |
383 | // writing-mode / direction scrollOrigin.x() set scrollOrigin.y() set |
384 | // horizontal-tb / ltr NO NO |
385 | // horizontal-tb / rtl YES NO |
386 | // horizontal-bt / ltr NO YES |
387 | // horizontal-bt / rtl YES YES |
388 | // vertical-lr / ltr NO NO |
389 | // vertical-lr / rtl NO YES |
390 | // vertical-rl / ltr YES NO |
391 | // vertical-rl / rtl YES YES |
392 | IntPoint m_scrollOrigin; |
393 | |
394 | unsigned m_constrainsScrollingToContentEdge : 1; |
395 | |
396 | unsigned m_inLiveResize : 1; |
397 | |
398 | unsigned m_verticalScrollElasticity : 2; // ScrollElasticity |
399 | unsigned m_horizontalScrollElasticity : 2; // ScrollElasticity |
400 | |
401 | unsigned m_scrollbarOverlayStyle : 2; // ScrollbarOverlayStyle |
402 | |
403 | unsigned m_scrollOriginChanged : 1; |
404 | unsigned m_currentScrollType : 1; // ScrollType |
405 | unsigned m_scrollShouldClearLatchedState : 1; |
406 | }; |
407 | |
408 | } // namespace WebCore |
409 | |
410 | |