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