Files
iOS/Sources/Shared/Extensions/View+RoundedCorner.swift
Bruno Pantaleão 0008727ff3 WIP
2026-06-15 09:36:28 +02:00

58 lines
1.9 KiB
Swift

import Foundation
import SwiftUI
#if os(macOS)
/// Minimal UIKit-parity stand-in for `UIRectCorner` on native macOS.
public struct UIRectCorner: OptionSet {
public let rawValue: UInt
public init(rawValue: UInt) {
self.rawValue = rawValue
}
public static let topLeft = UIRectCorner(rawValue: 1 << 0)
public static let topRight = UIRectCorner(rawValue: 1 << 1)
public static let bottomLeft = UIRectCorner(rawValue: 1 << 2)
public static let bottomRight = UIRectCorner(rawValue: 1 << 3)
public static let allCorners: UIRectCorner = [.topLeft, .topRight, .bottomLeft, .bottomRight]
}
#endif
public struct RoundedCorner: Shape {
public var radius: CGFloat = .infinity
public var corners: UIRectCorner = .allCorners
public init(radius: CGFloat = .infinity, corners: UIRectCorner = .allCorners) {
self.radius = radius
self.corners = corners
}
public func path(in rect: CGRect) -> Path {
#if os(macOS)
let limitedRadius = min(radius, min(rect.width, rect.height) / 2)
return Path(
roundedRect: rect,
cornerRadii: RectangleCornerRadii(
topLeading: corners.contains(.topLeft) ? limitedRadius : 0,
bottomLeading: corners.contains(.bottomLeft) ? limitedRadius : 0,
bottomTrailing: corners.contains(.bottomRight) ? limitedRadius : 0,
topTrailing: corners.contains(.topRight) ? limitedRadius : 0
)
)
#else
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius)
)
return Path(path.cgPath)
#endif
}
}
public extension View {
func roundedCorner(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
}