feat(ui): 实现全卡堆叠趋势与单页资源进程布局
This commit is contained in:
@@ -1,11 +1,43 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Historical visual speed tiers; they deliberately do not reproduce physical RPM.
|
||||
nonisolated struct FanRotation {
|
||||
private(set) var phase: Double = 0
|
||||
private(set) var speed: Double = 0
|
||||
private var reference: TimeInterval = 0
|
||||
|
||||
static func speed(rpm: Double?, reducedMotion: Bool) -> Double {
|
||||
guard !reducedMotion, let rpm, rpm.isFinite, rpm > 0 else { return 0 }
|
||||
switch rpm {
|
||||
case ..<2000: return 90
|
||||
case ..<4000: return 180
|
||||
case ..<6000: return 300
|
||||
default: return 450
|
||||
}
|
||||
}
|
||||
|
||||
func angle(at time: TimeInterval) -> Double {
|
||||
(phase + max(0, time - reference) * speed).truncatingRemainder(dividingBy: 360)
|
||||
}
|
||||
|
||||
mutating func update(rpm: Double?, reducedMotion: Bool, at time: TimeInterval) {
|
||||
phase = angle(at: time)
|
||||
reference = time
|
||||
speed = Self.speed(rpm: rpm, reducedMotion: reducedMotion)
|
||||
}
|
||||
}
|
||||
|
||||
struct FanSpeedIndicator: View {
|
||||
let title: Text
|
||||
let rpm: Double?
|
||||
var isActive = true
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 4) {
|
||||
HStack(spacing: 3) {
|
||||
FanRotor(rpm: rpm, isActive: isActive, reduceMotion: reduceMotion)
|
||||
.frame(width: 11, height: 11).accessibilityHidden(true)
|
||||
title.foregroundStyle(.secondary)
|
||||
MetricText(value: rpm.map { "\(Int($0.rounded())) RPM" } ?? "-- RPM")
|
||||
.monospacedDigit().fontWeight(.medium)
|
||||
@@ -13,3 +45,64 @@ struct FanSpeedIndicator: View {
|
||||
.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.7)
|
||||
}
|
||||
}
|
||||
|
||||
/// Only this native icon animates; sampling updates do not create display timers.
|
||||
struct FanRotor: NSViewRepresentable {
|
||||
let rpm: Double?
|
||||
let isActive: Bool
|
||||
let reduceMotion: Bool
|
||||
|
||||
func makeNSView(context: Context) -> RotorView { RotorView() }
|
||||
func updateNSView(_ view: RotorView, context: Context) {
|
||||
view.configure(rpm: rpm, active: isActive, reducedMotion: reduceMotion)
|
||||
}
|
||||
static func dismantleNSView(_ view: RotorView, coordinator: ()) { view.stop() }
|
||||
|
||||
final class RotorView: NSImageView {
|
||||
static let animationKey = "fan.rotation"
|
||||
private var rpm: Double?
|
||||
private var active = false
|
||||
private var reducedMotion = false
|
||||
private(set) var rotation = FanRotation()
|
||||
|
||||
init() {
|
||||
super.init(frame: .zero)
|
||||
wantsLayer = true
|
||||
image = NSImage(systemSymbolName: "fanblades.fill", accessibilityDescription: nil)
|
||||
contentTintColor = .secondaryLabelColor
|
||||
imageScaling = .scaleProportionallyUpOrDown
|
||||
setAccessibilityElement(false)
|
||||
}
|
||||
required init?(coder: NSCoder) { nil }
|
||||
|
||||
func configure(rpm: Double?, active: Bool, reducedMotion: Bool) {
|
||||
self.rpm = rpm; self.active = active; self.reducedMotion = reducedMotion
|
||||
refresh()
|
||||
}
|
||||
func stop() {
|
||||
active = false; refresh()
|
||||
layer?.removeAnimation(forKey: Self.animationKey)
|
||||
}
|
||||
override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); refresh() }
|
||||
|
||||
private func refresh() {
|
||||
let readable = active && window != nil ? rpm : nil
|
||||
let speed = FanRotation.speed(rpm: readable, reducedMotion: reducedMotion)
|
||||
guard speed != rotation.speed else { return }
|
||||
rotation.update(rpm: readable, reducedMotion: reducedMotion, at: CACurrentMediaTime())
|
||||
let radians = rotation.phase * .pi / 180
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
layer?.removeAnimation(forKey: Self.animationKey)
|
||||
layer?.transform = CATransform3DMakeRotation(radians, 0, 0, 1)
|
||||
if speed > 0 {
|
||||
let animation = CABasicAnimation(keyPath: "transform.rotation.z")
|
||||
animation.fromValue = radians; animation.toValue = radians + .pi * 2
|
||||
animation.duration = 360 / speed
|
||||
animation.repeatCount = .infinity
|
||||
animation.timingFunction = CAMediaTimingFunction(name: .linear)
|
||||
layer?.add(animation, forKey: Self.animationKey)
|
||||
}
|
||||
CATransaction.commit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ struct CardField<Content: View>: View {
|
||||
|
||||
struct CardSurface: ViewModifier {
|
||||
func body(content: Content) -> some View {
|
||||
content.modifier(PanelCardBackground(radius: 10))
|
||||
.overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(.primary.opacity(0.14)))
|
||||
content.modifier(PanelCardBackground(radius: ResourceCardMetrics.radius))
|
||||
.overlay(RoundedRectangle(cornerRadius: ResourceCardMetrics.radius).strokeBorder(.primary.opacity(0.06)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
GeometryReader { bounds in
|
||||
ZStack(alignment: .bottomLeading) {
|
||||
chart()
|
||||
.frame(width: bounds.size.width, height: ResourceCardMetrics.plotHeight)
|
||||
.frame(width: bounds.size.width, height: bounds.size.height)
|
||||
VStack(alignment: .leading, spacing: ResourceCardMetrics.gap) {
|
||||
header
|
||||
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
||||
@@ -42,7 +42,6 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
}
|
||||
.font(.system(size: 9))
|
||||
.padding(ResourceCardMetrics.padding)
|
||||
.padding(.bottom, ResourceCardMetrics.plotHeight - ResourceCardMetrics.padding)
|
||||
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
||||
}
|
||||
.background(Color("PanelCard"))
|
||||
|
||||
@@ -6,7 +6,6 @@ nonisolated enum ResourceCardMetrics {
|
||||
static let minimumHeight: CGFloat = 176
|
||||
static let padding: CGFloat = 10
|
||||
static let radius: CGFloat = 16
|
||||
static let plotHeight: CGFloat = 48
|
||||
static let titleHeight: CGFloat = 14
|
||||
static let headlineHeight: CGFloat = 34
|
||||
static let gap: CGFloat = 4
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Four square blocks fill from bottom to top; partial blocks retain the actual fraction.
|
||||
struct SegmentedCoreBar: View {
|
||||
let value: Double?
|
||||
let color: Color
|
||||
|
||||
static func portions(_ value: Double?, count: Int = 4) -> [Double] {
|
||||
guard count > 0 else { return [] }
|
||||
let fraction = value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0
|
||||
return (0..<count).map { min(1, max(0, fraction * Double(count) - Double($0))) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let fills = Self.portions(value)
|
||||
VStack(spacing: 2) {
|
||||
ForEach(Array(fills.indices.reversed()), id: \.self) { index in
|
||||
ZStack(alignment: .bottom) {
|
||||
Rectangle().fill(color.opacity(0.15))
|
||||
Rectangle().fill(color).frame(height: 6 * fills[index])
|
||||
}.frame(width: 6, height: 6).clipped()
|
||||
}
|
||||
}
|
||||
.accessibilityHidden(true)
|
||||
.transaction { $0.animation = nil; $0.disablesAnimations = true }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user