From 73ba67c6edabe0cecca1f1c2612f9c2f57adfb1c Mon Sep 17 00:00:00 2001 From: Steven Grosmark Date: Wed, 14 Aug 2024 09:41:28 -0400 Subject: [PATCH] Unnest Box.CornerStyle (#508) 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`. --- .../Sources/AccessibilityElement.swift | 8 +-- BlueprintUICommonControls/Sources/Box.swift | 65 +----------------- .../Sources/CornerStyle.swift | 67 +++++++++++++++++++ .../Internal/UIBezierPath+Extensions.swift | 2 +- CHANGELOG.md | 2 + 5 files changed, 75 insertions(+), 69 deletions(-) create mode 100644 BlueprintUICommonControls/Sources/CornerStyle.swift diff --git a/BlueprintUICommonControls/Sources/AccessibilityElement.swift b/BlueprintUICommonControls/Sources/AccessibilityElement.swift index 391b46835..0decb11f1 100644 --- a/BlueprintUICommonControls/Sources/AccessibilityElement.swift +++ b/BlueprintUICommonControls/Sources/AccessibilityElement.swift @@ -13,7 +13,7 @@ public struct AccessibilityElement: Element { public var identifier: String? public var traits: Set 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. @@ -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, @@ -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)? @@ -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 { diff --git a/BlueprintUICommonControls/Sources/Box.swift b/BlueprintUICommonControls/Sources/Box.swift index f26c6e58e..804c64b0c 100644 --- a/BlueprintUICommonControls/Sources/Box.swift +++ b/BlueprintUICommonControls/Sources/Box.swift @@ -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 { diff --git a/BlueprintUICommonControls/Sources/CornerStyle.swift b/BlueprintUICommonControls/Sources/CornerStyle.swift new file mode 100644 index 000000000..7ed3ca664 --- /dev/null +++ b/BlueprintUICommonControls/Sources/CornerStyle.swift @@ -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 + } + } +} diff --git a/BlueprintUICommonControls/Sources/Internal/UIBezierPath+Extensions.swift b/BlueprintUICommonControls/Sources/Internal/UIBezierPath+Extensions.swift index 2444a045c..49508cbbe 100644 --- a/BlueprintUICommonControls/Sources/Internal/UIBezierPath+Extensions.swift +++ b/BlueprintUICommonControls/Sources/Internal/UIBezierPath+Extensions.swift @@ -5,7 +5,7 @@ extension UIBezierPath { public convenience init( rect: CGRect, - corners: Box.CornerStyle + corners: CornerStyle ) { switch corners { case .square: diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d734d2d..80261cc1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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