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

Side by Side Diff: ios/chrome/browser/ui/omnibox/location_bar_controller_impl.mm

Issue 2796793003: [ios] Adds a LocationBarController interface. (Closed)
Patch Set: Review. Created 3 years, 8 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "ios/chrome/browser/ui/omnibox/location_bar_controller_impl.h"
6
5 #import <UIKit/UIKit.h> 7 #import <UIKit/UIKit.h>
6 8
7 #include "base/command_line.h" 9 #include "base/command_line.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h"
10 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
11 #include "components/omnibox/browser/omnibox_edit_model.h" 14 #include "components/omnibox/browser/omnibox_edit_model.h"
12 #include "components/security_state/core/security_state_ui.h" 15 #include "components/security_state/core/security_state_ui.h"
13 #include "components/strings/grit/components_strings.h" 16 #include "components/strings/grit/components_strings.h"
14 #include "components/toolbar/toolbar_model.h" 17 #include "components/toolbar/toolbar_model.h"
15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 18 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
16 #include "ios/chrome/browser/chrome_url_constants.h" 19 #include "ios/chrome/browser/chrome_url_constants.h"
17 #include "ios/chrome/browser/experimental_flags.h" 20 #include "ios/chrome/browser/experimental_flags.h"
18 #include "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" 21 #include "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
19 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" 22 #include "ios/chrome/browser/ui/commands/ios_command_ids.h"
20 #include "ios/chrome/browser/ui/omnibox/location_bar_view_ios.h"
21 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" 23 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h"
22 #include "ios/chrome/browser/ui/omnibox/omnibox_view_ios.h" 24 #include "ios/chrome/browser/ui/omnibox/omnibox_view_ios.h"
23 #include "ios/chrome/browser/ui/ui_util.h" 25 #include "ios/chrome/browser/ui/ui_util.h"
24 #import "ios/chrome/browser/ui/uikit_ui_util.h" 26 #import "ios/chrome/browser/ui/uikit_ui_util.h"
25 #include "ios/chrome/grit/ios_strings.h" 27 #include "ios/chrome/grit/ios_strings.h"
26 #include "ios/chrome/grit/ios_theme_resources.h" 28 #include "ios/chrome/grit/ios_theme_resources.h"
29 #include "ios/shared/chrome/browser/ui/omnibox/location_bar_delegate.h"
27 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h" 30 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
28 #include "ios/web/public/navigation_item.h" 31 #include "ios/web/public/navigation_item.h"
29 #include "ios/web/public/navigation_manager.h" 32 #include "ios/web/public/navigation_manager.h"
30 #include "ios/web/public/ssl_status.h" 33 #include "ios/web/public/ssl_status.h"
31 #include "ios/web/public/web_state/web_state.h" 34 #include "ios/web/public/web_state/web_state.h"
32 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
33 36
34 #if !defined(__has_feature) || !__has_feature(objc_arc) 37 #if !defined(__has_feature) || !__has_feature(objc_arc)
35 #error "This file requires ARC support." 38 #error "This file requires ARC support."
36 #endif 39 #endif
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 NOTREACHED(); 106 NOTREACHED();
104 return nil; 107 return nil;
105 } 108 }
106 109
107 - (void)clearText { 110 - (void)clearText {
108 _omniboxView->ClearText(); 111 _omniboxView->ClearText();
109 } 112 }
110 113
111 @end 114 @end
112 115
113 LocationBarViewIOS::LocationBarViewIOS(OmniboxTextFieldIOS* field, 116 LocationBarControllerImpl::LocationBarControllerImpl(
114 ios::ChromeBrowserState* browser_state, 117 OmniboxTextFieldIOS* field,
115 id<PreloadProvider> preloader, 118 ios::ChromeBrowserState* browser_state,
116 id<OmniboxPopupPositioner> positioner, 119 id<PreloadProvider> preloader,
117 id<LocationBarDelegate> delegate) 120 id<OmniboxPopupPositioner> positioner,
118 : edit_view_(new OmniboxViewIOS(field, 121 id<LocationBarDelegate> delegate)
119 this, 122 : edit_view_(base::MakeUnique<OmniboxViewIOS>(field,
120 browser_state, 123 this,
121 preloader, 124 browser_state,
122 positioner)), 125 preloader,
126 positioner)),
123 field_(field), 127 field_(field),
124 delegate_(delegate) { 128 delegate_(delegate) {
125 DCHECK([delegate_ toolbarModel]); 129 DCHECK([delegate_ toolbarModel]);
126 show_hint_text_ = true; 130 show_hint_text_ = true;
127 131
128 InstallLocationIcon(); 132 InstallLocationIcon();
129 CreateClearTextIcon(browser_state->IsOffTheRecord()); 133 CreateClearTextIcon(browser_state->IsOffTheRecord());
130 } 134 }
131 135
132 LocationBarViewIOS::~LocationBarViewIOS() {} 136 LocationBarControllerImpl::~LocationBarControllerImpl() {}
133 137
134 void LocationBarViewIOS::HideKeyboardAndEndEditing() { 138 void LocationBarControllerImpl::HideKeyboardAndEndEditing() {
135 edit_view_->HideKeyboardAndEndEditing(); 139 edit_view_->HideKeyboardAndEndEditing();
136 } 140 }
137 141
138 void LocationBarViewIOS::SetShouldShowHintText(bool show_hint_text) { 142 void LocationBarControllerImpl::SetShouldShowHintText(bool show_hint_text) {
139 show_hint_text_ = show_hint_text; 143 show_hint_text_ = show_hint_text;
140 } 144 }
141 145
142 const OmniboxView* LocationBarViewIOS::GetLocationEntry() const { 146 const OmniboxView* LocationBarControllerImpl::GetLocationEntry() const {
143 return edit_view_.get(); 147 return edit_view_.get();
144 } 148 }
145 149
146 OmniboxView* LocationBarViewIOS::GetLocationEntry() { 150 OmniboxView* LocationBarControllerImpl::GetLocationEntry() {
147 return edit_view_.get(); 151 return edit_view_.get();
148 } 152 }
149 153
150 void LocationBarViewIOS::OnToolbarUpdated() { 154 void LocationBarControllerImpl::OnToolbarUpdated() {
151 edit_view_->UpdateAppearance(); 155 edit_view_->UpdateAppearance();
152 OnChanged(); 156 OnChanged();
153 } 157 }
154 158
155 void LocationBarViewIOS::OnAutocompleteAccept( 159 void LocationBarControllerImpl::OnAutocompleteAccept(
156 const GURL& gurl, 160 const GURL& gurl,
157 WindowOpenDisposition disposition, 161 WindowOpenDisposition disposition,
158 ui::PageTransition transition, 162 ui::PageTransition transition,
159 AutocompleteMatchType::Type type) { 163 AutocompleteMatchType::Type type) {
160 if (gurl.is_valid()) { 164 if (gurl.is_valid()) {
161 transition = ui::PageTransitionFromInt( 165 transition = ui::PageTransitionFromInt(
162 transition | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); 166 transition | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
163 [delegate_ loadGURLFromLocationBar:gurl transition:transition]; 167 [delegate_ loadGURLFromLocationBar:gurl transition:transition];
164 } 168 }
165 } 169 }
166 170
167 void LocationBarViewIOS::OnChanged() { 171 void LocationBarControllerImpl::OnChanged() {
168 const bool page_is_offline = IsCurrentPageOffline(GetWebState()); 172 const bool page_is_offline = IsCurrentPageOffline(GetWebState());
169 const int resource_id = edit_view_->GetIcon(page_is_offline); 173 const int resource_id = edit_view_->GetIcon(page_is_offline);
170 [field_ setPlaceholderImage:resource_id]; 174 [field_ setPlaceholderImage:resource_id];
171 175
172 // TODO(rohitrao): Can we get focus information from somewhere other than the 176 // TODO(rohitrao): Can we get focus information from somewhere other than the
173 // model? 177 // model?
174 if (!IsIPadIdiom() && !edit_view_->model()->has_focus()) { 178 if (!IsIPadIdiom() && !edit_view_->model()->has_focus()) {
175 ToolbarModel* toolbarModel = [delegate_ toolbarModel]; 179 ToolbarModel* toolbarModel = [delegate_ toolbarModel];
176 if (toolbarModel) { 180 if (toolbarModel) {
177 bool show_icon_for_state = security_state::ShouldAlwaysShowIcon( 181 bool show_icon_for_state = security_state::ShouldAlwaysShowIcon(
(...skipping 11 matching lines...) Expand all
189 } 193 }
190 } 194 }
191 UpdateRightDecorations(); 195 UpdateRightDecorations();
192 [delegate_ locationBarChanged]; 196 [delegate_ locationBarChanged];
193 197
194 NSString* placeholderText = 198 NSString* placeholderText =
195 show_hint_text_ ? l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT) : nil; 199 show_hint_text_ ? l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT) : nil;
196 [field_ setPlaceholder:placeholderText]; 200 [field_ setPlaceholder:placeholderText];
197 } 201 }
198 202
199 bool LocationBarViewIOS::IsShowingPlaceholderWhileCollapsed() { 203 bool LocationBarControllerImpl::IsShowingPlaceholderWhileCollapsed() {
200 return is_showing_placeholder_while_collapsed_; 204 return is_showing_placeholder_while_collapsed_;
201 } 205 }
202 206
203 void LocationBarViewIOS::OnInputInProgress(bool in_progress) { 207 void LocationBarControllerImpl::OnInputInProgress(bool in_progress) {
204 if ([delegate_ toolbarModel]) 208 if ([delegate_ toolbarModel])
205 [delegate_ toolbarModel]->set_input_in_progress(in_progress); 209 [delegate_ toolbarModel]->set_input_in_progress(in_progress);
206 if (in_progress) 210 if (in_progress)
207 [delegate_ locationBarBeganEdit]; 211 [delegate_ locationBarBeganEdit];
208 } 212 }
209 213
210 void LocationBarViewIOS::OnKillFocus() { 214 void LocationBarControllerImpl::OnKillFocus() {
211 // Hide the location icon on phone. A subsequent call to OnChanged() will 215 // Hide the location icon on phone. A subsequent call to OnChanged() will
212 // bring the icon back if needed. 216 // bring the icon back if needed.
213 if (!IsIPadIdiom()) { 217 if (!IsIPadIdiom()) {
214 [field_ hidePlaceholderImage]; 218 [field_ hidePlaceholderImage];
215 is_showing_placeholder_while_collapsed_ = false; 219 is_showing_placeholder_while_collapsed_ = false;
216 } 220 }
217 221
218 // Update the placeholder icon. 222 // Update the placeholder icon.
219 const int resource_id = 223 const int resource_id =
220 edit_view_->GetIcon(IsCurrentPageOffline(GetWebState())); 224 edit_view_->GetIcon(IsCurrentPageOffline(GetWebState()));
221 [field_ setPlaceholderImage:resource_id]; 225 [field_ setPlaceholderImage:resource_id];
222 226
223 // Show the placeholder text on iPad. 227 // Show the placeholder text on iPad.
224 if (IsIPadIdiom()) { 228 if (IsIPadIdiom()) {
225 NSString* placeholderText = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT); 229 NSString* placeholderText = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT);
226 [field_ setPlaceholder:placeholderText]; 230 [field_ setPlaceholder:placeholderText];
227 } 231 }
228 232
229 UpdateRightDecorations(); 233 UpdateRightDecorations();
230 [delegate_ locationBarHasResignedFirstResponder]; 234 [delegate_ locationBarHasResignedFirstResponder];
231 } 235 }
232 236
233 void LocationBarViewIOS::OnSetFocus() { 237 void LocationBarControllerImpl::OnSetFocus() {
234 // Show the location icon on phone. 238 // Show the location icon on phone.
235 if (!IsIPadIdiom()) 239 if (!IsIPadIdiom())
236 [field_ showPlaceholderImage]; 240 [field_ showPlaceholderImage];
237 241
238 // Update the placeholder icon. 242 // Update the placeholder icon.
239 const int resource_id = 243 const int resource_id =
240 edit_view_->GetIcon(IsCurrentPageOffline(GetWebState())); 244 edit_view_->GetIcon(IsCurrentPageOffline(GetWebState()));
241 [field_ setPlaceholderImage:resource_id]; 245 [field_ setPlaceholderImage:resource_id];
242 246
243 // Hide the placeholder text on iPad. 247 // Hide the placeholder text on iPad.
244 if (IsIPadIdiom()) { 248 if (IsIPadIdiom()) {
245 [field_ setPlaceholder:nil]; 249 [field_ setPlaceholder:nil];
246 } 250 }
247 UpdateRightDecorations(); 251 UpdateRightDecorations();
248 [delegate_ locationBarHasBecomeFirstResponder]; 252 [delegate_ locationBarHasBecomeFirstResponder];
249 } 253 }
250 254
251 const ToolbarModel* LocationBarViewIOS::GetToolbarModel() const { 255 const ToolbarModel* LocationBarControllerImpl::GetToolbarModel() const {
252 return [delegate_ toolbarModel]; 256 return [delegate_ toolbarModel];
253 } 257 }
254 258
255 ToolbarModel* LocationBarViewIOS::GetToolbarModel() { 259 ToolbarModel* LocationBarControllerImpl::GetToolbarModel() {
256 return [delegate_ toolbarModel]; 260 return [delegate_ toolbarModel];
257 } 261 }
258 262
259 web::WebState* LocationBarViewIOS::GetWebState() { 263 web::WebState* LocationBarControllerImpl::GetWebState() {
260 return [delegate_ getWebState]; 264 return [delegate_ getWebState];
261 } 265 }
262 266
263 void LocationBarViewIOS::InstallLocationIcon() { 267 void LocationBarControllerImpl::InstallLocationIcon() {
264 // Set the placeholder for empty omnibox. 268 // Set the placeholder for empty omnibox.
265 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 269 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
266 UIImage* image = NativeImage(IDR_IOS_OMNIBOX_SEARCH); 270 UIImage* image = NativeImage(IDR_IOS_OMNIBOX_SEARCH);
267 [button setImage:image forState:UIControlStateNormal]; 271 [button setImage:image forState:UIControlStateNormal];
268 [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; 272 [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
269 [button addTarget:nil 273 [button addTarget:nil
270 action:@selector(chromeExecuteCommand:) 274 action:@selector(chromeExecuteCommand:)
271 forControlEvents:UIControlEventTouchUpInside]; 275 forControlEvents:UIControlEventTouchUpInside];
272 [button setTag:IDC_SHOW_PAGE_INFO]; 276 [button setTag:IDC_SHOW_PAGE_INFO];
273 SetA11yLabelAndUiAutomationName( 277 SetA11yLabelAndUiAutomationName(
274 button, IDS_IOS_PAGE_INFO_SECURITY_BUTTON_ACCESSIBILITY_LABEL, 278 button, IDS_IOS_PAGE_INFO_SECURITY_BUTTON_ACCESSIBILITY_LABEL,
275 @"Page Security Info"); 279 @"Page Security Info");
276 [button setIsAccessibilityElement:YES]; 280 [button setIsAccessibilityElement:YES];
277 281
278 // Set chip text options. 282 // Set chip text options.
279 [button setTitleColor:[UIColor colorWithWhite:0.631 alpha:1] 283 [button setTitleColor:[UIColor colorWithWhite:0.631 alpha:1]
280 forState:UIControlStateNormal]; 284 forState:UIControlStateNormal];
281 [button titleLabel].font = 285 [button titleLabel].font =
282 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12]; 286 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12];
283 [field_ setLeftView:button]; 287 [field_ setLeftView:button];
284 288
285 // The placeholder image is only shown when in edit mode on iPhone, and always 289 // The placeholder image is only shown when in edit mode on iPhone, and always
286 // shown on iPad. 290 // shown on iPad.
287 if (IsIPadIdiom()) 291 if (IsIPadIdiom())
288 [field_ setLeftViewMode:UITextFieldViewModeAlways]; 292 [field_ setLeftViewMode:UITextFieldViewModeAlways];
289 else 293 else
290 [field_ setLeftViewMode:UITextFieldViewModeNever]; 294 [field_ setLeftViewMode:UITextFieldViewModeNever];
291 } 295 }
292 296
293 void LocationBarViewIOS::CreateClearTextIcon(bool is_incognito) { 297 void LocationBarControllerImpl::CreateClearTextIcon(bool is_incognito) {
294 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 298 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
295 UIImage* omniBoxClearImage = is_incognito 299 UIImage* omniBoxClearImage = is_incognito
296 ? NativeImage(IDR_IOS_OMNIBOX_CLEAR_OTR) 300 ? NativeImage(IDR_IOS_OMNIBOX_CLEAR_OTR)
297 : NativeImage(IDR_IOS_OMNIBOX_CLEAR); 301 : NativeImage(IDR_IOS_OMNIBOX_CLEAR);
298 UIImage* omniBoxClearPressedImage = 302 UIImage* omniBoxClearPressedImage =
299 is_incognito ? NativeImage(IDR_IOS_OMNIBOX_CLEAR_OTR_PRESSED) 303 is_incognito ? NativeImage(IDR_IOS_OMNIBOX_CLEAR_OTR_PRESSED)
300 : NativeImage(IDR_IOS_OMNIBOX_CLEAR_PRESSED); 304 : NativeImage(IDR_IOS_OMNIBOX_CLEAR_PRESSED);
301 [button setImage:omniBoxClearImage forState:UIControlStateNormal]; 305 [button setImage:omniBoxClearImage forState:UIControlStateNormal];
302 [button setImage:omniBoxClearPressedImage forState:UIControlStateHighlighted]; 306 [button setImage:omniBoxClearPressedImage forState:UIControlStateHighlighted];
303 307
304 CGRect frame = CGRectZero; 308 CGRect frame = CGRectZero;
305 frame.size = CGSizeMake(kClearTextButtonWidth, kClearTextButtonHeight); 309 frame.size = CGSizeMake(kClearTextButtonWidth, kClearTextButtonHeight);
306 [button setFrame:frame]; 310 [button setFrame:frame];
307 311
308 clear_button_bridge_.reset( 312 clear_button_bridge_.reset(
309 [[OmniboxClearButtonBridge alloc] initWithOmniboxView:edit_view_.get()]); 313 [[OmniboxClearButtonBridge alloc] initWithOmniboxView:edit_view_.get()]);
310 [button addTarget:clear_button_bridge_ 314 [button addTarget:clear_button_bridge_
311 action:@selector(clearText) 315 action:@selector(clearText)
312 forControlEvents:UIControlEventTouchUpInside]; 316 forControlEvents:UIControlEventTouchUpInside];
313 clear_text_button_.reset(button); 317 clear_text_button_.reset(button);
314 318
315 SetA11yLabelAndUiAutomationName(clear_text_button_, 319 SetA11yLabelAndUiAutomationName(clear_text_button_,
316 IDS_IOS_ACCNAME_CLEAR_TEXT, @"Clear Text"); 320 IDS_IOS_ACCNAME_CLEAR_TEXT, @"Clear Text");
317 } 321 }
318 322
319 void LocationBarViewIOS::UpdateRightDecorations() { 323 void LocationBarControllerImpl::UpdateRightDecorations() {
320 DCHECK(clear_text_button_); 324 DCHECK(clear_text_button_);
321 if (!edit_view_->model()->has_focus()) { 325 if (!edit_view_->model()->has_focus()) {
322 // Do nothing for iPhone. The right view will be set to nil after the 326 // Do nothing for iPhone. The right view will be set to nil after the
323 // omnibox animation is completed. 327 // omnibox animation is completed.
324 if (IsIPadIdiom()) 328 if (IsIPadIdiom())
325 [field_ setRightView:nil]; 329 [field_ setRightView:nil];
326 } else if ([field_ displayedText].empty() && 330 } else if ([field_ displayedText].empty() &&
327 ![field_ isShowingQueryRefinementChip]) { 331 ![field_ isShowingQueryRefinementChip]) {
328 [field_ setRightView:nil]; 332 [field_ setRightView:nil];
329 } else { 333 } else {
330 [field_ setRightView:clear_text_button_]; 334 [field_ setRightView:clear_text_button_];
331 [clear_text_button_ setAlpha:1]; 335 [clear_text_button_ setAlpha:1];
332 } 336 }
333 } 337 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/omnibox/location_bar_controller_impl.h ('k') | ios/chrome/browser/ui/omnibox/location_bar_view_ios.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698