Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential crash when creating a shadow path in box #149

Merged
merged 4 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions BlueprintUICommonControls/Sources/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ extension Box.CornerStyle {
case .square:
return 0
case .capsule:
return min(bounds.height, bounds.width) / 2.0
return min(bounds.width, bounds.height) / 2
case let .rounded(radius: radius):
return radius
let maximumRadius = min(bounds.width, bounds.height) / 2
return min(maximumRadius, radius)
}
}

Expand Down Expand Up @@ -236,12 +237,10 @@ fileprivate final class BoxView: UIView {
contentView.frame = bounds

if layer.shadowColor != nil {
layer.shadowPath = CGPath(
layer.shadowPath = UIBezierPath(
roundedRect: bounds,
cornerWidth: layer.cornerRadius,
cornerHeight: layer.cornerRadius,
transform: nil
)
cornerRadius: layer.cornerRadius
).cgPath
}
}

Expand Down
10 changes: 10 additions & 0 deletions BlueprintUICommonControls/Tests/Sources/BoxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ class BoxTests: XCTestCase {
of: element,
size: CGSize(width: 100, height: 100))
}

func test_largeCornerRadius() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test crashes on main on iOS 11

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snapshot looks real funky... is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the "diamond" shape without clipping - but am going to update to use the min of width/height

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right; looks way better now

var element = InsettingElement()
element.box.backgroundColor = .blue
element.box.cornerStyle = .rounded(radius: 100)
element.box.shadowStyle = .simple(radius: 2, opacity: 1, offset: .zero, color: .red)
compareSnapshot(
of: element,
size: CGSize(width: 120, height: 100))
}

}

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixes a crash that can occur in `Box` when specifying a corner radius and shadow. ([#149])

### Added

### Removed
Expand Down Expand Up @@ -407,6 +409,7 @@ Box(cornerStyle: .capsule)
[0.3.1]: https://github.com/square/Blueprint/compare/0.3.0...0.3.1
[0.3.0]: https://github.com/square/Blueprint/compare/0.2.2...0.3.0
[0.2.2]: https://github.com/square/Blueprint/releases/tag/0.2.2
[#149]: https://github.com/square/Blueprint/pull/149
[#147]: https://github.com/square/Blueprint/pull/147
[#145]: https://github.com/square/Blueprint/pull/145
[#144]: https://github.com/square/Blueprint/pull/144
Expand Down