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 namespace { | |
15 // Horizontal padding for label and buttons. | |
16 const CGFloat kHorizontalPadding = 40; | |
17 // Vertical padding for the image and the label. | |
18 const CGFloat kVerticalPadding = 12; | |
19 // Vertical padding for buttons. | |
20 const CGFloat kButtonVerticalPadding = 6; | |
21 // Image size for warm start. | |
22 const CGFloat kProfileImageFixedSize = 48; | |
23 // Image size for cold start. | |
24 const CGFloat kChromeImageFixedSize = 24; | |
25 // Button height. | |
26 const CGFloat kButtonHeight = 36; | |
27 } | |
28 | |
29 @implementation SigninPromoView { | |
msarda
2017/03/16 22:15:31
Louis: May I ask you to do an in-depth review of t
| |
30 SigninPromoViewMode _mode; | |
31 UIImageView* _imageView; | |
32 UILabel* _textLabel; | |
33 MDCFlatButton* _primaryButton; | |
34 MDCFlatButton* _secondaryButton; | |
35 | |
36 NSArray<NSLayoutConstraint*>* _coldStartConstraints; | |
37 NSArray<NSLayoutConstraint*>* _warmStartConstraints; | |
38 } | |
39 | |
40 @synthesize mode = _mode; | |
41 @synthesize imageView = _imageView; | |
42 @synthesize textLabel = _textLabel; | |
43 @synthesize primaryButton = _primaryButton; | |
44 @synthesize secondaryButton = _secondaryButton; | |
45 | |
46 - (instancetype)initWithCoder:(NSCoder*)aDecoder { | |
47 NOTREACHED(); | |
48 return nil; | |
49 } | |
50 | |
51 - (instancetype)initWithFrame:(CGRect)frame { | |
52 self = [super initWithFrame:frame]; | |
53 if (self) { | |
54 self.translatesAutoresizingMaskIntoConstraints = NO; | |
55 self.isAccessibilityElement = YES; | |
56 [self addSubviews]; | |
57 [self setDefaultViewStyling]; | |
58 [self setViewConstraints]; | |
59 _mode = SigninPromoViewColdStartMode; | |
60 [self activateColdMode]; | |
61 } | |
62 return self; | |
63 } | |
64 | |
65 - (void)addSubviews { | |
66 self.clipsToBounds = YES; | |
67 | |
68 _imageView = [[UIImageView alloc] init]; | |
69 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
70 [self addSubview:_imageView]; | |
71 | |
72 _textLabel = [[UILabel alloc] init]; | |
73 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
74 [self addSubview:_textLabel]; | |
75 | |
76 _primaryButton = [[MDCFlatButton alloc] init]; | |
77 _primaryButton.translatesAutoresizingMaskIntoConstraints = NO; | |
78 _primaryButton.accessibilityIdentifier = @"signin_promo_primary_button"; | |
79 [self addSubview:_primaryButton]; | |
80 | |
81 _secondaryButton = [[MDCFlatButton alloc] init]; | |
82 _secondaryButton.translatesAutoresizingMaskIntoConstraints = NO; | |
83 _secondaryButton.accessibilityIdentifier = @"signin_promo_secondary_button"; | |
84 [self addSubview:_secondaryButton]; | |
85 } | |
86 | |
87 - (void)setDefaultViewStyling { | |
88 _imageView.contentMode = UIViewContentModeCenter; | |
89 _imageView.layer.masksToBounds = YES; | |
90 _imageView.contentMode = UIViewContentModeScaleAspectFit; | |
91 | |
92 _textLabel.font = [MDCTypography buttonFont]; | |
93 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
94 _textLabel.numberOfLines = 0; | |
95 _textLabel.textAlignment = NSTextAlignmentCenter; | |
96 | |
97 [_primaryButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] | |
98 forState:UIControlStateNormal]; | |
99 _primaryButton.customTitleColor = [UIColor whiteColor]; | |
100 _primaryButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; | |
101 | |
102 _secondaryButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; | |
103 _secondaryButton.uppercaseTitle = NO; | |
104 } | |
105 | |
106 - (void)setViewConstraints { | |
107 [NSLayoutConstraint activateConstraints:@[ | |
108 // Set vertical anchors. | |
109 [_imageView.topAnchor constraintEqualToAnchor:self.topAnchor | |
msarda
2017/03/16 22:15:31
I have to say I find this constraints code very ha
jlebel
2017/03/21 17:22:29
Done.
| |
110 constant:kVerticalPadding * 2], | |
111 [_textLabel.topAnchor constraintEqualToAnchor:_imageView.bottomAnchor | |
112 constant:kVerticalPadding], | |
113 [_primaryButton.topAnchor | |
114 constraintEqualToAnchor:_textLabel.bottomAnchor | |
115 constant:kVerticalPadding + kButtonVerticalPadding], | |
116 | |
117 // Set horizontal anchors. | |
118 [_imageView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor], | |
119 [_textLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor], | |
120 [_primaryButton.centerXAnchor constraintEqualToAnchor:self.centerXAnchor], | |
121 [_primaryButton.leadingAnchor constraintEqualToAnchor:self.leadingAnchor | |
122 constant:kHorizontalPadding], | |
123 [self.trailingAnchor constraintEqualToAnchor:_primaryButton.trailingAnchor | |
124 constant:kHorizontalPadding], | |
125 | |
126 // Fix width and height. | |
127 [_primaryButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
128 ]]; | |
129 _coldStartConstraints = @[ | |
130 // Set vertical anchors. | |
131 [self.bottomAnchor | |
132 constraintEqualToAnchor:_primaryButton.bottomAnchor | |
133 constant:kVerticalPadding + kButtonVerticalPadding], | |
134 | |
135 // Fix width and height. | |
136 [_imageView.widthAnchor constraintEqualToConstant:kChromeImageFixedSize], | |
137 [_imageView.heightAnchor constraintEqualToConstant:kChromeImageFixedSize], | |
138 ]; | |
139 _warmStartConstraints = @[ | |
140 // Set vertical anchors. | |
141 [_secondaryButton.topAnchor | |
142 constraintEqualToAnchor:_primaryButton.bottomAnchor | |
143 constant:kButtonVerticalPadding * 2], | |
144 [self.bottomAnchor | |
145 constraintEqualToAnchor:_secondaryButton.bottomAnchor | |
146 constant:kVerticalPadding + kButtonVerticalPadding], | |
147 | |
148 // Set horizontal anchors. | |
149 [self.trailingAnchor constraintEqualToAnchor:_secondaryButton.trailingAnchor | |
150 constant:kHorizontalPadding], | |
151 [_secondaryButton.centerXAnchor constraintEqualToAnchor:self.centerXAnchor], | |
152 [_secondaryButton.leadingAnchor constraintEqualToAnchor:self.leadingAnchor | |
153 constant:kHorizontalPadding], | |
154 | |
155 // Fix width and height. | |
156 [_imageView.widthAnchor constraintEqualToConstant:kProfileImageFixedSize], | |
157 [_imageView.heightAnchor constraintEqualToConstant:kProfileImageFixedSize], | |
158 [_secondaryButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
159 ]; | |
160 [NSLayoutConstraint deactivateConstraints:_coldStartConstraints]; | |
msarda
2017/03/16 22:15:31
Why is this needed?
jlebel
2017/03/21 17:22:29
Done.
| |
161 _mode = SigninPromoViewWarmStartMode; | |
162 } | |
163 | |
164 - (void)setMode:(SigninPromoViewMode)mode { | |
165 if (mode == _mode) { | |
166 return; | |
167 } | |
168 _mode = mode; | |
169 switch (_mode) { | |
170 case SigninPromoViewColdStartMode: | |
171 [self activateColdMode]; | |
172 return; | |
173 case SigninPromoViewWarmStartMode: | |
174 [self activateWarmMode]; | |
175 return; | |
176 } | |
177 NOTREACHED(); | |
178 } | |
179 | |
180 - (void)activateColdMode { | |
181 // TODO(jlebel) Needs to set the chrome/chromium icon in |imageView|. | |
182 [NSLayoutConstraint deactivateConstraints:_warmStartConstraints]; | |
183 [NSLayoutConstraint activateConstraints:_coldStartConstraints]; | |
184 _secondaryButton.hidden = YES; | |
185 } | |
186 | |
187 - (void)activateWarmMode { | |
188 [NSLayoutConstraint deactivateConstraints:_coldStartConstraints]; | |
189 [NSLayoutConstraint activateConstraints:_warmStartConstraints]; | |
190 _secondaryButton.hidden = NO; | |
191 } | |
192 | |
193 - (void)setProfileImage:(UIImage*)image { | |
194 if (SigninPromoViewColdStartMode == _mode) { | |
195 return; | |
196 } | |
197 _imageView.image = CircularImageFromImage(image, kProfileImageFixedSize); | |
198 } | |
199 | |
200 - (void)accessibilityPrimaryAction:(id)unused { | |
201 [_primaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
202 } | |
203 | |
204 - (void)accessibilitySecondaryAction:(id)unused { | |
205 [_secondaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
206 } | |
207 | |
208 - (CGFloat)horizontalPadding { | |
209 return kHorizontalPadding; | |
210 } | |
211 | |
212 #pragma mark - NSObject(Accessibility) | |
213 | |
214 - (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { | |
215 NSString* primaryActionName = | |
216 [_primaryButton titleForState:UIControlStateNormal]; | |
217 UIAccessibilityCustomAction* primaryCustomAction = | |
218 [[UIAccessibilityCustomAction alloc] | |
219 initWithName:primaryActionName | |
220 target:self | |
221 selector:@selector(accessibilityPrimaryAction:)]; | |
222 if (_mode == SigninPromoViewColdStartMode) { | |
223 return @[ primaryCustomAction ]; | |
224 } | |
225 NSString* secondaryActionName = | |
226 [_secondaryButton titleForState:UIControlStateNormal]; | |
227 UIAccessibilityCustomAction* secondaryCustomAction = | |
228 [[UIAccessibilityCustomAction alloc] | |
229 initWithName:secondaryActionName | |
230 target:self | |
231 selector:@selector(accessibilitySecondaryAction:)]; | |
232 return @[ primaryCustomAction, secondaryCustomAction ]; | |
233 } | |
234 | |
235 - (NSString*)accessibilityLabel { | |
236 return _textLabel.text; | |
237 } | |
238 | |
239 @end | |
OLD | NEW |