| // 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/mac/foundation_util.h" |
| #include "base/strings/sys_string_conversions.h" |
| #include "components/translate/core/browser/translate_infobar_delegate.h" |
| #import "ios/chrome/browser/infobars/infobar_controller+protected.h" |
| #include "ios/chrome/browser/infobars/infobar_controller_delegate.h" |
| #include "ios/chrome/browser/translate/translate_infobar_tags.h" |
| #import "ios/chrome/browser/ui/infobars/confirm_infobar_view.h" |
| #include "ui/gfx/image/image.h" |
| |
| #if !defined(__has_feature) || !__has_feature(objc_arc) |
| #error "This file requires ARC support." |
| #endif |
| |
| @interface TranslateMessageInfoBarController () |
| |
| // Overrides superclass property. |
| @property(nonatomic, readonly) |
| translate::TranslateInfoBarDelegate* infoBarDelegate; |
| |
| @end |
| |
| @implementation TranslateMessageInfoBarController |
| |
| @dynamic infoBarDelegate; |
| |
| - (instancetype)initWithInfoBarDelegate: |
| (translate::TranslateInfoBarDelegate*)infoBarDelegate { |
| return [super initWithInfoBarDelegate:infoBarDelegate]; |
| } |
| |
| - (UIView*)infobarView { |
| ConfirmInfoBarView* infoBarView = |
| [[ConfirmInfoBarView alloc] initWithFrame:CGRectZero]; |
| // Icon |
| gfx::Image icon = self.infoBarDelegate->GetIcon(); |
| if (!icon.IsEmpty()) |
| [infoBarView addLeftIcon:icon.ToUIImage()]; |
| // Text. |
| [infoBarView addLabel:base::SysUTF16ToNSString( |
| self.infoBarDelegate->GetMessageInfoBarText())]; |
| // Close button. |
| [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE |
| target:self |
| action:@selector(infoBarButtonDidPress:)]; |
| // Other button. |
| base::string16 buttonText( |
| self.infoBarDelegate->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 { |
| if ([self shouldIgnoreUserInteraction]) |
| return; |
| |
| NSUInteger buttonId = base::mac::ObjCCastStrict<UIButton>(sender).tag; |
| switch (buttonId) { |
| case TranslateInfoBarIOSTag::CLOSE: |
| self.infoBarDelegate->InfoBarDismissed(); |
| self.delegate->RemoveInfoBar(); |
| break; |
| case TranslateInfoBarIOSTag::MESSAGE: |
| self.infoBarDelegate->MessageInfoBarButtonPressed(); |
| break; |
| default: |
| NOTREACHED() << "Unexpected Translate button label"; |
| break; |
| } |
| } |
| |
| @end |