OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef RootScroller_h | 5 #ifndef RootScroller_h |
6 #define RootScroller_h | 6 #define RootScroller_h |
7 | 7 |
8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
9 #include "platform/heap/Handle.h" | 9 #include "platform/heap/Handle.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
| 13 class Document; |
13 class Element; | 14 class Element; |
14 class FrameHost; | 15 class OverscrollController; |
| 16 class TopControls; |
15 class ViewportScrollCallback; | 17 class ViewportScrollCallback; |
16 | 18 |
17 // This class manages the root scroller associated with the top-level document. | 19 // Manages the root scroller associated with a given document. The root scroller |
18 // The root scroller causes top controls movement, overscroll effects and | 20 // causes top controls movement, overscroll effects and prevents chaining |
19 // prevents chaining up further in the DOM. It can be set using | 21 // scrolls up further in the DOM. It can be set from script using |
20 // document.setRootScroller. By default, the rootScroller is the | 22 // document.setRootScroller. |
21 // documentElement. | |
22 // | 23 // |
23 // Not all elements can be set as the root scroller. A valid element must be | 24 // There are two notions of a root scroller in this class: m_rootScroller and |
24 // in the top-level document and must have a LayoutObject that is at least a | 25 // m_effectiveRootScroller. The former is the Element that was set as the root |
25 // LayoutBlockFlow or LayoutIFrame. In addition, only the top-level document can | 26 // scroller using document.setRootScroller. If the page didn't set a root |
26 // set a non-default root scroller. | 27 // scroller this will be nullptr. The "effective" root scroller is the current |
| 28 // element we're using internally to apply viewport scroll actions. i.e It's the |
| 29 // element with the ViewportScrollCallback set as its apply-scroll callback. |
| 30 // The effective root scroller will only be null during document initialization. |
| 31 // |
| 32 // If the root scroller element is a valid element to become the root scroller, |
| 33 // it will be promoted to the effective root scroller. If it is not valid, the |
| 34 // effective root scroller will fall back to a default Element (see |
| 35 // defaultEffectiveRootScroller()). The rules for what makes an element a valid |
| 36 // root scroller are set in isValidRootScroller(). The validity of the current |
| 37 // root scroller is re-checked after each layout. |
27 class CORE_EXPORT RootScroller : public GarbageCollected<RootScroller> { | 38 class CORE_EXPORT RootScroller : public GarbageCollected<RootScroller> { |
28 public: | 39 public: |
29 static RootScroller* create(FrameHost& frameHost) | 40 // Creates a RootScroller for the given document. An optional |
| 41 // ViewportScrollCallback can be provided. If it is, RootScroller will |
| 42 // ensure that the effectiveRootScroller element always has this set as the |
| 43 // apply scroll callback. |
| 44 static RootScroller* create( |
| 45 Document& document, |
| 46 ViewportScrollCallback* applyScrollCallback) |
30 { | 47 { |
31 return new RootScroller(frameHost); | 48 return new RootScroller(document, applyScrollCallback); |
32 } | 49 } |
33 | 50 |
34 // This method returns true if the element was set as the root scroller. If | 51 // Creates an apply scroll callback that handles viewport actions like |
35 // the element isn't eligible to be the root scroller or we're in some bad | 52 // TopControls movement and Overscroll. |
36 // state, the method returns false without changing the current root | 53 static ViewportScrollCallback* createViewportApplyScroll( |
37 // scroller. | 54 TopControls&, OverscrollController&); |
38 bool set(Element&); | |
39 | |
40 // Returns the element currently set as the root scroller. | |
41 Element* get() const; | |
42 | |
43 // This class needs to be informed of changes in layout so that it can | |
44 // determine if the current scroller is still valid or if it must be | |
45 // replaced by the defualt root scroller. | |
46 void didUpdateTopDocumentLayout(); | |
47 | 55 |
48 DECLARE_TRACE(); | 56 DECLARE_TRACE(); |
49 | 57 |
| 58 // Sets the element that will be used as the root scroller. This can be |
| 59 // nullptr, in which case we'll use the default element (documentElement) as |
| 60 // the effective root scroller. |
| 61 void set(Element*); |
| 62 |
| 63 // Returns the element currently set as the root scroller from script. This |
| 64 // differs from the effective root scroller since the set Element may not |
| 65 // currently be a valid root scroller. e.g. If the page sets an Element |
| 66 // with `display: none`, get() will return that element, even though the |
| 67 // effective root scroller will remain the element returned by |
| 68 // defaultEffectiveRootScroller(). |
| 69 Element* get() const; |
| 70 |
| 71 // This returns the Element that's actually being used to control viewport |
| 72 // actions right now. This is different from get() if a root scroller hasn't |
| 73 // been set, or if the set root scroller isn't currently a valid scroller. |
| 74 Element* effectiveRootScroller() const; |
| 75 |
| 76 // This class needs to be informed of changes in layout so that it can |
| 77 // determine if the current root scroller is still valid or if it must be |
| 78 // replaced by the defualt root scroller. |
| 79 void didUpdateLayout(); |
| 80 |
50 private: | 81 private: |
51 RootScroller(FrameHost&); | 82 RootScroller(Document&, ViewportScrollCallback*); |
52 | 83 |
53 bool isValid(Element&) const; | 84 Element* defaultEffectiveRootScroller(); |
54 void resetToDefault(); | |
55 void createApplyScrollIfNeeded(); | |
56 | 85 |
57 WeakMember<FrameHost> m_frameHost; | 86 // Ensures the effective root scroller is currently valid and replaces it |
| 87 // with the default if not. |
| 88 void updateEffectiveRootScroller(); |
| 89 void moveViewportApplyScroll(Element* target); |
| 90 |
| 91 WeakMember<Document> m_document; |
58 Member<ViewportScrollCallback> m_viewportApplyScroll; | 92 Member<ViewportScrollCallback> m_viewportApplyScroll; |
59 | 93 |
60 WeakMember<Element> m_rootScroller; | 94 WeakMember<Element> m_rootScroller; |
| 95 WeakMember<Element> m_effectiveRootScroller; |
61 }; | 96 }; |
62 | 97 |
63 } // namespace blink | 98 } // namespace blink |
64 | 99 |
65 #endif // RootScroller_h | 100 #endif // RootScroller_h |
OLD | NEW |