OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/translate/before_translate_infobar_controller.h" | 5 #include "ios/chrome/browser/translate/before_translate_infobar_controller.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Navigation bar associated with the picker with "Done" and "Cancel" buttons. | 117 // Navigation bar associated with the picker with "Done" and "Cancel" buttons. |
118 base::scoped_nsobject<UINavigationBar> _navigationBar; | 118 base::scoped_nsobject<UINavigationBar> _navigationBar; |
119 // The controller of the languagePicker. Needs to be an ivar because | 119 // The controller of the languagePicker. Needs to be an ivar because |
120 // |_languagePicker| does not retain it. | 120 // |_languagePicker| does not retain it. |
121 base::scoped_nsobject<LanguagePickerController> _languagePickerController; | 121 base::scoped_nsobject<LanguagePickerController> _languagePickerController; |
122 } | 122 } |
123 | 123 |
124 #pragma mark - | 124 #pragma mark - |
125 #pragma mark InfoBarControllerProtocol | 125 #pragma mark InfoBarControllerProtocol |
126 | 126 |
127 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate | 127 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>) |
128 frame:(CGRect)frame { | 128 viewForDelegate:(infobars::InfoBarDelegate*)delegate |
| 129 frame:(CGRect)frame { |
| 130 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView; |
129 _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate(); | 131 _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate(); |
130 DCHECK(!infoBarView_); | 132 infoBarView.reset( |
131 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView() | 133 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate)); |
132 initWithFrame:frame | |
133 delegate:delegate_]); | |
134 // Icon | 134 // Icon |
135 gfx::Image icon = _translateInfoBarDelegate->GetIcon(); | 135 gfx::Image icon = _translateInfoBarDelegate->GetIcon(); |
136 if (!icon.IsEmpty()) | 136 if (!icon.IsEmpty()) |
137 [infoBarView_ addLeftIcon:icon.ToUIImage()]; | 137 [infoBarView addLeftIcon:icon.ToUIImage()]; |
138 | 138 |
139 // Main text. | 139 // Main text. |
140 [self updateInfobarLabel]; | 140 [self updateInfobarLabel]; |
141 | 141 |
142 // Close button. | 142 // Close button. |
143 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::BEFORE_DENY | 143 [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::BEFORE_DENY |
144 target:self | 144 target:self |
145 action:@selector(infoBarButtonDidPress:)]; | 145 action:@selector(infoBarButtonDidPress:)]; |
146 // Other buttons. | 146 // Other buttons. |
147 NSString* buttonAccept = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_ACCEPT); | 147 NSString* buttonAccept = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_ACCEPT); |
148 NSString* buttonDeny = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_DENY); | 148 NSString* buttonDeny = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_DENY); |
149 [infoBarView_ addButton1:buttonAccept | 149 [infoBarView addButton1:buttonAccept |
150 tag1:TranslateInfoBarIOSTag::BEFORE_ACCEPT | 150 tag1:TranslateInfoBarIOSTag::BEFORE_ACCEPT |
151 button2:buttonDeny | 151 button2:buttonDeny |
152 tag2:TranslateInfoBarIOSTag::BEFORE_DENY | 152 tag2:TranslateInfoBarIOSTag::BEFORE_DENY |
153 target:self | 153 target:self |
154 action:@selector(infoBarButtonDidPress:)]; | 154 action:@selector(infoBarButtonDidPress:)]; |
| 155 return infoBarView; |
155 } | 156 } |
156 | 157 |
157 - (void)updateInfobarLabel { | 158 - (void)updateInfobarLabel { |
158 NSString* originalLanguage = | 159 NSString* originalLanguage = |
159 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( | 160 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( |
160 _translateInfoBarDelegate->original_language_index())); | 161 _translateInfoBarDelegate->original_language_index())); |
161 NSString* targetLanguage = | 162 NSString* targetLanguage = |
162 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( | 163 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( |
163 _translateInfoBarDelegate->target_language_index())); | 164 _translateInfoBarDelegate->target_language_index())); |
164 base::string16 originalLanguageWithLink = | 165 base::string16 originalLanguageWithLink = |
165 base::SysNSStringToUTF16([[infoBarView_ class] | 166 base::SysNSStringToUTF16([[self.view class] |
166 stringAsLink:originalLanguage | 167 stringAsLink:originalLanguage |
167 tag:TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE]); | 168 tag:TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE]); |
168 base::string16 targetLanguageWithLink = | 169 base::string16 targetLanguageWithLink = |
169 base::SysNSStringToUTF16([[infoBarView_ class] | 170 base::SysNSStringToUTF16([[self.view class] |
170 stringAsLink:targetLanguage | 171 stringAsLink:targetLanguage |
171 tag:TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE]); | 172 tag:TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE]); |
172 NSString* label = | 173 NSString* label = |
173 l10n_util::GetNSStringF(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE_IOS, | 174 l10n_util::GetNSStringF(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE_IOS, |
174 originalLanguageWithLink, targetLanguageWithLink); | 175 originalLanguageWithLink, targetLanguageWithLink); |
175 [infoBarView_ addLabel:label | 176 [self.view addLabel:label target:self action:@selector(infobarLinkDidPress:)]; |
176 target:self | |
177 action:@selector(infobarLinkDidPress:)]; | |
178 } | 177 } |
179 | 178 |
180 - (void)languageSelectionDone { | 179 - (void)languageSelectionDone { |
181 size_t selectedRow = [_languagePicker selectedRowInComponent:0]; | 180 size_t selectedRow = [_languagePicker selectedRowInComponent:0]; |
182 if (_languageSelectionType == | 181 if (_languageSelectionType == |
183 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE && | 182 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE && |
184 selectedRow != _translateInfoBarDelegate->target_language_index()) { | 183 selectedRow != _translateInfoBarDelegate->target_language_index()) { |
185 _translateInfoBarDelegate->UpdateOriginalLanguageIndex(selectedRow); | 184 _translateInfoBarDelegate->UpdateOriginalLanguageIndex(selectedRow); |
186 } | 185 } |
187 if (_languageSelectionType == | 186 if (_languageSelectionType == |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 animations:animations | 228 animations:animations |
230 completion:completion]; | 229 completion:completion]; |
231 } | 230 } |
232 | 231 |
233 #pragma mark - Handling of User Events | 232 #pragma mark - Handling of User Events |
234 | 233 |
235 - (void)infoBarButtonDidPress:(id)sender { | 234 - (void)infoBarButtonDidPress:(id)sender { |
236 // This press might have occurred after the user has already pressed a button, | 235 // This press might have occurred after the user has already pressed a button, |
237 // in which case the view has been detached from the delegate and this press | 236 // in which case the view has been detached from the delegate and this press |
238 // should be ignored. | 237 // should be ignored. |
239 if (!delegate_) { | 238 if (!self.delegate) { |
240 return; | 239 return; |
241 } | 240 } |
242 if ([sender isKindOfClass:[UIButton class]]) { | 241 if ([sender isKindOfClass:[UIButton class]]) { |
243 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; | 242 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; |
244 DCHECK(buttonId == TranslateInfoBarIOSTag::BEFORE_ACCEPT || | 243 DCHECK(buttonId == TranslateInfoBarIOSTag::BEFORE_ACCEPT || |
245 buttonId == TranslateInfoBarIOSTag::BEFORE_DENY); | 244 buttonId == TranslateInfoBarIOSTag::BEFORE_DENY); |
246 delegate_->InfoBarButtonDidPress(buttonId); | 245 self.delegate->InfoBarButtonDidPress(buttonId); |
247 } | 246 } |
248 } | 247 } |
249 | 248 |
250 - (void)infobarLinkDidPress:(NSNumber*)tag { | 249 - (void)infobarLinkDidPress:(NSNumber*)tag { |
251 DCHECK([tag isKindOfClass:[NSNumber class]]); | 250 DCHECK([tag isKindOfClass:[NSNumber class]]); |
252 _languageSelectionType = [tag unsignedIntegerValue]; | 251 _languageSelectionType = [tag unsignedIntegerValue]; |
253 DCHECK(_languageSelectionType == | 252 DCHECK(_languageSelectionType == |
254 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE || | 253 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE || |
255 _languageSelectionType == | 254 _languageSelectionType == |
256 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE); | 255 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 initWithDelegate:_translateInfoBarDelegate | 321 initWithDelegate:_translateInfoBarDelegate |
323 initialRow:selectedRow | 322 initialRow:selectedRow |
324 disabledRow:disabledRow]); | 323 disabledRow:disabledRow]); |
325 _languagePicker.reset( | 324 _languagePicker.reset( |
326 [[UIPickerView alloc] initWithFrame:initialPickerFrame]); | 325 [[UIPickerView alloc] initWithFrame:initialPickerFrame]); |
327 [_languagePicker setAutoresizingMask:resizingMask]; | 326 [_languagePicker setAutoresizingMask:resizingMask]; |
328 [_languagePicker setShowsSelectionIndicator:YES]; | 327 [_languagePicker setShowsSelectionIndicator:YES]; |
329 [_languagePicker setDataSource:_languagePickerController]; | 328 [_languagePicker setDataSource:_languagePickerController]; |
330 [_languagePicker setDelegate:_languagePickerController]; | 329 [_languagePicker setDelegate:_languagePickerController]; |
331 [_languagePicker setShowsSelectionIndicator:YES]; | 330 [_languagePicker setShowsSelectionIndicator:YES]; |
332 [_languagePicker setBackgroundColor:[infoBarView_ backgroundColor]]; | 331 [_languagePicker setBackgroundColor:[self.view backgroundColor]]; |
333 [_languagePicker selectRow:selectedRow inComponent:0 animated:NO]; | 332 [_languagePicker selectRow:selectedRow inComponent:0 animated:NO]; |
334 | 333 |
335 auto blockLanguagePicker(_languagePicker); | 334 auto blockLanguagePicker(_languagePicker); |
336 auto blockNavigationBar(_navigationBar); | 335 auto blockNavigationBar(_navigationBar); |
337 [UIView animateWithDuration:kPickerAnimationDurationInSeconds | 336 [UIView animateWithDuration:kPickerAnimationDurationInSeconds |
338 animations:^{ | 337 animations:^{ |
339 blockLanguagePicker.get().frame = finalPickerFrame; | 338 blockLanguagePicker.get().frame = finalPickerFrame; |
340 blockNavigationBar.get().frame = finalNavigationBarFrame; | 339 blockNavigationBar.get().frame = finalNavigationBarFrame; |
341 }]; | 340 }]; |
342 | 341 |
343 // Add the subviews. | 342 // Add the subviews. |
344 [_languageSelectionView addSubview:_languagePicker]; | 343 [_languageSelectionView addSubview:_languagePicker]; |
345 [_languageSelectionView addSubview:_navigationBar]; | 344 [_languageSelectionView addSubview:_navigationBar]; |
346 } | 345 } |
347 | 346 |
348 - (void)removeView { | 347 - (void)removeView { |
349 [super removeView]; | 348 [super removeView]; |
350 [self dismissLanguageSelectionView]; | 349 [self dismissLanguageSelectionView]; |
351 } | 350 } |
352 | 351 |
353 - (void)detachView { | 352 - (void)detachView { |
354 [super detachView]; | 353 [super detachView]; |
355 [self dismissLanguageSelectionView]; | 354 [self dismissLanguageSelectionView]; |
356 } | 355 } |
357 | 356 |
358 @end | 357 @end |
OLD | NEW |