Skip to content

Commit 36dde24

Browse files
committed
Moved default elevation logic into MDCButton+Subclassing.h.
1 parent c0ee3e0 commit 36dde24

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

components/Buttons/src/MDCButton.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@
9393
@property(nonatomic) UIEdgeInsets hitAreaInsets;
9494

9595
/**
96-
* This is the color of the view behind the button. It's used by flat buttons to calculate accessible text colors.
96+
* The color of the view behind the button, used to calculate accessible text colors.
9797
*
98-
* For Flat buttons, this is the color of both the surrounding area and the button's background.
99-
* For Raised and Floating buttons, this is the color of view underneath the button.
98+
* For flat buttons with the default transparent background, this is the color of the surrounding
99+
* area (and thus the button's background). For all other buttons, this is the color of view
100+
* underneath the button.
100101
*
101-
* The default is nil, but if left unset, Raised/Floating buttons will have possibly-incorrect
102-
* disabled state, and flat buttons will additionally have incorrect text colors.
102+
* The default is nil. If left unset, buttons will likely have an incorrect appearance when
103+
* disabled. Additionally, flat buttons will have incorrect text colors.
103104
*/
104105
@property(nonatomic, strong, nullable) UIColor *underlyingColor;
105106

components/Buttons/src/MDCButton.m

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,24 +454,7 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)s
454454

455455
- (CGFloat)elevationForState:(UIControlState)state {
456456
NSNumber *elevation = _userElevations[@(state)];
457-
458-
if (elevation) {
459-
return [elevation floatValue];
460-
}
461-
if (state & UIControlStateSelected) {
462-
CGFloat elevationForNormal = [self elevationForState:UIControlStateNormal];
463-
// TODO(ajsecord): Why is this using the raised button values for all buttons?
464-
// This should just be a factor of 2, I believe.
465-
if (MDCButtonFloatIsExactlyZero(elevationForNormal)) {
466-
return MDCShadowElevationRaisedButtonPressed;
467-
} else {
468-
CGFloat pressedDeltaForRaisedButton = MDCShadowElevationRaisedButtonPressed -
469-
MDCShadowElevationRaisedButtonResting;
470-
return elevationForNormal + pressedDeltaForRaisedButton;
471-
}
472-
} else {
473-
return MDCShadowElevationNone;
474-
}
457+
return elevation ? [elevation floatValue] : [self defaultElevationForState:state];
475458
}
476459

477460
- (void)setElevation:(CGFloat)elevation forState:(UIControlState)state {
@@ -563,6 +546,10 @@ - (UIEdgeInsets)defaultContentEdgeInsets {
563546
return UIEdgeInsetsMake(8, 16, 8, 16);
564547
}
565548

549+
- (CGFloat)defaultElevationForState:(UIControlState)state {
550+
return 0;
551+
}
552+
566553
- (BOOL)shouldHaveOpaqueBackground {
567554
BOOL isFlatButton = MDCButtonFloatIsExactlyZero([self elevationForState:UIControlStateNormal]);
568555
return !isFlatButton;

components/Buttons/src/MDCFloatingButton.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ - (UIEdgeInsets)defaultContentEdgeInsets {
110110
}
111111
}
112112

113+
- (CGFloat)defaultElevationForState:(UIControlState)state {
114+
return (state & UIControlStateSelected) == UIControlStateSelected ?
115+
MDCShadowElevationFABPressed :
116+
MDCShadowElevationFABResting;
117+
}
118+
113119
@end

components/Buttons/src/MDCRaisedButton.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ - (instancetype)initWithFrame:(CGRect)frame {
3333
return self;
3434
}
3535

36+
#pragma mark - Subclassing
37+
38+
- (CGFloat)defaultElevationForState:(UIControlState)state {
39+
return (state & UIControlStateSelected) == UIControlStateSelected ?
40+
MDCShadowElevationRaisedButtonPressed :
41+
MDCShadowElevationRaisedButtonResting;
42+
}
43+
3644
@end

components/Buttons/src/Private/MDCButton+Subclassing.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@
5757
*/
5858
- (UIEdgeInsets)defaultContentEdgeInsets;
5959

60+
/**
61+
The default elevation for a particular button state (if not set by the calling code).
62+
*/
63+
- (CGFloat)defaultElevationForState:(UIControlState)state;
64+
6065
@end

0 commit comments

Comments
 (0)