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