feat(ui): 统一实现卡片背景趋势与大数值布局
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Bounded static Core Graphics paths. No animation, gradient fill or per-frame work.
|
||||
/// Bounded static lines and per-run area paths. Hover never rebuilds geometry.
|
||||
struct NativePanelCurve: NSViewRepresentable {
|
||||
let frame: PanelCurveFrame
|
||||
let colors: [Color]
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
var visibleSeriesCount: Int? = nil
|
||||
var filledSeriesCount = 1
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
|
||||
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
|
||||
@@ -16,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))
|
||||
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount))
|
||||
}
|
||||
struct Input: Equatable {
|
||||
let frame: PanelCurveFrame
|
||||
@@ -24,6 +26,8 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
var visibleSeriesCount: Int? = nil
|
||||
var filledSeriesCount = 1
|
||||
}
|
||||
final class PlotView: NSView {
|
||||
private(set) var input: Input?
|
||||
@@ -43,6 +47,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
layer?.masksToBounds = true
|
||||
curves.anchorPoint = .zero
|
||||
grid.fillColor = nil; grid.lineWidth = 0.5
|
||||
grid.isHidden = true
|
||||
hoverLine.fillColor = nil; hoverLine.lineWidth = 1; hoverLine.lineDashPattern = [3, 2]
|
||||
for child in [grid, curves, hoverLine] { layer?.addSublayer(child) }
|
||||
setAccessibilityElement(false)
|
||||
@@ -57,7 +62,11 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
curves.isHidden = value.colors.isEmpty || value.scales.isEmpty
|
||||
ensureLayers(value.frame.series.count)
|
||||
if old?.colors != value.colors || old?.frame.series.count != value.frame.series.count { updatePalette() }
|
||||
if old?.colors != value.colors || old?.frame.series.count != value.frame.series.count
|
||||
|| old?.filledSeriesCount != value.filledSeriesCount { updatePalette() }
|
||||
for index in seriesLayers.indices {
|
||||
seriesLayers[index].root.isHidden = index >= (value.visibleSeriesCount ?? value.frame.series.count)
|
||||
}
|
||||
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap {
|
||||
rebuildPaths()
|
||||
}
|
||||
@@ -83,6 +92,9 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
for index in seriesLayers.indices {
|
||||
let color = palette[min(index, palette.count - 1)], series = seriesLayers[index]
|
||||
series.line.strokeColor = color.cgColor; series.points.fillColor = color.cgColor
|
||||
let dark = effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua
|
||||
series.area.fillColor = index < (input?.filledSeriesCount ?? 0)
|
||||
? color.withAlphaComponent(dark ? 0.16 : 0.12).cgColor : nil
|
||||
hoverDots[index].fillColor = color.cgColor
|
||||
}
|
||||
}
|
||||
@@ -117,7 +129,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let scale = value.isFinite && value > 0 ? value : 1
|
||||
let runs = PanelCurveGeometry.runs(dates: frame.dates, values: frame.series[index], maximumGap: input.maximumGap,
|
||||
boundaryDates: frame.boundaryDates, intervals: frame.intervals)
|
||||
let line = CGMutablePath(), dots = CGMutablePath()
|
||||
let line = CGMutablePath(), dots = CGMutablePath(), area = CGMutablePath()
|
||||
for run in runs {
|
||||
let points = run.map { CGPoint(x: bounds.width * domain.position($0.date),
|
||||
y: bounds.height * (1 - min(1, max(0, $0.value / scale)))) }
|
||||
@@ -131,9 +143,15 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
segment.addCurve(to: b, control1: CGPoint(x: mid, y: a.y), control2: CGPoint(x: mid, y: b.y))
|
||||
}
|
||||
line.addPath(segment)
|
||||
// Each continuous run closes against its own baseline, preserving real gaps.
|
||||
let fill = CGMutablePath(); fill.addPath(segment)
|
||||
fill.addLine(to: CGPoint(x: points.last!.x, y: bounds.height))
|
||||
fill.addLine(to: CGPoint(x: start.x, y: bounds.height)); fill.closeSubpath()
|
||||
area.addPath(fill)
|
||||
}
|
||||
series.line.path = line
|
||||
series.points.path = dots
|
||||
series.area.path = area
|
||||
}
|
||||
}
|
||||
private func updateHover() {
|
||||
@@ -145,6 +163,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let x = bounds.width * domain.position(input.frame.dates[index])
|
||||
let path = CGMutablePath(); path.move(to: CGPoint(x: x, y: 0)); path.addLine(to: CGPoint(x: x, y: bounds.height)); hoverLine.path = path
|
||||
for series in input.frame.series.indices {
|
||||
guard series < (input.visibleSeriesCount ?? input.frame.series.count) else { continue }
|
||||
let values = input.frame.series[series]
|
||||
guard values.indices.contains(index), let value = values[index], value.isFinite else { continue }
|
||||
let scale = input.scales[min(series, input.scales.count - 1)]
|
||||
@@ -171,17 +190,17 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
updatePalette(); CATransaction.commit()
|
||||
}
|
||||
private final class SeriesLayer {
|
||||
let root = CALayer(), line = CAShapeLayer(), points = CAShapeLayer()
|
||||
let root = CALayer(), area = CAShapeLayer(), line = CAShapeLayer(), points = CAShapeLayer()
|
||||
init() {
|
||||
line.fillColor = nil; line.lineWidth = 1.5; line.lineCap = .round; line.lineJoin = .round
|
||||
root.addSublayer(line); root.addSublayer(points)
|
||||
line.fillColor = nil; line.lineWidth = 1.8; line.lineCap = .round; line.lineJoin = .round
|
||||
root.addSublayer(area); root.addSublayer(line); root.addSublayer(points)
|
||||
}
|
||||
func resize(_ bounds: CGRect) {
|
||||
root.frame = bounds
|
||||
let local = CGRect(origin: .zero, size: bounds.size)
|
||||
line.frame = local; points.frame = local
|
||||
area.frame = local; line.frame = local; points.frame = local
|
||||
}
|
||||
func clear() { line.path = nil; points.path = nil }
|
||||
func clear() { area.path = nil; line.path = nil; points.path = nil }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user