feat(settings): 默认启用短动效并提供独立减弱动画选项
This commit is contained in:
@@ -8,6 +8,7 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
let lineWidth: CGFloat
|
||||
let trackOpacity: Double
|
||||
let inset: CGFloat
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: RingView, context: Context) -> CGSize? {
|
||||
// Owners supply explicit geometry; do not query AppKit's intrinsic layout on every update.
|
||||
@@ -18,7 +19,7 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
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))
|
||||
lineWidth: lineWidth, trackOpacity: trackOpacity, inset: inset, animates: animates))
|
||||
}
|
||||
struct Input: Equatable {
|
||||
let value: Double
|
||||
@@ -26,9 +27,13 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
let lineWidth: CGFloat
|
||||
let trackOpacity: Double
|
||||
let inset: CGFloat
|
||||
var animates = false
|
||||
}
|
||||
final class RingView: NSView {
|
||||
private(set) var input: Input?
|
||||
private(set) var displayedValue = 0.0
|
||||
private let motion = MetricMotion()
|
||||
var isAnimating: Bool { motion.isRunning }
|
||||
override var isFlipped: Bool { true }
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
init() {
|
||||
@@ -39,7 +44,20 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
required init?(coder: NSCoder) { nil }
|
||||
func configure(_ value: Input) {
|
||||
guard value != input else { return }
|
||||
input = value; needsDisplay = true
|
||||
let previous = input
|
||||
input = value
|
||||
displayedValue = value.value
|
||||
if !value.animates { motion.stop() }
|
||||
else if previous?.value != value.value {
|
||||
motion.stop()
|
||||
if previous != nil, window != nil { motion.start(on: layer) }
|
||||
}
|
||||
needsDisplay = true
|
||||
}
|
||||
private func settle() { motion.stop(); displayedValue = input?.value ?? 0; needsDisplay = true }
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
if window == nil { settle() }
|
||||
}
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
let changed = newSize != frame.size
|
||||
@@ -59,10 +77,10 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
context.setLineWidth(input.lineWidth); context.setLineCap(.round)
|
||||
context.setStrokeColor(tint.withAlphaComponent(input.trackOpacity).cgColor)
|
||||
context.strokeEllipse(in: CGRect(x: center.x - radius, y: center.y - radius, width: radius * 2, height: radius * 2))
|
||||
guard input.value > 0 else { return }
|
||||
guard displayedValue > 0 else { return }
|
||||
context.setStrokeColor(tint.cgColor)
|
||||
context.addArc(center: center, radius: radius, startAngle: -.pi / 2,
|
||||
endAngle: -.pi / 2 + 2 * .pi * input.value, clockwise: false)
|
||||
endAngle: -.pi / 2 + 2 * .pi * displayedValue, clockwise: false)
|
||||
context.strokePath()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user