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