Skip to content

Commit

Permalink
Add properties to configure the border color/width of MDCShapedView.
Browse files Browse the repository at this point in the history
`MDCShapedView` has properties to configure the elevation and shape generator but no properties to configure the border color/width. In order to do so, one must cast the view’s layer to `MDCShapedShadowLayer` and set its border color/width. (There is also no property exposing the underlying `MDCShapedShadowLayer`.)

PiperOrigin-RevId: 351710014
  • Loading branch information
fumoboy007 authored and material-automation committed Jan 14, 2021
1 parent 48808bf commit 2d6db96
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
14 changes: 14 additions & 0 deletions components/Shapes/src/MDCShapedView.h
Expand Up @@ -43,6 +43,20 @@
*/
@property(nonatomic, strong, nullable) IBOutlet id<MDCShapeGenerating> shapeGenerator;

/**
The stroke color of the shape generated by shapeGenerator.
The default is nil, which results in no stroke being drawn.
*/
@property(nonatomic, copy, nullable) UIColor *shapedBorderColor;

/**
The stroke width of the shape generated by shapeGenerator.
The default is 0, which results in no stroke being drawn.
*/
@property(nonatomic, assign) CGFloat shapedBorderWidth;

/**
Initializes an MDCShapedView.
Expand Down
16 changes: 16 additions & 0 deletions components/Shapes/src/MDCShapedView.m
Expand Up @@ -65,6 +65,22 @@ - (void)setShapeGenerator:(id<MDCShapeGenerating>)shapeGenerator {
return self.layer.shapeGenerator;
}

- (UIColor *)shapedBorderColor {
return self.layer.shapedBorderColor;
}

- (void)setShapedBorderColor:(UIColor *)shapedBorderColor {
self.layer.shapedBorderColor = shapedBorderColor;
}

- (CGFloat)shapedBorderWidth {
return self.layer.shapedBorderWidth;
}

- (void)setShapedBorderWidth:(CGFloat)shapedBorderWidth {
self.layer.shapedBorderWidth = shapedBorderWidth;
}

// MDCShapedView captures backgroundColor assigments so that they can be set to the
// MDCShapedShadowLayer fillColor. If we don't do this the background of the layer will obscure any
// shapes drawn by the shape layer.
Expand Down
21 changes: 10 additions & 11 deletions components/Shapes/tests/snapshot/MDCShapedViewSnapshotTests.m
Expand Up @@ -54,19 +54,18 @@ - (void)generateSnapshotAndVerifyView {

- (void)testRectShapedViewWithBorderWidthAndCorrectMaskingOfContent {
// Given
MDCShapedShadowLayer *shadowLayer = (MDCShapedShadowLayer *)self.shapedView.layer;
MDCRectangleShapeGenerator *shapeGenerator = [[MDCRectangleShapeGenerator alloc] init];
MDCRoundedCornerTreatment *cornerTreatment = [MDCRoundedCornerTreatment cornerWithRadius:50.f];
[shapeGenerator setCorners:cornerTreatment];
shadowLayer.shapedBorderWidth = 10;
shadowLayer.shapedBorderColor = UIColor.redColor;
self.shapedView.shapedBorderWidth = 10;
self.shapedView.shapedBorderColor = UIColor.redColor;
UIView *contentView = [[UIView alloc] initWithFrame:self.shapedView.bounds];
contentView.backgroundColor = UIColor.systemPinkColor;
[self.shapedView addSubview:contentView];

// When
self.shapedView.shapeGenerator = shapeGenerator;
contentView.layer.mask = shadowLayer.shapeLayer;
contentView.layer.mask = ((MDCShapedShadowLayer *)self.shapedView.layer).shapeLayer;

// Then
[self generateSnapshotAndVerifyView];
Expand All @@ -77,12 +76,12 @@ - (void)testRectShapedViewWithCornerRadiusBySettingABorderWidthToPositiveThenZer
MDCRectangleShapeGenerator *shapeGenerator = [[MDCRectangleShapeGenerator alloc] init];
MDCRoundedCornerTreatment *cornerTreatment = [MDCRoundedCornerTreatment cornerWithRadius:50.f];
[shapeGenerator setCorners:cornerTreatment];
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderWidth = 10;
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderColor = UIColor.redColor;
self.shapedView.shapedBorderWidth = 10;
self.shapedView.shapedBorderColor = UIColor.redColor;

// When
self.shapedView.shapeGenerator = shapeGenerator;
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderWidth = 0;
self.shapedView.shapedBorderWidth = 0;

// Then
[self generateSnapshotAndVerifyView];
Expand All @@ -93,8 +92,8 @@ - (void)testRectShapedViewWithCornerRadius {
MDCRectangleShapeGenerator *shapeGenerator = [[MDCRectangleShapeGenerator alloc] init];
MDCRoundedCornerTreatment *cornerTreatment = [MDCRoundedCornerTreatment cornerWithRadius:50.f];
[shapeGenerator setCorners:cornerTreatment];
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderWidth = 10;
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderColor = UIColor.redColor;
self.shapedView.shapedBorderWidth = 10;
self.shapedView.shapedBorderColor = UIColor.redColor;

// When
self.shapedView.shapeGenerator = shapeGenerator;
Expand All @@ -112,8 +111,8 @@ - (void)testSmallRectShapedViewWithCornerRadiusSameAsBoarderWidth {
shapeGenerator.topRightCornerOffset = CGPointMake(-40.f, 40.f);
shapeGenerator.bottomLeftCornerOffset = CGPointMake(40.f, -40.f);
shapeGenerator.bottomRightCornerOffset = CGPointMake(-40.f, -40.f);
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderWidth = 10;
((MDCShapedShadowLayer *)self.shapedView.layer).shapedBorderColor = UIColor.redColor;
self.shapedView.shapedBorderWidth = 10;
self.shapedView.shapedBorderColor = UIColor.redColor;

// When
self.shapedView.shapeGenerator = shapeGenerator;
Expand Down

0 comments on commit 2d6db96

Please sign in to comment.