summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/translate/translate_message_infobar_controller.mm
blob: c0b2c4abedcebefb9e056563011d48bf16747f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/chrome/browser/translate/translate_message_infobar_controller.h"

#include "base/strings/sys_string_conversions.h"
#include "components/translate/core/browser/translate_infobar_delegate.h"
#include "ios/chrome/browser/translate/translate_infobar_tags.h"
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
#import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h"
#import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h"
#include "ui/gfx/image/image.h"

@interface TranslateMessageInfoBarController ()

// Action for any of the user defined buttons.
- (void)infoBarButtonDidPress:(id)sender;

@end

@implementation TranslateMessageInfoBarController

- (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
    viewForDelegate:(infobars::InfoBarDelegate*)delegate
              frame:(CGRect)frame {
  base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
  translate::TranslateInfoBarDelegate* translateInfoBarDelegate =
      delegate->AsTranslateInfoBarDelegate();
  infoBarView.reset(
      ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
  // Icon
  gfx::Image icon = translateInfoBarDelegate->GetIcon();
  if (!icon.IsEmpty())
    [infoBarView addLeftIcon:icon.ToUIImage()];
  // Text.
  [infoBarView addLabel:base::SysUTF16ToNSString(
                            translateInfoBarDelegate->GetMessageInfoBarText())];
  // Close button.
  [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
                              target:self
                              action:@selector(infoBarButtonDidPress:)];
  // Other button.
  base::string16 buttonText(
      translateInfoBarDelegate->GetMessageInfoBarButtonText());
  if (!buttonText.empty()) {
    [infoBarView addButton:base::SysUTF16ToNSString(buttonText)
                       tag:TranslateInfoBarIOSTag::MESSAGE
                    target:self
                    action:@selector(infoBarButtonDidPress:)];
  }
  return infoBarView;
}

#pragma mark - Handling of User Events

- (void)infoBarButtonDidPress:(id)sender {
  // This press might have occurred after the user has already pressed a button,
  // in which case the view has been detached from the delegate and this press
  // should be ignored.
  if (!self.delegate) {
    return;
  }
  if ([sender isKindOfClass:[UIButton class]]) {
    NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
    if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
      self.delegate->InfoBarDidCancel();
    } else {
      DCHECK(buttonId == TranslateInfoBarIOSTag::MESSAGE);
      self.delegate->InfoBarButtonDidPress(buttonId);
    }
  }
}

@end