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