fix(ui): 修正风扇转轴与速率图并精简进程表

This commit is contained in:
2026-11-15 19:21:18 +08:00
parent 99e558625b
commit 60c146f8b7
21 changed files with 303 additions and 107 deletions
+41 -8
View File
@@ -58,7 +58,9 @@ struct FanRotor: NSViewRepresentable {
}
static func dismantleNSView(_ view: RotorView, coordinator: ()) { view.stop() }
final class RotorView: NSImageView {
final class RotorView: NSView {
let rotorLayer = CALayer()
private var artworkSize = CGSize.zero
static let animationKey = "fan.rotation"
private var rpm: Double?
private var active = false
@@ -68,9 +70,9 @@ struct FanRotor: NSViewRepresentable {
init() {
super.init(frame: .zero)
wantsLayer = true
image = NSImage(systemSymbolName: "fanblades.fill", accessibilityDescription: nil)
contentTintColor = .secondaryLabelColor
imageScaling = .scaleProportionallyUpOrDown
rotorLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
rotorLayer.contentsGravity = .resizeAspect
layer?.addSublayer(rotorLayer)
setAccessibilityElement(false)
}
required init?(coder: NSCoder) { nil }
@@ -81,10 +83,41 @@ struct FanRotor: NSViewRepresentable {
}
func stop() {
active = false; refresh()
layer?.removeAnimation(forKey: Self.animationKey)
rotorLayer.removeAnimation(forKey: Self.animationKey)
}
override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); refresh() }
override func layout() {
super.layout()
CATransaction.begin(); CATransaction.setDisableActions(true)
rotorLayer.bounds = CGRect(origin: .zero, size: bounds.size)
rotorLayer.position = CGPoint(x: bounds.midX, y: bounds.midY)
if artworkSize != bounds.size { updateArtwork() }
CATransaction.commit()
}
override func viewDidChangeEffectiveAppearance() {
super.viewDidChangeEffectiveAppearance(); updateArtwork()
}
override func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties(); updateArtwork()
}
private func updateArtwork() {
artworkSize = bounds.size
guard bounds.width > 0, bounds.height > 0 else { return }
effectiveAppearance.performAsCurrentDrawingAppearance {
let symbol = NSImage(systemSymbolName: "fanblades.fill", accessibilityDescription: nil)
let artwork = NSImage(size: bounds.size, flipped: false) { rect in
symbol?.draw(in: rect)
NSColor.secondaryLabelColor.setFill(); rect.fill(using: .sourceIn)
return true
}
CATransaction.begin(); CATransaction.setDisableActions(true)
rotorLayer.contentsScale = window?.backingScaleFactor ?? 2
rotorLayer.contents = artwork.cgImage(forProposedRect: nil, context: nil, hints: nil)
CATransaction.commit()
}
}
private func refresh() {
let readable = active && window != nil ? rpm : nil
let speed = FanRotation.speed(rpm: readable, reducedMotion: reducedMotion)
@@ -92,15 +125,15 @@ struct FanRotor: NSViewRepresentable {
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)
rotorLayer.removeAnimation(forKey: Self.animationKey)
rotorLayer.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)
rotorLayer.add(animation, forKey: Self.animationKey)
}
CATransaction.commit()
}