Chromium Code Reviews| Index: ios/chrome/browser/ui/authentication/signin_promo_view.mm |
| diff --git a/ios/chrome/browser/ui/authentication/signin_promo_view.mm b/ios/chrome/browser/ui/authentication/signin_promo_view.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0df61dd63e8223a672aa408415d13fa6e162e8b2 |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/authentication/signin_promo_view.mm |
| @@ -0,0 +1,248 @@ |
| +// Copyright 2017 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. |
| + |
| +#import "ios/chrome/browser/ui/authentication/signin_promo_view.h" |
| + |
| +#include "base/logging.h" |
| +#import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| +#import "ios/chrome/browser/ui/uikit_ui_util.h" |
| +#include "ios/chrome/grit/ios_chromium_strings.h" |
| +#import "ios/third_party/material_components_ios/src/components/Buttons/src/MaterialButtons.h" |
| +#import "ios/third_party/material_components_ios/src/components/Typography/src/MaterialTypography.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +// Horizontal padding for label and buttons. |
| +const CGFloat kHorizontalPadding = 40; |
| +// Vertical padding for the image and the label. |
| +const CGFloat kVerticalPadding = 12; |
| +// Vertical padding for buttons. |
| +const CGFloat kButtonVerticalPadding = 6; |
| +// Image size for warm state. |
| +const CGFloat kProfileImageFixedSize = 48; |
| +// Image size for cold state. |
| +const CGFloat kChromeImageFixedSize = 24; |
| +// Button height. |
| +const CGFloat kButtonHeight = 36; |
| +} |
| + |
| +@implementation SigninPromoView { |
| + NSArray<NSLayoutConstraint*>* _coldStateConstraints; |
| + NSArray<NSLayoutConstraint*>* _warmStateConstraints; |
| +} |
| + |
| +@synthesize mode = _mode; |
| +@synthesize imageView = _imageView; |
| +@synthesize textLabel = _textLabel; |
| +@synthesize primaryButton = _primaryButton; |
| +@synthesize secondaryButton = _secondaryButton; |
| + |
| +- (instancetype)initWithFrame:(CGRect)frame { |
| + self = [super initWithFrame:frame]; |
| + if (self) { |
| + self.isAccessibilityElement = YES; |
| + |
| + // Adding subviews. |
| + self.clipsToBounds = YES; |
| + _imageView = [[UIImageView alloc] init]; |
| + _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| + [self addSubview:_imageView]; |
| + |
| + _textLabel = [[UILabel alloc] init]; |
| + _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| + [self addSubview:_textLabel]; |
| + |
| + _primaryButton = [[MDCFlatButton alloc] init]; |
| + _primaryButton.translatesAutoresizingMaskIntoConstraints = NO; |
| + _primaryButton.accessibilityIdentifier = @"signin_promo_primary_button"; |
| + [self addSubview:_primaryButton]; |
| + |
| + _secondaryButton = [[MDCFlatButton alloc] init]; |
| + _secondaryButton.translatesAutoresizingMaskIntoConstraints = NO; |
| + _secondaryButton.accessibilityIdentifier = @"signin_promo_secondary_button"; |
| + [self addSubview:_secondaryButton]; |
| + |
| + // Adding style. |
| + _imageView.contentMode = UIViewContentModeCenter; |
| + _imageView.layer.masksToBounds = YES; |
| + _imageView.contentMode = UIViewContentModeScaleAspectFit; |
| + |
| + _textLabel.font = [MDCTypography buttonFont]; |
| + _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| + _textLabel.numberOfLines = 0; |
| + _textLabel.textAlignment = NSTextAlignmentCenter; |
| + |
| + [_primaryButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] |
| + forState:UIControlStateNormal]; |
| + _primaryButton.customTitleColor = [UIColor whiteColor]; |
| + _primaryButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; |
| + |
| + _secondaryButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; |
| + _secondaryButton.uppercaseTitle = NO; |
| + |
| + // Adding constraints. |
| + NSDictionary* metrics = @{ |
| + @"kButtonHeight" : @(kButtonHeight), |
| + @"kButtonVerticalPadding" : @(kButtonVerticalPadding), |
| + @"kButtonVerticalPaddingx2" : @(kButtonVerticalPadding * 2), |
| + @"kChromeImageFixedSize" : @(kChromeImageFixedSize), |
| + @"kHorizontalPadding" : @(kHorizontalPadding), |
| + @"kVerticalPadding" : @(kVerticalPadding), |
| + @"kVerticalPaddingx2" : @(kVerticalPadding * 2), |
| + @"kVerticalPaddingkButtonVerticalPadding" : |
| + @(kVerticalPadding + kButtonVerticalPadding), |
| + }; |
| + NSDictionary* views = @{ |
| + @"imageView" : _imageView, |
| + @"primaryButton" : _primaryButton, |
| + @"secondaryButton" : _secondaryButton, |
| + @"textLabel" : _textLabel, |
| + }; |
| + |
| + // Constraints shared between modes. |
| + NSString* formatString = @"V:|-kVerticalPaddingx2-[imageView]-" |
| + "kVerticalPadding-[textLabel]-" |
| + "kVerticalPaddingkButtonVerticalPadding-[" |
| + "primaryButton(kButtonHeight)]"; |
| + NSArray* visualConstraints = @[ |
| + formatString, |
| + @"H:|-kHorizontalPadding-[primaryButton]-kHorizontalPadding-|" |
| + ]; |
| + ApplyVisualConstraintsWithMetricsAndOptions( |
| + visualConstraints, views, metrics, NSLayoutFormatAlignAllCenterX); |
| + |
| + // Constraints for cold state mode. |
| + NSMutableArray* constraints = [NSMutableArray array]; |
| + NSString* primaryButtonVerticalConstraint = |
| + @"V:[primaryButton]-kVerticalPaddingkButtonVerticalPadding-|"; |
| + [constraints addObjectsFromArray:[NSLayoutConstraint |
| + constraintsWithVisualFormat: |
| + primaryButtonVerticalConstraint |
| + options:0 |
| + metrics:metrics |
| + views:views]]; |
| + // Remove this rule once chrome image is added. |
| + NSString* imageViewVerticalSizeConstraint = |
| + @"V:[imageView(kChromeImageFixedSize)]"; |
| + [constraints addObjectsFromArray:[NSLayoutConstraint |
| + constraintsWithVisualFormat: |
| + imageViewVerticalSizeConstraint |
| + options:0 |
| + metrics:metrics |
| + views:views]]; |
| + _coldStateConstraints = [constraints copy]; |
| + |
| + // Constraints for warm state mode. |
| + constraints = [NSMutableArray array]; |
| + NSString* buttonsVerticalConstraint = |
| + @"V:[primaryButton]-kButtonVerticalPaddingx2-[" |
| + "secondaryButton(kButtonHeight)]-" |
| + "kVerticalPaddingkButtonVerticalPadding-|"; |
| + [constraints addObjectsFromArray: |
| + [NSLayoutConstraint |
| + constraintsWithVisualFormat:buttonsVerticalConstraint |
| + options:0 |
| + metrics:metrics |
| + views:views]]; |
| + NSString* secondaryButtonHorizontalConstraint = |
| + @"H:|-kHorizontalPadding-[secondaryButton]-kHorizontalPadding-|"; |
| + [constraints addObjectsFromArray:[NSLayoutConstraint |
| + constraintsWithVisualFormat: |
| + secondaryButtonHorizontalConstraint |
| + options:0 |
| + metrics:metrics |
| + views:views]]; |
| + _warmStateConstraints = [constraints copy]; |
| + |
| + _mode = SigninPromoViewModeColdState; |
| + [self activateColdMode]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)setMode:(SigninPromoViewMode)mode { |
| + if (mode == _mode) { |
| + return; |
| + } |
| + _mode = mode; |
| + switch (_mode) { |
| + case SigninPromoViewModeColdState: |
| + [self activateColdMode]; |
| + return; |
| + case SigninPromoViewModeWarmState: |
| + [self activateWarmMode]; |
| + return; |
| + } |
| + NOTREACHED(); |
| +} |
| + |
| +- (void)activateColdMode { |
| + // Needs to set the chromium icon in |imageView|. |
| + DCHECK_EQ(_mode, SigninPromoViewModeColdState); |
| + [NSLayoutConstraint deactivateConstraints:_warmStateConstraints]; |
| + [NSLayoutConstraint activateConstraints:_coldStateConstraints]; |
| + [_primaryButton |
| + setTitle:l10n_util::GetNSString(IDS_IOS_OPTIONS_IMPORT_DATA_TITLE_SIGNIN) |
| + forState:UIControlStateNormal]; |
| + _secondaryButton.hidden = YES; |
| +} |
| + |
| +- (void)activateWarmMode { |
| + DCHECK_EQ(_mode, SigninPromoViewModeWarmState); |
| + [NSLayoutConstraint deactivateConstraints:_coldStateConstraints]; |
| + [NSLayoutConstraint activateConstraints:_warmStateConstraints]; |
| + _secondaryButton.hidden = NO; |
| +} |
| + |
| +- (void)setProfileImage:(UIImage*)image { |
| + if (SigninPromoViewModeColdState == _mode) { |
|
msarda
2017/03/28 08:12:52
DCHECK_EQ is better than the if IMHO, for the foll
jlebel
2017/03/28 12:27:21
Done.
|
| + return; |
| + } |
| + _imageView.image = CircularImageFromImage(image, kProfileImageFixedSize); |
| +} |
| + |
| +- (void)accessibilityPrimaryAction:(id)unused { |
| + [_primaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; |
| +} |
| + |
| +- (void)accessibilitySecondaryAction:(id)unused { |
| + [_secondaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; |
| +} |
| + |
| +- (CGFloat)horizontalPadding { |
| + return kHorizontalPadding; |
| +} |
| + |
| +#pragma mark - NSObject(Accessibility) |
| + |
| +- (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { |
| + NSString* primaryActionName = |
| + [_primaryButton titleForState:UIControlStateNormal]; |
| + UIAccessibilityCustomAction* primaryCustomAction = |
| + [[UIAccessibilityCustomAction alloc] |
| + initWithName:primaryActionName |
| + target:self |
| + selector:@selector(accessibilityPrimaryAction:)]; |
| + if (_mode == SigninPromoViewModeColdState) { |
| + return @[ primaryCustomAction ]; |
| + } |
| + NSString* secondaryActionName = |
| + [_secondaryButton titleForState:UIControlStateNormal]; |
| + UIAccessibilityCustomAction* secondaryCustomAction = |
| + [[UIAccessibilityCustomAction alloc] |
| + initWithName:secondaryActionName |
| + target:self |
| + selector:@selector(accessibilitySecondaryAction:)]; |
| + return @[ primaryCustomAction, secondaryCustomAction ]; |
| +} |
| + |
| +- (NSString*)accessibilityLabel { |
| + return _textLabel.text; |
| +} |
| + |
| +@end |