Skip to content

Commit e2f4fd3

Browse files
yarneomaterial-automation
authored andcommitted
[Shadows] Code improvements for the new Shadow component.
PiperOrigin-RevId: 367871950
1 parent caaed1b commit e2f4fd3

File tree

11 files changed

+439
-281
lines changed

11 files changed

+439
-281
lines changed

components/Shadow/examples/ShadowTypicalUseExample.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ final class ShadowedView: UIView {
2929

3030
override func layoutSubviews() {
3131
super.layoutSubviews()
32-
MDCConfigureShadow(for: self, color: MDCShadowColor(), elevation: 2)
32+
MDCConfigureShadow(
33+
for: self, shadow: MDCShadowForElevation(1, MDCShadowsCollectionDefault()),
34+
color: MDCShadowColor())
3335
}
3436
}
3537

3638
/// Typical use-case for a shaped view with Material Shadows.
3739
final class ShapedView: UIView {
3840
let shapeLayer = CAShapeLayer()
39-
let shadow = MDCShadowForElevation(2)
4041

4142
override init(frame: CGRect) {
4243
super.init(frame: frame)
@@ -61,14 +62,17 @@ final class ShapedView: UIView {
6162
super.layoutSubviews()
6263
guard let path = polygonPath(bounds: self.bounds, numSides: 3, numPoints: 3) else { return }
6364
shapeLayer.path = path
64-
MDCConfigureShadow(for: self, color: MDCShadowColor(), shadow: shadow, path: path)
65+
66+
MDCConfigureShadow(
67+
for: self, shadow: MDCShadowForElevation(1, MDCShadowsCollectionDefault()),
68+
color: MDCShadowColor(),
69+
path: path)
6570
}
6671
}
6772

6873
/// More complex use-case for a view with a custom shape which animates.
6974
final class AnimatedShapedView: UIView {
7075
let shapeLayer = CAShapeLayer()
71-
let shadow = MDCShadowForElevation(2)
7276
let firstNumSides = 3
7377
let lastNumSides = 12
7478
let animationStepDuration: CFTimeInterval = 0.6
@@ -104,7 +108,10 @@ final class AnimatedShapedView: UIView {
104108
bounds: bounds, numSides: firstNumSides, numPoints: lastNumSides)
105109
else { return }
106110
shapeLayer.path = startPath
107-
MDCConfigureShadow(for: self, color: MDCShadowColor(), shadow: shadow, path: startPath)
111+
MDCConfigureShadow(
112+
for: self, shadow: MDCShadowForElevation(1, MDCShadowsCollectionDefault()),
113+
color: MDCShadowColor(),
114+
path: startPath)
108115

109116
var polygonPaths = (firstNumSides...lastNumSides).map {
110117
polygonPath(bounds: bounds, numSides: $0, numPoints: lastNumSides)

components/Shadow/src/MDCShadow.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
#import <UIKit/UIKit.h>
1616

1717
/**
18-
Immutable value type holding shadow metrics to apply to a view's layer. Use
19-
`MDCShadowForElevation()` or `MDCShadowBuilder` to create this object.
18+
An immutable shadow object that consists of shadow properties (opacity, radius, offset).
19+
20+
To generate a shadow instance, please use the MDCShadowBuilder APIs.
2021
*/
2122
__attribute__((objc_subclassing_restricted)) @interface MDCShadow : NSObject
2223

24+
- (nonnull instancetype)init NS_UNAVAILABLE;
25+
2326
/** CALayer.shadowOpacity */
2427
@property(nonatomic, readonly) CGFloat opacity;
2528

@@ -29,8 +32,6 @@ __attribute__((objc_subclassing_restricted)) @interface MDCShadow : NSObject
2932
/** CALayer.shadowOffset */
3033
@property(nonatomic, readonly) CGSize offset;
3134

32-
- (nonnull instancetype)init NS_UNAVAILABLE;
33-
3435
@end
3536

3637
/**
@@ -50,15 +51,9 @@ __attribute__((objc_subclassing_restricted)) @interface MDCShadowBuilder : NSObj
5051
/** Returns an immutable value type containing a snapshot of the values in this object. */
5152
- (nonnull MDCShadow *)build;
5253

53-
@end
54-
55-
/**
56-
Default color for a Material shadow. On iOS >= 13, this is a dynamic color.
57-
*/
58-
FOUNDATION_EXTERN UIColor *_Nonnull MDCShadowColor(void);
54+
/** Returns a builder with the provided opacity, radius, and offset properties. */
55+
+ (nonnull MDCShadowBuilder *)builderWithOpacity:(CGFloat)opacity
56+
radius:(CGFloat)radius
57+
offset:(CGSize)offset;
5958

60-
/**
61-
Returns an `MDCShadow` representing the Material shadow metrics for the given elevation (in
62-
points).
63-
*/
64-
FOUNDATION_EXTERN MDCShadow *_Nonnull MDCShadowForElevation(CGFloat elevation);
59+
@end

components/Shadow/src/MDCShadow.m

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
#import "MDCShadow.h"
1616

17-
#import "MaterialAvailability.h"
18-
#import "MDCShadow+Internal.h"
19-
2017
@implementation MDCShadow
2118

2219
- (instancetype)initWithOpacity:(CGFloat)opacity radius:(CGFloat)radius offset:(CGSize)offset {
@@ -59,68 +56,14 @@ - (MDCShadow *)build {
5956
return [[MDCShadow alloc] initWithOpacity:self.opacity radius:self.radius offset:self.offset];
6057
}
6158

62-
@end
63-
64-
static UIColor *LightStyleShadowColor(void) {
65-
static UIColor *lightStyleShadowColor;
66-
static dispatch_once_t onceToken;
67-
dispatch_once(&onceToken, ^{
68-
lightStyleShadowColor = [UIColor colorWithRed:0.235 green:0.251 blue:0.263 alpha:1];
69-
});
70-
return lightStyleShadowColor;
71-
}
72-
73-
UIColor *MDCShadowColor(void) {
74-
#if MDC_AVAILABLE_SDK_IOS(13_0)
75-
if (@available(iOS 13.0, *)) {
76-
return [UIColor colorWithDynamicProvider:^(UITraitCollection *traitCollection) {
77-
switch (traitCollection.userInterfaceStyle) {
78-
case UIUserInterfaceStyleUnspecified:
79-
__attribute__((fallthrough));
80-
case UIUserInterfaceStyleLight:
81-
return LightStyleShadowColor();
82-
case UIUserInterfaceStyleDark:
83-
return UIColor.blackColor;
84-
}
85-
__builtin_unreachable();
86-
}];
87-
}
88-
#endif // MDC_AVAILABLE_SDK_IOS(13_0)
89-
return LightStyleShadowColor();
59+
+ (MDCShadowBuilder *)builderWithOpacity:(CGFloat)opacity
60+
radius:(CGFloat)radius
61+
offset:(CGSize)offset {
62+
MDCShadowBuilder *builder = [[MDCShadowBuilder alloc] init];
63+
builder.opacity = opacity;
64+
builder.radius = radius;
65+
builder.offset = offset;
66+
return builder;
9067
}
9168

92-
static int ShadowElevationToLevel(CGFloat elevation) {
93-
if (elevation < 1) {
94-
return 0;
95-
}
96-
if (elevation < 3) {
97-
return 1;
98-
}
99-
if (elevation < 6) {
100-
return 2;
101-
}
102-
if (elevation < 8) {
103-
return 3;
104-
}
105-
if (elevation < 12) {
106-
return 4;
107-
}
108-
return 5;
109-
}
110-
111-
MDCShadow *MDCShadowForElevation(CGFloat elevation) {
112-
static NSArray *shadowLevels;
113-
static dispatch_once_t onceToken;
114-
dispatch_once(&onceToken, ^{
115-
shadowLevels = @[
116-
[[MDCShadow alloc] initWithOpacity:0 radius:0 offset:CGSizeMake(0, 0)],
117-
[[MDCShadow alloc] initWithOpacity:0.43 radius:2.5 offset:CGSizeMake(0, 1)],
118-
[[MDCShadow alloc] initWithOpacity:0.4 radius:3.25 offset:CGSizeMake(0, 1.25)],
119-
[[MDCShadow alloc] initWithOpacity:0.34 radius:4.75 offset:CGSizeMake(0, 2.25)],
120-
[[MDCShadow alloc] initWithOpacity:0.42 radius:6 offset:CGSizeMake(0, 3)],
121-
[[MDCShadow alloc] initWithOpacity:0.4 radius:7.25 offset:CGSizeMake(0, 5)],
122-
];
123-
});
124-
int shadowLevel = ShadowElevationToLevel(elevation);
125-
return shadowLevels[shadowLevel];
126-
}
69+
@end

components/Shadow/src/MDCShadowConfiguring.h

Lines changed: 0 additions & 87 deletions
This file was deleted.

components/Shadow/src/MDCShadowConfiguring.m

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)