113 lines
4.8 KiB
Swift
113 lines
4.8 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// Arc geometry is submitted once; the compositor interpolates strokeEnd continuously.
|
|
struct NativeMetricRing: NSViewRepresentable {
|
|
let value: Double
|
|
let color: Color
|
|
let lineWidth: CGFloat
|
|
let trackOpacity: Double
|
|
let inset: CGFloat
|
|
@Environment(\.metricAnimationsEnabled) private var animates
|
|
|
|
func sizeThatFits(_ proposal: ProposedViewSize, nsView: RingView, context: Context) -> CGSize? {
|
|
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
|
|
height: proposal.height.flatMap { $0.isFinite ? $0 : nil } ?? 10)
|
|
}
|
|
func makeNSView(context: Context) -> RingView { RingView() }
|
|
func updateNSView(_ view: RingView, context: Context) {
|
|
view.configure(Input(value: value.isFinite ? min(1, max(0, value)) : 0, color: color,
|
|
lineWidth: lineWidth, trackOpacity: trackOpacity, inset: inset, animates: animates))
|
|
}
|
|
struct Input: Equatable {
|
|
let value: Double
|
|
let color: Color
|
|
let lineWidth: CGFloat
|
|
let trackOpacity: Double
|
|
let inset: CGFloat
|
|
var animates = false
|
|
}
|
|
final class RingView: NSView {
|
|
private(set) var input: Input?
|
|
let track = CAShapeLayer()
|
|
let arc = CAShapeLayer()
|
|
private let motion = MetricMotion()
|
|
var isAnimating: Bool { motion.isRunning }
|
|
var displayedValue: Double {
|
|
if isAnimating, let presentation = arc.presentation() { return Double(presentation.strokeEnd) }
|
|
return Double(arc.strokeEnd)
|
|
}
|
|
override var isFlipped: Bool { true }
|
|
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
|
init() {
|
|
super.init(frame: .zero)
|
|
wantsLayer = true
|
|
for shape in [track, arc] {
|
|
shape.fillColor = nil; shape.lineCap = .round
|
|
layer?.addSublayer(shape)
|
|
}
|
|
setAccessibilityElement(false)
|
|
}
|
|
required init?(coder: NSCoder) { nil }
|
|
func configure(_ value: Input) {
|
|
guard value != input else { return }
|
|
let old = input
|
|
let start = displayedValue
|
|
input = value
|
|
CATransaction.begin(); CATransaction.setDisableActions(true)
|
|
if old?.color != value.color || old?.trackOpacity != value.trackOpacity { updateColors() }
|
|
if old?.inset != value.inset || old?.lineWidth != value.lineWidth { updateGeometry() }
|
|
arc.strokeEnd = value.value
|
|
CATransaction.commit()
|
|
if !value.animates { motion.stop() }
|
|
else if old?.value != value.value {
|
|
motion.stop()
|
|
if old != nil, window != nil, start != value.value {
|
|
let animation = CABasicAnimation(keyPath: "strokeEnd")
|
|
animation.fromValue = start; animation.toValue = value.value
|
|
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
|
|
motion.start(on: arc, animation: animation)
|
|
}
|
|
}
|
|
}
|
|
private func updateColors() {
|
|
guard let input else { return }
|
|
effectiveAppearance.performAsCurrentDrawingAppearance {
|
|
let color = NSColor(input.color)
|
|
track.strokeColor = color.withAlphaComponent(input.trackOpacity).cgColor
|
|
arc.strokeColor = color.cgColor
|
|
}
|
|
}
|
|
private func updateGeometry() {
|
|
guard let input else { return }
|
|
let radius = max(0, min(bounds.width, bounds.height) / 2 - input.inset)
|
|
let path = CGMutablePath()
|
|
path.addArc(center: CGPoint(x: bounds.midX, y: bounds.midY), radius: radius,
|
|
startAngle: -.pi / 2, endAngle: 3 * .pi / 2, clockwise: false)
|
|
for shape in [track, arc] {
|
|
shape.frame = bounds; shape.path = path; shape.lineWidth = input.lineWidth
|
|
}
|
|
}
|
|
override func setFrameSize(_ size: NSSize) {
|
|
let changed = frame.size != size
|
|
super.setFrameSize(size)
|
|
if changed {
|
|
motion.stop()
|
|
CATransaction.begin(); CATransaction.setDisableActions(true)
|
|
updateGeometry(); CATransaction.commit()
|
|
}
|
|
}
|
|
override func viewDidMoveToWindow() {
|
|
super.viewDidMoveToWindow()
|
|
if window == nil { motion.stop() }
|
|
CATransaction.begin(); CATransaction.setDisableActions(true)
|
|
updateColors(); CATransaction.commit()
|
|
}
|
|
override func viewDidChangeEffectiveAppearance() {
|
|
super.viewDidChangeEffectiveAppearance()
|
|
CATransaction.begin(); CATransaction.setDisableActions(true)
|
|
updateColors(); CATransaction.commit()
|
|
}
|
|
}
|
|
}
|