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: 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) } .font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9) } } /// 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: NSView { let rotorLayer = CALayer() private var artworkSize = CGSize.zero 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 rotorLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) rotorLayer.contentsGravity = .resizeAspect layer?.addSublayer(rotorLayer) 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() 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) 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) 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) rotorLayer.add(animation, forKey: Self.animationKey) } CATransaction.commit() } } }