OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/translate/translate_message_infobar_controller.h" |
| 6 |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 9 #include "ios/chrome/browser/translate/translate_infobar_tags.h" |
| 10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 11 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h" |
| 12 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h" |
| 13 #include "ui/gfx/image/image.h" |
| 14 |
| 15 @interface TranslateMessageInfoBarController () |
| 16 |
| 17 // Action for any of the user defined buttons. |
| 18 - (void)infoBarButtonDidPress:(id)sender; |
| 19 |
| 20 @end |
| 21 |
| 22 @implementation TranslateMessageInfoBarController |
| 23 |
| 24 #pragma mark - |
| 25 #pragma mark InfoBarControllerProtocol |
| 26 |
| 27 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate |
| 28 frame:(CGRect)frame { |
| 29 translate::TranslateInfoBarDelegate* translateInfoBarDelegate = |
| 30 delegate->AsTranslateInfoBarDelegate(); |
| 31 infobars::InfoBarDelegate* infoBarDelegate = |
| 32 static_cast<infobars::InfoBarDelegate*>(translateInfoBarDelegate); |
| 33 DCHECK(!infoBarView_); |
| 34 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView() |
| 35 initWithFrame:frame |
| 36 delegate:delegate_ |
| 37 isWarning:infoBarDelegate->GetInfoBarType() == |
| 38 infobars::InfoBarDelegate::WARNING_TYPE]); |
| 39 // Icon |
| 40 gfx::Image icon = translateInfoBarDelegate->GetIcon(); |
| 41 if (!icon.IsEmpty()) |
| 42 [infoBarView_ addLeftIcon:icon.ToUIImage()]; |
| 43 // Text. |
| 44 [infoBarView_ |
| 45 addLabel:base::SysUTF16ToNSString( |
| 46 translateInfoBarDelegate->GetMessageInfoBarText())]; |
| 47 // Close button. |
| 48 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE |
| 49 target:self |
| 50 action:@selector(infoBarButtonDidPress:)]; |
| 51 // Other button. |
| 52 base::string16 buttonText( |
| 53 translateInfoBarDelegate->GetMessageInfoBarButtonText()); |
| 54 if (!buttonText.empty()) { |
| 55 [infoBarView_ addButton:base::SysUTF16ToNSString(buttonText) |
| 56 tag:TranslateInfoBarIOSTag::MESSAGE |
| 57 target:self |
| 58 action:@selector(infoBarButtonDidPress:)]; |
| 59 } |
| 60 } |
| 61 |
| 62 #pragma mark - Handling of User Events |
| 63 |
| 64 - (void)infoBarButtonDidPress:(id)sender { |
| 65 // This press might have occurred after the user has already pressed a button, |
| 66 // in which case the view has been detached from the delegate and this press |
| 67 // should be ignored. |
| 68 if (!delegate_) { |
| 69 return; |
| 70 } |
| 71 if ([sender isKindOfClass:[UIButton class]]) { |
| 72 NSUInteger buttonId = static_cast<UIButton*>(sender).tag; |
| 73 if (buttonId == TranslateInfoBarIOSTag::CLOSE) { |
| 74 delegate_->InfoBarDidCancel(); |
| 75 } else { |
| 76 DCHECK(buttonId == TranslateInfoBarIOSTag::MESSAGE); |
| 77 delegate_->InfoBarButtonDidPress(buttonId); |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 @end |
OLD | NEW |