Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: ios/web_view/shell/shell_view_controller.m

Issue 2723433004: Add CWVUIDelegate with method to customize context menu. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #import "ios/web_view/shell/shell_view_controller.h" 5 #import "ios/web_view/shell/shell_view_controller.h"
6 6
7 #import <MobileCoreServices/UTCoreTypes.h>
Eugene But (OOO till 7-30) 2017/02/27 16:34:07 <MobileCoreServices/MobileCoreServices.h>
michaeldo 2017/02/27 19:02:06 Done.
8
7 #import "ios/web_view/public/cwv.h" 9 #import "ios/web_view/public/cwv.h"
10 #import "ios/web_view/public/cwv_html_element.h"
8 #import "ios/web_view/public/cwv_web_view.h" 11 #import "ios/web_view/public/cwv_web_view.h"
9 #import "ios/web_view/shell/translate_controller.h" 12 #import "ios/web_view/shell/translate_controller.h"
10 13
11 #if !defined(__has_feature) || !__has_feature(objc_arc) 14 #if !defined(__has_feature) || !__has_feature(objc_arc)
12 #error "This file requires ARC support." 15 #error "This file requires ARC support."
13 #endif 16 #endif
14 17
15 @interface ShellViewController () 18 @interface ShellViewController ()
16 // Container for |webView|. 19 // Container for |webView|.
17 @property(nonatomic, strong) UIView* containerView; 20 @property(nonatomic, strong) UIView* containerView;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 action:@selector(stopLoading) 118 action:@selector(stopLoading)
116 forControlEvents:UIControlEventTouchUpInside]; 119 forControlEvents:UIControlEventTouchUpInside];
117 120
118 [_toolbar addSubview:back]; 121 [_toolbar addSubview:back];
119 [_toolbar addSubview:forward]; 122 [_toolbar addSubview:forward];
120 [_toolbar addSubview:stop]; 123 [_toolbar addSubview:stop];
121 [_toolbar addSubview:_field]; 124 [_toolbar addSubview:_field];
122 125
123 self.webView = [CWV webViewWithFrame:[_containerView bounds]]; 126 self.webView = [CWV webViewWithFrame:[_containerView bounds]];
124 [_webView setDelegate:self]; 127 [_webView setDelegate:self];
128 [_webView setUIDelegate:self];
125 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | 129 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |
126 UIViewAutoresizingFlexibleHeight]; 130 UIViewAutoresizingFlexibleHeight];
127 [_containerView addSubview:_webView]; 131 [_containerView addSubview:_webView];
128 132
129 NSURLRequest* request = [NSURLRequest 133 NSURLRequest* request = [NSURLRequest
130 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]]; 134 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]];
131 [_webView loadRequest:request]; 135 [_webView loadRequest:request];
132 } 136 }
133 137
134 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 138 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
(...skipping 30 matching lines...) Expand all
165 169
166 - (void)updateToolbar { 170 - (void)updateToolbar {
167 // Do not update the URL if the text field is currently being edited. 171 // Do not update the URL if the text field is currently being edited.
168 if ([_field isFirstResponder]) { 172 if ([_field isFirstResponder]) {
169 return; 173 return;
170 } 174 }
171 175
172 [_field setText:[[_webView visibleURL] absoluteString]]; 176 [_field setText:[[_webView visibleURL] absoluteString]];
173 } 177 }
174 178
179 #pragma mark CWVUIDelegate methods
180
181 - (void)webView:(CWVWebView*)webView
182 runContextMenuWithTitle:(NSString*)menuTitle
183 forHTMLElement:(CWVHTMLElement*)element
184 inView:(UIView*)view
185 userGestureLocation:(CGPoint)location {
186 if (element.hyperlink == nil) {
187 return;
188 }
189
190 UIAlertController* alert = [UIAlertController
191 alertControllerWithTitle:menuTitle
192 message:nil
193 preferredStyle:UIAlertControllerStyleActionSheet];
194 alert.popoverPresentationController.sourceView = view;
195 alert.popoverPresentationController.sourceRect =
196 CGRectMake(location.x, location.y, 1.0, 1.0);
197
198 void (^handler)(UIAlertAction*) = ^(UIAlertAction* action) {
Eugene But (OOO till 7-30) 2017/02/27 16:34:07 nit: s/handler/copyHandler ?
michaeldo 2017/02/27 19:02:06 Done.
199 NSDictionary* item = @{
200 (NSString*)(kUTTypeURL) : element.hyperlink,
201 (NSString*)(kUTTypeUTF8PlainText) : [[element.hyperlink absoluteString]
202 dataUsingEncoding:NSUTF8StringEncoding],
203 };
204 [[UIPasteboard generalPasteboard] setItems:@[ item ]];
205 };
206 [alert addAction:[UIAlertAction actionWithTitle:@"Copy Link"
207 style:UIAlertActionStyleDefault
208 handler:handler]];
209
210 [alert addAction:[UIAlertAction actionWithTitle:@"Cancel"
211 style:UIAlertActionStyleCancel
212 handler:nil]];
213
214 [self presentViewController:alert animated:YES completion:nil];
215 }
216
175 #pragma mark CWVWebViewDelegate methods 217 #pragma mark CWVWebViewDelegate methods
176 218
177 - (void)webView:(CWVWebView*)webView 219 - (void)webView:(CWVWebView*)webView
178 didFinishLoadingWithURL:(NSURL*)url 220 didFinishLoadingWithURL:(NSURL*)url
179 loadSuccess:(BOOL)loadSuccess { 221 loadSuccess:(BOOL)loadSuccess {
180 // TODO(crbug.com/679895): Add some visual indication that the page load has 222 // TODO(crbug.com/679895): Add some visual indication that the page load has
181 // finished. 223 // finished.
182 [self updateToolbar]; 224 [self updateToolbar];
183 } 225 }
184 226
(...skipping 12 matching lines...) Expand all
197 } 239 }
198 } 240 }
199 241
200 - (id<CWVTranslateDelegate>)translateDelegate { 242 - (id<CWVTranslateDelegate>)translateDelegate {
201 if (!_translateController) 243 if (!_translateController)
202 self.translateController = [[TranslateController alloc] init]; 244 self.translateController = [[TranslateController alloc] init];
203 return _translateController; 245 return _translateController;
204 } 246 }
205 247
206 @end 248 @end
OLDNEW
« ios/web_view/shell/shell_view_controller.h ('K') | « ios/web_view/shell/shell_view_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698