Skip to content

Commit 5451274

Browse files
committed
Review changes.
Fixed default elevations and added unit tests. Removed useless backwards-compatible decoding logic.
1 parent aa8bd22 commit 5451274

File tree

5 files changed

+67
-55
lines changed

5 files changed

+67
-55
lines changed

components/Buttons/ButtonsTests/ButtonsTests.m

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
// values.
2525
static const UIControlState kNumUIControlStates = 2 * UIControlStateSelected - 1;
2626

27-
static inline UIColor *MDCColorFromRGB(NSInteger rgbValue) {
28-
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0
29-
green:((float)((rgbValue & 0x00FF00) >> 8)) / 255.0
30-
blue:((float)((rgbValue & 0x0000FF) >> 0)) / 255.0
31-
alpha:1.0];
32-
}
33-
3427
static CGFloat randomNumber() {
3528
return arc4random_uniform(100) / (CGFloat)10;
3629
}
@@ -78,7 +71,7 @@ - (void)testUppercaseTitleYes {
7871
NSString *originalTitle = @"some Text";
7972

8073
// When
81-
button.uppercaseTitle = YES;
74+
button.shouldCapitalizeTitle = YES;
8275
[button setTitle:originalTitle forState:UIControlStateNormal];
8376

8477
// Then
@@ -91,7 +84,7 @@ - (void)testUppercaseTitleNo {
9184
NSString *originalTitle = @"some Text";
9285

9386
// When
94-
button.uppercaseTitle = NO;
87+
button.shouldCapitalizeTitle = NO;
9588
[button setTitle:originalTitle forState:UIControlStateNormal];
9689

9790
// Then
@@ -104,9 +97,9 @@ - (void)testUppercaseTitleNoChangedToYes {
10497
NSString *originalTitle = @"some Text";
10598

10699
// When
107-
button.uppercaseTitle = NO;
100+
button.shouldCapitalizeTitle = NO;
108101
[button setTitle:originalTitle forState:UIControlStateNormal];
109-
button.uppercaseTitle = YES;
102+
button.shouldCapitalizeTitle = YES;
110103

111104
// Then
112105
XCTAssertEqualObjects(button.currentTitle, [originalTitle uppercaseStringWithLocale:[NSLocale currentLocale]]);
@@ -161,6 +154,34 @@ - (void)testResetElevationForState {
161154
}
162155
}
163156

157+
- (void)testDefaultElevationsForState {
158+
// Given
159+
MDCButton *button = [[MDCButton alloc] init];
160+
161+
// When
162+
163+
// Then
164+
XCTAssertEqual([button elevationForState:UIControlStateNormal], 0);
165+
XCTAssertEqual([button elevationForState:UIControlStateHighlighted], 0);
166+
XCTAssertEqual([button elevationForState:UIControlStateDisabled], 0);
167+
XCTAssertEqual([button elevationForState:UIControlStateSelected], 1);
168+
}
169+
170+
- (void)testDefaultElevationRelationships {
171+
// Given
172+
MDCButton *button = [[MDCButton alloc] init];
173+
CGFloat normalElevation = randomNumber();
174+
175+
// When
176+
[button setElevation:normalElevation forState:UIControlStateNormal];
177+
178+
// Then
179+
XCTAssertEqual([button elevationForState:UIControlStateNormal], normalElevation);
180+
XCTAssertEqual([button elevationForState:UIControlStateHighlighted], normalElevation);
181+
XCTAssertEqual([button elevationForState:UIControlStateDisabled], normalElevation);
182+
XCTAssertEqual([button elevationForState:UIControlStateSelected], 2 * normalElevation);
183+
}
184+
164185
- (void)testBackgroundColorForState {
165186
// Given
166187
MDCButton *button = [[MDCButton alloc] init];

components/Buttons/src/MDCButton.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@param state The state.
4141
@return The background color.
4242
*/
43-
- (nonnull UIColor *)backgroundColorForState:(UIControlState)state;
43+
- (nullable UIColor *)backgroundColorForState:(UIControlState)state;
4444

4545
/**
4646
A color used as the button's @c backgroundColor.
@@ -51,7 +51,7 @@
5151
- (void)setBackgroundColor:(nullable UIColor *)backgroundColor forState:(UIControlState)state;
5252

5353
/** The ink color of the button. */
54-
@property(null_resettable, nonatomic, strong) UIColor *inkColor;
54+
@property(nonatomic, strong, null_resettable) UIColor *inkColor;
5555

5656
/**
5757
* A custom title color for the non-disabled states. The default is nil, which means that the button
@@ -77,12 +77,12 @@
7777
@property(nonatomic) BOOL shouldRaiseOnTouch;
7878

7979
/**
80-
* Converts the button title to uppercase. Changing this property to NO will not update the current
81-
* title string.
80+
* If true, converts the button title to uppercase. Changing this property to NO will not update the
81+
* current title string.
8282
*
8383
* Default is YES and is recommended whenever possible.
8484
*/
85-
@property(nonatomic, getter=isUppercaseTitle) BOOL uppercaseTitle;
85+
@property(nonatomic) BOOL shouldCapitalizeTitle;
8686

8787
/**
8888
* Allows the button to detect touches outside of its bounds. A negative value indicates an
@@ -117,6 +117,7 @@
117117
/**
118118
* Returns the elevation for a particular control state.
119119
*
120+
* The default values are particular to each subclass of MDCButton.
120121
* The default value for UIControlStateNormal is 0. The default value for UIControlStateSelected is
121122
* twice greater than the value of UIControlStateNormal. The default values for all other states is
122123
* the value of UIControlStateNormal.

components/Buttons/src/MDCButton.m

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@
3636
@"MDCButtonDisabledBackgroundColorDarkKey";
3737
static NSString *const MDCButtonInkViewInkColorKey = @"MDCButtonInkViewInkColorKey";
3838
static NSString *const MDCButtonShouldRaiseOnTouchKey = @"MDCButtonShouldRaiseOnTouchKey";
39-
static NSString *const MDCButtonUppercaseTitleKey = @"MDCButtonUppercaseTitleKey";
39+
static NSString *const MDCButtonShouldCapitalizeTitleKey = @"MDCButtonShouldCapitalizeTitleKey";
4040
static NSString *const MDCButtonUnderlyingColorKey = @"MDCButtonUnderlyingColorKey";
4141
static NSString *const MDCButtonUserElevationsKey = @"MDCButtonUserElevationsKey";
4242

43-
// MDCButtonUserZIndicesKey provides backward compatibility with old z-index shadows values.
44-
// TODO: Remove from MDC, it is only useful for internal clients.
45-
static NSString *const MDCButtonUserZIndicesKey = @"MDCButtonUserZIndicesKey";
46-
4743
static const NSTimeInterval MDCButtonAnimationDuration = 0.2;
4844

4945
// http://www.google.com/design/spec/components/buttons.html#buttons-main-buttons
@@ -96,13 +92,11 @@ - (instancetype)initWithFrame:(CGRect)frame {
9692
- (void)commonInit {
9793
_disabledAlpha = MDCButtonDisabledAlpha;
9894
_shouldRaiseOnTouch = YES;
99-
_uppercaseTitle = YES;
95+
_shouldCapitalizeTitle = YES;
10096
_userElevations = [NSMutableDictionary dictionary];
10197
_backgroundColors = [NSMutableDictionary dictionary];
10298
_accessibilityLabelForState = [NSMutableDictionary dictionary];
10399

104-
[self setElevation:MDCShadowElevationNone forState:UIControlStateDisabled];
105-
106100
// Disable default highlight state.
107101
self.adjustsImageWhenHighlighted = NO;
108102
self.showsTouchWhenHighlighted = NO;
@@ -111,12 +105,15 @@ - (void)commonInit {
111105
self.titleLabel.font = [MDCTypography buttonFont];
112106
[self updateTitleColor];
113107
[self updateDisabledTitleColor];
108+
[self updateAlphaAndBackgroundColorAnimated:NO];
114109

115110
// Default content insets
116111
self.contentEdgeInsets = [self defaultContentEdgeInsets];
117112

118-
self.layer.shadowPath = [self boundingPath].CGPath;
119-
self.layer.shadowColor = [UIColor blackColor].CGColor;
113+
MDCShadowLayer *shadowLayer = [self shadowLayer];
114+
shadowLayer.shadowPath = [self boundingPath].CGPath;
115+
shadowLayer.shadowColor = [UIColor blackColor].CGColor;
116+
shadowLayer.elevation = [self elevationForState:self.state];
120117

121118
// Set up ink layer.
122119
_inkView = [[MDCInkView alloc] initWithFrame:self.bounds];
@@ -197,26 +194,18 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
197194
if ([aDecoder containsValueForKey:MDCButtonShouldRaiseOnTouchKey]) {
198195
self.shouldRaiseOnTouch = [aDecoder decodeBoolForKey:MDCButtonShouldRaiseOnTouchKey];
199196
}
200-
if ([aDecoder containsValueForKey:MDCButtonUppercaseTitleKey]) {
201-
self.uppercaseTitle = [aDecoder decodeBoolForKey:MDCButtonUppercaseTitleKey];
197+
198+
if ([aDecoder containsValueForKey:MDCButtonShouldCapitalizeTitleKey]) {
199+
self.shouldCapitalizeTitle = [aDecoder decodeBoolForKey:MDCButtonShouldCapitalizeTitleKey];
202200
}
201+
203202
if ([aDecoder containsValueForKey:MDCButtonUnderlyingColorKey]) {
204203
self.underlyingColor = [aDecoder decodeObjectForKey:MDCButtonUnderlyingColorKey];
205204
}
206205

207206
if ([aDecoder containsValueForKey:MDCButtonUserElevationsKey]) {
208207
_userElevations = [aDecoder decodeObjectForKey:MDCButtonUserElevationsKey];
209208
}
210-
// For backward compatibility
211-
if ([aDecoder containsValueForKey:MDCButtonUserZIndicesKey]) {
212-
NSMutableDictionary *userZIndices = [aDecoder decodeObjectForKey:MDCButtonUserZIndicesKey];
213-
NSArray *userZIndicesStates = [userZIndices allKeys];
214-
for (NSNumber *stateNum in userZIndicesStates) {
215-
NSNumber *zIndex = userZIndices[stateNum];
216-
CGFloat elevation = (float)pow(2.f, [zIndex floatValue]);
217-
[_userElevations setObject:@(elevation) forKey:stateNum];
218-
}
219-
}
220209
}
221210
return self;
222211
}
@@ -229,7 +218,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
229218
}
230219

231220
[aCoder encodeBool:_shouldRaiseOnTouch forKey:MDCButtonShouldRaiseOnTouchKey];
232-
[aCoder encodeBool:_uppercaseTitle forKey:MDCButtonUppercaseTitleKey];
221+
[aCoder encodeBool:_shouldCapitalizeTitle forKey:MDCButtonShouldCapitalizeTitleKey];
233222
if (_underlyingColor) {
234223
[aCoder encodeObject:_underlyingColor forKey:MDCButtonUnderlyingColorKey];
235224
}
@@ -308,9 +297,9 @@ - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated {
308297

309298
#pragma mark - Title Uppercasing
310299

311-
- (void)setUppercaseTitle:(BOOL)uppercaseTitle {
312-
_uppercaseTitle = uppercaseTitle;
313-
if (_uppercaseTitle) {
300+
- (void)setShouldCapitalizeTitle:(BOOL)shouldCapitalizeTitle {
301+
_shouldCapitalizeTitle = shouldCapitalizeTitle;
302+
if (_shouldCapitalizeTitle) {
314303
// This ensures existing titles will get upper cased
315304
UIControlState allControlStates =
316305
(UIControlStateHighlighted | UIControlStateDisabled | UIControlStateSelected);
@@ -332,7 +321,7 @@ - (void)setTitle:(NSString *)title forState:(UIControlState)state {
332321
[_accessibilityLabelForState removeObjectForKey:@(state)];
333322
}
334323

335-
if (_uppercaseTitle) {
324+
if (_shouldCapitalizeTitle) {
336325
title = [title uppercaseStringWithLocale:[NSLocale currentLocale]];
337326
}
338327
[super setTitle:title forState:state];
@@ -347,7 +336,7 @@ - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)
347336
[_accessibilityLabelForState removeObjectForKey:@(state)];
348337
}
349338

350-
if (_uppercaseTitle) {
339+
if (_shouldCapitalizeTitle) {
351340
// Store the attributes.
352341
NSMutableArray *attributes = [NSMutableArray array];
353342
[title enumerateAttributesInRange:NSMakeRange(0, [title length])
@@ -386,7 +375,7 @@ - (void)setAccessibilityLabel:(NSString *)accessibilityLabel {
386375
}
387376

388377
- (NSString *)accessibilityLabel {
389-
if (!_uppercaseTitle) {
378+
if (!_shouldCapitalizeTitle) {
390379
return [super accessibilityLabel];
391380
}
392381

@@ -463,6 +452,7 @@ - (void)setElevation:(CGFloat)elevation forState:(UIControlState)state {
463452

464453
// The elevation of the normal state controls whether this button is flat or not, and flat buttons
465454
// have different background color requirements than raised buttons.
455+
// TODO(ajsecord): Move to MDCFlatButton and update this comment.
466456
if (state == UIControlStateNormal) {
467457
[self updateAlphaAndBackgroundColorAnimated:NO];
468458
[self updateTitleColor];
@@ -547,7 +537,16 @@ - (UIEdgeInsets)defaultContentEdgeInsets {
547537
}
548538

549539
- (CGFloat)defaultElevationForState:(UIControlState)state {
550-
return 0;
540+
if (state == UIControlStateNormal) {
541+
return 0;
542+
}
543+
544+
if ((state & UIControlStateSelected) == UIControlStateSelected) {
545+
CGFloat normalElevation = [self elevationForState:UIControlStateNormal];
546+
return normalElevation > 0 ? 2 * normalElevation : 1;
547+
}
548+
549+
return [self elevationForState:UIControlStateNormal];
551550
}
552551

553552
- (BOOL)shouldHaveOpaqueBackground {

components/Buttons/src/MDCFloatingButton.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ - (instancetype)initWithFrame:(CGRect)frame shape:(MDCFloatingButtonShape)shape
5151
self = [super initWithFrame:frame];
5252
if (self) {
5353
_shape = shape;
54-
[self setElevation:MDCShadowElevationRaisedButtonResting forState:UIControlStateNormal];
5554
}
5655
return self;
5756
}

components/Buttons/src/MDCRaisedButton.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@
2525

2626
@implementation MDCRaisedButton
2727

28-
- (instancetype)initWithFrame:(CGRect)frame {
29-
self = [super initWithFrame:frame];
30-
if (self) {
31-
[self setElevation:MDCShadowElevationRaisedButtonResting forState:UIControlStateNormal];
32-
}
33-
return self;
34-
}
35-
3628
#pragma mark - Subclassing
3729

3830
- (CGFloat)defaultElevationForState:(UIControlState)state {

0 commit comments

Comments
 (0)