Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: Source/core/platform/mac/ScrollElasticityController.mm

Issue 99103006: Moving GraphicsContext and dependencies from core to platform. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Final patch - fixes Android Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "core/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 WebCore {
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_momentumScrollInProgress(false)
103 , m_ignoreMomentumScrolls(false)
104 , m_lastMomentumScrollTimestamp(0)
105 , m_startTime(0)
106 , m_snapRubberbandTimerIsActive(false)
107 {
108 }
109
110 bool ScrollElasticityController::handleWheelEvent(const PlatformWheelEvent& whee lEvent)
111 {
112 if (wheelEvent.phase() == PlatformWheelEventPhaseBegan) {
113 // First, check if we should rubber-band at all.
114 if (m_client->pinnedInDirection(FloatSize(-wheelEvent.deltaX(), 0)) &&
115 !shouldRubberBandInHorizontalDirection(wheelEvent))
116 return false;
117
118 m_inScrollGesture = true;
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 return true;
132 }
133
134 if (wheelEvent.phase() == PlatformWheelEventPhaseEnded) {
135 bool wasRubberBandInProgress = isRubberBandInProgress();
136 // Call snapRubberBand() even if isRubberBandInProgress() is false. For example,
137 // m_inScrollGesture may be true (and needs to be reset on a phase end) even if
138 // isRubberBandInProgress() is not (e.g. the overhang area is empty).
139 snapRubberBand();
140 return wasRubberBandInProgress;
141 }
142
143 bool isMomentumScrollEvent = (wheelEvent.momentumPhase() != PlatformWheelEve ntPhaseNone);
144 if (m_ignoreMomentumScrolls && (isMomentumScrollEvent || m_snapRubberbandTim erIsActive)) {
145 if (wheelEvent.momentumPhase() == PlatformWheelEventPhaseEnded) {
146 m_ignoreMomentumScrolls = false;
147 return true;
148 }
149 return false;
150 }
151
152 float deltaX = m_overflowScrollDelta.width();
153 float deltaY = m_overflowScrollDelta.height();
154
155 // Reset overflow values because we may decide to remove delta at various po ints and put it into overflow.
156 m_overflowScrollDelta = FloatSize();
157
158 IntSize stretchAmount = m_client->stretchAmount();
159 bool isVerticallyStretched = stretchAmount.height();
160 bool isHorizontallyStretched = stretchAmount.width();
161
162 float eventCoalescedDeltaX;
163 float eventCoalescedDeltaY;
164
165 if (isVerticallyStretched || isHorizontallyStretched) {
166 eventCoalescedDeltaX = -wheelEvent.unacceleratedScrollingDeltaX();
167 eventCoalescedDeltaY = -wheelEvent.unacceleratedScrollingDeltaY();
168 } else {
169 eventCoalescedDeltaX = -wheelEvent.deltaX();
170 eventCoalescedDeltaY = -wheelEvent.deltaY();
171 }
172
173 deltaX += eventCoalescedDeltaX;
174 deltaY += eventCoalescedDeltaY;
175
176 // Slightly prefer scrolling vertically by applying the = case to deltaY
177 if (fabsf(deltaY) >= fabsf(deltaX))
178 deltaX = 0;
179 else
180 deltaY = 0;
181
182 bool shouldStretch = false;
183
184 PlatformWheelEventPhase momentumPhase = wheelEvent.momentumPhase();
185
186 // If we are starting momentum scrolling then do some setup.
187 if (!m_momentumScrollInProgress && (momentumPhase == PlatformWheelEventPhase Began || momentumPhase == PlatformWheelEventPhaseChanged)) {
188 m_momentumScrollInProgress = true;
189 // Start the snap rubber band timer if it's not running. This is needed to
190 // snap back from the over scroll caused by momentum events.
191 if (!m_snapRubberbandTimerIsActive && m_startTime == 0)
192 snapRubberBand();
193 }
194
195 CFTimeInterval timeDelta = wheelEvent.timestamp() - m_lastMomentumScrollTime stamp;
196 if (m_inScrollGesture || m_momentumScrollInProgress) {
197 if (m_lastMomentumScrollTimestamp && timeDelta > 0 && timeDelta < scroll VelocityZeroingTimeout) {
198 m_momentumVelocity.setWidth(eventCoalescedDeltaX / (float)timeDelta) ;
199 m_momentumVelocity.setHeight(eventCoalescedDeltaY / (float)timeDelta );
200 m_lastMomentumScrollTimestamp = wheelEvent.timestamp();
201 } else {
202 m_lastMomentumScrollTimestamp = wheelEvent.timestamp();
203 m_momentumVelocity = FloatSize();
204 }
205
206 if (isVerticallyStretched) {
207 if (!isHorizontallyStretched && m_client->pinnedInDirection(FloatSiz e(deltaX, 0))) {
208 // Stretching only in the vertical.
209 if (deltaY != 0 && (fabsf(deltaX / deltaY) < rubberbandDirection LockStretchRatio))
210 deltaX = 0;
211 else if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStr etch) {
212 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.width() + deltaX);
213 deltaX = 0;
214 } else
215 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.width() + deltaX);
216 }
217 } else if (isHorizontallyStretched) {
218 // Stretching only in the horizontal.
219 if (m_client->pinnedInDirection(FloatSize(0, deltaY))) {
220 if (deltaX != 0 && (fabsf(deltaY / deltaX) < rubberbandDirection LockStretchRatio))
221 deltaY = 0;
222 else if (fabsf(deltaY) < rubberbandMinimumRequiredDeltaBeforeStr etch) {
223 m_overflowScrollDelta.setHeight(m_overflowScrollDelta.height () + deltaY);
224 deltaY = 0;
225 } else
226 m_overflowScrollDelta.setHeight(m_overflowScrollDelta.height () + deltaY);
227 }
228 } else {
229 // Not stretching at all yet.
230 if (m_client->pinnedInDirection(FloatSize(deltaX, deltaY))) {
231 if (fabsf(deltaY) >= fabsf(deltaX)) {
232 if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStre tch) {
233 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.wid th() + deltaX);
234 deltaX = 0;
235 } else
236 m_overflowScrollDelta.setWidth(m_overflowScrollDelta.wid th() + deltaX);
237 }
238 shouldStretch = true;
239 }
240 }
241 }
242
243 if (deltaX != 0 || deltaY != 0) {
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->immediateScrollBy(m_origOrigin - m_client->absoluteScrollP osition());
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::shouldRubberBandInHorizontalDirection(const Pla tformWheelEvent& wheelEvent)
417 {
418 if (wheelEvent.deltaX() > 0)
419 return m_client->shouldRubberBandInDirection(ScrollLeft);
420 if (wheelEvent.deltaX() < 0)
421 return m_client->shouldRubberBandInDirection(ScrollRight);
422
423 return true;
424 }
425
426 } // namespace WebCore
427
428 #endif // USE(RUBBER_BANDING)
OLDNEW
« no previous file with comments | « Source/core/platform/mac/ScrollElasticityController.h ('k') | Source/core/platform/mac/ThemeMac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698