OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
6 | 6 |
7 import android.widget.OverScroller; | 7 import android.widget.OverScroller; |
8 | 8 |
9 import com.google.common.annotations.VisibleForTesting; | 9 import com.google.common.annotations.VisibleForTesting; |
10 | 10 |
11 import org.chromium.base.CalledByNative; | 11 import org.chromium.base.CalledByNative; |
12 | 12 |
13 /** | 13 /** |
14 * Takes care of syncing the scroll offset between the Android View system and t
he | 14 * Takes care of syncing the scroll offset between the Android View system and t
he |
15 * InProcessViewRenderer. | 15 * InProcessViewRenderer. |
16 * | 16 * |
17 * Unless otherwise values (sizes, scroll offsets) are in physical pixels. | 17 * Unless otherwise values (sizes, scroll offsets) are in physical pixels. |
18 */ | 18 */ |
19 @VisibleForTesting | 19 @VisibleForTesting |
20 public class AwScrollOffsetManager { | 20 public class AwScrollOffsetManager { |
| 21 // Values taken from WebViewClassic. |
| 22 |
| 23 // The amount of content to overlap between two screens when using pageUp/pa
geDown methiods. |
| 24 private static final int PAGE_SCROLL_OVERLAP = 24; |
| 25 // Standard animated scroll speed. |
| 26 private static final int STD_SCROLL_ANIMATION_SPEED_PIX_PER_SEC = 480; |
| 27 // Time for the longest scroll animation. |
| 28 private static final int MAX_SCROLL_ANIMATION_DURATION_MILLISEC = 750; |
| 29 |
21 // The unit of all the values in this delegate are physical pixels. | 30 // The unit of all the values in this delegate are physical pixels. |
22 public interface Delegate { | 31 public interface Delegate { |
23 // Call View#overScrollBy on the containerView. | 32 // Call View#overScrollBy on the containerView. |
24 void overScrollContainerViewBy(int deltaX, int deltaY, int scrollX, int
scrollY, | 33 void overScrollContainerViewBy(int deltaX, int deltaY, int scrollX, int
scrollY, |
25 int scrollRangeX, int scrollRangeY, boolean isTouchEvent); | 34 int scrollRangeX, int scrollRangeY, boolean isTouchEvent); |
26 // Call View#scrollTo on the containerView. | 35 // Call View#scrollTo on the containerView. |
27 void scrollContainerViewTo(int x, int y); | 36 void scrollContainerViewTo(int x, int y); |
28 // Store the scroll offset in the native side. This should really be a s
imple store | 37 // Store the scroll offset in the native side. This should really be a s
imple store |
29 // operation, the native side shouldn't synchronously alter the scroll o
ffset from within | 38 // operation, the native side shouldn't synchronously alter the scroll o
ffset from within |
30 // this call. | 39 // this call. |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 overScrollGlow.absorbGlow(x, y, oldX, oldY, rangeX, rangeY, | 292 overScrollGlow.absorbGlow(x, y, oldX, oldY, rangeX, rangeY, |
284 mScroller.getCurrVelocity()); | 293 mScroller.getCurrVelocity()); |
285 } | 294 } |
286 | 295 |
287 // The mScroller is configured not to go outside of the scrollable range
, so this call | 296 // The mScroller is configured not to go outside of the scrollable range
, so this call |
288 // should never result in attempting to scroll outside of the scrollable
region. | 297 // should never result in attempting to scroll outside of the scrollable
region. |
289 scrollBy(x - oldX, y - oldY); | 298 scrollBy(x - oldX, y - oldY); |
290 | 299 |
291 mDelegate.invalidate(); | 300 mDelegate.invalidate(); |
292 } | 301 } |
| 302 |
| 303 private static int computeDurationInMilliSec(int dx, int dy) { |
| 304 int distance = Math.max(Math.abs(dx), Math.abs(dy)); |
| 305 int duration = distance * 1000 / STD_SCROLL_ANIMATION_SPEED_PIX_PER_SEC; |
| 306 return Math.min(duration, MAX_SCROLL_ANIMATION_DURATION_MILLISEC); |
| 307 } |
| 308 |
| 309 private boolean animateScrollTo(int x, int y) { |
| 310 final int scrollX = mDelegate.getContainerViewScrollX(); |
| 311 final int scrollY = mDelegate.getContainerViewScrollY(); |
| 312 |
| 313 x = clampHorizontalScroll(x); |
| 314 y = clampVerticalScroll(y); |
| 315 |
| 316 int dx = x - scrollX; |
| 317 int dy = y - scrollY; |
| 318 |
| 319 if (dx == 0 && dy == 0) |
| 320 return false; |
| 321 |
| 322 mScroller.startScroll(scrollX, scrollY, dx, dy, computeDurationInMilliSe
c(dx, dy)); |
| 323 mDelegate.invalidate(); |
| 324 |
| 325 return true; |
| 326 } |
| 327 |
| 328 /** |
| 329 * See {@link WebView#pageUp(boolean)} |
| 330 */ |
| 331 public boolean pageUp(boolean top) { |
| 332 final int scrollX = mDelegate.getContainerViewScrollX(); |
| 333 final int scrollY = mDelegate.getContainerViewScrollY(); |
| 334 |
| 335 if (top) { |
| 336 // go to the top of the document |
| 337 return animateScrollTo(scrollX, 0); |
| 338 } |
| 339 int dy = -mContainerViewHeight / 2; |
| 340 if (mContainerViewHeight > 2 * PAGE_SCROLL_OVERLAP) { |
| 341 dy = -mContainerViewHeight + PAGE_SCROLL_OVERLAP; |
| 342 } |
| 343 // animateScrollTo clamps the argument to the scrollable range so using
(scrollY + dy) is |
| 344 // fine. |
| 345 return animateScrollTo(scrollX, scrollY + dy); |
| 346 } |
| 347 |
| 348 /** |
| 349 * See {@link WebView#pageDown(boolean)} |
| 350 */ |
| 351 public boolean pageDown(boolean bottom) { |
| 352 final int scrollX = mDelegate.getContainerViewScrollX(); |
| 353 final int scrollY = mDelegate.getContainerViewScrollY(); |
| 354 |
| 355 if (bottom) { |
| 356 return animateScrollTo(scrollX, computeVerticalScrollRange()); |
| 357 } |
| 358 int dy = mContainerViewHeight / 2; |
| 359 if (mContainerViewHeight > 2 * PAGE_SCROLL_OVERLAP) { |
| 360 dy = mContainerViewHeight - PAGE_SCROLL_OVERLAP; |
| 361 } |
| 362 // animateScrollTo clamps the argument to the scrollable range so using
(scrollY + dy) is |
| 363 // fine. |
| 364 return animateScrollTo(scrollX, scrollY + dy); |
| 365 } |
293 } | 366 } |
OLD | NEW |