Commit 80fc6613 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Chromium LUCI CQ

[iOS] Prevent default ContextMenu preview in WebView

This CL prevents the default preview when the user is long pressing on
the WebView and the system ContextMenu UI is used (instead of the
action sheet).

Bug: 1140387
Change-Id: Ie3449e3d4f185f9bf4506601882e0a7204c3cdc9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2584962
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarMike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842014}
parent 9597ec50
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#import "ios/web/web_state/ui/crw_context_menu_controller.h" #import "ios/web/web_state/ui/crw_context_menu_controller.h"
#import "ios/web/public/ui/context_menu_params.h" #import "ios/web/public/ui/context_menu_params.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_delegate.h"
#import "ios/web/web_state/context_menu_params_utils.h"
#import "ios/web/web_state/ui/crw_context_menu_element_fetcher.h" #import "ios/web/web_state/ui/crw_context_menu_element_fetcher.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -22,6 +25,12 @@ const CGFloat kJavaScriptTimeout = 1; ...@@ -22,6 +25,12 @@ const CGFloat kJavaScriptTimeout = 1;
// The context menu responsible for the interaction. // The context menu responsible for the interaction.
@property(nonatomic, strong) UIContextMenuInteraction* contextMenu; @property(nonatomic, strong) UIContextMenuInteraction* contextMenu;
// Views used to do the highlight/dismiss animation. Those view are empty and
// are used to override the default animation which is to focus the whole
// WebView (as the interaction is used on the whole WebView).
@property(nonatomic, strong, readonly) UIView* highlightView;
@property(nonatomic, strong, readonly) UIView* dismissView;
@property(nonatomic, strong) WKWebView* webView; @property(nonatomic, strong) WKWebView* webView;
@property(nonatomic, assign) web::WebState* webState; @property(nonatomic, assign) web::WebState* webState;
...@@ -32,6 +41,9 @@ const CGFloat kJavaScriptTimeout = 1; ...@@ -32,6 +41,9 @@ const CGFloat kJavaScriptTimeout = 1;
@implementation CRWContextMenuController @implementation CRWContextMenuController
@synthesize highlightView = _highlightView;
@synthesize dismissView = _dismissView;
- (instancetype)initWithWebView:(WKWebView*)webView - (instancetype)initWithWebView:(WKWebView*)webView
webState:(web::WebState*)webState { webState:(web::WebState*)webState {
self = [super init]; self = [super init];
...@@ -50,6 +62,28 @@ const CGFloat kJavaScriptTimeout = 1; ...@@ -50,6 +62,28 @@ const CGFloat kJavaScriptTimeout = 1;
return self; return self;
} }
#pragma mark - Property
- (UIView*)highlightView {
if (!_highlightView) {
// If the views have a CGRectZero size, it is not taken into account.
CGRect rectSizedOne = CGRectMake(0, 0, 1, 1);
_highlightView = [[UIView alloc] initWithFrame:rectSizedOne];
_highlightView.backgroundColor = UIColor.clearColor;
}
return _highlightView;
}
- (UIView*)dismissView {
if (!_dismissView) {
// If the views have a CGRectZero size, it is not taken into account.
CGRect rectSizedOne = CGRectMake(0, 0, 1, 1);
_dismissView = [[UIView alloc] initWithFrame:rectSizedOne];
_dismissView.backgroundColor = UIColor.clearColor;
}
return _dismissView;
}
#pragma mark - UIContextMenuInteractionDelegate #pragma mark - UIContextMenuInteractionDelegate
- (UIContextMenuConfiguration*)contextMenuInteraction: - (UIContextMenuConfiguration*)contextMenuInteraction:
...@@ -105,12 +139,38 @@ const CGFloat kJavaScriptTimeout = 1; ...@@ -105,12 +139,38 @@ const CGFloat kJavaScriptTimeout = 1;
isRunLoopComplete = YES; isRunLoopComplete = YES;
if (!web::CanShowContextMenuForParams(self.params))
return nil;
// Adding the highlight/dismiss view here so they can be used in the
// delegate's methods.
[interaction.view addSubview:self.highlightView];
[interaction.view addSubview:self.dismissView];
self.params.location = [self.webView convertPoint:location self.params.location = [self.webView convertPoint:location
fromView:interaction.view]; fromView:interaction.view];
// TODO(crbug.com/1140387): Present the context menu with the params. __block UIContextMenuConfiguration* configuration;
self.webState->GetDelegate()->ContextMenuConfiguration(
self.webState, self.params, ^(UIContextMenuConfiguration* conf) {
configuration = conf;
});
return configuration;
}
- (UITargetedPreview*)contextMenuInteraction:
(UIContextMenuInteraction*)interaction
previewForHighlightingMenuWithConfiguration:
(UIContextMenuConfiguration*)configuration {
return [[UITargetedPreview alloc] initWithView:self.highlightView];
}
return nil; - (UITargetedPreview*)contextMenuInteraction:
(UIContextMenuInteraction*)interaction
previewForDismissingMenuWithConfiguration:
(UIContextMenuConfiguration*)configuration {
return [[UITargetedPreview alloc] initWithView:self.dismissView];
} }
@end @end
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