Chromium Code Reviews| 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_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 | |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 15 #error "This file requires ARC support." | |
| 16 #endif | |
| 17 | |
| 18 namespace { | |
| 19 // Horizontal padding for label and buttons. | |
| 20 const CGFloat kHorizontalPadding = 40; | |
| 21 // Vertical padding for the image and the label. | |
| 22 const CGFloat kVerticalPadding = 12; | |
| 23 // Vertical padding for buttons. | |
| 24 const CGFloat kButtonVerticalPadding = 6; | |
| 25 // Image size for warm start. | |
| 26 const CGFloat kProfileImageFixedSize = 48; | |
| 27 // Image size for cold start. | |
| 28 const CGFloat kChromeImageFixedSize = 24; | |
|
msarda
2017/03/22 12:18:39
I expect the profile image and the chrome image sh
lpromero
2017/03/24 10:36:20
That would be simpler for us. Try to push back to
jlebel
2017/03/24 20:59:08
I don't understand my code already handles the 2 s
| |
| 29 // Button height. | |
| 30 const CGFloat kButtonHeight = 36; | |
| 31 } | |
| 32 | |
| 33 @implementation SigninPromoView { | |
| 34 SigninPromoViewMode _mode; | |
|
lpromero
2017/03/24 10:36:20
These 5 ivars are not needed, as they are generate
jlebel
2017/03/24 20:59:09
Done.
| |
| 35 UIImageView* _imageView; | |
| 36 UILabel* _textLabel; | |
| 37 MDCFlatButton* _primaryButton; | |
| 38 MDCFlatButton* _secondaryButton; | |
| 39 | |
| 40 NSArray<NSLayoutConstraint*>* _coldStartConstraints; | |
|
msarda
2017/03/22 12:18:39
s/_coldStartConstraints/_secondaryButtonVisibleCon
lpromero
2017/03/24 10:36:20
Actually, I like the abstraction the names give.
jlebel
2017/03/24 20:59:09
Acknowledged.
| |
| 41 NSArray<NSLayoutConstraint*>* _warmStartConstraints; | |
|
msarda
2017/03/22 12:18:39
s/_coldStartConstraints/_secondaryButtonHiddenCons
jlebel
2017/03/24 20:59:09
Acknowledged.
| |
| 42 } | |
| 43 | |
| 44 @synthesize mode = _mode; | |
| 45 @synthesize imageView = _imageView; | |
| 46 @synthesize textLabel = _textLabel; | |
| 47 @synthesize primaryButton = _primaryButton; | |
| 48 @synthesize secondaryButton = _secondaryButton; | |
| 49 | |
| 50 - (instancetype)initWithCoder:(NSCoder*)aDecoder { | |
|
lpromero
2017/03/24 10:36:20
No need for this (see similar comment above or bel
jlebel
2017/03/24 20:59:09
Done.
| |
| 51 NOTREACHED(); | |
| 52 return nil; | |
| 53 } | |
| 54 | |
| 55 - (instancetype)initWithFrame:(CGRect)frame { | |
| 56 self = [super initWithFrame:frame]; | |
| 57 if (self) { | |
| 58 self.translatesAutoresizingMaskIntoConstraints = NO; | |
| 59 self.isAccessibilityElement = YES; | |
| 60 [self addSubviews]; | |
| 61 [self setDefaultViewStyling]; | |
| 62 [self setViewConstraints]; | |
|
msarda
2017/03/22 12:18:39
Consider calling [self setMode:SigninPromoViewCold
lpromero
2017/03/24 10:36:20
Ha! Mihai, you advised against calling methods in
jlebel
2017/03/24 20:59:09
I can't call -[SigninPromoView setMode:] in the in
| |
| 63 _mode = SigninPromoViewColdStartMode; | |
| 64 [self activateColdMode]; | |
| 65 } | |
| 66 return self; | |
| 67 } | |
| 68 | |
| 69 - (void)addSubviews { | |
| 70 self.clipsToBounds = YES; | |
| 71 | |
| 72 _imageView = [[UIImageView alloc] init]; | |
| 73 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
| 74 [self addSubview:_imageView]; | |
| 75 | |
| 76 _textLabel = [[UILabel alloc] init]; | |
| 77 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 78 [self addSubview:_textLabel]; | |
| 79 | |
| 80 _primaryButton = [[MDCFlatButton alloc] init]; | |
| 81 _primaryButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 82 _primaryButton.accessibilityIdentifier = @"signin_promo_primary_button"; | |
| 83 [self addSubview:_primaryButton]; | |
| 84 | |
| 85 _secondaryButton = [[MDCFlatButton alloc] init]; | |
| 86 _secondaryButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 87 _secondaryButton.accessibilityIdentifier = @"signin_promo_secondary_button"; | |
| 88 [self addSubview:_secondaryButton]; | |
| 89 } | |
| 90 | |
| 91 - (void)setDefaultViewStyling { | |
| 92 _imageView.contentMode = UIViewContentModeCenter; | |
| 93 _imageView.layer.masksToBounds = YES; | |
| 94 _imageView.contentMode = UIViewContentModeScaleAspectFit; | |
| 95 | |
| 96 _textLabel.font = [MDCTypography buttonFont]; | |
| 97 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
| 98 _textLabel.numberOfLines = 0; | |
| 99 _textLabel.textAlignment = NSTextAlignmentCenter; | |
| 100 | |
| 101 [_primaryButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] | |
| 102 forState:UIControlStateNormal]; | |
| 103 _primaryButton.customTitleColor = [UIColor whiteColor]; | |
| 104 _primaryButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; | |
| 105 | |
| 106 _secondaryButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; | |
| 107 _secondaryButton.uppercaseTitle = NO; | |
| 108 } | |
| 109 | |
| 110 - (void)setViewConstraints { | |
| 111 NSDictionary* metrics = @{ | |
| 112 @"kButtonHeight" : @(kButtonHeight), | |
| 113 @"kButtonVerticalPadding" : @(kButtonVerticalPadding), | |
| 114 @"kButtonVerticalPaddingx2" : @(kButtonVerticalPadding * 2), | |
| 115 @"kChromeImageFixedSize" : @(kChromeImageFixedSize), | |
| 116 @"kHorizontalPadding" : @(kHorizontalPadding), | |
| 117 @"kProfileImageFixedSize" : @(kProfileImageFixedSize), | |
| 118 @"kVerticalPadding" : @(kVerticalPadding), | |
| 119 @"kVerticalPaddingx2" : @(kVerticalPadding * 2), | |
| 120 @"kVerticalPaddingkButtonVerticalPadding" : | |
| 121 @(kVerticalPadding + kButtonVerticalPadding), | |
| 122 }; | |
| 123 NSDictionary* views = @{ | |
| 124 @"imageView" : _imageView, | |
| 125 @"primaryButton" : _primaryButton, | |
| 126 @"secondaryButton" : _secondaryButton, | |
| 127 @"textLabel" : _textLabel, | |
| 128 }; | |
| 129 | |
| 130 // Constraints in shared between modes. | |
|
msarda
2017/03/22 12:18:39
Constraints shared between modes.
jlebel
2017/03/24 20:59:09
Done.
| |
| 131 NSString* formatString = | |
| 132 @"V:|-kVerticalPaddingx2-[imageView]-kVerticalPadding-[textLabel]-" | |
| 133 @"kVerticalPaddingkButtonVerticalPadding-[primaryButton(kButtonHeight)]"; | |
|
msarda
2017/03/22 12:18:39
Personal pref: Consider removing the @ on this sec
lpromero
2017/03/24 10:36:20
Agreed, I thought it was a second string.
jlebel
2017/03/24 20:59:09
Done.
| |
| 134 [NSLayoutConstraint | |
| 135 activateConstraints: | |
| 136 [NSLayoutConstraint | |
| 137 constraintsWithVisualFormat:formatString | |
| 138 options:NSLayoutFormatAlignAllCenterX | |
| 139 metrics:metrics | |
| 140 views:views]]; | |
| 141 | |
| 142 formatString = @"H:|-kHorizontalPadding-[primaryButton]-kHorizontalPadding-|"; | |
|
msarda
2017/03/22 12:18:39
You can put all these visual constraints in a sing
jlebel
2017/03/24 20:59:08
Done.
| |
| 143 [NSLayoutConstraint | |
|
msarda
2017/03/22 12:18:39
I think this code would be so much simpler to read
jlebel
2017/03/24 20:59:09
I will do that in another patch.
msarda
2017/03/27 09:34:16
I have a strong preference for this as I think it
| |
| 144 activateConstraints:[NSLayoutConstraint | |
| 145 constraintsWithVisualFormat:formatString | |
| 146 options:0 | |
| 147 metrics:metrics | |
| 148 views:views]]; | |
| 149 | |
| 150 // Constraints for cold start mode. | |
| 151 NSMutableArray* constraints = [NSMutableArray array]; | |
| 152 formatString = @"V:[primaryButton]-kVerticalPaddingkButtonVerticalPadding-|"; | |
| 153 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 154 constraintsWithVisualFormat:formatString | |
| 155 options:0 | |
| 156 metrics:metrics | |
| 157 views:views]]; | |
| 158 formatString = @"H:[imageView(kChromeImageFixedSize)]"; | |
| 159 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 160 constraintsWithVisualFormat:formatString | |
| 161 options:0 | |
| 162 metrics:metrics | |
| 163 views:views]]; | |
| 164 formatString = @"V:[imageView(kChromeImageFixedSize)]"; | |
| 165 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 166 constraintsWithVisualFormat:formatString | |
| 167 options:0 | |
| 168 metrics:metrics | |
| 169 views:views]]; | |
| 170 _coldStartConstraints = [constraints copy]; | |
| 171 | |
| 172 // Constraints for warm start mode. | |
| 173 constraints = [NSMutableArray array]; | |
| 174 formatString = @"V:[primaryButton]-kButtonVerticalPaddingx2-[secondaryButton(" | |
| 175 @"kButtonHeight)]-kVerticalPaddingkButtonVerticalPadding-|"; | |
| 176 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 177 constraintsWithVisualFormat:formatString | |
| 178 options:0 | |
| 179 metrics:metrics | |
| 180 views:views]]; | |
| 181 formatString = | |
| 182 @"H:|-kHorizontalPadding-[secondaryButton]-kHorizontalPadding-|"; | |
| 183 [constraints addObjectsFromArray: | |
| 184 [NSLayoutConstraint | |
| 185 constraintsWithVisualFormat:formatString | |
| 186 options:NSLayoutFormatAlignAllCenterX | |
|
lpromero
2017/03/24 10:36:20
Isn't tis option already done with line 138?
jlebel
2017/03/24 20:59:09
I didn't know it applied to the secondary button e
lpromero
2017/03/27 13:14:46
Oh good catch. Line 138 might not set this then.
| |
| 187 metrics:metrics | |
| 188 views:views]]; | |
| 189 formatString = @"H:[imageView(kProfileImageFixedSize)]"; | |
| 190 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 191 constraintsWithVisualFormat:formatString | |
| 192 options:0 | |
| 193 metrics:metrics | |
| 194 views:views]]; | |
| 195 formatString = @"V:[imageView(kProfileImageFixedSize)]"; | |
| 196 [constraints addObjectsFromArray:[NSLayoutConstraint | |
| 197 constraintsWithVisualFormat:formatString | |
| 198 options:0 | |
| 199 metrics:metrics | |
| 200 views:views]]; | |
| 201 _warmStartConstraints = [constraints copy]; | |
| 202 _mode = SigninPromoViewWarmStartMode; | |
|
msarda
2017/03/22 12:18:39
I think this line needs to be removed.
jlebel
2017/03/24 20:59:09
Done.
| |
| 203 } | |
| 204 | |
| 205 - (void)setMode:(SigninPromoViewMode)mode { | |
| 206 if (mode == _mode) { | |
| 207 return; | |
| 208 } | |
| 209 _mode = mode; | |
| 210 switch (_mode) { | |
| 211 case SigninPromoViewColdStartMode: | |
| 212 [self activateColdMode]; | |
| 213 return; | |
| 214 case SigninPromoViewWarmStartMode: | |
| 215 [self activateWarmMode]; | |
| 216 return; | |
| 217 } | |
| 218 NOTREACHED(); | |
| 219 } | |
| 220 | |
| 221 - (void)activateColdMode { | |
|
msarda
2017/03/22 12:18:39
DCHECK(_mode, COLD);
jlebel
2017/03/24 20:59:09
Done.
| |
| 222 // TODO(jlebel) Needs to set the chrome/chromium icon in |imageView|. | |
|
msarda
2017/03/22 12:18:39
Please remove this TODO as it is the job of the co
jlebel
2017/03/24 20:59:09
I'm going to follow Louis suggestion and keep it t
| |
| 223 [NSLayoutConstraint deactivateConstraints:_warmStartConstraints]; | |
| 224 [NSLayoutConstraint activateConstraints:_coldStartConstraints]; | |
| 225 _secondaryButton.hidden = YES; | |
| 226 } | |
| 227 | |
| 228 - (void)activateWarmMode { | |
|
msarda
2017/03/22 12:18:39
DCHECK(_mode, WARM);
jlebel
2017/03/24 20:59:09
Done.
| |
| 229 [NSLayoutConstraint deactivateConstraints:_coldStartConstraints]; | |
| 230 [NSLayoutConstraint activateConstraints:_warmStartConstraints]; | |
| 231 _secondaryButton.hidden = NO; | |
| 232 } | |
| 233 | |
| 234 - (void)setProfileImage:(UIImage*)image { | |
|
lpromero
2017/03/24 10:36:20
Can you add a comment in the header that this is a
jlebel
2017/03/24 20:59:09
Should I rephrase:
"Should be called only with the
| |
| 235 if (SigninPromoViewColdStartMode == _mode) { | |
| 236 return; | |
| 237 } | |
| 238 _imageView.image = CircularImageFromImage(image, kProfileImageFixedSize); | |
| 239 } | |
| 240 | |
| 241 - (void)accessibilityPrimaryAction:(id)unused { | |
| 242 [_primaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
| 243 } | |
| 244 | |
| 245 - (void)accessibilitySecondaryAction:(id)unused { | |
| 246 [_secondaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
| 247 } | |
| 248 | |
| 249 - (CGFloat)horizontalPadding { | |
|
lpromero
2017/03/24 10:36:20
Doesn't seem used.
jlebel
2017/03/24 20:59:09
It is used. It is explained in the header why this
lpromero
2017/03/27 13:14:46
Sounds good.
| |
| 250 return kHorizontalPadding; | |
| 251 } | |
| 252 | |
| 253 #pragma mark - NSObject(Accessibility) | |
| 254 | |
| 255 - (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { | |
| 256 NSString* primaryActionName = | |
| 257 [_primaryButton titleForState:UIControlStateNormal]; | |
| 258 UIAccessibilityCustomAction* primaryCustomAction = | |
| 259 [[UIAccessibilityCustomAction alloc] | |
| 260 initWithName:primaryActionName | |
| 261 target:self | |
| 262 selector:@selector(accessibilityPrimaryAction:)]; | |
| 263 if (_mode == SigninPromoViewColdStartMode) { | |
| 264 return @[ primaryCustomAction ]; | |
| 265 } | |
| 266 NSString* secondaryActionName = | |
| 267 [_secondaryButton titleForState:UIControlStateNormal]; | |
| 268 UIAccessibilityCustomAction* secondaryCustomAction = | |
| 269 [[UIAccessibilityCustomAction alloc] | |
| 270 initWithName:secondaryActionName | |
| 271 target:self | |
| 272 selector:@selector(accessibilitySecondaryAction:)]; | |
| 273 return @[ primaryCustomAction, secondaryCustomAction ]; | |
| 274 } | |
| 275 | |
| 276 - (NSString*)accessibilityLabel { | |
| 277 return _textLabel.text; | |
| 278 } | |
| 279 | |
| 280 @end | |
| OLD | NEW |