Skip to content

Commit

Permalink
[Shapes] Updates the shapeGenerator with a line width to inset the li…
Browse files Browse the repository at this point in the history
…ne rather than center it.

There is a visual difference when using lineWidth instead of borderWidth due to lineWidth drawing on the path, and borderWidth insetting from the path.
To resolve this, the path is made smaller by half the width of the line, thus to create a similar visual appearance as if borderWidth was set.

PiperOrigin-RevId: 322615641
  • Loading branch information
yarneo authored and material-automation committed Jul 22, 2020
1 parent 93fb883 commit 3f3816f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions components/Shapes/src/MDCShapedShadowLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,25 @@ - (void)setPath:(CGPathRef)path {
_colorLayer.fillColor = self.shapedBackgroundColor.CGColor;
_colorLayer.strokeColor = self.shapedBorderColor.CGColor;
_colorLayer.lineWidth = self.shapedBorderWidth;
[self generateColorPathGivenLineWidth];
}
}

- (void)generateColorPathGivenLineWidth {
if (CGPathIsEmpty(self.path) || _colorLayer.lineWidth <= 0) {
return;
}
CGFloat halfOfBorderWidth = self.shapedBorderWidth / 2.f;
CGRect standardizedBounds = CGRectStandardize(self.bounds);
CGRect insetBounds = CGRectInset(standardizedBounds, halfOfBorderWidth, halfOfBorderWidth);
CGAffineTransform transform =
CGAffineTransformMakeTranslation(halfOfBorderWidth, halfOfBorderWidth);
transform = CGAffineTransformScale(
transform, CGRectGetWidth(insetBounds) / CGRectGetWidth(standardizedBounds),
CGRectGetHeight(insetBounds) / CGRectGetHeight(standardizedBounds));
_colorLayer.path = CGPathCreateCopyByTransformingPath(_colorLayer.path, &transform);
}

- (CGPathRef)path {
return _colorLayer.path;
}
Expand Down Expand Up @@ -155,6 +171,7 @@ - (void)setShapedBorderWidth:(CGFloat)shapedBorderWidth {
} else {
self.borderWidth = 0;
_colorLayer.lineWidth = _shapedBorderWidth;
[self generateColorPathGivenLineWidth];
}
}

Expand Down
31 changes: 31 additions & 0 deletions components/Shapes/tests/snapshot/MDCShapedViewSnapshotTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ - (void)generateSnapshotAndVerifyView {

#pragma mark - Tests

- (void)testRectShapedViewWithCornerRadiusBySettingABorderWidthToPositiveThenZero {
// Given
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;

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

// Then
[self generateSnapshotAndVerifyView];
}

- (void)testRectShapedViewWithCornerRadius {
// Given
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;

// When
self.shapedView.shapeGenerator = shapeGenerator;

// Then
[self generateSnapshotAndVerifyView];
}

- (void)testCurvedRectShapedViewElevation00 {
// When
self.shapedView.shapeGenerator =
Expand Down

0 comments on commit 3f3816f

Please sign in to comment.