| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web_view/shell/shell_view_controller.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #import "ios/web_view/public/criwv.h" |
| 10 #import "ios/web_view/public/criwv_web_view.h" |
| 11 #import "ios/web_view/shell/translate_controller.h" |
| 12 |
| 13 namespace { |
| 14 const CGFloat kButtonSize = 44; |
| 15 } |
| 16 |
| 17 @interface ShellViewController () { |
| 18 base::scoped_nsobject<UITextField> _field; |
| 19 base::scoped_nsprotocol<id<CRIWVWebView>> _webView; |
| 20 base::scoped_nsobject<TranslateController> _translateController; |
| 21 } |
| 22 - (void)back; |
| 23 - (void)forward; |
| 24 - (void)stopLoading; |
| 25 @end |
| 26 |
| 27 @implementation ShellViewController |
| 28 |
| 29 @synthesize containerView = _containerView; |
| 30 @synthesize toolbarView = _toolbarView; |
| 31 |
| 32 - (instancetype)init { |
| 33 self = [super initWithNibName:@"MainView" bundle:nil]; |
| 34 return self; |
| 35 } |
| 36 |
| 37 - (void)viewDidLoad { |
| 38 [super viewDidLoad]; |
| 39 |
| 40 // Set up the toolbar buttons. |
| 41 // Back. |
| 42 UIButton* back = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 43 [back setImage:[UIImage imageNamed:@"toolbar_back"] |
| 44 forState:UIControlStateNormal]; |
| 45 [back setFrame:CGRectMake(0, 0, kButtonSize, kButtonSize)]; |
| 46 UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 4, 4); |
| 47 [back setImageEdgeInsets:insets]; |
| 48 [back setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; |
| 49 [back addTarget:self |
| 50 action:@selector(back) |
| 51 forControlEvents:UIControlEventTouchUpInside]; |
| 52 |
| 53 // Forward. |
| 54 UIButton* forward = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 55 [forward setImage:[UIImage imageNamed:@"toolbar_forward"] |
| 56 forState:UIControlStateNormal]; |
| 57 [forward setFrame:CGRectMake(kButtonSize, 0, kButtonSize, kButtonSize)]; |
| 58 [forward setImageEdgeInsets:insets]; |
| 59 [forward setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; |
| 60 [forward addTarget:self |
| 61 action:@selector(forward) |
| 62 forControlEvents:UIControlEventTouchUpInside]; |
| 63 |
| 64 // Stop. |
| 65 UIButton* stop = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 66 [stop setImage:[UIImage imageNamed:@"toolbar_stop"] |
| 67 forState:UIControlStateNormal]; |
| 68 [stop setFrame:CGRectMake(2 * kButtonSize, 0, kButtonSize, kButtonSize)]; |
| 69 [stop setImageEdgeInsets:insets]; |
| 70 [stop setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; |
| 71 [stop addTarget:self |
| 72 action:@selector(stopLoading) |
| 73 forControlEvents:UIControlEventTouchUpInside]; |
| 74 |
| 75 // Text field. |
| 76 const int kButtonCount = 3; |
| 77 _field.reset([[UITextField alloc] |
| 78 initWithFrame:CGRectMake(kButtonCount * kButtonSize, 6, |
| 79 CGRectGetWidth([_toolbarView frame]) - |
| 80 kButtonCount * kButtonSize - 10, |
| 81 31)]); |
| 82 [_field setDelegate:self]; |
| 83 [_field setBackground:[[UIImage imageNamed:@"textfield_background"] |
| 84 resizableImageWithCapInsets:UIEdgeInsetsMake( |
| 85 12, 12, 12, 12)]]; |
| 86 [_field setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 87 [_field setKeyboardType:UIKeyboardTypeWebSearch]; |
| 88 [_field setAutocorrectionType:UITextAutocorrectionTypeNo]; |
| 89 [_field setClearButtonMode:UITextFieldViewModeWhileEditing]; |
| 90 |
| 91 [_toolbarView addSubview:back]; |
| 92 [_toolbarView addSubview:forward]; |
| 93 [_toolbarView addSubview:stop]; |
| 94 [_toolbarView addSubview:_field]; |
| 95 |
| 96 _webView.reset([[CRIWV webView] retain]); |
| 97 [_webView setDelegate:self]; |
| 98 [_containerView addSubview:[_webView view]]; |
| 99 [_webView loadURL:[NSURL URLWithString:@"https://www.google.com/"]]; |
| 100 } |
| 101 |
| 102 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { |
| 103 if (bar == _toolbarView) { |
| 104 return UIBarPositionTopAttached; |
| 105 } |
| 106 return UIBarPositionAny; |
| 107 } |
| 108 |
| 109 - (void)back { |
| 110 if ([_webView canGoBack]) { |
| 111 [_webView goBack]; |
| 112 } |
| 113 } |
| 114 |
| 115 - (void)forward { |
| 116 if ([_webView canGoForward]) { |
| 117 [_webView goForward]; |
| 118 } |
| 119 } |
| 120 |
| 121 - (void)stopLoading { |
| 122 [_webView stopLoading]; |
| 123 } |
| 124 |
| 125 - (BOOL)textFieldShouldReturn:(UITextField*)field { |
| 126 [_webView loadURL:[NSURL URLWithString:[field text]]]; |
| 127 [field resignFirstResponder]; |
| 128 [self updateToolbar]; |
| 129 return YES; |
| 130 } |
| 131 |
| 132 - (void)updateToolbar { |
| 133 // Do not update the URL if the text field is currently being edited. |
| 134 if ([_field isFirstResponder]) { |
| 135 return; |
| 136 } |
| 137 |
| 138 [_field setText:[[_webView visibleURL] absoluteString]]; |
| 139 } |
| 140 |
| 141 #pragma mark CRIWVWebViewDelegate methods |
| 142 |
| 143 - (void)webView:(id<CRIWVWebView>)webView |
| 144 didFinishLoadingWithURL:(NSURL*)url |
| 145 loadSuccess:(BOOL)loadSuccess { |
| 146 // TODO(crbug.com/679895): Add some visual indication that the page load has |
| 147 // finished. |
| 148 [self updateToolbar]; |
| 149 } |
| 150 |
| 151 - (void)webView:(id<CRIWVWebView>)webView |
| 152 didUpdateWithChanges:(CRIWVWebViewUpdateType)changes { |
| 153 if (changes & CRIWVWebViewUpdateTypeProgress) { |
| 154 // TODO(crbug.com/679895): Add a progress indicator. |
| 155 } |
| 156 |
| 157 if (changes & CRIWVWebViewUpdateTypeTitle) { |
| 158 // TODO(crbug.com/679895): Add a title display. |
| 159 } |
| 160 |
| 161 if (changes & CRIWVWebViewUpdateTypeURL) { |
| 162 [self updateToolbar]; |
| 163 } |
| 164 } |
| 165 |
| 166 - (id<CRIWVTranslateDelegate>)translateDelegate { |
| 167 if (!_translateController) |
| 168 _translateController.reset([[TranslateController alloc] init]); |
| 169 return _translateController; |
| 170 } |
| 171 |
| 172 @end |
| OLD | NEW |