Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et tw=80 : */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "OverscrollHandoffState.h"
8 :
9 : #include <algorithm> // for std::stable_sort
10 : #include "mozilla/Assertions.h"
11 : #include "AsyncPanZoomController.h"
12 :
13 : namespace mozilla {
14 : namespace layers {
15 :
16 0 : OverscrollHandoffChain::~OverscrollHandoffChain() {}
17 :
18 : void
19 0 : OverscrollHandoffChain::Add(AsyncPanZoomController* aApzc)
20 : {
21 0 : mChain.push_back(aApzc);
22 0 : }
23 :
24 : struct CompareByScrollPriority
25 : {
26 0 : bool operator()(const RefPtr<AsyncPanZoomController>& a,
27 : const RefPtr<AsyncPanZoomController>& b) const
28 : {
29 0 : return a->HasScrollgrab() && !b->HasScrollgrab();
30 : }
31 : };
32 :
33 : void
34 0 : OverscrollHandoffChain::SortByScrollPriority()
35 : {
36 : // The sorting being stable ensures that the relative order between
37 : // non-scrollgrabbing APZCs remains child -> parent.
38 : // (The relative order between scrollgrabbing APZCs will also remain
39 : // child -> parent, though that's just an artefact of the implementation
40 : // and users of 'scrollgrab' should not rely on this.)
41 0 : std::stable_sort(mChain.begin(), mChain.end(), CompareByScrollPriority());
42 0 : }
43 :
44 : const RefPtr<AsyncPanZoomController>&
45 0 : OverscrollHandoffChain::GetApzcAtIndex(uint32_t aIndex) const
46 : {
47 0 : MOZ_ASSERT(aIndex < Length());
48 0 : return mChain[aIndex];
49 : }
50 :
51 : uint32_t
52 0 : OverscrollHandoffChain::IndexOf(const AsyncPanZoomController* aApzc) const
53 : {
54 : uint32_t i;
55 0 : for (i = 0; i < Length(); ++i) {
56 0 : if (mChain[i] == aApzc) {
57 0 : break;
58 : }
59 : }
60 0 : return i;
61 : }
62 :
63 : void
64 0 : OverscrollHandoffChain::ForEachApzc(APZCMethod aMethod) const
65 : {
66 0 : for (uint32_t i = 0; i < Length(); ++i) {
67 0 : (mChain[i]->*aMethod)();
68 : }
69 0 : }
70 :
71 : bool
72 0 : OverscrollHandoffChain::AnyApzc(APZCPredicate aPredicate) const
73 : {
74 0 : MOZ_ASSERT(Length() > 0);
75 0 : for (uint32_t i = 0; i < Length(); ++i) {
76 0 : if ((mChain[i]->*aPredicate)()) {
77 0 : return true;
78 : }
79 : }
80 0 : return false;
81 : }
82 :
83 : void
84 0 : OverscrollHandoffChain::FlushRepaints() const
85 : {
86 0 : ForEachApzc(&AsyncPanZoomController::FlushRepaintForOverscrollHandoff);
87 0 : }
88 :
89 : void
90 0 : OverscrollHandoffChain::CancelAnimations(CancelAnimationFlags aFlags) const
91 : {
92 0 : MOZ_ASSERT(Length() > 0);
93 0 : for (uint32_t i = 0; i < Length(); ++i) {
94 0 : mChain[i]->CancelAnimation(aFlags);
95 : }
96 0 : }
97 :
98 : void
99 0 : OverscrollHandoffChain::ClearOverscroll() const
100 : {
101 0 : ForEachApzc(&AsyncPanZoomController::ClearOverscroll);
102 0 : }
103 :
104 : void
105 0 : OverscrollHandoffChain::SnapBackOverscrolledApzc(const AsyncPanZoomController* aStart) const
106 : {
107 0 : uint32_t i = IndexOf(aStart);
108 0 : for (; i < Length(); ++i) {
109 0 : AsyncPanZoomController* apzc = mChain[i];
110 0 : if (!apzc->IsDestroyed()) {
111 0 : apzc->SnapBackIfOverscrolled();
112 : }
113 : }
114 0 : }
115 :
116 : bool
117 0 : OverscrollHandoffChain::CanBePanned(const AsyncPanZoomController* aApzc) const
118 : {
119 : // Find |aApzc| in the handoff chain.
120 0 : uint32_t i = IndexOf(aApzc);
121 :
122 : // See whether any APZC in the handoff chain starting from |aApzc|
123 : // has room to be panned.
124 0 : for (uint32_t j = i; j < Length(); ++j) {
125 0 : if (mChain[j]->IsPannable()) {
126 0 : return true;
127 : }
128 : }
129 :
130 0 : return false;
131 : }
132 :
133 : bool
134 0 : OverscrollHandoffChain::CanScrollInDirection(const AsyncPanZoomController* aApzc,
135 : ScrollDirection aDirection) const
136 : {
137 : // Find |aApzc| in the handoff chain.
138 0 : uint32_t i = IndexOf(aApzc);
139 :
140 : // See whether any APZC in the handoff chain starting from |aApzc|
141 : // has room to scroll in the given direction.
142 0 : for (uint32_t j = i; j < Length(); ++j) {
143 0 : if (mChain[j]->CanScroll(aDirection)) {
144 0 : return true;
145 : }
146 : }
147 :
148 0 : return false;
149 : }
150 :
151 : bool
152 0 : OverscrollHandoffChain::HasOverscrolledApzc() const
153 : {
154 0 : return AnyApzc(&AsyncPanZoomController::IsOverscrolled);
155 : }
156 :
157 : bool
158 0 : OverscrollHandoffChain::HasFastFlungApzc() const
159 : {
160 0 : return AnyApzc(&AsyncPanZoomController::IsFlingingFast);
161 : }
162 :
163 : RefPtr<AsyncPanZoomController>
164 0 : OverscrollHandoffChain::FindFirstScrollable(const InputData& aInput) const
165 : {
166 0 : for (size_t i = 0; i < Length(); i++) {
167 0 : if (mChain[i]->CanScroll(aInput)) {
168 0 : return mChain[i];
169 : }
170 : }
171 0 : return nullptr;
172 : }
173 :
174 : } // namespace layers
175 : } // namespace mozilla
|