mirror of
https://github.com/kean/Pulse.git
synced 2026-05-30 21:07:33 +00:00
52 lines
1.4 KiB
Swift
52 lines
1.4 KiB
Swift
// The MIT License (MIT)
|
||
//
|
||
// Copyright (c) 2020–2022 Alexander Grebenyuk (github.com/kean).
|
||
|
||
import SwiftUI
|
||
|
||
extension View {
|
||
func border(width: CGFloat, edges: [Edge], color: Color) -> some View {
|
||
overlay(EdgeBorder(width: width, edges: edges).foregroundColor(color))
|
||
}
|
||
}
|
||
|
||
struct EdgeBorder: Shape {
|
||
let width: CGFloat
|
||
let edges: [Edge]
|
||
|
||
func path(in rect: CGRect) -> Path {
|
||
var path = Path()
|
||
for edge in edges {
|
||
var x: CGFloat {
|
||
switch edge {
|
||
case .top, .bottom, .leading: return rect.minX
|
||
case .trailing: return rect.maxX - width
|
||
}
|
||
}
|
||
|
||
var y: CGFloat {
|
||
switch edge {
|
||
case .top, .leading, .trailing: return rect.minY
|
||
case .bottom: return rect.maxY - width
|
||
}
|
||
}
|
||
|
||
var w: CGFloat {
|
||
switch edge {
|
||
case .top, .bottom: return rect.width
|
||
case .leading, .trailing: return self.width
|
||
}
|
||
}
|
||
|
||
var h: CGFloat {
|
||
switch edge {
|
||
case .top, .bottom: return self.width
|
||
case .leading, .trailing: return rect.height
|
||
}
|
||
}
|
||
path.addPath(Path(CGRect(x: x, y: y, width: w, height: h)))
|
||
}
|
||
return path
|
||
}
|
||
}
|