OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 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. AND ITS CONTRIBUTORS ``AS IS'' | |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
23 * THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "platform/mac/ScrollElasticityController.h" | |
28 | |
29 #include "platform/PlatformWheelEvent.h" | |
30 #include <sys/sysctl.h> | |
31 #include <sys/time.h> | |
32 | |
33 #if USE(RUBBER_BANDING) | |
34 | |
35 static NSTimeInterval systemUptime() | |
36 { | |
37 if ([[NSProcessInfo processInfo] respondsToSelector:@selector(systemUptime)]
) | |
38 return [[NSProcessInfo processInfo] systemUptime]; | |
39 | |
40 // Get how long system has been up. Found by looking getting "boottime" from
the kernel. | |
41 static struct timeval boottime = {0, 0}; | |
42 if (!boottime.tv_sec) { | |
43 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | |
44 size_t size = sizeof(boottime); | |
45 if (-1 == sysctl(mib, 2, &boottime, &size, 0, 0)) | |
46 boottime.tv_sec = 0; | |
47 } | |
48 struct timeval now; | |
49 if (boottime.tv_sec && -1 != gettimeofday(&now, 0)) { | |
50 struct timeval uptime; | |
51 timersub(&now, &boottime, &uptime); | |
52 NSTimeInterval result = uptime.tv_sec + (uptime.tv_usec / 1E+6); | |
53 return result; | |
54 } | |
55 return 0; | |
56 } | |
57 | |
58 namespace blink { | |
59 | |
60 static const float scrollVelocityZeroingTimeout = 0.10f; | |
61 static const float rubberbandDirectionLockStretchRatio = 1; | |
62 static const float rubberbandMinimumRequiredDeltaBeforeStretch = 10; | |
63 | |
64 static const float rubberbandStiffness = 20; | |
65 static const float rubberbandAmplitude = 0.31f; | |
66 static const float rubberbandPeriod = 1.6f; | |
67 | |
68 static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci
ty, float elapsedTime) | |
69 { | |
70 float amplitude = rubberbandAmplitude; | |
71 float period = rubberbandPeriod; | |
72 float criticalDampeningFactor = expf((-elapsedTime * rubberbandStiffness) /
period); | |
73 | |
74 return (initialPosition + (-initialVelocity * elapsedTime * amplitude)) * cr
iticalDampeningFactor; | |
75 } | |
76 | |
77 static float elasticDeltaForReboundDelta(float delta) | |
78 { | |
79 float stiffness = std::max(rubberbandStiffness, 1.0f); | |
80 return delta / stiffness; | |
81 } | |
82 | |
83 static float reboundDeltaForElasticDelta(float delta) | |
84 { | |
85 return delta * rubberbandStiffness; | |
86 } | |
87 | |
88 static float scrollWheelMultiplier() | |
89 { | |
90 static float multiplier = -1; | |
91 if (multiplier < 0) { | |
92 multiplier = [[NSUserDefaults standardUserDefaults] floatForKey:@"NSScro
llWheelMultiplier"]; | |
93 if (multiplier <= 0) | |
94 multiplier = 1; | |
95 } | |
96 return multiplier; | |
97 } | |
98 | |
99 ScrollElasticityController::ScrollElasticityController(ScrollElasticityControlle
rClient* client) | |
100 : m_client(client) | |
101 , m_inScrollGesture(false) | |
102 , m_hasScrolled(false) | |
103 , m_momentumScrollInProgress(false) | |
104 , m_ignoreMomentumScrolls(false) | |
105 , m_lastMomentumScrollTimestamp(0) | |
106 , m_startTime(0) | |
107 , m_snapRubberbandTimerIsActive(false) | |
108 { | |
109 } | |
110 | |
111 bool ScrollElasticityController::handleWheelEvent(const PlatformWheelEvent& whee
lEvent) | |
112 { | |
113 if (wheelEvent.phase() == PlatformWheelEventPhaseMayBegin) | |
114 return false; | |
115 | |
116 if (wheelEvent.phase() == PlatformWheelEventPhaseBegan) { | |
117 m_inScrollGesture = true; | |
118 m_hasScrolled = false; | |
119 m_momentumScrollInProgress = false; | |
120 m_ignoreMomentumScrolls = false; | |
121 m_lastMomentumScrollTimestamp = 0; | |
122 m_momentumVelocity = FloatSize(); | |
123 | |
124 IntSize stretchAmount = m_client->stretchAmount(); | |
125 m_stretchScrollForce.setWidth(reboundDeltaForElasticDelta(stretchAmount.
width())); | |
126 m_stretchScrollForce.setHeight(reboundDeltaForElasticDelta(stretchAmount
.height())); | |
127 m_overflowScrollDelta = FloatSize(); | |
128 | |
129 stopSnapRubberbandTimer(); | |
130 | |
131 // TODO(erikchen): Use the commented out line once Chromium uses the ret
urn value correctly. | |
132 // crbug.com/375512 | |
133 // return shouldHandleEvent(wheelEvent); | |
134 | |
135 // This logic is incorrect, since diagonal wheel events are not consumed
. | |
136 if (m_client->pinnedInDirection(FloatSize(-wheelEvent.deltaX(), 0))) { | |
137 if (wheelEvent.deltaX() > 0 && !wheelEvent.canRubberbandLeft()) | |
138 return false; | |
139 if (wheelEvent.deltaX() < 0 && !wheelEvent.canRubberbandRight()) | |
140 return false; | |
141 } | |
142 | |
143 return true; | |
144 } | |
145 | |
146 if (wheelEvent.phase() == PlatformWheelEventPhaseEnded || wheelEvent.phase()
== PlatformWheelEventPhaseCancelled) { | |
147 snapRubberBand(); | |
148 return m_hasScrolled; | |
149 } | |
150 | |
151 bool isMomentumScrollEvent = (wheelEvent.momentumPhase() != PlatformWheelEve
ntPhaseNone); | |
152 if (m_ignoreMomentumScrolls && (isMomentumScrollEvent || m_snapRubberbandTim
erIsActive)) { | |
153 if (wheelEvent.momentumPhase() == PlatformWheelEventPhaseEnded) { | |
154 m_ignoreMomentumScrolls = false; | |
155 return true; | |
156 } | |
157 return false; | |
158 } | |
159 | |
160 if (!shouldHandleEvent(wheelEvent)) | |
161 return false; | |
162 | |
163 float deltaX = m_overflowScrollDelta.width() - wheelEvent.deltaX(); | |
164 float deltaY = m_overflowScrollDelta.height() - wheelEvent.deltaY(); | |
165 float eventCoalescedDeltaX = -wheelEvent.deltaX(); | |
166 float eventCoalescedDeltaY = -wheelEvent.deltaY(); | |
167 | |
168 // Reset overflow values because we may decide to remove delta at various po
ints and put it into overflow. | |
169 m_overflowScrollDelta = FloatSize(); | |
170 | |
171 IntSize stretchAmount = m_client->stretchAmount(); | |
172 bool isVerticallyStretched = stretchAmount.height(); | |
173 bool isHorizontallyStretched = stretchAmount.width(); | |
174 | |
175 // Slightly prefer scrolling vertically by applying the = case to deltaY | |
176 if (fabsf(deltaY) >= fabsf(deltaX)) | |
177 deltaX = 0; | |
178 else | |
179 deltaY = 0; | |
180 | |
181 bool shouldStretch = false; | |
182 | |
183 PlatformWheelEventPhase momentumPhase = wheelEvent.momentumPhase(); | |
184 | |
185 // If we are starting momentum scrolling then do some setup. | |
186 if (!m_momentumScrollInProgress && (momentumPhase == PlatformWheelEventPhase
Began || momentumPhase == PlatformWheelEventPhaseChanged)) { | |
187 m_momentumScrollInProgress = true; | |
188 // Start the snap rubber band timer if it's not running. This is needed
to | |
189 // snap back from the over scroll caused by momentum events. | |
190 if (!m_snapRubberbandTimerIsActive && m_startTime == 0) | |
191 snapRubberBand(); | |
192 } | |
193 | |
194 CFTimeInterval timeDelta = wheelEvent.timestamp() - m_lastMomentumScrollTime
stamp; | |
195 if (m_inScrollGesture || m_momentumScrollInProgress) { | |
196 if (m_lastMomentumScrollTimestamp && timeDelta > 0 && timeDelta < scroll
VelocityZeroingTimeout) { | |
197 m_momentumVelocity.setWidth(eventCoalescedDeltaX / (float)timeDelta)
; | |
198 m_momentumVelocity.setHeight(eventCoalescedDeltaY / (float)timeDelta
); | |
199 m_lastMomentumScrollTimestamp = wheelEvent.timestamp(); | |
200 } else { | |
201 m_lastMomentumScrollTimestamp = wheelEvent.timestamp(); | |
202 m_momentumVelocity = FloatSize(); | |
203 } | |
204 | |
205 if (isVerticallyStretched) { | |
206 if (!isHorizontallyStretched && m_client->pinnedInDirection(FloatSiz
e(deltaX, 0))) { | |
207 // Stretching only in the vertical. | |
208 if (deltaY != 0 && (fabsf(deltaX / deltaY) < rubberbandDirection
LockStretchRatio)) | |
209 deltaX = 0; | |
210 else if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStr
etch) { | |
211 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.width()
+ deltaX); | |
212 deltaX = 0; | |
213 } else | |
214 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.width()
+ deltaX); | |
215 } | |
216 } else if (isHorizontallyStretched) { | |
217 // Stretching only in the horizontal. | |
218 if (m_client->pinnedInDirection(FloatSize(0, deltaY))) { | |
219 if (deltaX != 0 && (fabsf(deltaY / deltaX) < rubberbandDirection
LockStretchRatio)) | |
220 deltaY = 0; | |
221 else if (fabsf(deltaY) < rubberbandMinimumRequiredDeltaBeforeStr
etch) { | |
222 m_overflowScrollDelta.setHeight(m_overflowScrollDelta.height
() + deltaY); | |
223 deltaY = 0; | |
224 } else | |
225 m_overflowScrollDelta.setHeight(m_overflowScrollDelta.height
() + deltaY); | |
226 } | |
227 } else { | |
228 // Not stretching at all yet. | |
229 if (m_client->pinnedInDirection(FloatSize(deltaX, deltaY))) { | |
230 if (fabsf(deltaY) >= fabsf(deltaX)) { | |
231 if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStre
tch) { | |
232 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.wid
th() + deltaX); | |
233 deltaX = 0; | |
234 } else | |
235 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.wid
th() + deltaX); | |
236 } | |
237 shouldStretch = true; | |
238 } | |
239 } | |
240 } | |
241 | |
242 if (deltaX != 0 || deltaY != 0) { | |
243 m_hasScrolled = true; | |
244 if (!(shouldStretch || isVerticallyStretched || isHorizontallyStretched)
) { | |
245 if (deltaY != 0) { | |
246 deltaY *= scrollWheelMultiplier(); | |
247 m_client->immediateScrollBy(FloatSize(0, deltaY)); | |
248 } | |
249 if (deltaX != 0) { | |
250 deltaX *= scrollWheelMultiplier(); | |
251 m_client->immediateScrollBy(FloatSize(deltaX, 0)); | |
252 } | |
253 } else { | |
254 if (!m_client->allowsHorizontalStretching()) { | |
255 deltaX = 0; | |
256 eventCoalescedDeltaX = 0; | |
257 } else if ((deltaX != 0) && !isHorizontallyStretched && !m_client->p
innedInDirection(FloatSize(deltaX, 0))) { | |
258 deltaX *= scrollWheelMultiplier(); | |
259 | |
260 m_client->immediateScrollByWithoutContentEdgeConstraints(FloatSi
ze(deltaX, 0)); | |
261 deltaX = 0; | |
262 } | |
263 | |
264 if (!m_client->allowsVerticalStretching()) { | |
265 deltaY = 0; | |
266 eventCoalescedDeltaY = 0; | |
267 } else if ((deltaY != 0) && !isVerticallyStretched && !m_client->pin
nedInDirection(FloatSize(0, deltaY))) { | |
268 deltaY *= scrollWheelMultiplier(); | |
269 | |
270 m_client->immediateScrollByWithoutContentEdgeConstraints(FloatSi
ze(0, deltaY)); | |
271 deltaY = 0; | |
272 } | |
273 | |
274 IntSize stretchAmount = m_client->stretchAmount(); | |
275 | |
276 if (m_momentumScrollInProgress) { | |
277 if ((m_client->pinnedInDirection(FloatSize(eventCoalescedDeltaX,
eventCoalescedDeltaY)) || (fabsf(eventCoalescedDeltaX) + fabsf(eventCoalescedDe
ltaY) <= 0)) && m_lastMomentumScrollTimestamp) { | |
278 m_ignoreMomentumScrolls = true; | |
279 m_momentumScrollInProgress = false; | |
280 snapRubberBand(); | |
281 } | |
282 } | |
283 | |
284 m_stretchScrollForce.setWidth(m_stretchScrollForce.width() + deltaX)
; | |
285 m_stretchScrollForce.setHeight(m_stretchScrollForce.height() + delta
Y); | |
286 | |
287 FloatSize dampedDelta(ceilf(elasticDeltaForReboundDelta(m_stretchScr
ollForce.width())), ceilf(elasticDeltaForReboundDelta(m_stretchScrollForce.heigh
t()))); | |
288 | |
289 m_client->immediateScrollByWithoutContentEdgeConstraints(dampedDelta
- stretchAmount); | |
290 } | |
291 } | |
292 | |
293 if (m_momentumScrollInProgress && momentumPhase == PlatformWheelEventPhaseEn
ded) { | |
294 m_momentumScrollInProgress = false; | |
295 m_ignoreMomentumScrolls = false; | |
296 m_lastMomentumScrollTimestamp = 0; | |
297 } | |
298 | |
299 return true; | |
300 } | |
301 | |
302 static inline float roundTowardZero(float num) | |
303 { | |
304 return num > 0 ? ceilf(num - 0.5f) : floorf(num + 0.5f); | |
305 } | |
306 | |
307 static inline float roundToDevicePixelTowardZero(float num) | |
308 { | |
309 float roundedNum = roundf(num); | |
310 if (fabs(num - roundedNum) < 0.125) | |
311 num = roundedNum; | |
312 | |
313 return roundTowardZero(num); | |
314 } | |
315 | |
316 void ScrollElasticityController::snapRubberBandTimerFired() | |
317 { | |
318 if (!m_momentumScrollInProgress || m_ignoreMomentumScrolls) { | |
319 CFTimeInterval timeDelta = [NSDate timeIntervalSinceReferenceDate] - m_s
tartTime; | |
320 | |
321 if (m_startStretch == FloatSize()) { | |
322 m_startStretch = m_client->stretchAmount(); | |
323 if (m_startStretch == FloatSize()) { | |
324 stopSnapRubberbandTimer(); | |
325 | |
326 m_stretchScrollForce = FloatSize(); | |
327 m_startTime = 0; | |
328 m_origOrigin = FloatPoint(); | |
329 m_origVelocity = FloatSize(); | |
330 return; | |
331 } | |
332 | |
333 m_origOrigin = m_client->absoluteScrollPosition() - m_startStretch; | |
334 m_origVelocity = m_momentumVelocity; | |
335 | |
336 // Just like normal scrolling, prefer vertical rubberbanding | |
337 if (fabsf(m_origVelocity.height()) >= fabsf(m_origVelocity.width())) | |
338 m_origVelocity.setWidth(0); | |
339 | |
340 // Don't rubber-band horizontally if it's not possible to scroll hor
izontally | |
341 if (!m_client->canScrollHorizontally()) | |
342 m_origVelocity.setWidth(0); | |
343 | |
344 // Don't rubber-band vertically if it's not possible to scroll verti
cally | |
345 if (!m_client->canScrollVertically()) | |
346 m_origVelocity.setHeight(0); | |
347 } | |
348 | |
349 FloatPoint delta(roundToDevicePixelTowardZero(elasticDeltaForTimeDelta(m
_startStretch.width(), -m_origVelocity.width(), (float)timeDelta)), | |
350 roundToDevicePixelTowardZero(elasticDeltaForTimeDelta(m
_startStretch.height(), -m_origVelocity.height(), (float)timeDelta))); | |
351 | |
352 if (fabs(delta.x()) >= 1 || fabs(delta.y()) >= 1) { | |
353 m_client->immediateScrollByWithoutContentEdgeConstraints(FloatSize(d
elta.x(), delta.y()) - m_client->stretchAmount()); | |
354 | |
355 FloatSize newStretch = m_client->stretchAmount(); | |
356 | |
357 m_stretchScrollForce.setWidth(reboundDeltaForElasticDelta(newStretch
.width())); | |
358 m_stretchScrollForce.setHeight(reboundDeltaForElasticDelta(newStretc
h.height())); | |
359 } else { | |
360 m_client->adjustScrollPositionToBoundsIfNecessary(); | |
361 | |
362 stopSnapRubberbandTimer(); | |
363 m_stretchScrollForce = FloatSize(); | |
364 m_startTime = 0; | |
365 m_startStretch = FloatSize(); | |
366 m_origOrigin = FloatPoint(); | |
367 m_origVelocity = FloatSize(); | |
368 } | |
369 } else { | |
370 m_startTime = [NSDate timeIntervalSinceReferenceDate]; | |
371 m_startStretch = FloatSize(); | |
372 } | |
373 } | |
374 | |
375 bool ScrollElasticityController::isRubberBandInProgress() const | |
376 { | |
377 if (!m_inScrollGesture && !m_momentumScrollInProgress && !m_snapRubberbandTi
merIsActive) | |
378 return false; | |
379 | |
380 return !m_client->stretchAmount().isZero(); | |
381 } | |
382 | |
383 void ScrollElasticityController::stopSnapRubberbandTimer() | |
384 { | |
385 m_client->stopSnapRubberbandTimer(); | |
386 m_snapRubberbandTimerIsActive = false; | |
387 } | |
388 | |
389 void ScrollElasticityController::snapRubberBand() | |
390 { | |
391 CFTimeInterval timeDelta = systemUptime() - m_lastMomentumScrollTimestamp; | |
392 if (m_lastMomentumScrollTimestamp && timeDelta >= scrollVelocityZeroingTimeo
ut) | |
393 m_momentumVelocity = FloatSize(); | |
394 | |
395 m_inScrollGesture = false; | |
396 | |
397 if (m_snapRubberbandTimerIsActive) | |
398 return; | |
399 | |
400 m_startStretch = FloatSize(); | |
401 m_origOrigin = FloatPoint(); | |
402 m_origVelocity = FloatSize(); | |
403 | |
404 // If there's no momentum scroll or stretch amount, no need to start the tim
er. | |
405 if (!m_momentumScrollInProgress && m_client->stretchAmount() == FloatSize())
{ | |
406 m_startTime = 0; | |
407 m_stretchScrollForce = FloatSize(); | |
408 return; | |
409 } | |
410 | |
411 m_startTime = [NSDate timeIntervalSinceReferenceDate]; | |
412 m_client->startSnapRubberbandTimer(); | |
413 m_snapRubberbandTimerIsActive = true; | |
414 } | |
415 | |
416 bool ScrollElasticityController::shouldHandleEvent(const PlatformWheelEvent& whe
elEvent) | |
417 { | |
418 // Once any scrolling has happened, all future events should be handled. | |
419 if (m_hasScrolled) | |
420 return true; | |
421 | |
422 // The event can't cause scrolling to start if its delta is 0. | |
423 if (wheelEvent.deltaX() == 0 && wheelEvent.deltaY() == 0) | |
424 return false; | |
425 | |
426 // If the client isn't pinned, then the event is guaranteed to cause scrolli
ng. | |
427 if (!m_client->pinnedInDirection(FloatSize(-wheelEvent.deltaX(), 0))) | |
428 return true; | |
429 | |
430 // If the event is pinned, then the client can't scroll, but it might rubber
band. | |
431 // Check if the event allows rubber banding. | |
432 if (wheelEvent.deltaY() == 0) { | |
433 if (wheelEvent.deltaX() > 0 && !wheelEvent.canRubberbandLeft()) | |
434 return false; | |
435 if (wheelEvent.deltaX() < 0 && !wheelEvent.canRubberbandRight()) | |
436 return false; | |
437 } | |
438 | |
439 // The event is going to either cause scrolling or rubber banding. | |
440 return true; | |
441 } | |
442 | |
443 } // namespace blink | |
444 | |
445 #endif // USE(RUBBER_BANDING) | |
OLD | NEW |