Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 24 CGFloat kUIPickerHeight = 216; | 24 CGFloat kUIPickerHeight = 216; |
| 25 CGFloat kUIPickerFontSize = 26; | 25 CGFloat kUIPickerFontSize = 26; |
| 26 NSTimeInterval kPickerAnimationDurationInSeconds = 0.2; | 26 NSTimeInterval kPickerAnimationDurationInSeconds = 0.2; |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 // The class is a data source and delegate to the UIPickerView that contains the | 30 // The class is a data source and delegate to the UIPickerView that contains the |
| 31 // language list. | 31 // language list. |
| 32 @interface LanguagePickerController | 32 @interface LanguagePickerController |
| 33 : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> { | 33 : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> { |
| 34 __weak translate::TranslateInfoBarDelegate* translateInfoBarDelegate_; | 34 __weak translate::TranslateInfoBarDelegate* _translateInfoBarDelegate; |
| 35 NSInteger initialRow_; // Displayed in bold font. | 35 NSInteger _initialRow; // Displayed in bold font. |
| 36 NSInteger disabledRow_; // Grayed out. | 36 NSInteger _disabledRow; // Grayed out. |
| 37 } | 37 } |
| 38 @end | 38 @end |
| 39 | 39 |
| 40 @implementation LanguagePickerController | 40 @implementation LanguagePickerController |
| 41 | 41 |
| 42 - (instancetype)initWithDelegate:(translate::TranslateInfoBarDelegate*) | 42 - (instancetype)initWithDelegate:(translate::TranslateInfoBarDelegate*) |
| 43 translateInfoBarDelegate | 43 translateInfoBarDelegate |
| 44 initialRow:(NSInteger)initialRow | 44 initialRow:(NSInteger)initialRow |
| 45 disabledRow:(NSInteger)disabledRow { | 45 disabledRow:(NSInteger)disabledRow { |
| 46 if ((self = [super init])) { | 46 if ((self = [super init])) { |
| 47 translateInfoBarDelegate_ = translateInfoBarDelegate; | 47 _translateInfoBarDelegate = translateInfoBarDelegate; |
| 48 initialRow_ = initialRow; | 48 _initialRow = initialRow; |
| 49 disabledRow_ = disabledRow; | 49 _disabledRow = disabledRow; |
| 50 } | 50 } |
| 51 return self; | 51 return self; |
| 52 } | 52 } |
| 53 | 53 |
| 54 #pragma mark - | 54 #pragma mark - |
| 55 #pragma mark UIPickerViewDataSource | 55 #pragma mark UIPickerViewDataSource |
| 56 | 56 |
| 57 - (NSInteger)pickerView:(UIPickerView*)pickerView | 57 - (NSInteger)pickerView:(UIPickerView*)pickerView |
| 58 numberOfRowsInComponent:(NSInteger)component { | 58 numberOfRowsInComponent:(NSInteger)component { |
| 59 NSUInteger numRows = translateInfoBarDelegate_->num_languages(); | 59 NSUInteger numRows = _translateInfoBarDelegate->num_languages(); |
| 60 return numRows; | 60 return numRows; |
| 61 } | 61 } |
| 62 | 62 |
| 63 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { | 63 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { |
| 64 return 1; | 64 return 1; |
| 65 } | 65 } |
| 66 | 66 |
| 67 #pragma mark - | 67 #pragma mark - |
| 68 #pragma mark UIPickerViewDelegate | 68 #pragma mark UIPickerViewDelegate |
| 69 | 69 |
| 70 - (UIView*)pickerView:(UIPickerView*)pickerView | 70 - (UIView*)pickerView:(UIPickerView*)pickerView |
| 71 viewForRow:(NSInteger)row | 71 viewForRow:(NSInteger)row |
| 72 forComponent:(NSInteger)component | 72 forComponent:(NSInteger)component |
| 73 reusingView:(UIView*)view { | 73 reusingView:(UIView*)view { |
| 74 DCHECK_EQ(0, component); | 74 DCHECK_EQ(0, component); |
| 75 UILabel* label = [view isKindOfClass:[UILabel class]] | 75 UILabel* label = [view isKindOfClass:[UILabel class]] |
| 76 ? (UILabel*)view | 76 ? (UILabel*)view |
| 77 : [[[UILabel alloc] init] autorelease]; | 77 : [[[UILabel alloc] init] autorelease]; |
| 78 [label setText:base::SysUTF16ToNSString( | 78 [label setText:base::SysUTF16ToNSString( |
| 79 translateInfoBarDelegate_->language_name_at(row))]; | 79 _translateInfoBarDelegate->language_name_at(row))]; |
| 80 [label setTextAlignment:NSTextAlignmentCenter]; | 80 [label setTextAlignment:NSTextAlignmentCenter]; |
| 81 UIFont* font = [UIFont systemFontOfSize:kUIPickerFontSize]; | 81 UIFont* font = [UIFont systemFontOfSize:kUIPickerFontSize]; |
| 82 BOOL enabled = YES; | 82 BOOL enabled = YES; |
| 83 if (row == initialRow_) | 83 if (row == _initialRow) |
| 84 font = [UIFont boldSystemFontOfSize:kUIPickerFontSize]; | 84 font = [UIFont boldSystemFontOfSize:kUIPickerFontSize]; |
| 85 else if (row == disabledRow_) | 85 else if (row == _disabledRow) |
| 86 enabled = NO; | 86 enabled = NO; |
| 87 [label setFont:font]; | 87 [label setFont:font]; |
| 88 [label setEnabled:enabled]; | 88 [label setEnabled:enabled]; |
| 89 return label; | 89 return label; |
| 90 } | 90 } |
| 91 | 91 |
| 92 @end | 92 @end |
| 93 | 93 |
| 94 @interface BeforeTranslateInfoBarController () | 94 @interface BeforeTranslateInfoBarController () |
| 95 | 95 |
| 96 // Action for any of the user defined buttons. | 96 // Action for any of the user defined buttons. |
| 97 - (void)infoBarButtonDidPress:(id)sender; | 97 - (void)infoBarButtonDidPress:(id)sender; |
| 98 // Action for any of the user defined links. | 98 // Action for any of the user defined links. |
| 99 - (void)infobarLinkDidPress:(NSNumber*)tag; | 99 - (void)infobarLinkDidPress:(NSNumber*)tag; |
| 100 // Action for the language selection "Done" button. | 100 // Action for the language selection "Done" button. |
| 101 - (void)languageSelectionDone; | 101 - (void)languageSelectionDone; |
| 102 // Dismisses the language selection view. | 102 // Dismisses the language selection view. |
| 103 - (void)dismissLanguageSelectionView; | 103 - (void)dismissLanguageSelectionView; |
| 104 | 104 |
| 105 @end | 105 @end |
| 106 | 106 |
| 107 @implementation BeforeTranslateInfoBarController { | 107 @implementation BeforeTranslateInfoBarController { |
| 108 __weak translate::TranslateInfoBarDelegate* translateInfoBarDelegate_; | 108 __weak translate::TranslateInfoBarDelegate* _translateInfoBarDelegate; |
| 109 // A fullscreen view that catches all touch events and contains a UIPickerView | 109 // A fullscreen view that catches all touch events and contains a UIPickerView |
| 110 // and a UINavigationBar. | 110 // and a UINavigationBar. |
| 111 base::scoped_nsobject<UIView> languageSelectionView_; | 111 base::scoped_nsobject<UIView> _languageSelectionView; |
| 112 // Stores whether the user is currently choosing in the UIPickerView the | 112 // Stores whether the user is currently choosing in the UIPickerView the |
| 113 // original language, or the target language. | 113 // original language, or the target language. |
| 114 NSUInteger languageSelectionType_; | 114 NSUInteger _languageSelectionType; |
| 115 // The language picker. | 115 // The language picker. |
| 116 base::scoped_nsobject<UIPickerView> languagePicker_; | 116 base::scoped_nsobject<UIPickerView> _languagePicker; |
| 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 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate |
| 128 frame:(CGRect)frame { | 128 frame:(CGRect)frame { |
| 129 translateInfoBarDelegate_ = delegate->AsTranslateInfoBarDelegate(); | 129 _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate(); |
| 130 infobars::InfoBarDelegate* infoBarDelegate = | 130 infobars::InfoBarDelegate* infoBarDelegate = |
| 131 static_cast<infobars::InfoBarDelegate*>(translateInfoBarDelegate_); | 131 static_cast<infobars::InfoBarDelegate*>(_translateInfoBarDelegate); |
| 132 DCHECK(!infoBarView_); | 132 DCHECK(!infoBarView_); |
| 133 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView() | 133 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView() |
| 134 initWithFrame:frame | 134 initWithFrame:frame |
| 135 delegate:delegate_ | 135 delegate:delegate_ |
| 136 isWarning:infoBarDelegate->GetInfoBarType() == | 136 isWarning:infoBarDelegate->GetInfoBarType() == |
| 137 infobars::InfoBarDelegate::WARNING_TYPE]); | 137 infobars::InfoBarDelegate::WARNING_TYPE]); |
| 138 // Icon | 138 // Icon |
| 139 gfx::Image icon = translateInfoBarDelegate_->GetIcon(); | 139 gfx::Image icon = _translateInfoBarDelegate->GetIcon(); |
| 140 if (!icon.IsEmpty()) | 140 if (!icon.IsEmpty()) |
| 141 [infoBarView_ addLeftIcon:icon.ToUIImage()]; | 141 [infoBarView_ addLeftIcon:icon.ToUIImage()]; |
| 142 | 142 |
| 143 // Main text. | 143 // Main text. |
| 144 [self updateInfobarLabel]; | 144 [self updateInfobarLabel]; |
| 145 | 145 |
| 146 // Close button. | 146 // Close button. |
| 147 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::BEFORE_DENY | 147 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::BEFORE_DENY |
| 148 target:self | 148 target:self |
| 149 action:@selector(infoBarButtonDidPress:)]; | 149 action:@selector(infoBarButtonDidPress:)]; |
| 150 // Other buttons. | 150 // Other buttons. |
| 151 NSString* buttonAccept = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_ACCEPT); | 151 NSString* buttonAccept = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_ACCEPT); |
| 152 NSString* buttonDeny = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_DENY); | 152 NSString* buttonDeny = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_DENY); |
| 153 [infoBarView_ addButton1:buttonAccept | 153 [infoBarView_ addButton1:buttonAccept |
| 154 tag1:TranslateInfoBarIOSTag::BEFORE_ACCEPT | 154 tag1:TranslateInfoBarIOSTag::BEFORE_ACCEPT |
| 155 button2:buttonDeny | 155 button2:buttonDeny |
| 156 tag2:TranslateInfoBarIOSTag::BEFORE_DENY | 156 tag2:TranslateInfoBarIOSTag::BEFORE_DENY |
| 157 target:self | 157 target:self |
| 158 action:@selector(infoBarButtonDidPress:)]; | 158 action:@selector(infoBarButtonDidPress:)]; |
| 159 } | 159 } |
| 160 | 160 |
| 161 - (void)updateInfobarLabel { | 161 - (void)updateInfobarLabel { |
| 162 NSString* originalLanguage = | 162 NSString* originalLanguage = |
| 163 base::SysUTF16ToNSString(translateInfoBarDelegate_->language_name_at( | 163 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( |
| 164 translateInfoBarDelegate_->original_language_index())); | 164 _translateInfoBarDelegate->original_language_index())); |
| 165 NSString* targetLanguage = | 165 NSString* targetLanguage = |
| 166 base::SysUTF16ToNSString(translateInfoBarDelegate_->language_name_at( | 166 base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at( |
| 167 translateInfoBarDelegate_->target_language_index())); | 167 _translateInfoBarDelegate->target_language_index())); |
| 168 base::string16 originalLanguageWithLink = | 168 base::string16 originalLanguageWithLink = |
| 169 base::SysNSStringToUTF16([[infoBarView_ class] | 169 base::SysNSStringToUTF16([[infoBarView_ class] |
| 170 stringAsLink:originalLanguage | 170 stringAsLink:originalLanguage |
| 171 tag:TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE]); | 171 tag:TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE]); |
| 172 base::string16 targetLanguageWithLink = | 172 base::string16 targetLanguageWithLink = |
| 173 base::SysNSStringToUTF16([[infoBarView_ class] | 173 base::SysNSStringToUTF16([[infoBarView_ class] |
| 174 stringAsLink:targetLanguage | 174 stringAsLink:targetLanguage |
| 175 tag:TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE]); | 175 tag:TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE]); |
| 176 NSString* label = | 176 NSString* label = |
| 177 l10n_util::GetNSStringF(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE_IOS, | 177 l10n_util::GetNSStringF(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE_IOS, |
| 178 originalLanguageWithLink, targetLanguageWithLink); | 178 originalLanguageWithLink, targetLanguageWithLink); |
| 179 [infoBarView_ addLabel:label | 179 [infoBarView_ addLabel:label |
| 180 target:self | 180 target:self |
| 181 action:@selector(infobarLinkDidPress:)]; | 181 action:@selector(infobarLinkDidPress:)]; |
| 182 } | 182 } |
| 183 | 183 |
| 184 - (void)languageSelectionDone { | 184 - (void)languageSelectionDone { |
| 185 size_t selectedRow = [languagePicker_ selectedRowInComponent:0]; | 185 size_t selectedRow = [_languagePicker selectedRowInComponent:0]; |
| 186 if (languageSelectionType_ == | 186 if (_languageSelectionType == |
| 187 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE && | 187 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE && |
| 188 selectedRow != translateInfoBarDelegate_->target_language_index()) { | 188 selectedRow != _translateInfoBarDelegate->target_language_index()) { |
| 189 translateInfoBarDelegate_->UpdateOriginalLanguageIndex(selectedRow); | 189 _translateInfoBarDelegate->UpdateOriginalLanguageIndex(selectedRow); |
| 190 } | 190 } |
| 191 if (languageSelectionType_ == | 191 if (_languageSelectionType == |
| 192 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE && | 192 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE && |
| 193 selectedRow != translateInfoBarDelegate_->original_language_index()) { | 193 selectedRow != _translateInfoBarDelegate->original_language_index()) { |
| 194 translateInfoBarDelegate_->UpdateTargetLanguageIndex(selectedRow); | 194 _translateInfoBarDelegate->UpdateTargetLanguageIndex(selectedRow); |
| 195 } | 195 } |
| 196 [self updateInfobarLabel]; | 196 [self updateInfobarLabel]; |
| 197 [self dismissLanguageSelectionView]; | 197 [self dismissLanguageSelectionView]; |
| 198 } | 198 } |
| 199 | 199 |
| 200 - (void)dismissLanguageSelectionView { | 200 - (void)dismissLanguageSelectionView { |
| 201 DCHECK_EQ(languagePicker_ == nil, navigationBar_ == nil); | 201 DCHECK_EQ(_languagePicker == nil, _navigationBar == nil); |
| 202 if (languagePicker_ == nil) | 202 if (_languagePicker == nil) |
| 203 return; | 203 return; |
| 204 // Sets the picker's delegate and data source to nil, because the | 204 // Sets the picker's delegate and data source to nil, because the |
| 205 // |languagePickerController_| may be destroyed before the picker is hidden, | 205 // |_languagePickerController| may be destroyed before the picker is hidden, |
| 206 // and even though the interactions with the picker are disabled, it might | 206 // and even though the interactions with the picker are disabled, it might |
| 207 // still be turning and requesting data from the data source or calling the | 207 // still be turning and requesting data from the data source or calling the |
| 208 // delegate. | 208 // delegate. |
| 209 [languagePicker_ setDataSource:nil]; | 209 [_languagePicker setDataSource:nil]; |
| 210 [languagePicker_ setDelegate:nil]; | 210 [_languagePicker setDelegate:nil]; |
| 211 languagePickerController_.reset(); | 211 _languagePickerController.reset(); |
| 212 // Animate the picker away. | 212 // Animate the picker away. |
| 213 CGRect languagePickerFrame = [languagePicker_ frame]; | 213 CGRect languagePickerFrame = [_languagePicker frame]; |
| 214 CGRect navigationBarFrame = [navigationBar_ frame]; | 214 CGRect navigationBarFrame = [_navigationBar frame]; |
| 215 const CGFloat animationHeight = | 215 const CGFloat animationHeight = |
| 216 languagePickerFrame.size.height + navigationBarFrame.size.height; | 216 languagePickerFrame.size.height + navigationBarFrame.size.height; |
| 217 languagePickerFrame.origin.y += animationHeight; | 217 languagePickerFrame.origin.y += animationHeight; |
| 218 navigationBarFrame.origin.y += animationHeight; | 218 navigationBarFrame.origin.y += animationHeight; |
| 219 UIView* blockLanguagePicker = languagePicker_.get(); | 219 UIView* blockLanguagePicker = _languagePicker.get(); |
|
sdefresne
2015/01/09 10:35:30
can you make blockNavigationBar and blockLanguageP
sdefresne
2015/01/09 10:35:30
can you make blockNavigationBar and blockLanguageP
| |
| 220 UIView* blockNavigationBar = navigationBar_.get(); | 220 UIView* blockNavigationBar = _navigationBar.get(); |
| 221 void (^animations)(void) = ^{ | 221 void (^animations)(void) = ^{ |
| 222 blockLanguagePicker.frame = languagePickerFrame; | 222 blockLanguagePicker.frame = languagePickerFrame; |
| 223 blockNavigationBar.frame = navigationBarFrame; | 223 blockNavigationBar.frame = navigationBarFrame; |
| 224 }; | 224 }; |
| 225 languagePicker_.reset(); | 225 _languagePicker.reset(); |
| 226 navigationBar_.reset(); | 226 _navigationBar.reset(); |
| 227 void (^completion)(BOOL finished) = ^(BOOL finished) { | 227 void (^completion)(BOOL finished) = ^(BOOL finished) { |
| 228 [languageSelectionView_ removeFromSuperview]; | 228 [_languageSelectionView removeFromSuperview]; |
|
sdefresne
2015/01/09 10:35:30
nit: maybe make it explicit that self is retained
droger
2015/01/09 13:17:52
I changed the block to no longer retain self.
| |
| 229 languageSelectionView_.reset(); | 229 _languageSelectionView.reset(); |
| 230 }; | 230 }; |
| 231 [UIView animateWithDuration:kPickerAnimationDurationInSeconds | 231 [UIView animateWithDuration:kPickerAnimationDurationInSeconds |
| 232 animations:animations | 232 animations:animations |
| 233 completion:completion]; | 233 completion:completion]; |
| 234 } | 234 } |
| 235 | 235 |
| 236 #pragma mark - Handling of User Events | 236 #pragma mark - Handling of User Events |
| 237 | 237 |
| 238 - (void)infoBarButtonDidPress:(id)sender { | 238 - (void)infoBarButtonDidPress:(id)sender { |
| 239 // This press might have occurred after the user has already pressed a button, | 239 // This press might have occurred after the user has already pressed a button, |
| 240 // in which case the view has been detached from the delegate and this press | 240 // in which case the view has been detached from the delegate and this press |
| 241 // should be ignored. | 241 // should be ignored. |
| 242 if (!delegate_) { | 242 if (!delegate_) { |
| 243 return; | 243 return; |
| 244 } | 244 } |
| 245 if ([sender isKindOfClass:[UIButton class]]) { | 245 if ([sender isKindOfClass:[UIButton class]]) { |
| 246 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; | 246 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; |
| 247 DCHECK(buttonId == TranslateInfoBarIOSTag::BEFORE_ACCEPT || | 247 DCHECK(buttonId == TranslateInfoBarIOSTag::BEFORE_ACCEPT || |
| 248 buttonId == TranslateInfoBarIOSTag::BEFORE_DENY); | 248 buttonId == TranslateInfoBarIOSTag::BEFORE_DENY); |
| 249 delegate_->InfoBarButtonDidPress(buttonId); | 249 delegate_->InfoBarButtonDidPress(buttonId); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 - (void)infobarLinkDidPress:(NSNumber*)tag { | 253 - (void)infobarLinkDidPress:(NSNumber*)tag { |
| 254 DCHECK([tag isKindOfClass:[NSNumber class]]); | 254 DCHECK([tag isKindOfClass:[NSNumber class]]); |
| 255 languageSelectionType_ = [tag unsignedIntegerValue]; | 255 _languageSelectionType = [tag unsignedIntegerValue]; |
| 256 DCHECK(languageSelectionType_ == | 256 DCHECK(_languageSelectionType == |
| 257 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE || | 257 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE || |
| 258 languageSelectionType_ == | 258 _languageSelectionType == |
| 259 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE); | 259 TranslateInfoBarIOSTag::BEFORE_TARGET_LANGUAGE); |
| 260 if (languagePicker_ != nil) | 260 if (_languagePicker != nil) |
| 261 return; // The UIPickerView is already up. | 261 return; // The UIPickerView is already up. |
| 262 | 262 |
| 263 // Creates and adds the view containing the UIPickerView and the | 263 // Creates and adds the view containing the UIPickerView and the |
| 264 // UINavigationBar. | 264 // UINavigationBar. |
| 265 UIView* parentView = | 265 UIView* parentView = |
| 266 [[UIApplication sharedApplication] keyWindow].rootViewController.view; | 266 [[UIApplication sharedApplication] keyWindow].rootViewController.view; |
| 267 // Convert the parent frame to handle device rotation. | 267 // Convert the parent frame to handle device rotation. |
| 268 CGRect parentFrame = | 268 CGRect parentFrame = |
| 269 CGRectApplyAffineTransform([parentView frame], [parentView transform]); | 269 CGRectApplyAffineTransform([parentView frame], [parentView transform]); |
| 270 const CGFloat totalPickerHeight = kUIPickerHeight + kNavigationBarHeight; | 270 const CGFloat totalPickerHeight = kUIPickerHeight + kNavigationBarHeight; |
| 271 CGRect languageSelectionViewFrame = | 271 CGRect languageSelectionViewFrame = |
| 272 CGRectMake(0, parentFrame.size.height - totalPickerHeight, | 272 CGRectMake(0, parentFrame.size.height - totalPickerHeight, |
| 273 parentFrame.size.width, totalPickerHeight); | 273 parentFrame.size.width, totalPickerHeight); |
| 274 languageSelectionView_.reset( | 274 _languageSelectionView.reset( |
| 275 [[UIView alloc] initWithFrame:languageSelectionViewFrame]); | 275 [[UIView alloc] initWithFrame:languageSelectionViewFrame]); |
| 276 [languageSelectionView_ | 276 [_languageSelectionView |
| 277 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | 277 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 278 UIViewAutoresizingFlexibleTopMargin]; | 278 UIViewAutoresizingFlexibleTopMargin]; |
| 279 [parentView addSubview:languageSelectionView_]; | 279 [parentView addSubview:_languageSelectionView]; |
| 280 | 280 |
| 281 // Creates the navigation bar and its buttons. | 281 // Creates the navigation bar and its buttons. |
| 282 CGRect finalPickerFrame = CGRectMake( | 282 CGRect finalPickerFrame = CGRectMake( |
| 283 0, [languageSelectionView_ frame].size.height - kUIPickerHeight, | 283 0, [_languageSelectionView frame].size.height - kUIPickerHeight, |
| 284 [languageSelectionView_ frame].size.width, kUIPickerHeight); | 284 [_languageSelectionView frame].size.width, kUIPickerHeight); |
| 285 CGRect finalNavigationBarFrame = CGRectMake( | 285 CGRect finalNavigationBarFrame = CGRectMake( |
| 286 0, 0, [languageSelectionView_ frame].size.width, kNavigationBarHeight); | 286 0, 0, [_languageSelectionView frame].size.width, kNavigationBarHeight); |
| 287 // The language picker animates from the bottom of the screen. | 287 // The language picker animates from the bottom of the screen. |
| 288 CGRect initialPickerFrame = finalPickerFrame; | 288 CGRect initialPickerFrame = finalPickerFrame; |
| 289 initialPickerFrame.origin.y += totalPickerHeight; | 289 initialPickerFrame.origin.y += totalPickerHeight; |
| 290 CGRect initialNavigationBarFrame = finalNavigationBarFrame; | 290 CGRect initialNavigationBarFrame = finalNavigationBarFrame; |
| 291 initialNavigationBarFrame.origin.y += totalPickerHeight; | 291 initialNavigationBarFrame.origin.y += totalPickerHeight; |
| 292 | 292 |
| 293 navigationBar_.reset( | 293 _navigationBar.reset( |
| 294 [[UINavigationBar alloc] initWithFrame:initialNavigationBarFrame]); | 294 [[UINavigationBar alloc] initWithFrame:initialNavigationBarFrame]); |
| 295 const UIViewAutoresizing resizingMask = | 295 const UIViewAutoresizing resizingMask = |
| 296 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; | 296 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; |
| 297 [navigationBar_ setAutoresizingMask:resizingMask]; | 297 [_navigationBar setAutoresizingMask:resizingMask]; |
| 298 base::scoped_nsobject<UIBarButtonItem> doneButton([[UIBarButtonItem alloc] | 298 base::scoped_nsobject<UIBarButtonItem> doneButton([[UIBarButtonItem alloc] |
| 299 initWithBarButtonSystemItem:UIBarButtonSystemItemDone | 299 initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
| 300 target:self | 300 target:self |
| 301 action:@selector(languageSelectionDone)]); | 301 action:@selector(languageSelectionDone)]); |
| 302 base::scoped_nsobject<UIBarButtonItem> cancelButton([[UIBarButtonItem alloc] | 302 base::scoped_nsobject<UIBarButtonItem> cancelButton([[UIBarButtonItem alloc] |
| 303 initWithBarButtonSystemItem:UIBarButtonSystemItemCancel | 303 initWithBarButtonSystemItem:UIBarButtonSystemItemCancel |
| 304 target:self | 304 target:self |
| 305 action:@selector(dismissLanguageSelectionView)]); | 305 action:@selector(dismissLanguageSelectionView)]); |
| 306 base::scoped_nsobject<UINavigationItem> item( | 306 base::scoped_nsobject<UINavigationItem> item( |
| 307 [[UINavigationItem alloc] initWithTitle:nil]); | 307 [[UINavigationItem alloc] initWithTitle:nil]); |
| 308 [item setRightBarButtonItem:doneButton]; | 308 [item setRightBarButtonItem:doneButton]; |
| 309 [item setLeftBarButtonItem:cancelButton]; | 309 [item setLeftBarButtonItem:cancelButton]; |
| 310 [item setHidesBackButton:YES]; | 310 [item setHidesBackButton:YES]; |
| 311 [navigationBar_ pushNavigationItem:item animated:NO]; | 311 [_navigationBar pushNavigationItem:item animated:NO]; |
| 312 | 312 |
| 313 // Creates the PickerView and its controller. | 313 // Creates the PickerView and its controller. |
| 314 NSInteger selectedRow; | 314 NSInteger selectedRow; |
| 315 NSInteger disabledRow; | 315 NSInteger disabledRow; |
| 316 if (languageSelectionType_ == | 316 if (_languageSelectionType == |
| 317 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE) { | 317 TranslateInfoBarIOSTag::BEFORE_SOURCE_LANGUAGE) { |
| 318 selectedRow = translateInfoBarDelegate_->original_language_index(); | 318 selectedRow = _translateInfoBarDelegate->original_language_index(); |
| 319 disabledRow = translateInfoBarDelegate_->target_language_index(); | 319 disabledRow = _translateInfoBarDelegate->target_language_index(); |
| 320 } else { | 320 } else { |
| 321 selectedRow = translateInfoBarDelegate_->target_language_index(); | 321 selectedRow = _translateInfoBarDelegate->target_language_index(); |
| 322 disabledRow = translateInfoBarDelegate_->original_language_index(); | 322 disabledRow = _translateInfoBarDelegate->original_language_index(); |
| 323 } | 323 } |
| 324 languagePickerController_.reset([[LanguagePickerController alloc] | 324 _languagePickerController.reset([[LanguagePickerController alloc] |
| 325 initWithDelegate:translateInfoBarDelegate_ | 325 initWithDelegate:_translateInfoBarDelegate |
| 326 initialRow:selectedRow | 326 initialRow:selectedRow |
| 327 disabledRow:disabledRow]); | 327 disabledRow:disabledRow]); |
| 328 languagePicker_.reset( | 328 _languagePicker.reset( |
| 329 [[UIPickerView alloc] initWithFrame:initialPickerFrame]); | 329 [[UIPickerView alloc] initWithFrame:initialPickerFrame]); |
| 330 [languagePicker_ setAutoresizingMask:resizingMask]; | 330 [_languagePicker setAutoresizingMask:resizingMask]; |
| 331 [languagePicker_ setShowsSelectionIndicator:YES]; | 331 [_languagePicker setShowsSelectionIndicator:YES]; |
| 332 [languagePicker_ setDataSource:languagePickerController_]; | 332 [_languagePicker setDataSource:_languagePickerController]; |
| 333 [languagePicker_ setDelegate:languagePickerController_]; | 333 [_languagePicker setDelegate:_languagePickerController]; |
| 334 [languagePicker_ setShowsSelectionIndicator:YES]; | 334 [_languagePicker setShowsSelectionIndicator:YES]; |
| 335 [languagePicker_ setBackgroundColor:[infoBarView_ backgroundColor]]; | 335 [_languagePicker setBackgroundColor:[infoBarView_ backgroundColor]]; |
| 336 [languagePicker_ selectRow:selectedRow inComponent:0 animated:NO]; | 336 [_languagePicker selectRow:selectedRow inComponent:0 animated:NO]; |
| 337 | 337 |
| 338 [UIView animateWithDuration:kPickerAnimationDurationInSeconds | 338 [UIView animateWithDuration:kPickerAnimationDurationInSeconds |
| 339 animations:^{ | 339 animations:^{ |
| 340 languagePicker_.get().frame = finalPickerFrame; | 340 _languagePicker.get().frame = finalPickerFrame; |
| 341 navigationBar_.get().frame = finalNavigationBarFrame; | 341 _navigationBar.get().frame = finalNavigationBarFrame; |
| 342 }]; | 342 }]; |
| 343 | 343 |
| 344 // Add the subviews. | 344 // Add the subviews. |
| 345 [languageSelectionView_ addSubview:languagePicker_]; | 345 [_languageSelectionView addSubview:_languagePicker]; |
| 346 [languageSelectionView_ addSubview:navigationBar_]; | 346 [_languageSelectionView addSubview:_navigationBar]; |
| 347 } | 347 } |
| 348 | 348 |
| 349 - (void)removeView { | 349 - (void)removeView { |
| 350 [super removeView]; | 350 [super removeView]; |
| 351 [self dismissLanguageSelectionView]; | 351 [self dismissLanguageSelectionView]; |
| 352 } | 352 } |
| 353 | 353 |
| 354 - (void)detachView { | 354 - (void)detachView { |
| 355 [super detachView]; | 355 [super detachView]; |
| 356 [self dismissLanguageSelectionView]; | 356 [self dismissLanguageSelectionView]; |
| 357 } | 357 } |
| 358 | 358 |
| 359 @end | 359 @end |
| OLD | NEW |