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 "ios/web_view/public/criwv.h" | |
8 #import "ios/web_view/public/criwv_web_view.h" | |
9 #import "ios/web_view/shell/translate_controller.h" | |
10 | |
11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
12 #error "This file requires ARC support." | |
13 #endif | |
14 | |
15 namespace { | |
16 const CGFloat kButtonSize = 44; | |
17 } | |
18 | |
19 @interface ShellViewController () | |
20 // Container for |webView|. | |
21 @property (nonatomic, strong) UIView* containerView; | |
22 // Text field used for navigating to URLs. | |
23 @property (nonatomic, strong) UITextField* field; | |
24 // Toolbar containing navigation buttons and |field|. | |
25 @property (nonatomic, strong) UIToolbar* toolbar; | |
26 // CRIWV view which renders the web page. | |
27 @property (nonatomic, strong) id<CRIWVWebView> webView; | |
28 // Handles the translation of the content displayed in |webView|. | |
29 @property (nonatomic, strong) TranslateController* translateController; | |
30 | |
31 - (void)back; | |
32 - (void)forward; | |
33 - (void)stopLoading; | |
34 @end | |
35 | |
36 @implementation ShellViewController | |
37 | |
38 @synthesize containerView = _containerView; | |
39 @synthesize field = _field; | |
40 @synthesize toolbar = _toolbar; | |
41 @synthesize webView = _webView; | |
42 @synthesize translateController = _translateController; | |
43 | |
44 - (void)viewDidLoad { | |
45 [super viewDidLoad]; | |
46 | |
47 CGRect bounds = self.view.bounds; | |
48 | |
49 // Set up the toolbar. | |
50 self.toolbar = [[UIToolbar alloc] init]; | |
51 [_toolbar setBarTintColor:[UIColor colorWithRed:0.337 | |
52 green:0.467 | |
53 blue:0.988 | |
54 alpha:1.0]]; | |
55 [_toolbar setFrame:CGRectMake(0, 20, CGRectGetWidth(bounds), 44)]; | |
56 [_toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | |
57 UIViewAutoresizingFlexibleBottomMargin]; | |
58 [self.view addSubview:_toolbar]; | |
59 | |
60 // Set up the container view. | |
61 self.containerView = [[UIView alloc] init]; | |
62 [_containerView setFrame:CGRectMake(0, 64, CGRectGetWidth(bounds), | |
63 CGRectGetHeight(bounds) - 64)]; | |
64 [_containerView setBackgroundColor:[UIColor lightGrayColor]]; | |
65 [_containerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | |
66 UIViewAutoresizingFlexibleHeight]; | |
67 [self.view addSubview:_containerView]; | |
68 | |
69 // Text field. | |
70 const int kButtonCount = 3; | |
71 self.field = [[UITextField alloc] | |
72 initWithFrame:CGRectMake(kButtonCount * kButtonSize, 6, | |
73 CGRectGetWidth([_toolbar frame]) - | |
74 kButtonCount * kButtonSize - 10, | |
75 31)]; | |
76 [_field setDelegate:self]; | |
77 [_field setBackground:[[UIImage imageNamed:@"textfield_background"] | |
78 resizableImageWithCapInsets:UIEdgeInsetsMake( | |
79 12, 12, 12, 12)]]; | |
80 [_field setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | |
81 [_field setKeyboardType:UIKeyboardTypeWebSearch]; | |
82 [_field setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
83 [_field setClearButtonMode:UITextFieldViewModeWhileEditing]; | |
84 | |
85 // Set up the toolbar buttons. | |
86 // Back. | |
87 UIButton* back = [UIButton buttonWithType:UIButtonTypeCustom]; | |
88 [back setImage:[UIImage imageNamed:@"toolbar_back"] | |
89 forState:UIControlStateNormal]; | |
90 [back setFrame:CGRectMake(0, 0, kButtonSize, kButtonSize)]; | |
91 UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 4, 4); | |
92 [back setImageEdgeInsets:insets]; | |
93 [back setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; | |
94 [back addTarget:self | |
95 action:@selector(back) | |
96 forControlEvents:UIControlEventTouchUpInside]; | |
97 | |
98 // Forward. | |
99 UIButton* forward = [UIButton buttonWithType:UIButtonTypeCustom]; | |
100 [forward setImage:[UIImage imageNamed:@"toolbar_forward"] | |
101 forState:UIControlStateNormal]; | |
102 [forward setFrame:CGRectMake(kButtonSize, 0, kButtonSize, kButtonSize)]; | |
103 [forward setImageEdgeInsets:insets]; | |
104 [forward setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; | |
105 [forward addTarget:self | |
106 action:@selector(forward) | |
107 forControlEvents:UIControlEventTouchUpInside]; | |
108 | |
109 // Stop. | |
110 UIButton* stop = [UIButton buttonWithType:UIButtonTypeCustom]; | |
111 [stop setImage:[UIImage imageNamed:@"toolbar_stop"] | |
112 forState:UIControlStateNormal]; | |
113 [stop setFrame:CGRectMake(2 * kButtonSize, 0, kButtonSize, kButtonSize)]; | |
114 [stop setImageEdgeInsets:insets]; | |
115 [stop setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; | |
116 [stop addTarget:self | |
117 action:@selector(stopLoading) | |
118 forControlEvents:UIControlEventTouchUpInside]; | |
119 | |
120 [_toolbar addSubview:back]; | |
121 [_toolbar addSubview:forward]; | |
122 [_toolbar addSubview:stop]; | |
123 [_toolbar addSubview:_field]; | |
124 | |
125 self.webView = [CRIWV webView]; | |
126 [_webView setDelegate:self]; | |
127 UIView* view = [_webView view]; | |
128 [_containerView addSubview:view]; | |
129 [view setFrame:[_containerView bounds]]; | |
130 [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | |
131 UIViewAutoresizingFlexibleHeight]; | |
132 | |
133 [_webView loadURL:[NSURL URLWithString:@"https://www.google.com/"]]; | |
134 } | |
135 | |
136 - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { | |
137 if (bar == _toolbar) { | |
138 return UIBarPositionTopAttached; | |
139 } | |
140 return UIBarPositionAny; | |
141 } | |
142 | |
143 - (void)back { | |
144 if ([_webView canGoBack]) { | |
145 [_webView goBack]; | |
146 } | |
147 } | |
148 | |
149 - (void)forward { | |
150 if ([_webView canGoForward]) { | |
151 [_webView goForward]; | |
152 } | |
153 } | |
154 | |
155 - (void)stopLoading { | |
156 [_webView stopLoading]; | |
157 } | |
158 | |
159 - (BOOL)textFieldShouldReturn:(UITextField*)field { | |
160 [_webView loadURL:[NSURL URLWithString:[field text]]]; | |
161 [field resignFirstResponder]; | |
162 [self updateToolbar]; | |
163 return YES; | |
164 } | |
165 | |
166 - (void)updateToolbar { | |
167 // Do not update the URL if the text field is currently being edited. | |
168 if ([_field isFirstResponder]) { | |
169 return; | |
170 } | |
171 | |
172 [_field setText:[[_webView visibleURL] absoluteString]]; | |
173 } | |
174 | |
175 #pragma mark CRIWVWebViewDelegate methods | |
176 | |
177 - (void)webView:(id<CRIWVWebView>)webView | |
178 didFinishLoadingWithURL:(NSURL*)url | |
179 loadSuccess:(BOOL)loadSuccess { | |
180 // TODO(crbug.com/679895): Add some visual indication that the page load has | |
181 // finished. | |
182 [self updateToolbar]; | |
183 } | |
184 | |
185 - (void)webView:(id<CRIWVWebView>)webView | |
186 didUpdateWithChanges:(CRIWVWebViewUpdateType)changes { | |
187 if (changes & CRIWVWebViewUpdateTypeProgress) { | |
188 // TODO(crbug.com/679895): Add a progress indicator. | |
189 } | |
190 | |
191 if (changes & CRIWVWebViewUpdateTypeTitle) { | |
192 // TODO(crbug.com/679895): Add a title display. | |
193 } | |
194 | |
195 if (changes & CRIWVWebViewUpdateTypeURL) { | |
196 [self updateToolbar]; | |
197 } | |
198 } | |
199 | |
200 - (id<CRIWVTranslateDelegate>)translateDelegate { | |
201 if (!_translateController) | |
202 self.translateController = [[TranslateController alloc] init]; | |
203 return _translateController; | |
204 } | |
205 | |
206 @end | |
OLD | NEW |