feat(menubar): 支持固定居中面板与独立刷新频率及图表浮层
This commit is contained in:
@@ -6,20 +6,20 @@ nonisolated enum PanelCurveGeometry {
|
||||
let value: Double
|
||||
}
|
||||
|
||||
static func isAppend(from old: [Date], to new: [Date]) -> Bool {
|
||||
static func isAppend(from old: [Date], to new: [Date], maximumGap: TimeInterval = 5) -> Bool {
|
||||
guard old.count > 1, let previous = old.last, let next = new.last,
|
||||
next > previous, next.timeIntervalSince(previous) <= 5 else { return false }
|
||||
next > previous, next.timeIntervalSince(previous) <= maximumGap else { return false }
|
||||
if new.count == old.count + 1 { return Array(new.dropLast()) == old }
|
||||
return new.count == old.count && Array(old.dropFirst()) == Array(new.dropLast())
|
||||
}
|
||||
|
||||
static func runs(dates: [Date], values: [Double?]) -> [[Sample]] {
|
||||
static func runs(dates: [Date], values: [Double?], maximumGap: TimeInterval = 5, boundaryDates: Set<Date> = []) -> [[Sample]] {
|
||||
var result: [[Sample]] = []
|
||||
var run: [Sample] = []
|
||||
func flush() { if !run.isEmpty { result.append(run); run.removeAll(keepingCapacity: true) } }
|
||||
for index in dates.indices {
|
||||
guard index < values.count, let value = values[index], value.isFinite else { flush(); continue }
|
||||
if index > 0 && (dates[index].timeIntervalSince(dates[index - 1]) > 5 || dates[index] <= dates[index - 1]) { flush() }
|
||||
if index > 0 && (boundaryDates.contains(dates[index]) || dates[index].timeIntervalSince(dates[index - 1]) > maximumGap || dates[index] <= dates[index - 1]) { flush() }
|
||||
run.append(Sample(date: dates[index], value: value))
|
||||
}
|
||||
flush()
|
||||
|
||||
@@ -14,10 +14,13 @@ struct PanelTrendChart: View {
|
||||
var firstLabel: LocalizedStringKey = "panel.total"
|
||||
var secondLabel: LocalizedStringKey = "network.upload"
|
||||
var thirdLabel: LocalizedStringKey = "cpu.system"
|
||||
var expectedInterval: TimeInterval = 2
|
||||
var boundaryDates: Set<Date> = []
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var retainedScale: Double = 1
|
||||
@State private var smallSamples = 0
|
||||
@State private var hoveredDate: Date?
|
||||
@State private var hoverPoint: CGPoint?
|
||||
@State private var previousBegin: Date?
|
||||
@State private var previousEnd: Date?
|
||||
@State private var progress: CGFloat = 1
|
||||
@@ -35,18 +38,35 @@ struct PanelTrendChart: View {
|
||||
if fixedScale == nil { return MetricFormat.rate(value) }
|
||||
return MetricFormat.bytes(UInt64(max(0, value)))
|
||||
}
|
||||
private func value(_ values: [Double?]) -> String {
|
||||
guard let index, index < values.count else { return "--" }
|
||||
return formatted(values[index])
|
||||
}
|
||||
private func legend(_ label: LocalizedStringKey, values: [Double?], tint: Color) -> some View {
|
||||
HStack(spacing: 3) {
|
||||
RoundedRectangle(cornerRadius: 1).fill(tint).frame(width: 7, height: 7)
|
||||
Text(label)
|
||||
Text(verbatim: value(values)).monospacedDigit()
|
||||
Text(verbatim: formatted(values.last ?? nil)).monospacedDigit()
|
||||
}.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.8)
|
||||
}
|
||||
|
||||
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, index: Int) -> some View {
|
||||
HStack(spacing: 6) {
|
||||
RoundedRectangle(cornerRadius: 2).fill(tint).frame(width: 8, height: 8)
|
||||
Text(label)
|
||||
Spacer(minLength: 4)
|
||||
Text(verbatim: formatted(index < values.count ? values[index] : nil)).monospacedDigit()
|
||||
}
|
||||
}
|
||||
private func tooltip(index: Int) -> some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
Text(dates[index], format: .dateTime.hour().minute().second()).foregroundStyle(.secondary)
|
||||
tooltipRow(firstLabel, values: first, tint: color, index: index)
|
||||
if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, index: index) }
|
||||
if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) }
|
||||
}
|
||||
.font(.system(size: 10)).padding(8)
|
||||
.background(Color("PanelTooltip"), in: .rect(cornerRadius: 7))
|
||||
.overlay(RoundedRectangle(cornerRadius: 7).strokeBorder(.primary.opacity(0.2)))
|
||||
.shadow(color: .black.opacity(0.2), radius: 5, y: 2)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 3) {
|
||||
HStack(spacing: 10) {
|
||||
@@ -67,7 +87,7 @@ struct PanelTrendChart: View {
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
PanelCurveCanvas(dates: dates, series: [first, second, third], colors: [color, secondColor, thirdColor],
|
||||
scale: scale, previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
|
||||
scale: scale, maximumGap: max(5, expectedInterval * 2), boundaryDates: boundaryDates, previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
|
||||
if hoveredDate != nil, let index, let begin = dates.first, let end = dates.last, end > begin {
|
||||
let x = proxy.size.width * dates[index].timeIntervalSince(begin) / end.timeIntervalSince(begin)
|
||||
Path { path in
|
||||
@@ -87,22 +107,33 @@ struct PanelTrendChart: View {
|
||||
switch phase {
|
||||
case .active(let point):
|
||||
if let begin = dates.first, let end = dates.last {
|
||||
hoverPoint = point
|
||||
hoveredDate = begin.addingTimeInterval(end.timeIntervalSince(begin) * min(1, max(0, point.x / max(1, proxy.size.width))))
|
||||
}
|
||||
case .ended: hoveredDate = nil
|
||||
case .ended: hoveredDate = nil; hoverPoint = nil
|
||||
}
|
||||
}
|
||||
}.frame(height: 58).clipped()
|
||||
.clipped()
|
||||
.overlay(alignment: .topLeading) {
|
||||
if let hoverPoint, let index {
|
||||
tooltip(index: index)
|
||||
.frame(width: min(180, proxy.size.width))
|
||||
.offset(x: min(max(0, hoverPoint.x + 12), max(0, proxy.size.width - 180)),
|
||||
y: min(hoverPoint.y + 10, proxy.size.height - 12))
|
||||
.allowsHitTesting(false)
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
}
|
||||
}.frame(height: 58).zIndex(10)
|
||||
}
|
||||
HStack {
|
||||
if let date = dates.first { Text(date, style: .time) }
|
||||
Spacer(minLength: 0)
|
||||
if hoveredDate != nil, let index { Text(dates[index], format: .dateTime.hour().minute().second()).foregroundStyle(.primary) }
|
||||
Spacer(minLength: 0)
|
||||
if let date = dates.last { Text(date, style: .time) }
|
||||
}.font(.system(size: 9)).foregroundStyle(.secondary).frame(height: 12)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.zIndex(hoveredDate == nil ? 0 : 20)
|
||||
.onAppear { retainedScale = maximum }
|
||||
.onChange(of: dates) { old, new in
|
||||
let target = maximum
|
||||
@@ -111,7 +142,7 @@ struct PanelTrendChart: View {
|
||||
smallSamples += 1
|
||||
if smallSamples >= 10 { retainedScale = target; smallSamples = 0 }
|
||||
} else { smallSamples = 0 }
|
||||
let append = PanelCurveGeometry.isAppend(from: old, to: new)
|
||||
let append = PanelCurveGeometry.isAppend(from: old, to: new, maximumGap: max(5, expectedInterval * 2)) && !(new.last.map { boundaryDates.contains($0) } ?? false)
|
||||
var transaction = Transaction(); transaction.disablesAnimations = true
|
||||
withTransaction(transaction) {
|
||||
previousBegin = append && !reduceMotion ? old.first : nil
|
||||
@@ -134,6 +165,8 @@ private struct PanelCurveCanvas: View, Animatable {
|
||||
let series: [[Double?]]
|
||||
let colors: [Color]
|
||||
let scale: Double
|
||||
let maximumGap: TimeInterval
|
||||
let boundaryDates: Set<Date>
|
||||
let previousBegin: Date?
|
||||
let previousEnd: Date?
|
||||
var progress: CGFloat
|
||||
@@ -157,8 +190,9 @@ private struct PanelCurveCanvas: View, Animatable {
|
||||
guard let first = dates.first, let last = dates.last else { return }
|
||||
let begin = (previousBegin ?? first).timeIntervalSinceReferenceDate * (1 - progress) + first.timeIntervalSinceReferenceDate * progress
|
||||
let end = (previousEnd ?? last).timeIntervalSinceReferenceDate * (1 - progress) + last.timeIntervalSinceReferenceDate * progress
|
||||
for (values, tint) in zip(series, colors) {
|
||||
for run in PanelCurveGeometry.runs(dates: dates, values: values) {
|
||||
for (seriesIndex, entry) in zip(series, colors).enumerated() {
|
||||
let (values, tint) = entry
|
||||
for run in PanelCurveGeometry.runs(dates: dates, values: values, maximumGap: maximumGap, boundaryDates: boundaryDates) {
|
||||
let points = run.map { item in
|
||||
CGPoint(x: end > begin ? size.width * (item.date.timeIntervalSinceReferenceDate - begin) / (end - begin) : size.width,
|
||||
y: size.height * (1 - min(1, max(0, item.value / scale))))
|
||||
@@ -176,7 +210,7 @@ private struct PanelCurveCanvas: View, Animatable {
|
||||
var area = line
|
||||
area.addLine(to: CGPoint(x: finish.x, y: size.height)); area.addLine(to: CGPoint(x: start.x, y: size.height)); area.closeSubpath()
|
||||
context.fill(area, with: .linearGradient(Gradient(colors: [tint.opacity(0.20), tint.opacity(0.01)]), startPoint: .zero, endPoint: CGPoint(x: 0, y: size.height)))
|
||||
context.stroke(line, with: .color(tint), style: StrokeStyle(lineWidth: 1.5, lineCap: .round, lineJoin: .round))
|
||||
context.stroke(line, with: .color(tint), style: StrokeStyle(lineWidth: 1.5, lineCap: .round, lineJoin: .round, dash: seriesIndex == 1 ? [4, 2] : seriesIndex == 2 ? [1, 2] : []))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user