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