Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / translate / after_translate_infobar_controller.mm
blob4d7516f4c96e4559216853621ecab8a672742cf3
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.
5 #include "ios/chrome/browser/translate/after_translate_infobar_controller.h"
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"
19 @interface AfterTranslateInfoBarController () {
20   __weak translate::TranslateInfoBarDelegate* _translateInfoBarDelegate;
23 // Action for any of the user defined buttons.
24 - (void)infoBarButtonDidPress:(id)sender;
26 @end
28 @implementation AfterTranslateInfoBarController
30 #pragma mark -
31 #pragma mark InfoBarControllerProtocol
33 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
34     viewForDelegate:(infobars::InfoBarDelegate*)delegate
35               frame:(CGRect)frame {
36   base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
37   _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate();
38   DCHECK(_translateInfoBarDelegate);
39   infoBarView.reset(
40       ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
41   // Icon
42   gfx::Image icon = _translateInfoBarDelegate->GetIcon();
43   if (!icon.IsEmpty())
44     [infoBarView addLeftIcon:icon.ToUIImage()];
45   // Main text.
46   const bool autodeterminedSourceLanguage =
47       _translateInfoBarDelegate->original_language_index() ==
48       translate::TranslateInfoBarDelegate::kNoIndex;
49   bool swappedLanguageButtons;
50   std::vector<base::string16> strings;
51   translate::TranslateInfoBarDelegate::GetAfterTranslateStrings(
52       &strings, &swappedLanguageButtons, autodeterminedSourceLanguage);
53   DCHECK_EQ(autodeterminedSourceLanguage ? 2U : 3U, strings.size());
54   NSString* label1 = base::SysUTF16ToNSString(strings[0]);
55   NSString* label2 = base::SysUTF16ToNSString(strings[1]);
56   NSString* label3 = autodeterminedSourceLanguage
57                          ? @""
58                          : base::SysUTF16ToNSString(strings[2]);
59   base::string16 stdOriginal = _translateInfoBarDelegate->language_name_at(
60       _translateInfoBarDelegate->original_language_index());
61   NSString* original = base::SysUTF16ToNSString(stdOriginal);
62   NSString* target =
63       base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at(
64           _translateInfoBarDelegate->target_language_index()));
65   base::scoped_nsobject<NSString> label(
66       [[NSString alloc] initWithFormat:@"%@ %@ %@%@ %@.", label1, original,
67                                        label2, label3, target]);
68   [infoBarView addLabel:label];
69   // Close button.
70   [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
71                               target:self
72                               action:@selector(infoBarButtonDidPress:)];
73   // Other buttons.
74   NSString* buttonRevert = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_REVERT);
75   NSString* buttonOptions = base::SysUTF16ToNSString(
76       ios::GetChromeBrowserProvider()->GetStringProvider()->GetDoneString());
77   [infoBarView addButton1:buttonOptions
78                      tag1:TranslateInfoBarIOSTag::AFTER_DONE
79                   button2:buttonRevert
80                      tag2:TranslateInfoBarIOSTag::AFTER_REVERT
81                    target:self
82                    action:@selector(infoBarButtonDidPress:)];
83   // Always translate switch.
84   if (_translateInfoBarDelegate->ShouldShowAlwaysTranslateShortcut()) {
85     base::string16 alwaysTranslate = l10n_util::GetStringFUTF16(
86         IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, stdOriginal);
87     const BOOL switchValue = _translateInfoBarDelegate->ShouldAlwaysTranslate();
88     [infoBarView
89         addSwitchWithLabel:base::SysUTF16ToNSString(alwaysTranslate)
90                       isOn:switchValue
91                        tag:TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH
92                     target:self
93                     action:@selector(infoBarSwitchDidPress:)];
94   }
95   return infoBarView;
98 #pragma mark - Handling of User Events
100 - (void)infoBarButtonDidPress:(id)sender {
101   // This press might have occurred after the user has already pressed a button,
102   // in which case the view has been detached from the delegate and this press
103   // should be ignored.
104   if (!self.delegate) {
105     return;
106   }
107   if ([sender isKindOfClass:[UIButton class]]) {
108     NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
109     if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
110       self.delegate->InfoBarDidCancel();
111     } else {
112       DCHECK(buttonId == TranslateInfoBarIOSTag::AFTER_REVERT ||
113              buttonId == TranslateInfoBarIOSTag::AFTER_DONE);
114       self.delegate->InfoBarButtonDidPress(buttonId);
115     }
116   }
119 - (void)infoBarSwitchDidPress:(id)sender {
120   DCHECK_EQ(TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH, [sender tag]);
121   DCHECK([sender respondsToSelector:@selector(isOn)]);
122   if (_translateInfoBarDelegate->ShouldAlwaysTranslate() != [sender isOn])
123     _translateInfoBarDelegate->ToggleAlwaysTranslate();
126 @end