Skip to content

Commit

Permalink
Unnest Box.CornerStyle (#508)
Browse files Browse the repository at this point in the history
Since `Box.CornerStyle` is used for other things besides specifying the
corners of a `Box`, this PR moves the `CornerStyle` type to the root of
the framework.

To maintain compatibility, a typealias is added to `Box`, so consumers
can continue to use `Box.CornerRadius`.
  • Loading branch information
g-mark authored Aug 14, 2024
1 parent fca1db6 commit 73ba67c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 69 deletions.
8 changes: 4 additions & 4 deletions BlueprintUICommonControls/Sources/AccessibilityElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct AccessibilityElement: Element {
public var identifier: String?
public var traits: Set<Trait>
public var accessibilityFrameSize: CGSize?
public var accessibilityFrameCornerStyle: Box.CornerStyle
public var accessibilityFrameCornerStyle: CornerStyle
public var wrappedElement: Element

/// Used to provide custom behaviour when activated by voiceover. This will override the default behavior of issuing a tap event at the accessibility activation point.
Expand All @@ -34,7 +34,7 @@ public struct AccessibilityElement: Element {
hint: String? = nil,
identifier: String? = nil,
accessibilityFrameSize: CGSize? = nil,
accessibilityFrameCornerStyle: Box.CornerStyle = .square,
accessibilityFrameCornerStyle: CornerStyle = .square,
customActions: [AccessibilityElement.CustomAction] = [],
customContent: [AccessibilityElement.CustomContent] = [],
wrapping element: Element,
Expand Down Expand Up @@ -92,7 +92,7 @@ public struct AccessibilityElement: Element {

private final class AccessibilityView: UIView, AXCustomContentProvider {
var accessibilityFrameSize: CGSize?
var accessibilityFrameCornerStyle: Box.CornerStyle = .square
var accessibilityFrameCornerStyle: CornerStyle = .square
var accessibilityCustomContent: [AXCustomContent]! = [] // The exclamation `!` is in the protodol definition and required.

var increment: (() -> Void)?
Expand Down Expand Up @@ -175,7 +175,7 @@ extension Element {
hint: String? = nil,
identifier: String? = nil,
accessibilityFrameSize: CGSize? = nil,
accessibilityFrameCornerStyle: Box.CornerStyle = .square,
accessibilityFrameCornerStyle: CornerStyle = .square,
customActions: [AccessibilityElement.CustomAction] = [],
customContent: [AccessibilityElement.CustomContent] = []
) -> AccessibilityElement {
Expand Down
65 changes: 1 addition & 64 deletions BlueprintUICommonControls/Sources/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,70 +129,7 @@ public struct Box: Element {

extension Box {

public enum CornerStyle: Equatable {
case square
case capsule
case rounded(radius: CGFloat, corners: Corners = .all)

public struct Corners: OptionSet, Equatable {
public let rawValue: UInt8

public init(rawValue: UInt8) {
self.rawValue = rawValue
}

public static let topLeft = Corners(rawValue: 1)
public static let topRight = Corners(rawValue: 1 << 1)
public static let bottomLeft = Corners(rawValue: 1 << 2)
public static let bottomRight = Corners(rawValue: 1 << 3)

public static let all: Corners = [.topLeft, .topRight, .bottomLeft, .bottomRight]
public static let top: Corners = [.topRight, .topLeft]
public static let left: Corners = [.topLeft, .bottomLeft]
public static let bottom: Corners = [.bottomLeft, .bottomRight]
public static let right: Corners = [.topRight, .bottomRight]

var toCACornerMask: CACornerMask {
var mask: CACornerMask = []
if contains(.topLeft) {
mask.update(with: .layerMinXMinYCorner)
}

if contains(.topRight) {
mask.update(with: .layerMaxXMinYCorner)
}

if contains(.bottomLeft) {
mask.update(with: .layerMinXMaxYCorner)
}

if contains(.bottomRight) {
mask.update(with: .layerMaxXMaxYCorner)
}
return mask
}

var toUIRectCorner: UIRectCorner {
var rectCorner: UIRectCorner = []
if contains(.topLeft) {
rectCorner.update(with: .topLeft)
}

if contains(.topRight) {
rectCorner.update(with: .topRight)
}

if contains(.bottomLeft) {
rectCorner.update(with: .bottomLeft)
}

if contains(.bottomRight) {
rectCorner.update(with: .bottomRight)
}
return rectCorner
}
}
}
public typealias CornerStyle = BlueprintUICommonControls.CornerStyle

/// Specifies the curve style when showing rounded corners on a `Box`.
public enum CornerCurve: Equatable {
Expand Down
67 changes: 67 additions & 0 deletions BlueprintUICommonControls/Sources/CornerStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import BlueprintUI
import UIKit

public enum CornerStyle: Equatable {
case square
case capsule
case rounded(radius: CGFloat, corners: Corners = .all)

public struct Corners: OptionSet, Equatable {
public let rawValue: UInt8

public init(rawValue: UInt8) {
self.rawValue = rawValue
}

public static let topLeft = Corners(rawValue: 1)
public static let topRight = Corners(rawValue: 1 << 1)
public static let bottomLeft = Corners(rawValue: 1 << 2)
public static let bottomRight = Corners(rawValue: 1 << 3)

public static let all: Corners = [.topLeft, .topRight, .bottomLeft, .bottomRight]
public static let top: Corners = [.topRight, .topLeft]
public static let left: Corners = [.topLeft, .bottomLeft]
public static let bottom: Corners = [.bottomLeft, .bottomRight]
public static let right: Corners = [.topRight, .bottomRight]

var toCACornerMask: CACornerMask {
var mask: CACornerMask = []
if contains(.topLeft) {
mask.update(with: .layerMinXMinYCorner)
}

if contains(.topRight) {
mask.update(with: .layerMaxXMinYCorner)
}

if contains(.bottomLeft) {
mask.update(with: .layerMinXMaxYCorner)
}

if contains(.bottomRight) {
mask.update(with: .layerMaxXMaxYCorner)
}
return mask
}

var toUIRectCorner: UIRectCorner {
var rectCorner: UIRectCorner = []
if contains(.topLeft) {
rectCorner.update(with: .topLeft)
}

if contains(.topRight) {
rectCorner.update(with: .topRight)
}

if contains(.bottomLeft) {
rectCorner.update(with: .bottomLeft)
}

if contains(.bottomRight) {
rectCorner.update(with: .bottomRight)
}
return rectCorner
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension UIBezierPath {

public convenience init(
rect: CGRect,
corners: Box.CornerStyle
corners: CornerStyle
) {
switch corners {
case .square:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Moved `CornerStyle` out of the `Box` namespace, and is now a root type in `BlueprintUICommonControls`. `Box.CornerStyle` is still available as a typealias.

### Deprecated

### Security
Expand Down

0 comments on commit 73ba67c

Please sign in to comment.