Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : /*
7 : * interface that provides scroll APIs implemented by scrollable frames
8 : */
9 :
10 : #ifndef nsIScrollFrame_h___
11 : #define nsIScrollFrame_h___
12 :
13 : #include "nsCoord.h"
14 : #include "DisplayItemClip.h"
15 : #include "ScrollbarStyles.h"
16 : #include "mozilla/Maybe.h"
17 : #include "mozilla/gfx/Point.h"
18 : #include "nsIScrollbarMediator.h"
19 : #include "Units.h"
20 : #include "FrameMetrics.h"
21 :
22 : #define NS_DEFAULT_VERTICAL_SCROLL_DISTANCE 3
23 : #define NS_DEFAULT_HORIZONTAL_SCROLL_DISTANCE 5
24 :
25 : class gfxContext;
26 : class nsBoxLayoutState;
27 : class nsIScrollPositionListener;
28 : class nsIFrame;
29 : class nsPresContext;
30 : class nsIContent;
31 : class nsIAtom;
32 : class nsDisplayListBuilder;
33 :
34 : namespace mozilla {
35 : struct ContainerLayerParameters;
36 : namespace layers {
37 : class Layer;
38 : } // namespace layers
39 : } // namespace mozilla
40 :
41 : /**
42 : * Interface for frames that are scrollable. This interface exposes
43 : * APIs for examining scroll state, observing changes to scroll state,
44 : * and triggering scrolling.
45 : */
46 43 : class nsIScrollableFrame : public nsIScrollbarMediator {
47 : public:
48 : typedef mozilla::CSSIntPoint CSSIntPoint;
49 : typedef mozilla::ContainerLayerParameters ContainerLayerParameters;
50 : typedef mozilla::layers::FrameMetrics FrameMetrics;
51 : typedef mozilla::layers::ScrollSnapInfo ScrollSnapInfo;
52 :
53 : NS_DECL_QUERYFRAME_TARGET(nsIScrollableFrame)
54 :
55 : /**
56 : * Get the frame for the content that we are scrolling within
57 : * this scrollable frame.
58 : */
59 : virtual nsIFrame* GetScrolledFrame() const = 0;
60 :
61 : /**
62 : * Get the styles (NS_STYLE_OVERFLOW_SCROLL, NS_STYLE_OVERFLOW_HIDDEN,
63 : * or NS_STYLE_OVERFLOW_AUTO) governing the horizontal and vertical
64 : * scrollbars for this frame.
65 : */
66 : virtual mozilla::ScrollbarStyles GetScrollbarStyles() const = 0;
67 :
68 : enum { HORIZONTAL = 0x01, VERTICAL = 0x02 };
69 : /**
70 : * Return the scrollbars which are visible. It's OK to call this during reflow
71 : * of the scrolled contents, in which case it will reflect the current
72 : * assumptions about scrollbar visibility.
73 : */
74 : virtual uint32_t GetScrollbarVisibility() const = 0;
75 : /**
76 : * Returns the directions in which scrolling is perceived to be allowed.
77 : * A direction is perceived to be allowed if there is a visible scrollbar
78 : * for that direction or if the scroll range is at least one device pixel.
79 : */
80 : uint32_t GetPerceivedScrollingDirections() const;
81 : /**
82 : * Return the actual sizes of all possible scrollbars. Returns 0 for scrollbar
83 : * positions that don't have a scrollbar or where the scrollbar is not visible.
84 : * Do not call this while this frame's descendants are being reflowed, it won't be
85 : * accurate.
86 : */
87 : virtual nsMargin GetActualScrollbarSizes() const = 0;
88 : /**
89 : * Return the sizes of all scrollbars assuming that any scrollbars that could
90 : * be visible due to overflowing content, are. This can be called during reflow
91 : * of the scrolled contents.
92 : */
93 : virtual nsMargin GetDesiredScrollbarSizes(nsBoxLayoutState* aState) = 0;
94 : /**
95 : * Return the sizes of all scrollbars assuming that any scrollbars that could
96 : * be visible due to overflowing content, are. This can be called during reflow
97 : * of the scrolled contents.
98 : */
99 : virtual nsMargin GetDesiredScrollbarSizes(nsPresContext* aPresContext,
100 : gfxContext* aRC) = 0;
101 : /**
102 : * Return the width for non-disappearing scrollbars.
103 : */
104 : virtual nscoord
105 : GetNondisappearingScrollbarWidth(nsPresContext* aPresContext,
106 : gfxContext* aRC,
107 : mozilla::WritingMode aWM) = 0;
108 : /**
109 : * GetScrolledRect is designed to encapsulate deciding which
110 : * directions of overflow should be reachable by scrolling and which
111 : * should not. Callers should NOT depend on it having any particular
112 : * behavior (although nsXULScrollFrame currently does).
113 : *
114 : * This should only be called when the scrolled frame has been
115 : * reflowed with the scroll port size given in mScrollPort.
116 : *
117 : * Currently it allows scrolling down and to the right for
118 : * nsHTMLScrollFrames with LTR directionality and for all
119 : * nsXULScrollFrames, and allows scrolling down and to the left for
120 : * nsHTMLScrollFrames with RTL directionality.
121 : */
122 : virtual nsRect GetScrolledRect() const = 0;
123 : /**
124 : * Get the area of the scrollport relative to the origin of this frame's
125 : * border-box.
126 : * This is the area of this frame minus border and scrollbars.
127 : */
128 : virtual nsRect GetScrollPortRect() const = 0;
129 : /**
130 : * Get the offset of the scrollport origin relative to the scrolled
131 : * frame origin. Typically the position will be non-negative.
132 : * This will always be a multiple of device pixels.
133 : */
134 : virtual nsPoint GetScrollPosition() const = 0;
135 : /**
136 : * As GetScrollPosition(), but uses the top-right as origin for RTL frames.
137 : */
138 : virtual nsPoint GetLogicalScrollPosition() const = 0;
139 : /**
140 : * Get the area that must contain the scroll position. Typically
141 : * (but not always, e.g. for RTL content) x and y will be 0, and
142 : * width or height will be nonzero if the content can be scrolled in
143 : * that direction. Since scroll positions must be a multiple of
144 : * device pixels, the range extrema will also be a multiple of
145 : * device pixels.
146 : */
147 : virtual nsRect GetScrollRange() const = 0;
148 : /**
149 : * Get the size of the scroll port to use when clamping the scroll
150 : * position.
151 : */
152 : virtual nsSize GetScrollPositionClampingScrollPortSize() const = 0;
153 : /**
154 : * Return how much we would try to scroll by in each direction if
155 : * asked to scroll by one "line" vertically and horizontally.
156 : */
157 : virtual nsSize GetLineScrollAmount() const = 0;
158 : /**
159 : * Return how much we would try to scroll by in each direction if
160 : * asked to scroll by one "page" vertically and horizontally.
161 : */
162 : virtual nsSize GetPageScrollAmount() const = 0;
163 :
164 : /**
165 : * When a scroll operation is requested, we ask for instant, smooth,
166 : * smooth msd, or normal scrolling.
167 : *
168 : * SMOOTH scrolls have a symmetrical acceleration and deceleration curve
169 : * modeled with a set of splines that guarantee that the destination will be
170 : * reached over a fixed time interval. SMOOTH will only be smooth if smooth
171 : * scrolling is actually enabled. This behavior is utilized by keyboard and
172 : * mouse wheel scrolling events.
173 : *
174 : * SMOOTH_MSD implements a physically based model that approximates the
175 : * behavior of a mass-spring-damper system. SMOOTH_MSD scrolls have a
176 : * non-symmetrical acceleration and deceleration curve, can potentially
177 : * overshoot the destination on intermediate frames, and complete over a
178 : * variable time interval. SMOOTH_MSD will only be smooth if cssom-view
179 : * smooth-scrolling is enabled.
180 : *
181 : * INSTANT is always synchronous, NORMAL can be asynchronous.
182 : *
183 : * If an INSTANT scroll request happens while a SMOOTH or async scroll is
184 : * already in progress, the async scroll is interrupted and we instantly
185 : * scroll to the destination.
186 : *
187 : * If an INSTANT or SMOOTH scroll request happens while a SMOOTH_MSD scroll
188 : * is already in progress, the SMOOTH_MSD scroll is interrupted without
189 : * first scrolling to the destination.
190 : */
191 : enum ScrollMode { INSTANT, SMOOTH, SMOOTH_MSD, NORMAL };
192 : /**
193 : * Some platforms (OSX) may generate additional scrolling events even
194 : * after the user has stopped scrolling, simulating a momentum scrolling
195 : * effect resulting from fling gestures.
196 : * SYNTHESIZED_MOMENTUM_EVENT indicates that the scrolling is being requested
197 : * by such a synthesized event and may be ignored if another scroll has
198 : * been started since the last actual user input.
199 : */
200 : enum ScrollMomentum { NOT_MOMENTUM, SYNTHESIZED_MOMENTUM_EVENT };
201 : /**
202 : * @note This method might destroy the frame, pres shell and other objects.
203 : * Clamps aScrollPosition to GetScrollRange and sets the scroll position
204 : * to that value.
205 : * @param aRange If non-null, specifies area which contains aScrollPosition
206 : * and can be used for choosing a performance-optimized scroll position.
207 : * Any point within this area can be chosen.
208 : * The choosen point will be as close as possible to aScrollPosition.
209 : */
210 : virtual void ScrollTo(nsPoint aScrollPosition, ScrollMode aMode,
211 : const nsRect* aRange = nullptr,
212 : nsIScrollbarMediator::ScrollSnapMode aSnap
213 : = nsIScrollbarMediator::DISABLE_SNAP) = 0;
214 : /**
215 : * @note This method might destroy the frame, pres shell and other objects.
216 : * Scrolls to a particular position in integer CSS pixels.
217 : * Keeps the exact current horizontal or vertical position if the current
218 : * position, rounded to CSS pixels, matches aScrollPosition. If
219 : * aScrollPosition.x/y is different from the current CSS pixel position,
220 : * makes sure we only move in the direction given by the difference.
221 : *
222 : * When aMode is SMOOTH, INSTANT, or NORMAL, GetScrollPositionCSSPixels (the
223 : * scroll position after rounding to CSS pixels) will be exactly
224 : * aScrollPosition at the end of the scroll animation.
225 : *
226 : * When aMode is SMOOTH_MSD, intermediate animation frames may be outside the
227 : * range and / or moving in any direction; GetScrollPositionCSSPixels will be
228 : * exactly aScrollPosition at the end of the scroll animation unless the
229 : * SMOOTH_MSD animation is interrupted.
230 : */
231 : virtual void ScrollToCSSPixels(const CSSIntPoint& aScrollPosition,
232 : nsIScrollableFrame::ScrollMode aMode
233 : = nsIScrollableFrame::INSTANT) = 0;
234 : /**
235 : * @note This method might destroy the frame, pres shell and other objects.
236 : * Scrolls to a particular position in float CSS pixels.
237 : * This does not guarantee that GetScrollPositionCSSPixels equals
238 : * aScrollPosition afterward. It tries to scroll as close to
239 : * aScrollPosition as possible while scrolling by an integer
240 : * number of layer pixels (so the operation is fast and looks clean).
241 : */
242 : virtual void ScrollToCSSPixelsApproximate(const mozilla::CSSPoint& aScrollPosition,
243 : nsIAtom *aOrigin = nullptr) = 0;
244 :
245 : /**
246 : * Returns the scroll position in integer CSS pixels, rounded to the nearest
247 : * pixel.
248 : */
249 : virtual CSSIntPoint GetScrollPositionCSSPixels() = 0;
250 : /**
251 : * When scrolling by a relative amount, we can choose various units.
252 : */
253 : enum ScrollUnit { DEVICE_PIXELS, LINES, PAGES, WHOLE };
254 : /**
255 : * @note This method might destroy the frame, pres shell and other objects.
256 : * Modifies the current scroll position by aDelta units given by aUnit,
257 : * clamping it to GetScrollRange. If WHOLE is specified as the unit,
258 : * content is scrolled all the way in the direction(s) given by aDelta.
259 : * @param aOverflow if non-null, returns the amount that scrolling
260 : * was clamped by in each direction (how far we moved the scroll position
261 : * to bring it back into the legal range). This is never negative. The
262 : * values are in device pixels.
263 : */
264 : virtual void ScrollBy(nsIntPoint aDelta, ScrollUnit aUnit, ScrollMode aMode,
265 : nsIntPoint* aOverflow = nullptr,
266 : nsIAtom* aOrigin = nullptr,
267 : ScrollMomentum aMomentum = NOT_MOMENTUM,
268 : nsIScrollbarMediator::ScrollSnapMode aSnap
269 : = nsIScrollbarMediator::DISABLE_SNAP) = 0;
270 :
271 : /**
272 : * Perform scroll snapping, possibly resulting in a smooth scroll to
273 : * maintain the scroll snap position constraints. Velocity sampled from
274 : * main thread scrolling is used to determine best matching snap point
275 : * when called after a fling gesture on a trackpad or mouse wheel.
276 : */
277 : virtual void ScrollSnap() = 0;
278 :
279 : /**
280 : * @note This method might destroy the frame, pres shell and other objects.
281 : * This tells the scroll frame to try scrolling to the scroll
282 : * position that was restored from the history. This must be called
283 : * at least once after state has been restored. It is called by the
284 : * scrolled frame itself during reflow, but sometimes state can be
285 : * restored after reflows are done...
286 : * XXX should we take an aMode parameter here? Currently it's instant.
287 : */
288 : virtual void ScrollToRestoredPosition() = 0;
289 :
290 : /**
291 : * Add a scroll position listener. This listener must be removed
292 : * before it is destroyed.
293 : */
294 : virtual void AddScrollPositionListener(nsIScrollPositionListener* aListener) = 0;
295 : /**
296 : * Remove a scroll position listener.
297 : */
298 : virtual void RemoveScrollPositionListener(nsIScrollPositionListener* aListener) = 0;
299 :
300 : /**
301 : * Internal method used by scrollbars to notify their scrolling
302 : * container of changes.
303 : */
304 : virtual void CurPosAttributeChanged(nsIContent* aChild) = 0;
305 :
306 : /**
307 : * Allows the docshell to request that the scroll frame post an event
308 : * after being restored from history.
309 : */
310 : NS_IMETHOD PostScrolledAreaEventForCurrentArea() = 0;
311 :
312 : /**
313 : * Returns true if this scrollframe is being "actively scrolled".
314 : * This basically means that we should allocate resources in the
315 : * expectation that scrolling is going to happen.
316 : */
317 : virtual bool IsScrollingActive(nsDisplayListBuilder* aBuilder) = 0;
318 : /**
319 : * Returns true if the scrollframe is currently processing an async
320 : * or smooth scroll.
321 : */
322 : virtual bool IsProcessingAsyncScroll() = 0;
323 : /**
324 : * Call this when the layer(s) induced by active scrolling are being
325 : * completely redrawn.
326 : */
327 : virtual void ResetScrollPositionForLayerPixelAlignment() = 0;
328 : /**
329 : * Was the current presentation state for this frame restored from history?
330 : */
331 : virtual bool DidHistoryRestore() const = 0;
332 : /**
333 : * Clear the flag so that DidHistoryRestore() returns false until the next
334 : * RestoreState call.
335 : * @see nsIStatefulFrame::RestoreState
336 : */
337 : virtual void ClearDidHistoryRestore() = 0;
338 : /**
339 : * Determine if the passed in rect is nearly visible according to the frame
340 : * visibility heuristics for how close it is to the visible scrollport.
341 : */
342 : virtual bool IsRectNearlyVisible(const nsRect& aRect) = 0;
343 : /**
344 : * Expand the given rect taking into account which directions we can scroll
345 : * and how far we want to expand for frame visibility purposes.
346 : */
347 : virtual nsRect ExpandRectToNearlyVisible(const nsRect& aRect) const = 0;
348 : /**
349 : * Returns the origin that triggered the last instant scroll. Will equal
350 : * nsGkAtoms::apz when the compositor's replica frame metrics includes the
351 : * latest instant scroll.
352 : */
353 : virtual nsIAtom* LastScrollOrigin() = 0;
354 : /**
355 : * Sets a flag on the scrollframe that indicates the current scroll origin
356 : * has been sent over in a layers transaction, and subsequent changes to
357 : * the scroll position by "weaker" origins are permitted to overwrite the
358 : * the scroll origin. Scroll origins that nsLayoutUtils::CanScrollOriginClobberApz
359 : * returns false for are considered "weaker" than scroll origins for which
360 : * that function returns true.
361 : */
362 : virtual void AllowScrollOriginDowngrade() = 0;
363 : /**
364 : * Returns the origin that triggered the last smooth scroll.
365 : * Will equal nsGkAtoms::apz when the compositor's replica frame
366 : * metrics includes the latest smooth scroll. The compositor will always
367 : * perform an instant scroll prior to instantiating any smooth scrolls
368 : * if LastScrollOrigin and LastSmoothScrollOrigin indicate that
369 : * an instant scroll and a smooth scroll have occurred since the last
370 : * replication of the frame metrics.
371 : *
372 : * This is set to nullptr to when the compositor thread acknowledges that
373 : * the smooth scroll has been started. If the smooth scroll has been stomped
374 : * by an instant scroll before the smooth scroll could be started by the
375 : * compositor, this is set to nullptr to clear the smooth scroll.
376 : */
377 : virtual nsIAtom* LastSmoothScrollOrigin() = 0;
378 : /**
379 : * Returns the current generation counter for the scroll. This counter
380 : * increments every time the scroll position is set.
381 : */
382 : virtual uint32_t CurrentScrollGeneration() = 0;
383 : /**
384 : * LastScrollDestination returns the destination of the most recently
385 : * requested smooth scroll animation.
386 : */
387 : virtual nsPoint LastScrollDestination() = 0;
388 : /**
389 : * Clears the "origin of last scroll" property stored in this frame, if
390 : * the generation counter passed in matches the current scroll generation
391 : * counter.
392 : */
393 : virtual void ResetScrollInfoIfGeneration(uint32_t aGeneration) = 0;
394 : /**
395 : * Determine whether it is desirable to be able to asynchronously scroll this
396 : * scroll frame.
397 : */
398 : virtual bool WantAsyncScroll() const = 0;
399 : /**
400 : * aLayer's animated geometry root is this frame. If there needs to be a
401 : * ScrollMetadata contributed by this frame, append it to aOutput.
402 : */
403 : virtual mozilla::Maybe<mozilla::layers::ScrollMetadata> ComputeScrollMetadata(
404 : mozilla::layers::Layer* aLayer,
405 : nsIFrame* aContainerReferenceFrame,
406 : const ContainerLayerParameters& aParameters,
407 : const mozilla::DisplayItemClip* aClip) const = 0;
408 :
409 : /**
410 : * If this scroll frame is ignoring viewporting clipping
411 : */
412 : virtual bool IsIgnoringViewportClipping() const = 0;
413 :
414 : /**
415 : * Mark the scrollbar frames for reflow.
416 : */
417 : virtual void MarkScrollbarsDirtyForReflow() const = 0;
418 :
419 : virtual void SetTransformingByAPZ(bool aTransforming) = 0;
420 : virtual bool IsTransformingByAPZ() const = 0;
421 :
422 : /**
423 : * Notify this scroll frame that it can be scrolled by APZ. In particular,
424 : * this is called *after* the APZ code has created an APZC for this scroll
425 : * frame and verified that it is not a scrollinfo layer. Therefore, setting an
426 : * async transform on it is actually user visible.
427 : */
428 : virtual void SetScrollableByAPZ(bool aScrollable) = 0;
429 :
430 : /**
431 : * Notify this scroll frame that it can be zoomed by APZ.
432 : */
433 : virtual void SetZoomableByAPZ(bool aZoomable) = 0;
434 :
435 : /**
436 : * Whether or not this frame uses containerful scrolling.
437 : */
438 : virtual bool UsesContainerScrolling() const = 0;
439 :
440 : /**
441 : * Determine if we should build a scrollable layer for this scroll frame and
442 : * return the result. It will also record this result on the scroll frame.
443 : * Pass the dirty rect in aDirtyRect. On return it will be set to the
444 : * displayport if there is one (ie the dirty rect that should be used).
445 : * This function may create a display port where one did not exist before if
446 : * aAllowCreateDisplayPort is true. It is only allowed to be false if there
447 : * has been a call with it set to true before on the same paint.
448 : */
449 : virtual bool DecideScrollableLayer(nsDisplayListBuilder* aBuilder,
450 : nsRect* aDirtyRect,
451 : bool aAllowCreateDisplayPort) = 0;
452 :
453 : /**
454 : * Notification that this scroll frame is getting its frame visibility updated.
455 : * aIgnoreDisplayPort indicates that the display port was ignored (because
456 : * there was no suitable base rect)
457 : */
458 : virtual void NotifyApproximateFrameVisibilityUpdate(bool aIgnoreDisplayPort) = 0;
459 :
460 : /**
461 : * Returns true if this scroll frame had a display port at the last frame
462 : * visibility update and fills in aDisplayPort with that displayport. Returns
463 : * false otherwise, and doesn't touch aDisplayPort.
464 : */
465 : virtual bool GetDisplayPortAtLastApproximateFrameVisibilityUpdate(nsRect* aDisplayPort) = 0;
466 :
467 : /**
468 : * This is called when a descendant scrollframe's has its displayport expired.
469 : * This function will check to see if this scrollframe may safely expire its
470 : * own displayport and schedule a timer to do that if it is safe.
471 : */
472 : virtual void TriggerDisplayPortExpiration() = 0;
473 :
474 : /**
475 : * Returns information required to determine where to snap to after a scroll.
476 : */
477 : virtual ScrollSnapInfo GetScrollSnapInfo() const = 0;
478 :
479 : /**
480 : * Given the drag event aEvent, determine whether the mouse is near the edge
481 : * of the scrollable area, and scroll the view in the direction of that edge
482 : * if so. If scrolling occurred, true is returned. When false is returned, the
483 : * caller should look for an ancestor to scroll.
484 : */
485 : virtual bool DragScroll(mozilla::WidgetEvent* aEvent) = 0;
486 :
487 : virtual void AsyncScrollbarDragRejected() = 0;
488 :
489 : /**
490 : * Returns whether this scroll frame is the root scroll frame of the document
491 : * that it is in. Note that some documents don't have root scroll frames at
492 : * all (ie XUL documents) even though they may contain other scroll frames.
493 : */
494 : virtual bool IsRootScrollFrameOfDocument() const = 0;
495 : };
496 :
497 : #endif
|