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

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: Respond to comment. 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
« no previous file with comments | « ios/web_view/shell/shell_view_controller.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/MobileCoreServices.h>
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 ()<CWVUIDelegate,
19 CWVWebViewDelegate,
20 UITextFieldDelegate>
16 // Container for |webView|. 21 // Container for |webView|.
17 @property(nonatomic, strong) UIView* containerView; 22 @property(nonatomic, strong) UIView* containerView;
18 // Text field used for navigating to URLs. 23 // Text field used for navigating to URLs.
19 @property(nonatomic, strong) UITextField* field; 24 @property(nonatomic, strong) UITextField* field;
20 // Toolbar containing navigation buttons and |field|. 25 // Toolbar containing navigation buttons and |field|.
21 @property(nonatomic, strong) UIToolbar* toolbar; 26 @property(nonatomic, strong) UIToolbar* toolbar;
22 // CWV view which renders the web page. 27 // CWV view which renders the web page.
23 @property(nonatomic, strong) CWVWebView* webView; 28 @property(nonatomic, strong) CWVWebView* webView;
24 // Handles the translation of the content displayed in |webView|. 29 // Handles the translation of the content displayed in |webView|.
25 @property(nonatomic, strong) TranslateController* translateController; 30 @property(nonatomic, strong) TranslateController* translateController;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 action:@selector(stopLoading) 120 action:@selector(stopLoading)
116 forControlEvents:UIControlEventTouchUpInside]; 121 forControlEvents:UIControlEventTouchUpInside];
117 122
118 [_toolbar addSubview:back]; 123 [_toolbar addSubview:back];
119 [_toolbar addSubview:forward]; 124 [_toolbar addSubview:forward];
120 [_toolbar addSubview:stop]; 125 [_toolbar addSubview:stop];
121 [_toolbar addSubview:_field]; 126 [_toolbar addSubview:_field];
122 127
123 self.webView = [CWV webViewWithFrame:[_containerView bounds]]; 128 self.webView = [CWV webViewWithFrame:[_containerView bounds]];
124 [_webView setDelegate:self]; 129 [_webView setDelegate:self];
130 [_webView setUIDelegate:self];
125 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | 131 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |
126 UIViewAutoresizingFlexibleHeight]; 132 UIViewAutoresizingFlexibleHeight];
127 [_containerView addSubview:_webView]; 133 [_containerView addSubview:_webView];
128 134
129 NSURLRequest* request = [NSURLRequest 135 NSURLRequest* request = [NSURLRequest
130 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]]; 136 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]];
131 [_webView loadRequest:request]; 137 [_webView loadRequest:request];
132 } 138 }
133 139
134 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 140 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
(...skipping 30 matching lines...) Expand all
165 171
166 - (void)updateToolbar { 172 - (void)updateToolbar {
167 // Do not update the URL if the text field is currently being edited. 173 // Do not update the URL if the text field is currently being edited.
168 if ([_field isFirstResponder]) { 174 if ([_field isFirstResponder]) {
169 return; 175 return;
170 } 176 }
171 177
172 [_field setText:[[_webView visibleURL] absoluteString]]; 178 [_field setText:[[_webView visibleURL] absoluteString]];
173 } 179 }
174 180
181 #pragma mark CWVUIDelegate methods
182
183 - (void)webView:(CWVWebView*)webView
184 runContextMenuWithTitle:(NSString*)menuTitle
185 forHTMLElement:(CWVHTMLElement*)element
186 inView:(UIView*)view
187 userGestureLocation:(CGPoint)location {
188 if (!element.hyperlink) {
189 return;
190 }
191
192 UIAlertController* alert = [UIAlertController
193 alertControllerWithTitle:menuTitle
194 message:nil
195 preferredStyle:UIAlertControllerStyleActionSheet];
196 alert.popoverPresentationController.sourceView = view;
197 alert.popoverPresentationController.sourceRect =
198 CGRectMake(location.x, location.y, 1.0, 1.0);
199
200 void (^copyHandler)(UIAlertAction*) = ^(UIAlertAction* action) {
201 NSDictionary* item = @{
202 (NSString*)(kUTTypeURL) : element.hyperlink,
203 (NSString*)(kUTTypeUTF8PlainText) : [[element.hyperlink absoluteString]
204 dataUsingEncoding:NSUTF8StringEncoding],
205 };
206 [[UIPasteboard generalPasteboard] setItems:@[ item ]];
207 };
208 [alert addAction:[UIAlertAction actionWithTitle:@"Copy Link"
209 style:UIAlertActionStyleDefault
210 handler:copyHandler]];
211
212 [alert addAction:[UIAlertAction actionWithTitle:@"Cancel"
213 style:UIAlertActionStyleCancel
214 handler:nil]];
215
216 [self presentViewController:alert animated:YES completion:nil];
217 }
218
175 #pragma mark CWVWebViewDelegate methods 219 #pragma mark CWVWebViewDelegate methods
176 220
177 - (void)webView:(CWVWebView*)webView 221 - (void)webView:(CWVWebView*)webView
178 didFinishLoadingWithURL:(NSURL*)url 222 didFinishLoadingWithURL:(NSURL*)url
179 loadSuccess:(BOOL)loadSuccess { 223 loadSuccess:(BOOL)loadSuccess {
180 // TODO(crbug.com/679895): Add some visual indication that the page load has 224 // TODO(crbug.com/679895): Add some visual indication that the page load has
181 // finished. 225 // finished.
182 [self updateToolbar]; 226 [self updateToolbar];
183 } 227 }
184 228
(...skipping 12 matching lines...) Expand all
197 } 241 }
198 } 242 }
199 243
200 - (id<CWVTranslateDelegate>)translateDelegate { 244 - (id<CWVTranslateDelegate>)translateDelegate {
201 if (!_translateController) 245 if (!_translateController)
202 self.translateController = [[TranslateController alloc] init]; 246 self.translateController = [[TranslateController alloc] init];
203 return _translateController; 247 return _translateController;
204 } 248 }
205 249
206 @end 250 @end
OLDNEW
« no previous file with comments | « 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