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)
|
||||
|
||||
Reference in New Issue
Block a user