Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: ios/chrome/browser/translate/after_translate_infobar_controller.mm

Issue 809333003: Upstream //ios/chrome/browser/translate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/after_translate_infobar_controller.h"
6
7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #include "grit/components_strings.h"
11 #include "ios/chrome/browser/translate/translate_infobar_tags.h"
12 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
13 #include "ios/public/provider/chrome/browser/string_provider.h"
14 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h"
15 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/gfx/image/image.h"
18
19 @interface AfterTranslateInfoBarController () {
20 __weak translate::TranslateInfoBarDelegate* translateInfoBarDelegate_;
21 }
22
23 // Action for any of the user defined buttons.
24 - (void)infoBarButtonDidPress:(id)sender;
25
26 @end
27
28 @implementation AfterTranslateInfoBarController
29
30 #pragma mark -
31 #pragma mark InfoBarControllerProtocol
32
33 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate
34 frame:(CGRect)frame {
35 translateInfoBarDelegate_ = delegate->AsTranslateInfoBarDelegate();
36 DCHECK(translateInfoBarDelegate_);
37 infobars::InfoBarDelegate* infoBarDelegate =
38 static_cast<infobars::InfoBarDelegate*>(translateInfoBarDelegate_);
39 DCHECK(!infoBarView_);
40 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView()
41 initWithFrame:frame
42 delegate:delegate_
43 isWarning:infoBarDelegate->GetInfoBarType() ==
44 infobars::InfoBarDelegate::WARNING_TYPE]);
45 // Icon
46 gfx::Image icon = translateInfoBarDelegate_->GetIcon();
47 if (!icon.IsEmpty())
48 [infoBarView_ addLeftIcon:icon.ToUIImage()];
49 // Main text.
50 const bool autodeterminedSourceLanguage =
51 translateInfoBarDelegate_->original_language_index() ==
52 translate::TranslateInfoBarDelegate::kNoIndex;
53 bool swappedLanguageButtons;
54 std::vector<base::string16> strings;
55 translate::TranslateInfoBarDelegate::GetAfterTranslateStrings(
56 &strings, &swappedLanguageButtons, autodeterminedSourceLanguage);
57 DCHECK_EQ(autodeterminedSourceLanguage ? 2U : 3U, strings.size());
58 NSString* label1 = base::SysUTF16ToNSString(strings[0]);
59 NSString* label2 = base::SysUTF16ToNSString(strings[1]);
60 NSString* label3 = autodeterminedSourceLanguage
61 ? @""
62 : base::SysUTF16ToNSString(strings[2]);
63 base::string16 stdOriginal = translateInfoBarDelegate_->language_name_at(
64 translateInfoBarDelegate_->original_language_index());
65 NSString* original = base::SysUTF16ToNSString(stdOriginal);
66 NSString* target =
67 base::SysUTF16ToNSString(translateInfoBarDelegate_->language_name_at(
68 translateInfoBarDelegate_->target_language_index()));
69 base::scoped_nsobject<NSString> label(
70 [[NSString alloc] initWithFormat:@"%@ %@ %@%@ %@.", label1, original,
71 label2, label3, target]);
72 [infoBarView_ addLabel:label];
73 // Close button.
74 [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
75 target:self
76 action:@selector(infoBarButtonDidPress:)];
77 // Other buttons.
78 NSString* buttonRevert = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_REVERT);
79 NSString* buttonOptions = base::SysUTF16ToNSString(
80 ios::GetChromeBrowserProvider()->GetStringProvider()->GetDoneString());
81 [infoBarView_ addButton1:buttonOptions
82 tag1:TranslateInfoBarIOSTag::AFTER_DONE
83 button2:buttonRevert
84 tag2:TranslateInfoBarIOSTag::AFTER_REVERT
85 target:self
86 action:@selector(infoBarButtonDidPress:)];
87 // Always translate switch.
88 if (translateInfoBarDelegate_->ShouldShowAlwaysTranslateShortcut()) {
89 base::string16 alwaysTranslate = l10n_util::GetStringFUTF16(
90 IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, stdOriginal);
91 const BOOL switchValue = translateInfoBarDelegate_->ShouldAlwaysTranslate();
92 [infoBarView_
93 addSwitchWithLabel:base::SysUTF16ToNSString(alwaysTranslate)
94 isOn:switchValue
95 tag:TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH
96 target:self
97 action:@selector(infoBarSwitchDidPress:)];
98 }
99 }
100
101 #pragma mark - Handling of User Events
102
103 - (void)infoBarButtonDidPress:(id)sender {
104 // This press might have occurred after the user has already pressed a button,
105 // in which case the view has been detached from the delegate and this press
106 // should be ignored.
107 if (!delegate_) {
108 return;
109 }
110 if ([sender isKindOfClass:[UIButton class]]) {
111 NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
112 if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
113 delegate_->InfoBarDidCancel();
114 } else {
115 DCHECK(buttonId == TranslateInfoBarIOSTag::AFTER_REVERT ||
116 buttonId == TranslateInfoBarIOSTag::AFTER_DONE);
117 delegate_->InfoBarButtonDidPress(buttonId);
118 }
119 }
120 }
121
122 - (void)infoBarSwitchDidPress:(id)sender {
123 DCHECK_EQ(TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH, [sender tag]);
124 DCHECK([sender respondsToSelector:@selector(isOn)]);
125 if (translateInfoBarDelegate_->ShouldAlwaysTranslate() != [sender isOn])
126 translateInfoBarDelegate_->ToggleAlwaysTranslate();
127 }
128
129 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698