OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #import "ios/chrome/browser/ui/authentication/signin_promo_view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 9 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 10 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 11 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" |
| 12 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // Horizontal padding for label and buttons. |
| 21 const CGFloat kHorizontalPadding = 40; |
| 22 // Vertical padding for the image and the label. |
| 23 const CGFloat kVerticalPadding = 12; |
| 24 // Vertical padding for buttons. |
| 25 const CGFloat kButtonVerticalPadding = 6; |
| 26 // Image size for warm state. |
| 27 const CGFloat kProfileImageFixedSize = 48; |
| 28 // Image size for cold state. |
| 29 const CGFloat kChromeImageFixedSize = 24; |
| 30 // Button height. |
| 31 const CGFloat kButtonHeight = 36; |
| 32 } |
| 33 |
| 34 @implementation SigninPromoView { |
| 35 NSArray<NSLayoutConstraint*>* _coldStateConstraints; |
| 36 NSArray<NSLayoutConstraint*>* _warmStateConstraints; |
| 37 } |
| 38 |
| 39 @synthesize mode = _mode; |
| 40 @synthesize imageView = _imageView; |
| 41 @synthesize textLabel = _textLabel; |
| 42 @synthesize primaryButton = _primaryButton; |
| 43 @synthesize secondaryButton = _secondaryButton; |
| 44 |
| 45 - (instancetype)initWithFrame:(CGRect)frame { |
| 46 self = [super initWithFrame:frame]; |
| 47 if (self) { |
| 48 self.isAccessibilityElement = YES; |
| 49 |
| 50 // Adding subviews. |
| 51 self.clipsToBounds = YES; |
| 52 _imageView = [[UIImageView alloc] init]; |
| 53 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 54 [self addSubview:_imageView]; |
| 55 |
| 56 _textLabel = [[UILabel alloc] init]; |
| 57 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 58 [self addSubview:_textLabel]; |
| 59 |
| 60 _primaryButton = [[MDCFlatButton alloc] init]; |
| 61 _primaryButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 62 _primaryButton.accessibilityIdentifier = @"signin_promo_primary_button"; |
| 63 [self addSubview:_primaryButton]; |
| 64 |
| 65 _secondaryButton = [[MDCFlatButton alloc] init]; |
| 66 _secondaryButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 67 _secondaryButton.accessibilityIdentifier = @"signin_promo_secondary_button"; |
| 68 [self addSubview:_secondaryButton]; |
| 69 |
| 70 // Adding style. |
| 71 _imageView.contentMode = UIViewContentModeCenter; |
| 72 _imageView.layer.masksToBounds = YES; |
| 73 _imageView.contentMode = UIViewContentModeScaleAspectFit; |
| 74 |
| 75 _textLabel.font = [MDCTypography buttonFont]; |
| 76 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 77 _textLabel.numberOfLines = 0; |
| 78 _textLabel.textAlignment = NSTextAlignmentCenter; |
| 79 |
| 80 [_primaryButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] |
| 81 forState:UIControlStateNormal]; |
| 82 _primaryButton.customTitleColor = [UIColor whiteColor]; |
| 83 _primaryButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; |
| 84 |
| 85 _secondaryButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; |
| 86 _secondaryButton.uppercaseTitle = NO; |
| 87 |
| 88 // Adding constraints. |
| 89 NSDictionary* metrics = @{ |
| 90 @"kButtonHeight" : @(kButtonHeight), |
| 91 @"kButtonVerticalPadding" : @(kButtonVerticalPadding), |
| 92 @"kButtonVerticalPaddingx2" : @(kButtonVerticalPadding * 2), |
| 93 @"kChromeImageFixedSize" : @(kChromeImageFixedSize), |
| 94 @"kHorizontalPadding" : @(kHorizontalPadding), |
| 95 @"kVerticalPadding" : @(kVerticalPadding), |
| 96 @"kVerticalPaddingx2" : @(kVerticalPadding * 2), |
| 97 @"kVerticalPaddingkButtonVerticalPadding" : |
| 98 @(kVerticalPadding + kButtonVerticalPadding), |
| 99 }; |
| 100 NSDictionary* views = @{ |
| 101 @"imageView" : _imageView, |
| 102 @"primaryButton" : _primaryButton, |
| 103 @"secondaryButton" : _secondaryButton, |
| 104 @"textLabel" : _textLabel, |
| 105 }; |
| 106 |
| 107 // Constraints shared between modes. |
| 108 NSString* formatString = @"V:|-kVerticalPaddingx2-[imageView]-" |
| 109 "kVerticalPadding-[textLabel]-" |
| 110 "kVerticalPaddingkButtonVerticalPadding-[" |
| 111 "primaryButton(kButtonHeight)]"; |
| 112 NSArray* visualConstraints = @[ |
| 113 formatString, |
| 114 @"H:|-kHorizontalPadding-[primaryButton]-kHorizontalPadding-|" |
| 115 ]; |
| 116 ApplyVisualConstraintsWithMetricsAndOptions( |
| 117 visualConstraints, views, metrics, NSLayoutFormatAlignAllCenterX); |
| 118 |
| 119 // Constraints for cold state mode. |
| 120 NSMutableArray* constraints = [NSMutableArray array]; |
| 121 NSString* primaryButtonVerticalConstraint = |
| 122 @"V:[primaryButton]-kVerticalPaddingkButtonVerticalPadding-|"; |
| 123 [constraints addObjectsFromArray:[NSLayoutConstraint |
| 124 constraintsWithVisualFormat: |
| 125 primaryButtonVerticalConstraint |
| 126 options:0 |
| 127 metrics:metrics |
| 128 views:views]]; |
| 129 // Remove this rule once chrome image is added. |
| 130 NSString* imageViewVerticalSizeConstraint = |
| 131 @"V:[imageView(kChromeImageFixedSize)]"; |
| 132 [constraints addObjectsFromArray:[NSLayoutConstraint |
| 133 constraintsWithVisualFormat: |
| 134 imageViewVerticalSizeConstraint |
| 135 options:0 |
| 136 metrics:metrics |
| 137 views:views]]; |
| 138 _coldStateConstraints = [constraints copy]; |
| 139 |
| 140 // Constraints for warm state mode. |
| 141 constraints = [NSMutableArray array]; |
| 142 NSString* buttonsVerticalConstraint = |
| 143 @"V:[primaryButton]-kButtonVerticalPaddingx2-[" |
| 144 "secondaryButton(kButtonHeight)]-" |
| 145 "kVerticalPaddingkButtonVerticalPadding-|"; |
| 146 [constraints addObjectsFromArray: |
| 147 [NSLayoutConstraint |
| 148 constraintsWithVisualFormat:buttonsVerticalConstraint |
| 149 options:0 |
| 150 metrics:metrics |
| 151 views:views]]; |
| 152 NSString* secondaryButtonHorizontalConstraint = |
| 153 @"H:|-kHorizontalPadding-[secondaryButton]-kHorizontalPadding-|"; |
| 154 [constraints addObjectsFromArray:[NSLayoutConstraint |
| 155 constraintsWithVisualFormat: |
| 156 secondaryButtonHorizontalConstraint |
| 157 options:0 |
| 158 metrics:metrics |
| 159 views:views]]; |
| 160 _warmStateConstraints = [constraints copy]; |
| 161 |
| 162 _mode = SigninPromoViewModeColdState; |
| 163 [self activateColdMode]; |
| 164 } |
| 165 return self; |
| 166 } |
| 167 |
| 168 - (void)setMode:(SigninPromoViewMode)mode { |
| 169 if (mode == _mode) { |
| 170 return; |
| 171 } |
| 172 _mode = mode; |
| 173 switch (_mode) { |
| 174 case SigninPromoViewModeColdState: |
| 175 [self activateColdMode]; |
| 176 return; |
| 177 case SigninPromoViewModeWarmState: |
| 178 [self activateWarmMode]; |
| 179 return; |
| 180 } |
| 181 NOTREACHED(); |
| 182 } |
| 183 |
| 184 - (void)activateColdMode { |
| 185 // Needs to set the chromium icon in |imageView|. |
| 186 DCHECK_EQ(_mode, SigninPromoViewModeColdState); |
| 187 [NSLayoutConstraint deactivateConstraints:_warmStateConstraints]; |
| 188 [NSLayoutConstraint activateConstraints:_coldStateConstraints]; |
| 189 [_primaryButton |
| 190 setTitle:l10n_util::GetNSString(IDS_IOS_OPTIONS_IMPORT_DATA_TITLE_SIGNIN) |
| 191 forState:UIControlStateNormal]; |
| 192 _secondaryButton.hidden = YES; |
| 193 } |
| 194 |
| 195 - (void)activateWarmMode { |
| 196 DCHECK_EQ(_mode, SigninPromoViewModeWarmState); |
| 197 [NSLayoutConstraint deactivateConstraints:_coldStateConstraints]; |
| 198 [NSLayoutConstraint activateConstraints:_warmStateConstraints]; |
| 199 _secondaryButton.hidden = NO; |
| 200 } |
| 201 |
| 202 - (void)setProfileImage:(UIImage*)image { |
| 203 DCHECK_EQ(SigninPromoViewModeWarmState, _mode); |
| 204 _imageView.image = CircularImageFromImage(image, kProfileImageFixedSize); |
| 205 } |
| 206 |
| 207 - (void)accessibilityPrimaryAction:(id)unused { |
| 208 [_primaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; |
| 209 } |
| 210 |
| 211 - (void)accessibilitySecondaryAction:(id)unused { |
| 212 [_secondaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; |
| 213 } |
| 214 |
| 215 - (CGFloat)horizontalPadding { |
| 216 return kHorizontalPadding; |
| 217 } |
| 218 |
| 219 #pragma mark - NSObject(Accessibility) |
| 220 |
| 221 - (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { |
| 222 NSString* primaryActionName = |
| 223 [_primaryButton titleForState:UIControlStateNormal]; |
| 224 UIAccessibilityCustomAction* primaryCustomAction = |
| 225 [[UIAccessibilityCustomAction alloc] |
| 226 initWithName:primaryActionName |
| 227 target:self |
| 228 selector:@selector(accessibilityPrimaryAction:)]; |
| 229 if (_mode == SigninPromoViewModeColdState) { |
| 230 return @[ primaryCustomAction ]; |
| 231 } |
| 232 NSString* secondaryActionName = |
| 233 [_secondaryButton titleForState:UIControlStateNormal]; |
| 234 UIAccessibilityCustomAction* secondaryCustomAction = |
| 235 [[UIAccessibilityCustomAction alloc] |
| 236 initWithName:secondaryActionName |
| 237 target:self |
| 238 selector:@selector(accessibilitySecondaryAction:)]; |
| 239 return @[ primaryCustomAction, secondaryCustomAction ]; |
| 240 } |
| 241 |
| 242 - (NSString*)accessibilityLabel { |
| 243 return _textLabel.text; |
| 244 } |
| 245 |
| 246 @end |
OLD | NEW |