Commit 87c443e5 authored by justincohen's avatar justincohen Committed by Commit bot

[Find in Page] Prevent scrolling past the page edge.

Sometimes the find in page js returns a text position that is too far down.
Correct all |points| sent to -setContentOffset for overscroll.

BUG=472091

Review URL: https://codereview.chromium.org/1071723002

Cr-Commit-Position: refs/heads/master@{#330518}
parent d2fb08c9
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#import "ios/chrome/browser/find_in_page/find_in_page_controller.h" #import "ios/chrome/browser/find_in_page/find_in_page_controller.h"
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <cmath>
#include "base/ios/ios_util.h" #include "base/ios/ios_util.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -58,6 +59,9 @@ const NSTimeInterval kRecurringPumpDelay = .01; ...@@ -58,6 +59,9 @@ const NSTimeInterval kRecurringPumpDelay = .01;
- (void)processPumpResult:(BOOL)finished - (void)processPumpResult:(BOOL)finished
scrollPoint:(CGPoint)scrollPoint scrollPoint:(CGPoint)scrollPoint
completionHandler:(ProceduralBlock)completionHandler; completionHandler:(ProceduralBlock)completionHandler;
// Prevent scrolling past the end of the page.
- (CGPoint)limitOverscroll:(CRWWebViewScrollViewProxy*)scrollViewProxy
atPoint:(CGPoint)point;
// Returns the associated web state. May be null. // Returns the associated web state. May be null.
- (web::WebState*)webState; - (web::WebState*)webState;
@end @end
...@@ -133,11 +137,24 @@ const NSTimeInterval kRecurringPumpDelay = .01; ...@@ -133,11 +137,24 @@ const NSTimeInterval kRecurringPumpDelay = .01;
return [_webViewProxy scrollViewProxy]; return [_webViewProxy scrollViewProxy];
} }
- (CGPoint)limitOverscroll:(CRWWebViewScrollViewProxy*)scrollViewProxy
atPoint:(CGPoint)point {
CGFloat contentHeight = scrollViewProxy.contentSize.height;
CGFloat frameHeight = scrollViewProxy.frame.size.height;
CGFloat maxScroll = std::max<CGFloat>(0, contentHeight - frameHeight);
if (point.y > maxScroll) {
point.y = maxScroll;
}
return point;
}
- (void)processPumpResult:(BOOL)finished - (void)processPumpResult:(BOOL)finished
scrollPoint:(CGPoint)scrollPoint scrollPoint:(CGPoint)scrollPoint
completionHandler:(ProceduralBlock)completionHandler { completionHandler:(ProceduralBlock)completionHandler {
if (finished) { if (finished) {
[_delegate willAdjustScrollPosition]; [_delegate willAdjustScrollPosition];
scrollPoint = [self limitOverscroll:[_webViewProxy scrollViewProxy]
atPoint:scrollPoint];
[[_webViewProxy scrollViewProxy] setContentOffset:scrollPoint animated:YES]; [[_webViewProxy scrollViewProxy] setContentOffset:scrollPoint animated:YES];
if (completionHandler) if (completionHandler)
completionHandler(); completionHandler();
...@@ -199,6 +216,8 @@ const NSTimeInterval kRecurringPumpDelay = .01; ...@@ -199,6 +216,8 @@ const NSTimeInterval kRecurringPumpDelay = .01;
base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
if (finished) { if (finished) {
[[strongSelf delegate] willAdjustScrollPosition]; [[strongSelf delegate] willAdjustScrollPosition];
point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
atPoint:point];
[[strongSelf webViewScrollView] setContentOffset:point animated:YES]; [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
} }
completionHandler(finished); completionHandler(finished);
...@@ -212,12 +231,8 @@ const NSTimeInterval kRecurringPumpDelay = .01; ...@@ -212,12 +231,8 @@ const NSTimeInterval kRecurringPumpDelay = .01;
[_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) { [_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) {
base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
[[strongSelf delegate] willAdjustScrollPosition]; [[strongSelf delegate] willAdjustScrollPosition];
CGFloat contentHeight = [strongSelf webViewScrollView].contentSize.height; point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
CGFloat frameHeight = [strongSelf webViewScrollView].frame.size.height; atPoint:point];
CGFloat maxScroll = fmax(0, contentHeight - frameHeight);
if (point.y > maxScroll) {
point.y = maxScroll;
}
[[strongSelf webViewScrollView] setContentOffset:point animated:YES]; [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
if (completionHandler) if (completionHandler)
completionHandler(); completionHandler();
...@@ -232,6 +247,8 @@ const NSTimeInterval kRecurringPumpDelay = .01; ...@@ -232,6 +247,8 @@ const NSTimeInterval kRecurringPumpDelay = .01;
[_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) { [_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) {
base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
[[strongSelf delegate] willAdjustScrollPosition]; [[strongSelf delegate] willAdjustScrollPosition];
point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
atPoint:point];
[[strongSelf webViewScrollView] setContentOffset:point animated:YES]; [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
if (completionHandler) if (completionHandler)
completionHandler(); completionHandler();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment