Index: content/renderer/input/input_scroll_elasticity_controller.cc |
diff --git a/content/renderer/input/input_scroll_elasticity_controller.cc b/content/renderer/input/input_scroll_elasticity_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c89419d55c1e7ad97f4976d2bde1e0ae6aa3123c |
--- /dev/null |
+++ b/content/renderer/input/input_scroll_elasticity_controller.cc |
@@ -0,0 +1,397 @@ |
+#include "content/renderer/input/input_scroll_elasticity_controller.h" |
+ |
+namespace content { |
+ |
+static const float scrollVelocityZeroingTimeout = 0.10f; |
+static const float rubberbandDirectionLockStretchRatio = 1; |
+static const float rubberbandMinimumRequiredDeltaBeforeStretch = 10; |
+ |
+static const float rubberbandStiffness = 20; |
+static const float rubberbandAmplitude = 0.31f; |
+static const float rubberbandPeriod = 1.6f; |
+ |
+static float elasticDeltaForTimeDelta(float initialPosition, float initialVelocity, float elapsedTime) |
+{ |
+ float amplitude = rubberbandAmplitude; |
+ float period = rubberbandPeriod; |
+ float criticalDampeningFactor = expf((-elapsedTime * rubberbandStiffness) / period); |
+ |
+ return (initialPosition + (-initialVelocity * elapsedTime * amplitude)) * criticalDampeningFactor; |
+} |
+ |
+static float elasticDeltaForReboundDelta(float delta) |
+{ |
+ float stiffness = std::max(rubberbandStiffness, 1.0f); |
+ return delta / stiffness; |
+} |
+ |
+static float reboundDeltaForElasticDelta(float delta) |
+{ |
+ return delta * rubberbandStiffness; |
+} |
+ |
+static float scrollWheelMultiplier() |
+{ |
+ static float multiplier = -1; |
+ if (multiplier < 0) { |
+ // XXX |
+ // multiplier = [[NSUserDefaults standardUserDefaults] floatForKey:@"NSScrollWheelMultiplier"]; |
+ if (multiplier <= 0) |
+ multiplier = 1; |
+ } |
+ return multiplier; |
+} |
+ |
+InputScrollElasticityController::InputScrollElasticityController(cc::ScrollElasticityControllerClient* client) |
+ : m_client(client) |
+ , m_inScrollGesture(false) |
+ , m_hasScrolled(false) |
+ , m_momentumScrollInProgress(false) |
+ , m_ignoreMomentumScrolls(false) |
+ , m_snapRubberbandTimerIsActive(false) { } |
+ |
+InputScrollElasticityController::~InputScrollElasticityController() { } |
+ |
+void InputScrollElasticityController::WillShutdown() { |
+ m_client = NULL; |
+} |
+ |
+void InputScrollElasticityController::Animate(base::TimeTicks time) { |
+ snapRubberBandTimerFired(); |
+} |
+ |
+bool InputScrollElasticityController::handleWheelEvent(const blink::WebMouseWheelEvent& wheelEvent) |
+{ |
+ if (wheelEvent.phase == blink::WebMouseWheelEvent::PhaseMayBegin) |
+ return false; |
+ |
+ if (wheelEvent.phase == blink::WebMouseWheelEvent::PhaseBegan) { |
+ m_inScrollGesture = true; |
+ m_hasScrolled = false; |
+ m_momentumScrollInProgress = false; |
+ m_ignoreMomentumScrolls = false; |
+ m_lastMomentumScrollTimestamp = base::Time(); |
+ m_momentumVelocity = gfx::Vector2dF(); |
+ |
+ gfx::Vector2dF stretchAmount = m_client->stretchAmount(); |
+ m_stretchScrollForce.set_x(reboundDeltaForElasticDelta(stretchAmount.x())); |
+ m_stretchScrollForce.set_y(reboundDeltaForElasticDelta(stretchAmount.y())); |
+ m_overflowScrollDelta = gfx::Vector2dF(); |
+ |
+ stopSnapRubberbandTimer(); |
+ |
+ // TODO(erikchen): Use the commented out line once Chromium uses the return value correctly. |
+ // crbug.com/375512 |
+ // return shouldHandleEvent(wheelEvent); |
+ |
+ // This logic is incorrect, since diagonal wheel events are not consumed. |
+ if (m_client->pinnedInDirection(gfx::Vector2dF(-wheelEvent.deltaX, 0))) { |
+ if (wheelEvent.deltaX > 0 && !wheelEvent.canRubberbandLeft) |
+ return false; |
+ if (wheelEvent.deltaX < 0 && !wheelEvent.canRubberbandRight) |
+ return false; |
+ } |
+ |
+ return true; |
+ } |
+ |
+ if (wheelEvent.phase == blink::WebMouseWheelEvent::PhaseEnded || wheelEvent.phase == blink::WebMouseWheelEvent::PhaseCancelled) { |
+ snapRubberBand(); |
+ return m_hasScrolled; |
+ } |
+ |
+ bool isMomentumScrollEvent = (wheelEvent.momentumPhase != blink::WebMouseWheelEvent::PhaseNone); |
+ if (m_ignoreMomentumScrolls && (isMomentumScrollEvent || m_snapRubberbandTimerIsActive)) { |
+ if (wheelEvent.momentumPhase == blink::WebMouseWheelEvent::PhaseEnded) { |
+ m_ignoreMomentumScrolls = false; |
+ return true; |
+ } |
+ return false; |
+ } |
+ |
+ if (!shouldHandleEvent(wheelEvent)) |
+ return false; |
+ |
+ float deltaX = m_overflowScrollDelta.x() - wheelEvent.deltaX; |
+ float deltaY = m_overflowScrollDelta.y() - wheelEvent.deltaY; |
+ float eventCoalescedDeltaX = -wheelEvent.deltaX; |
+ float eventCoalescedDeltaY = -wheelEvent.deltaY; |
+ |
+ // Reset overflow values because we may decide to remove delta at various points and put it into overflow. |
+ m_overflowScrollDelta = gfx::Vector2dF(); |
+ |
+ gfx::Vector2dF stretchAmount = m_client->stretchAmount(); |
+ bool isVerticallyStretched = stretchAmount.y(); |
+ bool isHorizontallyStretched = stretchAmount.x(); |
+ |
+ // Slightly prefer scrolling vertically by applying the = case to deltaY |
+ if (fabsf(deltaY) >= fabsf(deltaX)) |
+ deltaX = 0; |
+ else |
+ deltaY = 0; |
+ |
+ bool shouldStretch = false; |
+ |
+ blink::WebMouseWheelEvent::Phase momentumPhase = wheelEvent.momentumPhase; |
+ |
+ // If we are starting momentum scrolling then do some setup. |
+ if (!m_momentumScrollInProgress && (momentumPhase == blink::WebMouseWheelEvent::PhaseBegan || momentumPhase == blink::WebMouseWheelEvent::PhaseChanged)) { |
+ m_momentumScrollInProgress = true; |
+ // Start the snap rubber band timer if it's not running. This is needed to |
+ // snap back from the over scroll caused by momentum events. |
+ if (!m_snapRubberbandTimerIsActive && m_startTime == base::Time()) |
+ snapRubberBand(); |
+ } |
+ |
+ // XXX |
+ base::Time wheelEventTimestamp = base::Time::Now(); |
+ float timeDelta = (wheelEventTimestamp - m_lastMomentumScrollTimestamp).InSecondsF(); |
+ if (m_inScrollGesture || m_momentumScrollInProgress) { |
+ if (m_lastMomentumScrollTimestamp != base::Time() && timeDelta > 0 && timeDelta < scrollVelocityZeroingTimeout) { |
+ m_momentumVelocity.set_x(eventCoalescedDeltaX / (float)timeDelta); |
+ m_momentumVelocity.set_y(eventCoalescedDeltaY / (float)timeDelta); |
+ m_lastMomentumScrollTimestamp = wheelEventTimestamp; |
+ } else { |
+ m_lastMomentumScrollTimestamp = wheelEventTimestamp; |
+ m_momentumVelocity = gfx::Vector2dF(); |
+ } |
+ |
+ if (isVerticallyStretched) { |
+ if (!isHorizontallyStretched && m_client->pinnedInDirection(gfx::Vector2dF(deltaX, 0))) { |
+ // Stretching only in the vertical. |
+ if (deltaY != 0 && (fabsf(deltaX / deltaY) < rubberbandDirectionLockStretchRatio)) |
+ deltaX = 0; |
+ else if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStretch) { |
+ m_overflowScrollDelta.set_x(m_overflowScrollDelta.x() + deltaX); |
+ deltaX = 0; |
+ } else |
+ m_overflowScrollDelta.set_x(m_overflowScrollDelta.x() + deltaX); |
+ } |
+ } else if (isHorizontallyStretched) { |
+ // Stretching only in the horizontal. |
+ if (m_client->pinnedInDirection(gfx::Vector2dF(0, deltaY))) { |
+ if (deltaX != 0 && (fabsf(deltaY / deltaX) < rubberbandDirectionLockStretchRatio)) |
+ deltaY = 0; |
+ else if (fabsf(deltaY) < rubberbandMinimumRequiredDeltaBeforeStretch) { |
+ m_overflowScrollDelta.set_y(m_overflowScrollDelta.y() + deltaY); |
+ deltaY = 0; |
+ } else |
+ m_overflowScrollDelta.set_y(m_overflowScrollDelta.y() + deltaY); |
+ } |
+ } else { |
+ // Not stretching at all yet. |
+ if (m_client->pinnedInDirection(gfx::Vector2dF(deltaX, deltaY))) { |
+ if (fabsf(deltaY) >= fabsf(deltaX)) { |
+ if (fabsf(deltaX) < rubberbandMinimumRequiredDeltaBeforeStretch) { |
+ m_overflowScrollDelta.set_x(m_overflowScrollDelta.x() + deltaX); |
+ deltaX = 0; |
+ } else |
+ m_overflowScrollDelta.set_x(m_overflowScrollDelta.x() + deltaX); |
+ } |
+ shouldStretch = true; |
+ } |
+ } |
+ } |
+ |
+ if (deltaX != 0 || deltaY != 0) { |
+ m_hasScrolled = true; |
+ if (!(shouldStretch || isVerticallyStretched || isHorizontallyStretched)) { |
+ if (deltaY != 0) { |
+ deltaY *= scrollWheelMultiplier(); |
+ m_client->immediateScrollBy(gfx::Vector2dF(0, deltaY)); |
+ } |
+ if (deltaX != 0) { |
+ deltaX *= scrollWheelMultiplier(); |
+ m_client->immediateScrollBy(gfx::Vector2dF(deltaX, 0)); |
+ } |
+ } else { |
+ if (!m_client->allowsHorizontalStretching()) { |
+ deltaX = 0; |
+ eventCoalescedDeltaX = 0; |
+ } else if ((deltaX != 0) && !isHorizontallyStretched && !m_client->pinnedInDirection(gfx::Vector2dF(deltaX, 0))) { |
+ deltaX *= scrollWheelMultiplier(); |
+ |
+ m_client->immediateScrollByWithoutContentEdgeConstraints(gfx::Vector2dF(deltaX, 0)); |
+ deltaX = 0; |
+ } |
+ |
+ if (!m_client->allowsVerticalStretching()) { |
+ deltaY = 0; |
+ eventCoalescedDeltaY = 0; |
+ } else if ((deltaY != 0) && !isVerticallyStretched && !m_client->pinnedInDirection(gfx::Vector2dF(0, deltaY))) { |
+ deltaY *= scrollWheelMultiplier(); |
+ |
+ m_client->immediateScrollByWithoutContentEdgeConstraints(gfx::Vector2dF(0, deltaY)); |
+ deltaY = 0; |
+ } |
+ |
+ gfx::Vector2dF stretchAmount = m_client->stretchAmount(); |
+ |
+ if (m_momentumScrollInProgress) { |
+ if ((m_client->pinnedInDirection(gfx::Vector2dF(eventCoalescedDeltaX, eventCoalescedDeltaY)) || (fabsf(eventCoalescedDeltaX) + fabsf(eventCoalescedDeltaY) <= 0)) && m_lastMomentumScrollTimestamp != base::Time()) { |
+ m_ignoreMomentumScrolls = true; |
+ m_momentumScrollInProgress = false; |
+ snapRubberBand(); |
+ } |
+ } |
+ |
+ m_stretchScrollForce.set_x(m_stretchScrollForce.x() + deltaX); |
+ m_stretchScrollForce.set_y(m_stretchScrollForce.y() + deltaY); |
+ |
+ gfx::Vector2dF dampedDelta(ceilf(elasticDeltaForReboundDelta(m_stretchScrollForce.x())), ceilf(elasticDeltaForReboundDelta(m_stretchScrollForce.y()))); |
+ |
+ m_client->immediateScrollByWithoutContentEdgeConstraints(dampedDelta - stretchAmount); |
+ } |
+ } |
+ |
+ if (m_momentumScrollInProgress && momentumPhase == blink::WebMouseWheelEvent::PhaseEnded) { |
+ m_momentumScrollInProgress = false; |
+ m_ignoreMomentumScrolls = false; |
+ m_lastMomentumScrollTimestamp = base::Time(); |
+ } |
+ |
+ return true; |
+} |
+ |
+static inline float roundTowardZero(float num) |
+{ |
+ return num > 0 ? ceilf(num - 0.5f) : floorf(num + 0.5f); |
+} |
+ |
+static inline float roundToDevicePixelTowardZero(float num) |
+{ |
+ float roundedNum = roundf(num); |
+ if (fabs(num - roundedNum) < 0.125) |
+ num = roundedNum; |
+ |
+ return roundTowardZero(num); |
+} |
+ |
+void InputScrollElasticityController::snapRubberBandTimerFired() |
+{ |
+ if (!m_momentumScrollInProgress || m_ignoreMomentumScrolls) { |
+ float timeDelta = (base::Time::Now() - m_startTime).InSecondsF(); |
+ |
+ if (m_startStretch == gfx::Vector2dF()) { |
+ m_startStretch = m_client->stretchAmount(); |
+ if (m_startStretch == gfx::Vector2dF()) { |
+ stopSnapRubberbandTimer(); |
+ |
+ m_stretchScrollForce = gfx::Vector2dF(); |
+ m_startTime = base::Time(); |
+ m_origOrigin = gfx::Vector2dF(); |
+ m_origVelocity = gfx::Vector2dF(); |
+ return; |
+ } |
+ |
+ m_origOrigin = m_client->absoluteScrollPosition() - m_startStretch; |
+ m_origVelocity = m_momentumVelocity; |
+ |
+ // Just like normal scrolling, prefer vertical rubberbanding |
+ if (fabsf(m_origVelocity.y()) >= fabsf(m_origVelocity.x())) |
+ m_origVelocity.set_x(0); |
+ |
+ // Don't rubber-band horizontally if it's not possible to scroll horizontally |
+ if (!m_client->canScrollHorizontally()) |
+ m_origVelocity.set_x(0); |
+ |
+ // Don't rubber-band vertically if it's not possible to scroll vertically |
+ if (!m_client->canScrollVertically()) |
+ m_origVelocity.set_y(0); |
+ } |
+ |
+ gfx::Vector2dF delta(roundToDevicePixelTowardZero(elasticDeltaForTimeDelta(m_startStretch.x(), -m_origVelocity.x(), (float)timeDelta)), |
+ roundToDevicePixelTowardZero(elasticDeltaForTimeDelta(m_startStretch.y(), -m_origVelocity.y(), (float)timeDelta))); |
+ |
+ if (fabs(delta.x()) >= 1 || fabs(delta.y()) >= 1) { |
+ m_client->immediateScrollByWithoutContentEdgeConstraints(gfx::Vector2dF(delta.x(), delta.y()) - m_client->stretchAmount()); |
+ |
+ gfx::Vector2dF newStretch = m_client->stretchAmount(); |
+ |
+ m_stretchScrollForce.set_x(reboundDeltaForElasticDelta(newStretch.x())); |
+ m_stretchScrollForce.set_y(reboundDeltaForElasticDelta(newStretch.y())); |
+ } else { |
+ m_client->adjustScrollPositionToBoundsIfNecessary(); |
+ |
+ stopSnapRubberbandTimer(); |
+ m_stretchScrollForce = gfx::Vector2dF(); |
+ m_startTime = base::Time(); |
+ m_startStretch = gfx::Vector2dF(); |
+ m_origOrigin = gfx::Vector2dF(); |
+ m_origVelocity = gfx::Vector2dF(); |
+ } |
+ } else { |
+ m_startTime = base::Time::Now(); |
+ m_startStretch = gfx::Vector2dF(); |
+ } |
+} |
+ |
+bool InputScrollElasticityController::isRubberBandInProgress() const |
+{ |
+ if (!m_inScrollGesture && !m_momentumScrollInProgress && !m_snapRubberbandTimerIsActive) |
+ return false; |
+ |
+ return !m_client->stretchAmount().IsZero(); |
+} |
+ |
+void InputScrollElasticityController::stopSnapRubberbandTimer() |
+{ |
+ m_client->stopSnapRubberbandTimer(); |
+ m_snapRubberbandTimerIsActive = false; |
+} |
+ |
+void InputScrollElasticityController::snapRubberBand() |
+{ |
+ float timeDelta = (base::Time::Now() - m_lastMomentumScrollTimestamp).InSecondsF(); |
+ if (m_lastMomentumScrollTimestamp != base::Time() && timeDelta >= scrollVelocityZeroingTimeout) |
+ m_momentumVelocity = gfx::Vector2dF(); |
+ |
+ m_inScrollGesture = false; |
+ |
+ if (m_snapRubberbandTimerIsActive) |
+ return; |
+ |
+ m_startStretch = gfx::Vector2dF(); |
+ m_origOrigin = gfx::Vector2dF(); |
+ m_origVelocity = gfx::Vector2dF(); |
+ |
+ // If there's no momentum scroll or stretch amount, no need to start the timer. |
+ if (!m_momentumScrollInProgress && m_client->stretchAmount() == gfx::Vector2dF()) { |
+ m_startTime = base::Time(); |
+ m_stretchScrollForce = gfx::Vector2dF(); |
+ return; |
+ } |
+ |
+ m_startTime = base::Time::Now(); |
+ m_client->startSnapRubberbandTimer(); |
+ m_snapRubberbandTimerIsActive = true; |
+} |
+ |
+bool InputScrollElasticityController::shouldHandleEvent(const blink::WebMouseWheelEvent& wheelEvent) |
+{ |
+ // Once any scrolling has happened, all future events should be handled. |
+ if (m_hasScrolled) |
+ return true; |
+ |
+ // The event can't cause scrolling to start if its delta is 0. |
+ if (wheelEvent.deltaX == 0 && wheelEvent.deltaY == 0) |
+ return false; |
+ |
+ // If the client isn't pinned, then the event is guaranteed to cause scrolling. |
+ if (!m_client->pinnedInDirection(gfx::Vector2dF(-wheelEvent.deltaX, 0))) |
+ return true; |
+ |
+ // If the event is pinned, then the client can't scroll, but it might rubber band. |
+ // Check if the event allows rubber banding. |
+ if (wheelEvent.deltaY == 0) { |
+ if (wheelEvent.deltaX > 0 && !wheelEvent.canRubberbandLeft) |
+ return false; |
+ if (wheelEvent.deltaX < 0 && !wheelEvent.canRubberbandRight) |
+ return false; |
+ } |
+ |
+ // The event is going to either cause scrolling or rubber banding. |
+ return true; |
+} |
+ |
+} // namespace content |