| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/input/input_scroll_elasticity_controller.h" | 5 #include "content/renderer/input/input_scroll_elasticity_controller.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 // ScrollElasticityController and ScrollElasticityControllerClient are based on | 9 // InputScrollElasticityController is based on |
| 10 // WebKit/Source/platform/mac/ScrollElasticityController.mm | 10 // WebKit/Source/platform/mac/InputScrollElasticityController.mm |
| 11 /* | 11 /* |
| 12 * Copyright (C) 2011 Apple Inc. All rights reserved. | 12 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 13 * | 13 * |
| 14 * Redistribution and use in source and binary forms, with or without | 14 * Redistribution and use in source and binary forms, with or without |
| 15 * modification, are permitted provided that the following conditions | 15 * modification, are permitted provided that the following conditions |
| 16 * are met: | 16 * are met: |
| 17 * 1. Redistributions of source code must retain the above copyright | 17 * 1. Redistributions of source code must retain the above copyright |
| 18 * notice, this list of conditions and the following disclaimer. | 18 * notice, this list of conditions and the following disclaimer. |
| 19 * 2. Redistributions in binary form must reproduce the above copyright | 19 * 2. Redistributions in binary form must reproduce the above copyright |
| 20 * notice, this list of conditions and the following disclaimer in the | 20 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // multiplier = [[NSUserDefaults standardUserDefaults] | 81 // multiplier = [[NSUserDefaults standardUserDefaults] |
| 82 // floatForKey:@"NSScrollWheelMultiplier"]; | 82 // floatForKey:@"NSScrollWheelMultiplier"]; |
| 83 if (multiplier <= 0) | 83 if (multiplier <= 0) |
| 84 multiplier = 1; | 84 multiplier = 1; |
| 85 } | 85 } |
| 86 return multiplier; | 86 return multiplier; |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 ScrollElasticityController::ScrollElasticityController( | 91 InputScrollElasticityController::InputScrollElasticityController( |
| 92 ScrollElasticityControllerClient* client) | 92 cc::ScrollElasticityHelper* helper) |
| 93 : client_(client), | 93 : helper_(helper), |
| 94 in_scroll_gesture_(false), | 94 in_scroll_gesture_(false), |
| 95 has_scrolled_(false), | 95 has_scrolled_(false), |
| 96 momentum_scroll_in_progress_(false), | 96 momentum_scroll_in_progress_(false), |
| 97 ignore_momentum_scrolls_(false), | 97 ignore_momentum_scrolls_(false), |
| 98 last_momentum_scroll_timestamp_(0), | 98 last_momentum_scroll_timestamp_(0), |
| 99 snap_rubberband_timer_is_active_(false) { | 99 snap_rubberband_timer_is_active_(false), |
| 100 weak_factory_(this) { |
| 100 } | 101 } |
| 101 | 102 |
| 102 bool ScrollElasticityController::HandleWheelEvent( | 103 InputScrollElasticityController::~InputScrollElasticityController() { |
| 104 DCHECK(!helper_); |
| 105 } |
| 106 |
| 107 base::WeakPtr<InputScrollElasticityController> |
| 108 InputScrollElasticityController::GetWeakPtr() { |
| 109 if (helper_) |
| 110 return weak_factory_.GetWeakPtr(); |
| 111 return base::WeakPtr<InputScrollElasticityController>(); |
| 112 } |
| 113 |
| 114 void InputScrollElasticityController::WillShutdown() { |
| 115 weak_factory_.InvalidateWeakPtrs(); |
| 116 helper_ = NULL; |
| 117 } |
| 118 |
| 119 void InputScrollElasticityController::Animate(base::TimeTicks time) { |
| 120 if (!snap_rubberband_timer_is_active_) |
| 121 return; |
| 122 |
| 123 // TODO(ccameron): This should send the time parameter to the snap function, |
| 124 // so that animation can be based on frame time, not arbitrarily-sampled |
| 125 // clock time. |
| 126 SnapRubberbandTimerFired(); |
| 127 } |
| 128 |
| 129 void InputScrollElasticityController::ObserveWheelEventAndResult( |
| 130 const blink::WebMouseWheelEvent& wheel_event, |
| 131 const cc::InputHandlerScrollResult& scroll_result) { |
| 132 // TODO(ccameron): Pull non-over-scrolling scroll logic out of |
| 133 // HandleWheelEvent, and read the scroll result instead. |
| 134 HandleWheelEvent(wheel_event); |
| 135 } |
| 136 |
| 137 bool InputScrollElasticityController::HandleWheelEvent( |
| 103 const blink::WebMouseWheelEvent& wheel_event) { | 138 const blink::WebMouseWheelEvent& wheel_event) { |
| 104 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseMayBegin) | 139 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseMayBegin) |
| 105 return false; | 140 return false; |
| 106 | 141 |
| 107 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseBegan) { | 142 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseBegan) { |
| 108 in_scroll_gesture_ = true; | 143 in_scroll_gesture_ = true; |
| 109 has_scrolled_ = false; | 144 has_scrolled_ = false; |
| 110 momentum_scroll_in_progress_ = false; | 145 momentum_scroll_in_progress_ = false; |
| 111 ignore_momentum_scrolls_ = false; | 146 ignore_momentum_scrolls_ = false; |
| 112 last_momentum_scroll_timestamp_ = 0; | 147 last_momentum_scroll_timestamp_ = 0; |
| 113 momentum_velocity_ = gfx::Vector2dF(); | 148 momentum_velocity_ = gfx::Vector2dF(); |
| 114 | 149 |
| 115 gfx::Vector2dF stretch_amount = client_->StretchAmount(); | 150 gfx::Vector2dF stretch_amount = helper_->StretchAmount(); |
| 116 stretch_scroll_force_.set_x( | 151 stretch_scroll_force_.set_x( |
| 117 ReboundDeltaForElasticDelta(stretch_amount.x())); | 152 ReboundDeltaForElasticDelta(stretch_amount.x())); |
| 118 stretch_scroll_force_.set_y( | 153 stretch_scroll_force_.set_y( |
| 119 ReboundDeltaForElasticDelta(stretch_amount.y())); | 154 ReboundDeltaForElasticDelta(stretch_amount.y())); |
| 120 overflow_scroll_delta_ = gfx::Vector2dF(); | 155 overflow_scroll_delta_ = gfx::Vector2dF(); |
| 121 | 156 |
| 122 StopSnapRubberbandTimer(); | 157 StopSnapRubberbandTimer(); |
| 123 | 158 |
| 124 // TODO(erikchen): Use the commented out line once Chromium uses the | 159 // TODO(erikchen): Use the commented out line once Chromium uses the |
| 125 // return value correctly. | 160 // return value correctly. |
| 126 // crbug.com/375512 | 161 // crbug.com/375512 |
| 127 // return ShouldHandleEvent(wheel_event); | 162 // return ShouldHandleEvent(wheel_event); |
| 128 | 163 |
| 129 // This logic is incorrect, since diagonal wheel events are not consumed. | 164 // This logic is incorrect, since diagonal wheel events are not consumed. |
| 130 if (client_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) { | 165 if (helper_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) { |
| 131 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) | 166 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) |
| 132 return false; | 167 return false; |
| 133 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) | 168 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) |
| 134 return false; | 169 return false; |
| 135 } | 170 } |
| 136 | 171 |
| 137 return true; | 172 return true; |
| 138 } | 173 } |
| 139 | 174 |
| 140 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseEnded || | 175 if (wheel_event.phase == blink::WebMouseWheelEvent::PhaseEnded || |
| (...skipping 18 matching lines...) Expand all Loading... |
| 159 | 194 |
| 160 float delta_x = overflow_scroll_delta_.x() - wheel_event.deltaX; | 195 float delta_x = overflow_scroll_delta_.x() - wheel_event.deltaX; |
| 161 float delta_y = overflow_scroll_delta_.y() - wheel_event.deltaY; | 196 float delta_y = overflow_scroll_delta_.y() - wheel_event.deltaY; |
| 162 float event_coalesced_delta_x = -wheel_event.deltaX; | 197 float event_coalesced_delta_x = -wheel_event.deltaX; |
| 163 float event_coalesced_delta_y = -wheel_event.deltaY; | 198 float event_coalesced_delta_y = -wheel_event.deltaY; |
| 164 | 199 |
| 165 // Reset overflow values because we may decide to remove delta at various | 200 // Reset overflow values because we may decide to remove delta at various |
| 166 // points and put it into overflow. | 201 // points and put it into overflow. |
| 167 overflow_scroll_delta_ = gfx::Vector2dF(); | 202 overflow_scroll_delta_ = gfx::Vector2dF(); |
| 168 | 203 |
| 169 gfx::Vector2dF stretch_amount = client_->StretchAmount(); | 204 gfx::Vector2dF stretch_amount = helper_->StretchAmount(); |
| 170 bool is_vertically_stretched = stretch_amount.y() != 0.f; | 205 bool is_vertically_stretched = stretch_amount.y() != 0.f; |
| 171 bool is_horizontally_stretched = stretch_amount.x() != 0.f; | 206 bool is_horizontally_stretched = stretch_amount.x() != 0.f; |
| 172 | 207 |
| 173 // Slightly prefer scrolling vertically by applying the = case to delta_y | 208 // Slightly prefer scrolling vertically by applying the = case to delta_y |
| 174 if (fabsf(delta_y) >= fabsf(delta_x)) | 209 if (fabsf(delta_y) >= fabsf(delta_x)) |
| 175 delta_x = 0; | 210 delta_x = 0; |
| 176 else | 211 else |
| 177 delta_y = 0; | 212 delta_y = 0; |
| 178 | 213 |
| 179 bool should_stretch = false; | 214 bool should_stretch = false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 199 momentum_velocity_.set_x(event_coalesced_delta_x / (float)time_delta); | 234 momentum_velocity_.set_x(event_coalesced_delta_x / (float)time_delta); |
| 200 momentum_velocity_.set_y(event_coalesced_delta_y / (float)time_delta); | 235 momentum_velocity_.set_y(event_coalesced_delta_y / (float)time_delta); |
| 201 last_momentum_scroll_timestamp_ = wheel_event.timeStampSeconds; | 236 last_momentum_scroll_timestamp_ = wheel_event.timeStampSeconds; |
| 202 } else { | 237 } else { |
| 203 last_momentum_scroll_timestamp_ = wheel_event.timeStampSeconds; | 238 last_momentum_scroll_timestamp_ = wheel_event.timeStampSeconds; |
| 204 momentum_velocity_ = gfx::Vector2dF(); | 239 momentum_velocity_ = gfx::Vector2dF(); |
| 205 } | 240 } |
| 206 | 241 |
| 207 if (is_vertically_stretched) { | 242 if (is_vertically_stretched) { |
| 208 if (!is_horizontally_stretched && | 243 if (!is_horizontally_stretched && |
| 209 client_->PinnedInDirection(gfx::Vector2dF(delta_x, 0))) { | 244 helper_->PinnedInDirection(gfx::Vector2dF(delta_x, 0))) { |
| 210 // Stretching only in the vertical. | 245 // Stretching only in the vertical. |
| 211 if (delta_y != 0 && | 246 if (delta_y != 0 && |
| 212 (fabsf(delta_x / delta_y) < kRubberbandDirectionLockStretchRatio)) | 247 (fabsf(delta_x / delta_y) < kRubberbandDirectionLockStretchRatio)) |
| 213 delta_x = 0; | 248 delta_x = 0; |
| 214 else if (fabsf(delta_x) < | 249 else if (fabsf(delta_x) < |
| 215 kRubberbandMinimumRequiredDeltaBeforeStretch) { | 250 kRubberbandMinimumRequiredDeltaBeforeStretch) { |
| 216 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); | 251 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); |
| 217 delta_x = 0; | 252 delta_x = 0; |
| 218 } else | 253 } else |
| 219 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); | 254 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); |
| 220 } | 255 } |
| 221 } else if (is_horizontally_stretched) { | 256 } else if (is_horizontally_stretched) { |
| 222 // Stretching only in the horizontal. | 257 // Stretching only in the horizontal. |
| 223 if (client_->PinnedInDirection(gfx::Vector2dF(0, delta_y))) { | 258 if (helper_->PinnedInDirection(gfx::Vector2dF(0, delta_y))) { |
| 224 if (delta_x != 0 && | 259 if (delta_x != 0 && |
| 225 (fabsf(delta_y / delta_x) < kRubberbandDirectionLockStretchRatio)) | 260 (fabsf(delta_y / delta_x) < kRubberbandDirectionLockStretchRatio)) |
| 226 delta_y = 0; | 261 delta_y = 0; |
| 227 else if (fabsf(delta_y) < | 262 else if (fabsf(delta_y) < |
| 228 kRubberbandMinimumRequiredDeltaBeforeStretch) { | 263 kRubberbandMinimumRequiredDeltaBeforeStretch) { |
| 229 overflow_scroll_delta_.set_y(overflow_scroll_delta_.y() + delta_y); | 264 overflow_scroll_delta_.set_y(overflow_scroll_delta_.y() + delta_y); |
| 230 delta_y = 0; | 265 delta_y = 0; |
| 231 } else | 266 } else |
| 232 overflow_scroll_delta_.set_y(overflow_scroll_delta_.y() + delta_y); | 267 overflow_scroll_delta_.set_y(overflow_scroll_delta_.y() + delta_y); |
| 233 } | 268 } |
| 234 } else { | 269 } else { |
| 235 // Not stretching at all yet. | 270 // Not stretching at all yet. |
| 236 if (client_->PinnedInDirection(gfx::Vector2dF(delta_x, delta_y))) { | 271 if (helper_->PinnedInDirection(gfx::Vector2dF(delta_x, delta_y))) { |
| 237 if (fabsf(delta_y) >= fabsf(delta_x)) { | 272 if (fabsf(delta_y) >= fabsf(delta_x)) { |
| 238 if (fabsf(delta_x) < kRubberbandMinimumRequiredDeltaBeforeStretch) { | 273 if (fabsf(delta_x) < kRubberbandMinimumRequiredDeltaBeforeStretch) { |
| 239 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); | 274 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); |
| 240 delta_x = 0; | 275 delta_x = 0; |
| 241 } else | 276 } else |
| 242 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); | 277 overflow_scroll_delta_.set_x(overflow_scroll_delta_.x() + delta_x); |
| 243 } | 278 } |
| 244 should_stretch = true; | 279 should_stretch = true; |
| 245 } | 280 } |
| 246 } | 281 } |
| 247 } | 282 } |
| 248 | 283 |
| 249 if (delta_x != 0 || delta_y != 0) { | 284 if (delta_x != 0 || delta_y != 0) { |
| 250 has_scrolled_ = true; | 285 has_scrolled_ = true; |
| 251 if (!(should_stretch || is_vertically_stretched || | 286 if (!(should_stretch || is_vertically_stretched || |
| 252 is_horizontally_stretched)) { | 287 is_horizontally_stretched)) { |
| 253 if (delta_y != 0) { | 288 if (delta_y != 0) { |
| 254 delta_y *= ScrollWheelMultiplier(); | 289 delta_y *= ScrollWheelMultiplier(); |
| 255 client_->ImmediateScrollBy(gfx::Vector2dF(0, delta_y)); | 290 helper_->ImmediateScrollBy(gfx::Vector2dF(0, delta_y)); |
| 256 } | 291 } |
| 257 if (delta_x != 0) { | 292 if (delta_x != 0) { |
| 258 delta_x *= ScrollWheelMultiplier(); | 293 delta_x *= ScrollWheelMultiplier(); |
| 259 client_->ImmediateScrollBy(gfx::Vector2dF(delta_x, 0)); | 294 helper_->ImmediateScrollBy(gfx::Vector2dF(delta_x, 0)); |
| 260 } | 295 } |
| 261 } else { | 296 } else { |
| 262 if (!client_->AllowsHorizontalStretching()) { | 297 if (!helper_->AllowsHorizontalStretching()) { |
| 263 delta_x = 0; | 298 delta_x = 0; |
| 264 event_coalesced_delta_x = 0; | 299 event_coalesced_delta_x = 0; |
| 265 } else if ((delta_x != 0) && !is_horizontally_stretched && | 300 } else if ((delta_x != 0) && !is_horizontally_stretched && |
| 266 !client_->PinnedInDirection(gfx::Vector2dF(delta_x, 0))) { | 301 !helper_->PinnedInDirection(gfx::Vector2dF(delta_x, 0))) { |
| 267 delta_x *= ScrollWheelMultiplier(); | 302 delta_x *= ScrollWheelMultiplier(); |
| 268 | 303 |
| 269 client_->ImmediateScrollByWithoutContentEdgeConstraints( | 304 helper_->ImmediateScrollByWithoutContentEdgeConstraints( |
| 270 gfx::Vector2dF(delta_x, 0)); | 305 gfx::Vector2dF(delta_x, 0)); |
| 271 delta_x = 0; | 306 delta_x = 0; |
| 272 } | 307 } |
| 273 | 308 |
| 274 if (!client_->AllowsVerticalStretching()) { | 309 if (!helper_->AllowsVerticalStretching()) { |
| 275 delta_y = 0; | 310 delta_y = 0; |
| 276 event_coalesced_delta_y = 0; | 311 event_coalesced_delta_y = 0; |
| 277 } else if ((delta_y != 0) && !is_vertically_stretched && | 312 } else if ((delta_y != 0) && !is_vertically_stretched && |
| 278 !client_->PinnedInDirection(gfx::Vector2dF(0, delta_y))) { | 313 !helper_->PinnedInDirection(gfx::Vector2dF(0, delta_y))) { |
| 279 delta_y *= ScrollWheelMultiplier(); | 314 delta_y *= ScrollWheelMultiplier(); |
| 280 | 315 |
| 281 client_->ImmediateScrollByWithoutContentEdgeConstraints( | 316 helper_->ImmediateScrollByWithoutContentEdgeConstraints( |
| 282 gfx::Vector2dF(0, delta_y)); | 317 gfx::Vector2dF(0, delta_y)); |
| 283 delta_y = 0; | 318 delta_y = 0; |
| 284 } | 319 } |
| 285 | 320 |
| 286 gfx::Vector2dF stretch_amount = client_->StretchAmount(); | 321 gfx::Vector2dF stretch_amount = helper_->StretchAmount(); |
| 287 | 322 |
| 288 if (momentum_scroll_in_progress_) { | 323 if (momentum_scroll_in_progress_) { |
| 289 if ((client_->PinnedInDirection(gfx::Vector2dF( | 324 if ((helper_->PinnedInDirection(gfx::Vector2dF( |
| 290 event_coalesced_delta_x, event_coalesced_delta_y)) || | 325 event_coalesced_delta_x, event_coalesced_delta_y)) || |
| 291 (fabsf(event_coalesced_delta_x) + fabsf(event_coalesced_delta_y) <= | 326 (fabsf(event_coalesced_delta_x) + fabsf(event_coalesced_delta_y) <= |
| 292 0)) && | 327 0)) && |
| 293 last_momentum_scroll_timestamp_) { | 328 last_momentum_scroll_timestamp_) { |
| 294 ignore_momentum_scrolls_ = true; | 329 ignore_momentum_scrolls_ = true; |
| 295 momentum_scroll_in_progress_ = false; | 330 momentum_scroll_in_progress_ = false; |
| 296 SnapRubberband(); | 331 SnapRubberband(); |
| 297 } | 332 } |
| 298 } | 333 } |
| 299 | 334 |
| 300 stretch_scroll_force_.set_x(stretch_scroll_force_.x() + delta_x); | 335 stretch_scroll_force_.set_x(stretch_scroll_force_.x() + delta_x); |
| 301 stretch_scroll_force_.set_y(stretch_scroll_force_.y() + delta_y); | 336 stretch_scroll_force_.set_y(stretch_scroll_force_.y() + delta_y); |
| 302 | 337 |
| 303 gfx::Vector2dF damped_delta( | 338 gfx::Vector2dF damped_delta( |
| 304 ceilf(ElasticDeltaForReboundDelta(stretch_scroll_force_.x())), | 339 ceilf(ElasticDeltaForReboundDelta(stretch_scroll_force_.x())), |
| 305 ceilf(ElasticDeltaForReboundDelta(stretch_scroll_force_.y()))); | 340 ceilf(ElasticDeltaForReboundDelta(stretch_scroll_force_.y()))); |
| 306 | 341 |
| 307 client_->ImmediateScrollByWithoutContentEdgeConstraints(damped_delta - | 342 helper_->ImmediateScrollByWithoutContentEdgeConstraints(damped_delta - |
| 308 stretch_amount); | 343 stretch_amount); |
| 309 } | 344 } |
| 310 } | 345 } |
| 311 | 346 |
| 312 if (momentum_scroll_in_progress_ && | 347 if (momentum_scroll_in_progress_ && |
| 313 momentum_phase == blink::WebMouseWheelEvent::PhaseEnded) { | 348 momentum_phase == blink::WebMouseWheelEvent::PhaseEnded) { |
| 314 momentum_scroll_in_progress_ = false; | 349 momentum_scroll_in_progress_ = false; |
| 315 ignore_momentum_scrolls_ = false; | 350 ignore_momentum_scrolls_ = false; |
| 316 last_momentum_scroll_timestamp_ = 0; | 351 last_momentum_scroll_timestamp_ = 0; |
| 317 } | 352 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 328 float RoundToDevicePixelTowardZero(float num) { | 363 float RoundToDevicePixelTowardZero(float num) { |
| 329 float rounded_num = roundf(num); | 364 float rounded_num = roundf(num); |
| 330 if (fabs(num - rounded_num) < 0.125) | 365 if (fabs(num - rounded_num) < 0.125) |
| 331 num = rounded_num; | 366 num = rounded_num; |
| 332 | 367 |
| 333 return RoundTowardZero(num); | 368 return RoundTowardZero(num); |
| 334 } | 369 } |
| 335 | 370 |
| 336 } // namespace | 371 } // namespace |
| 337 | 372 |
| 338 void ScrollElasticityController::SnapRubberbandTimerFired() { | 373 void InputScrollElasticityController::SnapRubberbandTimerFired() { |
| 339 if (!momentum_scroll_in_progress_ || ignore_momentum_scrolls_) { | 374 if (!momentum_scroll_in_progress_ || ignore_momentum_scrolls_) { |
| 340 float time_delta = (base::Time::Now() - start_time_).InSecondsF(); | 375 float time_delta = (base::Time::Now() - start_time_).InSecondsF(); |
| 341 | 376 |
| 342 if (start_stretch_ == gfx::Vector2dF()) { | 377 if (start_stretch_ == gfx::Vector2dF()) { |
| 343 start_stretch_ = client_->StretchAmount(); | 378 start_stretch_ = helper_->StretchAmount(); |
| 344 if (start_stretch_ == gfx::Vector2dF()) { | 379 if (start_stretch_ == gfx::Vector2dF()) { |
| 345 StopSnapRubberbandTimer(); | 380 StopSnapRubberbandTimer(); |
| 346 | 381 |
| 347 stretch_scroll_force_ = gfx::Vector2dF(); | 382 stretch_scroll_force_ = gfx::Vector2dF(); |
| 348 start_time_ = base::Time(); | 383 start_time_ = base::Time(); |
| 349 orig_origin_ = gfx::Vector2dF(); | 384 orig_origin_ = gfx::Vector2dF(); |
| 350 orig_velocity_ = gfx::Vector2dF(); | 385 orig_velocity_ = gfx::Vector2dF(); |
| 351 return; | 386 return; |
| 352 } | 387 } |
| 353 | 388 |
| 354 orig_origin_ = client_->AbsoluteScrollPosition() - start_stretch_; | 389 orig_origin_ = helper_->AbsoluteScrollPosition() - start_stretch_; |
| 355 orig_velocity_ = momentum_velocity_; | 390 orig_velocity_ = momentum_velocity_; |
| 356 | 391 |
| 357 // Just like normal scrolling, prefer vertical rubberbanding | 392 // Just like normal scrolling, prefer vertical rubberbanding |
| 358 if (fabsf(orig_velocity_.y()) >= fabsf(orig_velocity_.x())) | 393 if (fabsf(orig_velocity_.y()) >= fabsf(orig_velocity_.x())) |
| 359 orig_velocity_.set_x(0); | 394 orig_velocity_.set_x(0); |
| 360 | 395 |
| 361 // Don't rubber-band horizontally if it's not possible to scroll | 396 // Don't rubber-band horizontally if it's not possible to scroll |
| 362 // horizontally | 397 // horizontally |
| 363 if (!client_->CanScrollHorizontally()) | 398 if (!helper_->CanScrollHorizontally()) |
| 364 orig_velocity_.set_x(0); | 399 orig_velocity_.set_x(0); |
| 365 | 400 |
| 366 // Don't rubber-band vertically if it's not possible to scroll | 401 // Don't rubber-band vertically if it's not possible to scroll |
| 367 // vertically | 402 // vertically |
| 368 if (!client_->CanScrollVertically()) | 403 if (!helper_->CanScrollVertically()) |
| 369 orig_velocity_.set_y(0); | 404 orig_velocity_.set_y(0); |
| 370 } | 405 } |
| 371 | 406 |
| 372 gfx::Vector2dF delta( | 407 gfx::Vector2dF delta( |
| 373 RoundToDevicePixelTowardZero(ElasticDeltaForTimeDelta( | 408 RoundToDevicePixelTowardZero(ElasticDeltaForTimeDelta( |
| 374 start_stretch_.x(), -orig_velocity_.x(), time_delta)), | 409 start_stretch_.x(), -orig_velocity_.x(), time_delta)), |
| 375 RoundToDevicePixelTowardZero(ElasticDeltaForTimeDelta( | 410 RoundToDevicePixelTowardZero(ElasticDeltaForTimeDelta( |
| 376 start_stretch_.y(), -orig_velocity_.y(), time_delta))); | 411 start_stretch_.y(), -orig_velocity_.y(), time_delta))); |
| 377 | 412 |
| 378 if (fabs(delta.x()) >= 1 || fabs(delta.y()) >= 1) { | 413 if (fabs(delta.x()) >= 1 || fabs(delta.y()) >= 1) { |
| 379 client_->ImmediateScrollByWithoutContentEdgeConstraints( | 414 helper_->ImmediateScrollByWithoutContentEdgeConstraints( |
| 380 gfx::Vector2dF(delta.x(), delta.y()) - client_->StretchAmount()); | 415 gfx::Vector2dF(delta.x(), delta.y()) - helper_->StretchAmount()); |
| 381 | 416 |
| 382 gfx::Vector2dF new_stretch = client_->StretchAmount(); | 417 gfx::Vector2dF new_stretch = helper_->StretchAmount(); |
| 383 | 418 |
| 384 stretch_scroll_force_.set_x(ReboundDeltaForElasticDelta(new_stretch.x())); | 419 stretch_scroll_force_.set_x(ReboundDeltaForElasticDelta(new_stretch.x())); |
| 385 stretch_scroll_force_.set_y(ReboundDeltaForElasticDelta(new_stretch.y())); | 420 stretch_scroll_force_.set_y(ReboundDeltaForElasticDelta(new_stretch.y())); |
| 386 } else { | 421 } else { |
| 387 client_->AdjustScrollPositionToBoundsIfNecessary(); | 422 helper_->AdjustScrollPositionToBoundsIfNecessary(); |
| 388 | 423 |
| 389 StopSnapRubberbandTimer(); | 424 StopSnapRubberbandTimer(); |
| 390 stretch_scroll_force_ = gfx::Vector2dF(); | 425 stretch_scroll_force_ = gfx::Vector2dF(); |
| 391 start_time_ = base::Time(); | 426 start_time_ = base::Time(); |
| 392 start_stretch_ = gfx::Vector2dF(); | 427 start_stretch_ = gfx::Vector2dF(); |
| 393 orig_origin_ = gfx::Vector2dF(); | 428 orig_origin_ = gfx::Vector2dF(); |
| 394 orig_velocity_ = gfx::Vector2dF(); | 429 orig_velocity_ = gfx::Vector2dF(); |
| 395 } | 430 } |
| 396 } else { | 431 } else { |
| 397 start_time_ = base::Time::Now(); | 432 start_time_ = base::Time::Now(); |
| 398 start_stretch_ = gfx::Vector2dF(); | 433 start_stretch_ = gfx::Vector2dF(); |
| 399 } | 434 } |
| 435 |
| 436 helper_->SnapRubberbandTimerFired(); |
| 400 } | 437 } |
| 401 | 438 |
| 402 bool ScrollElasticityController::IsRubberbandInProgress() const { | 439 bool InputScrollElasticityController::IsRubberbandInProgress() const { |
| 403 if (!in_scroll_gesture_ && !momentum_scroll_in_progress_ && | 440 if (!in_scroll_gesture_ && !momentum_scroll_in_progress_ && |
| 404 !snap_rubberband_timer_is_active_) | 441 !snap_rubberband_timer_is_active_) |
| 405 return false; | 442 return false; |
| 406 | 443 |
| 407 return !client_->StretchAmount().IsZero(); | 444 return !helper_->StretchAmount().IsZero(); |
| 408 } | 445 } |
| 409 | 446 |
| 410 void ScrollElasticityController::StopSnapRubberbandTimer() { | 447 void InputScrollElasticityController::StopSnapRubberbandTimer() { |
| 411 client_->StopSnapRubberbandTimer(); | 448 helper_->StopSnapRubberbandTimer(); |
| 412 snap_rubberband_timer_is_active_ = false; | 449 snap_rubberband_timer_is_active_ = false; |
| 413 } | 450 } |
| 414 | 451 |
| 415 void ScrollElasticityController::SnapRubberband() { | 452 void InputScrollElasticityController::SnapRubberband() { |
| 416 double time_delta = SystemUptime() - last_momentum_scroll_timestamp_; | 453 double time_delta = SystemUptime() - last_momentum_scroll_timestamp_; |
| 417 if (last_momentum_scroll_timestamp_ && | 454 if (last_momentum_scroll_timestamp_ && |
| 418 time_delta >= kScrollVelocityZeroingTimeout) | 455 time_delta >= kScrollVelocityZeroingTimeout) |
| 419 momentum_velocity_ = gfx::Vector2dF(); | 456 momentum_velocity_ = gfx::Vector2dF(); |
| 420 | 457 |
| 421 in_scroll_gesture_ = false; | 458 in_scroll_gesture_ = false; |
| 422 | 459 |
| 423 if (snap_rubberband_timer_is_active_) | 460 if (snap_rubberband_timer_is_active_) |
| 424 return; | 461 return; |
| 425 | 462 |
| 426 start_stretch_ = gfx::Vector2dF(); | 463 start_stretch_ = gfx::Vector2dF(); |
| 427 orig_origin_ = gfx::Vector2dF(); | 464 orig_origin_ = gfx::Vector2dF(); |
| 428 orig_velocity_ = gfx::Vector2dF(); | 465 orig_velocity_ = gfx::Vector2dF(); |
| 429 | 466 |
| 430 // If there's no momentum scroll or stretch amount, no need to start the | 467 // If there's no momentum scroll or stretch amount, no need to start the |
| 431 // timer. | 468 // timer. |
| 432 if (!momentum_scroll_in_progress_ && | 469 if (!momentum_scroll_in_progress_ && |
| 433 client_->StretchAmount() == gfx::Vector2dF()) { | 470 helper_->StretchAmount() == gfx::Vector2dF()) { |
| 434 start_time_ = base::Time(); | 471 start_time_ = base::Time(); |
| 435 stretch_scroll_force_ = gfx::Vector2dF(); | 472 stretch_scroll_force_ = gfx::Vector2dF(); |
| 436 return; | 473 return; |
| 437 } | 474 } |
| 438 | 475 |
| 439 start_time_ = base::Time::Now(); | 476 start_time_ = base::Time::Now(); |
| 440 client_->StartSnapRubberbandTimer(); | 477 helper_->StartSnapRubberbandTimer(); |
| 441 snap_rubberband_timer_is_active_ = true; | 478 snap_rubberband_timer_is_active_ = true; |
| 442 } | 479 } |
| 443 | 480 |
| 444 bool ScrollElasticityController::ShouldHandleEvent( | 481 bool InputScrollElasticityController::ShouldHandleEvent( |
| 445 const blink::WebMouseWheelEvent& wheel_event) { | 482 const blink::WebMouseWheelEvent& wheel_event) { |
| 446 // Once any scrolling has happened, all future events should be handled. | 483 // Once any scrolling has happened, all future events should be handled. |
| 447 if (has_scrolled_) | 484 if (has_scrolled_) |
| 448 return true; | 485 return true; |
| 449 | 486 |
| 450 // The event can't cause scrolling to start if its delta is 0. | 487 // The event can't cause scrolling to start if its delta is 0. |
| 451 if (wheel_event.deltaX == 0 && wheel_event.deltaY == 0) | 488 if (wheel_event.deltaX == 0 && wheel_event.deltaY == 0) |
| 452 return false; | 489 return false; |
| 453 | 490 |
| 454 // If the client isn't pinned, then the event is guaranteed to cause | 491 // If the helper isn't pinned, then the event is guaranteed to cause |
| 455 // scrolling. | 492 // scrolling. |
| 456 if (!client_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) | 493 if (!helper_->PinnedInDirection(gfx::Vector2dF(-wheel_event.deltaX, 0))) |
| 457 return true; | 494 return true; |
| 458 | 495 |
| 459 // If the event is pinned, then the client can't scroll, but it might rubber | 496 // If the event is pinned, then the helper can't scroll, but it might rubber |
| 460 // band. | 497 // band. |
| 461 // Check if the event allows rubber banding. | 498 // Check if the event allows rubber banding. |
| 462 if (wheel_event.deltaY == 0) { | 499 if (wheel_event.deltaY == 0) { |
| 463 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) | 500 if (wheel_event.deltaX > 0 && !wheel_event.canRubberbandLeft) |
| 464 return false; | 501 return false; |
| 465 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) | 502 if (wheel_event.deltaX < 0 && !wheel_event.canRubberbandRight) |
| 466 return false; | 503 return false; |
| 467 } | 504 } |
| 468 | 505 |
| 469 // The event is going to either cause scrolling or rubber banding. | 506 // The event is going to either cause scrolling or rubber banding. |
| 470 return true; | 507 return true; |
| 471 } | 508 } |
| 472 | 509 |
| 473 } // namespace content | 510 } // namespace content |
| OLD | NEW |