Skip to content

Commit

Permalink
Merge pull request square#145 from nononoah/noahb/box-rounded-corners
Browse files Browse the repository at this point in the history
[Box] Add support for boxes with semicircular edges
  • Loading branch information
kylebshr authored Sep 14, 2020
2 parents e45260c + e2772d1 commit af5c605
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
13 changes: 8 additions & 5 deletions BlueprintUICommonControls/Sources/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public struct Box: Element {
view.backgroundColor = self.backgroundColor
}

if self.cornerStyle.radius != view.layer.cornerRadius {
view.layer.cornerRadius = self.cornerStyle.radius
if self.cornerStyle.radius(for: bounds) != view.layer.cornerRadius {
view.layer.cornerRadius = self.cornerStyle.radius(for: bounds)
}

if self.borderStyle.color?.cgColor != view.layer.borderColor {
Expand Down Expand Up @@ -83,8 +83,8 @@ public struct Box: Element {
view.contentView.clipsToBounds = self.clipsContent
}

if self.cornerStyle.radius != view.contentView.layer.cornerRadius {
view.contentView.layer.cornerRadius = self.cornerStyle.radius
if self.cornerStyle.radius(for: bounds) != view.contentView.layer.cornerRadius {
view.contentView.layer.cornerRadius = self.cornerStyle.radius(for: bounds)
}

})
Expand All @@ -102,6 +102,7 @@ extension Box {

public enum CornerStyle {
case square
case capsule
case rounded(radius: CGFloat)
}

Expand Down Expand Up @@ -140,10 +141,12 @@ public extension Element {

extension Box.CornerStyle {

fileprivate var radius: CGFloat {
fileprivate func radius(for bounds: CGRect) -> CGFloat {
switch self {
case .square:
return 0
case .capsule:
return min(bounds.height, bounds.width) / 2.0
case let .rounded(radius: radius):
return radius
}
Expand Down
32 changes: 25 additions & 7 deletions BlueprintUICommonControls/Tests/Sources/BoxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ class BoxTests: XCTestCase {
identifier: "clear")
}

func test_cornerRadius() {
var box = Box()
box.backgroundColor = .blue
box.cornerStyle = .rounded(radius: 10.0)
compareSnapshot(
of: box,
size: CGSize(width: 100, height: 100))
func test_cornerStyle() {
do {
var box = Box()
box.backgroundColor = .blue
box.cornerStyle = .capsule
compareSnapshot(
of: box,
size: CGSize(width: 200, height: 100),
identifier: "wideCapsule")

compareSnapshot(
of: box,
size: CGSize(width: 100, height: 200),
identifier: "longCapsule")
}

do {
var box = Box()
box.backgroundColor = .blue
box.cornerStyle = .rounded(radius: 10.0)
compareSnapshot(
of: box,
size: CGSize(width: 100, height: 100),
identifier: "rounded")
}
}

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

- Add `addFixed(child:)` and `addFlexible(child:)` methods to `StackElement` for adding children with a grow & shrink priority of 0.0 and 1.0 respectively.

- Add `capsule` case to `Box.CornerStyle` ([#145]). This addition sugars the following pattern

```
GeometryReader { geometry in
Box(cornerStyle: .rounded(geometry.constraint.height.maximum / 2.0))
}
```

into

```
Box(cornerStyle: .capsule)
```

### Removed

### Changed
Expand Down

0 comments on commit af5c605

Please sign in to comment.