perf(menubar): 隔离卡片动效并推进 1.1 低开销版本
This commit is contained in:
@@ -16,7 +16,7 @@ extension EnvironmentValues {
|
||||
static let duration: TimeInterval = 0.35
|
||||
static let key = "kymo.metric.movement"
|
||||
private weak var layer: CALayer?
|
||||
private var deadline: DispatchSourceTimer?
|
||||
private static let cleanup = MotionCleanup()
|
||||
var isRunning: Bool { layer?.animation(forKey: Self.key) != nil }
|
||||
|
||||
func start(on layer: CALayer?, animation: CAAnimation) {
|
||||
@@ -28,11 +28,7 @@ extension EnvironmentValues {
|
||||
animation.isRemovedOnCompletion = true
|
||||
layer.add(animation, forKey: Self.key)
|
||||
// Backing layers may retain completed animations. Bound their lifetime explicitly.
|
||||
let deadline = DispatchSource.makeTimerSource(queue: .main)
|
||||
deadline.schedule(deadline: .now() + Self.duration, leeway: .milliseconds(10))
|
||||
deadline.setEventHandler { [weak self] in MainActor.assumeIsolated { self?.stop() } }
|
||||
self.deadline = deadline
|
||||
deadline.resume()
|
||||
Self.cleanup.register(self)
|
||||
}
|
||||
/// Cubic timing curve used by the original SwiftUI easeInOut animation.
|
||||
static func easeInOut(_ time: Double) -> Double {
|
||||
@@ -48,8 +44,62 @@ extension EnvironmentValues {
|
||||
}
|
||||
|
||||
func stop() {
|
||||
deadline?.cancel(); deadline = nil
|
||||
Self.cleanup.remove(self)
|
||||
finish()
|
||||
}
|
||||
|
||||
fileprivate func finish() {
|
||||
layer?.removeAnimation(forKey: Self.key); layer = nil
|
||||
}
|
||||
deinit { deadline?.cancel() }
|
||||
}
|
||||
|
||||
/// One one-shot timer serves the whole visible batch, retaining neither views nor motions.
|
||||
@MainActor private final class MotionCleanup {
|
||||
private struct Entry {
|
||||
weak var motion: MetricMotion?
|
||||
let deadline: DispatchTime
|
||||
}
|
||||
private var entries: [ObjectIdentifier: Entry] = [:]
|
||||
private var timer: DispatchSourceTimer?
|
||||
private var scheduled: UInt64?
|
||||
|
||||
func register(_ motion: MetricMotion) {
|
||||
entries[ObjectIdentifier(motion)] = Entry(motion: motion, deadline: .now() + MetricMotion.duration)
|
||||
schedule()
|
||||
}
|
||||
|
||||
func remove(_ motion: MetricMotion) {
|
||||
guard entries.removeValue(forKey: ObjectIdentifier(motion)) != nil else { return }
|
||||
schedule()
|
||||
}
|
||||
|
||||
private func schedule() {
|
||||
guard let first = entries.values.map(\.deadline.uptimeNanoseconds).min() else {
|
||||
timer?.cancel(); timer = nil; scheduled = nil
|
||||
return
|
||||
}
|
||||
// Core Animation ends at 350 ms. Group nearby cleanup deadlines 10 ms later.
|
||||
let deadline = first + 10_000_000
|
||||
guard scheduled != deadline else { return }
|
||||
scheduled = deadline
|
||||
let newTimer = timer == nil
|
||||
if newTimer {
|
||||
let source = DispatchSource.makeTimerSource(queue: .main)
|
||||
source.setEventHandler { [weak self] in MainActor.assumeIsolated { self?.complete() } }
|
||||
timer = source
|
||||
}
|
||||
timer?.schedule(deadline: DispatchTime(uptimeNanoseconds: deadline), leeway: .milliseconds(10))
|
||||
if newTimer { timer?.resume() }
|
||||
}
|
||||
|
||||
private func complete() {
|
||||
let now = DispatchTime.now().uptimeNanoseconds
|
||||
let expired = entries.filter { $0.value.motion == nil || $0.value.deadline.uptimeNanoseconds <= now }
|
||||
for (id, entry) in expired {
|
||||
entries.removeValue(forKey: id)
|
||||
entry.motion?.finish()
|
||||
}
|
||||
scheduled = nil
|
||||
schedule()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user