Files
Kymo/kymo/UI/Components/NativeMetricRing.swift
T

92 lines
3.7 KiB
Swift

import AppKit
import SwiftUI
/// Static arc geometry; sampling publishes the final value directly.
struct NativeMetricRing: NSViewRepresentable {
let value: Double
let color: Color
let lineWidth: CGFloat
let trackOpacity: Double
let inset: CGFloat
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))
}
struct Input: Equatable {
let value: Double
let color: Color
let lineWidth: CGFloat
let trackOpacity: Double
let inset: CGFloat
}
final class RingView: NSView {
private(set) var input: Input?
let track = CAShapeLayer()
let arc = CAShapeLayer()
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
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()
}
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 {
CATransaction.begin(); CATransaction.setDisableActions(true)
updateGeometry(); CATransaction.commit()
}
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
CATransaction.begin(); CATransaction.setDisableActions(true)
updateColors(); CATransaction.commit()
}
override func viewDidChangeEffectiveAppearance() {
super.viewDidChangeEffectiveAppearance()
CATransaction.begin(); CATransaction.setDisableActions(true)
updateColors(); CATransaction.commit()
}
}
}