feat(settings): 默认启用短动效并提供独立减弱动画选项
This commit is contained in:
@@ -8,6 +8,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
|
||||
// Owners supply explicit geometry; do not query AppKit's intrinsic layout on every update.
|
||||
@@ -17,7 +18,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
|
||||
func makeNSView(context: Context) -> PlotView { PlotView() }
|
||||
func updateNSView(_ view: PlotView, context: Context) {
|
||||
view.configure(Input(frame: frame, colors: colors, scales: scales, maximumGap: maximumGap, hoveredIndex: hoveredIndex))
|
||||
view.configure(Input(frame: frame, colors: colors, scales: scales, maximumGap: maximumGap, hoveredIndex: hoveredIndex, animates: animates))
|
||||
}
|
||||
|
||||
struct Input: Equatable {
|
||||
@@ -26,10 +27,14 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
var animates = false
|
||||
}
|
||||
|
||||
final class PlotView: NSView {
|
||||
private(set) var input: Input?
|
||||
private(set) var drawingFrame: PanelCurveFrame?
|
||||
private let motion = MetricMotion()
|
||||
var isAnimating: Bool { motion.isRunning }
|
||||
override var isFlipped: Bool { true }
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
|
||||
@@ -43,9 +48,30 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
|
||||
func configure(_ value: Input) {
|
||||
guard input != value else { return }
|
||||
let previous = input
|
||||
let changedFrame = previous?.frame != value.frame
|
||||
input = value
|
||||
// Keep hover updates from restarting an existing transition.
|
||||
drawingFrame = value.frame
|
||||
if !value.animates { motion.stop() }
|
||||
else if changedFrame {
|
||||
motion.stop()
|
||||
if window != nil, let previous, let end = value.frame.dates.last,
|
||||
!value.frame.boundaryDates.contains(end),
|
||||
PanelCurveGeometry.isAppend(from: previous.frame.dates, to: value.frame.dates,
|
||||
maximumGap: value.maximumGap) {
|
||||
motion.start(on: layer)
|
||||
}
|
||||
}
|
||||
needsDisplay = true
|
||||
}
|
||||
private func settle() {
|
||||
motion.stop(); drawingFrame = input?.frame
|
||||
}
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
if window == nil { settle() }
|
||||
}
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
let changed = frame.size != newSize
|
||||
super.setFrameSize(newSize)
|
||||
@@ -71,15 +97,15 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
context.move(to: CGPoint(x: 0, y: y)); context.addLine(to: CGPoint(x: bounds.width, y: y))
|
||||
}
|
||||
context.strokePath()
|
||||
guard let input, let first = input.frame.dates.first, let last = input.frame.dates.last,
|
||||
guard let input, let frame = drawingFrame, let first = input.frame.dates.first, let last = input.frame.dates.last,
|
||||
!input.colors.isEmpty, !input.scales.isEmpty else { return }
|
||||
let domain = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: first, end: last, progress: 1)
|
||||
for index in input.frame.series.indices {
|
||||
for index in frame.series.indices {
|
||||
let tint = NSColor(input.colors[min(index, input.colors.count - 1)])
|
||||
let proposedScale = input.scales[min(index, input.scales.count - 1)]
|
||||
let scale = proposedScale.isFinite && proposedScale > 0 ? proposedScale : 1
|
||||
let runs = PanelCurveGeometry.runs(dates: input.frame.dates, values: input.frame.series[index],
|
||||
maximumGap: input.maximumGap, boundaryDates: input.frame.boundaryDates, intervals: input.frame.intervals)
|
||||
let runs = PanelCurveGeometry.runs(dates: frame.dates, values: frame.series[index],
|
||||
maximumGap: input.maximumGap, boundaryDates: frame.boundaryDates, intervals: frame.intervals)
|
||||
context.setFillColor(tint.cgColor)
|
||||
context.setStrokeColor(tint.cgColor)
|
||||
context.setLineWidth(1.5)
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Static numbers avoid per-sample glyph rasterization and transition caches.
|
||||
/// Short numeric transitions follow the owner-supplied motion policy.
|
||||
struct AnimatedMetricText: View {
|
||||
let value: String
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
var body: some View {
|
||||
Text(verbatim: value)
|
||||
.monospacedDigit()
|
||||
if animates {
|
||||
Text(verbatim: value).monospacedDigit()
|
||||
.contentTransition(.opacity)
|
||||
.animation(.easeInOut(duration: MetricMotion.duration), value: value)
|
||||
} else {
|
||||
Text(verbatim: value).monospacedDigit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import SwiftUI
|
||||
import QuartzCore
|
||||
|
||||
private struct MetricAnimationsKey: EnvironmentKey {
|
||||
static let defaultValue = false
|
||||
}
|
||||
extension EnvironmentValues {
|
||||
/// The owner supplies actual visibility and user/system motion policy.
|
||||
var metricAnimationsEnabled: Bool {
|
||||
get { self[MetricAnimationsKey.self] }
|
||||
set { self[MetricAnimationsKey.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// A finite compositor transition; native controls never create per-frame CPU timers.
|
||||
@MainActor final class MetricMotion {
|
||||
static let duration: TimeInterval = 0.25
|
||||
private weak var layer: CALayer?
|
||||
private var deadline: DispatchSourceTimer?
|
||||
// Core Animation stores every CATransition under this reserved key.
|
||||
private static let key = kCATransition
|
||||
var isRunning: Bool { layer?.animation(forKey: Self.key) != nil }
|
||||
|
||||
func start(on layer: CALayer?) {
|
||||
stop()
|
||||
guard let layer else { return }
|
||||
self.layer = layer
|
||||
let transition = CATransition()
|
||||
transition.type = .fade
|
||||
transition.duration = Self.duration
|
||||
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
|
||||
transition.isRemovedOnCompletion = true
|
||||
layer.add(transition, forKey: Self.key)
|
||||
// AppKit backing layers can retain a finished transition; enforce a finite lifetime.
|
||||
// One coalescible cleanup deadline, never a per-frame CPU callback.
|
||||
let deadline = DispatchSource.makeTimerSource(queue: .main)
|
||||
deadline.schedule(deadline: .now() + Self.duration, leeway: .milliseconds(10))
|
||||
deadline.setEventHandler { [weak self] in MainActor.assumeIsolated { self?.stop() } }
|
||||
self.deadline = deadline
|
||||
deadline.resume()
|
||||
}
|
||||
func stop() {
|
||||
deadline?.cancel(); deadline = nil
|
||||
layer?.removeAnimation(forKey: Self.key); layer = nil
|
||||
}
|
||||
deinit { deadline?.cancel() }
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import SwiftUI
|
||||
struct MetricProgressBar: View {
|
||||
let value: Double
|
||||
let color: Color
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
private var fraction: Double { value.isFinite ? min(1, max(0, value)) : 0 }
|
||||
|
||||
@@ -15,6 +16,8 @@ struct MetricProgressBar: View {
|
||||
}
|
||||
.frame(width: proxy.size.width, height: proxy.size.height)
|
||||
.clipShape(Capsule())
|
||||
.animation(animates ? .easeInOut(duration: MetricMotion.duration) : nil, value: fraction)
|
||||
.transaction { if !animates { $0.disablesAnimations = true } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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