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 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.
| |
204 return; | |
205 } | |
206 _imageView.image = CircularImageFromImage(image, kProfileImageFixedSize); | |
207 } | |
208 | |
209 - (void)accessibilityPrimaryAction:(id)unused { | |
210 [_primaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
211 } | |
212 | |
213 - (void)accessibilitySecondaryAction:(id)unused { | |
214 [_secondaryButton sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
215 } | |
216 | |
217 - (CGFloat)horizontalPadding { | |
218 return kHorizontalPadding; | |
219 } | |
220 | |
221 #pragma mark - NSObject(Accessibility) | |
222 | |
223 - (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { | |
224 NSString* primaryActionName = | |
225 [_primaryButton titleForState:UIControlStateNormal]; | |
226 UIAccessibilityCustomAction* primaryCustomAction = | |
227 [[UIAccessibilityCustomAction alloc] | |
228 initWithName:primaryActionName | |
229 target:self | |
230 selector:@selector(accessibilityPrimaryAction:)]; | |
231 if (_mode == SigninPromoViewModeColdState) { | |
232 return @[ primaryCustomAction ]; | |
233 } | |
234 NSString* secondaryActionName = | |
235 [_secondaryButton titleForState:UIControlStateNormal]; | |
236 UIAccessibilityCustomAction* secondaryCustomAction = | |
237 [[UIAccessibilityCustomAction alloc] | |
238 initWithName:secondaryActionName | |
239 target:self | |
240 selector:@selector(accessibilitySecondaryAction:)]; | |
241 return @[ primaryCustomAction, secondaryCustomAction ]; | |
242 } | |
243 | |
244 - (NSString*)accessibilityLabel { | |
245 return _textLabel.text; | |
246 } | |
247 | |
248 @end | |
OLD | NEW |