perf(menubar): 隔离卡片动效并推进 1.1 低开销版本
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
@main struct NativeContentHostChecks {
|
||||
@MainActor static func main() {
|
||||
_ = NSApplication.shared
|
||||
NSApp.setActivationPolicy(.accessory)
|
||||
Task { @MainActor in
|
||||
let probe = Probe()
|
||||
let window = NSWindow(contentRect: NSRect(x: 50, y: 50, width: 240, height: 80),
|
||||
styleMask: [.borderless], backing: .buffered, defer: false)
|
||||
window.isReleasedWhenClosed = false
|
||||
window.level = .floating
|
||||
let host = NSHostingView(rootView: Example(value: "128", probe: probe))
|
||||
host.sizingOptions = []
|
||||
window.contentView = host
|
||||
window.orderFrontRegardless()
|
||||
try? await Task.sleep(for: .milliseconds(250))
|
||||
let ideal = host.fittingSize
|
||||
precondition(ideal.width.isFinite && ideal.height.isFinite, "Unconstrained fitting must stay finite")
|
||||
let before = bitmap(host)
|
||||
precondition(probe.appearances == 1 && probe.animates && probe.locale == "en_US" && probe.font == .system(size: 22, weight: .medium))
|
||||
precondition(redPixels(before) > 30, "Foreground style must cross the native boundary")
|
||||
let originalLifetime = ObjectIdentifier(probe.lifetime!)
|
||||
host.rootView = Example(value: "345", probe: probe)
|
||||
try? await Task.sleep(for: .milliseconds(140))
|
||||
let middle = bitmap(host)
|
||||
try? await Task.sleep(for: .milliseconds(600))
|
||||
let after = bitmap(host)
|
||||
precondition(before.bitmapData != nil && after.bitmapData != nil)
|
||||
precondition(pixels(before) != pixels(after), "Changed values must reach the retained child host")
|
||||
precondition(pixels(middle) != pixels(before) && pixels(middle) != pixels(after),
|
||||
"The system numeric transition must still have an intermediate frame")
|
||||
precondition(probe.appearances == 1 && probe.lifetime.map(ObjectIdentifier.init) == originalLifetime,
|
||||
"Sampling must update mounted content, not remount it")
|
||||
host.rootView = Example(value: "345", probe: probe, reduced: true, locale: "zh_CN")
|
||||
try? await Task.sleep(for: .milliseconds(100))
|
||||
precondition(!probe.animates && probe.locale == "zh_CN")
|
||||
window.setContentSize(NSSize(width: 360, height: 100))
|
||||
try? await Task.sleep(for: .milliseconds(100))
|
||||
@MainActor func childHost(_ view: NSView) -> NSHostingView<AnyView>? {
|
||||
if let child = view as? NSHostingView<AnyView> { return child }
|
||||
return view.subviews.lazy.compactMap(childHost).first
|
||||
}
|
||||
let child = childHost(host)
|
||||
precondition(child?.bounds.size == host.bounds.size, "The parent must remain the sole frame owner")
|
||||
host.rootView = Example(value: "345", probe: probe, visible: false)
|
||||
try? await Task.sleep(for: .milliseconds(150))
|
||||
precondition(probe.disappearances == 1)
|
||||
let releaseDeadline = ContinuousClock.now.advanced(by: .seconds(1))
|
||||
while probe.lifetime != nil && ContinuousClock.now < releaseDeadline {
|
||||
try? await Task.sleep(for: .milliseconds(20))
|
||||
}
|
||||
precondition(probe.lifetime == nil, "A cached native view must release detached content")
|
||||
window.orderOut(nil)
|
||||
window.contentView = nil
|
||||
window.close()
|
||||
print("PASS: card host preserves system numeric intermediate frames, inherited font/style/locale/motion, child state, resize and unmount")
|
||||
fflush(stdout)
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
NSApp.run()
|
||||
}
|
||||
|
||||
@MainActor private final class Lifetime {}
|
||||
@MainActor private final class Probe {
|
||||
var appearances = 0
|
||||
var disappearances = 0
|
||||
var animates = false
|
||||
var locale = ""
|
||||
var font: Font?
|
||||
weak var lifetime: Lifetime?
|
||||
}
|
||||
private struct Example: View {
|
||||
let value: String
|
||||
let probe: Probe
|
||||
var reduced = false
|
||||
var locale = "en_US"
|
||||
var visible = true
|
||||
var body: some View {
|
||||
Group {
|
||||
if visible { NativeContentHost(content: Content(value: value, probe: probe)) }
|
||||
}
|
||||
.font(.system(size: 22, weight: .medium))
|
||||
.foregroundStyle(.red)
|
||||
.environment(\.locale, Locale(identifier: locale))
|
||||
.environment(\.metricAnimationsEnabled, !reduced)
|
||||
}
|
||||
}
|
||||
private struct Content: View {
|
||||
let value: String
|
||||
let probe: Probe
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
@Environment(\.locale) private var locale
|
||||
@Environment(\.font) private var font
|
||||
@State private var lifetime = Lifetime()
|
||||
var body: some View {
|
||||
AnimatedMetricText(value: value)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.onAppear {
|
||||
probe.appearances += 1; probe.lifetime = lifetime
|
||||
recordEnvironment()
|
||||
}
|
||||
.onDisappear { probe.disappearances += 1 }
|
||||
.onChange(of: animates) { recordEnvironment() }
|
||||
.onChange(of: locale) { recordEnvironment() }
|
||||
}
|
||||
private func recordEnvironment() {
|
||||
probe.animates = animates; probe.locale = locale.identifier; probe.font = font
|
||||
}
|
||||
}
|
||||
@MainActor private static func bitmap(_ view: NSView) -> NSBitmapImageRep {
|
||||
view.layoutSubtreeIfNeeded()
|
||||
let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
|
||||
view.cacheDisplay(in: view.bounds, to: bitmap)
|
||||
return bitmap
|
||||
}
|
||||
private static func pixels(_ bitmap: NSBitmapImageRep) -> Data {
|
||||
var data = Data()
|
||||
let width = bitmap.pixelsWide * bitmap.bitsPerPixel / 8
|
||||
for row in 0..<bitmap.pixelsHigh {
|
||||
data.append(bitmap.bitmapData!.advanced(by: row * bitmap.bytesPerRow), count: width)
|
||||
}
|
||||
return data
|
||||
}
|
||||
private static func redPixels(_ bitmap: NSBitmapImageRep) -> Int {
|
||||
var count = 0
|
||||
for x in 0..<bitmap.pixelsWide {
|
||||
for y in 0..<bitmap.pixelsHigh {
|
||||
if let color = bitmap.colorAt(x: x, y: y)?.usingColorSpace(.deviceRGB),
|
||||
color.redComponent > color.greenComponent + 0.2 && color.redComponent > color.blueComponent + 0.2 {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
}
|
||||
@@ -114,6 +114,30 @@ import QuartzCore
|
||||
precondition(!ring.isAnimating && ring.displayedValue == 0.8)
|
||||
ring.configure(NativeMetricRing.Input(value: 0.2, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2, animates: true))
|
||||
ring.removeFromSuperview(); precondition(!ring.isAnimating && ring.displayedValue == 0.2)
|
||||
// Shared cleanup must not stop a different motion or a newer replacement.
|
||||
let firstMotion = MetricMotion(), secondMotion = MetricMotion()
|
||||
let firstLayer = CALayer(), secondLayer = CALayer()
|
||||
root.wantsLayer = true
|
||||
for target in [firstLayer, secondLayer] {
|
||||
target.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
|
||||
target.backgroundColor = NSColor.red.cgColor
|
||||
root.layer?.addSublayer(target)
|
||||
}
|
||||
func makeMovement() -> CABasicAnimation {
|
||||
let animation = CABasicAnimation(keyPath: "position.x")
|
||||
animation.fromValue = 0; animation.toValue = 10
|
||||
return animation
|
||||
}
|
||||
firstMotion.start(on: firstLayer, animation: makeMovement())
|
||||
secondMotion.start(on: secondLayer, animation: makeMovement())
|
||||
try? await Task.sleep(for: .milliseconds(180))
|
||||
firstMotion.stop()
|
||||
secondMotion.start(on: secondLayer, animation: makeMovement())
|
||||
try? await Task.sleep(for: .milliseconds(210))
|
||||
precondition(!firstMotion.isRunning && secondMotion.isRunning,
|
||||
"An old batch deadline must not remove a restarted animation")
|
||||
try? await Task.sleep(for: .milliseconds(250))
|
||||
precondition(!secondMotion.isRunning, "The replacement must also finish and release its animation")
|
||||
window.orderOut(nil); window.close()
|
||||
print("PASS: native curve pixels, real gaps, missing series, hover, appearance, duplicate-input invalidation zero/quarter/full ring pixels and finite transitions/gap/policy/unmount cancellation")
|
||||
fflush(stdout); NSApp.terminate(nil)
|
||||
|
||||
@@ -38,6 +38,15 @@ import Darwin
|
||||
count += 1
|
||||
}
|
||||
precondition(count > 0)
|
||||
var monitor = NetworkMonitor()
|
||||
for _ in 0..<30 {
|
||||
guard let interfaces = monitor.sample() else { preconditionFailure("Live counter read failed") }
|
||||
for item in interfaces {
|
||||
precondition(item.download.map { $0.isFinite && $0 >= 0 } ?? true)
|
||||
precondition(item.upload.map { $0.isFinite && $0 >= 0 } ?? true)
|
||||
precondition(item.addresses == item.addresses.sorted())
|
||||
}
|
||||
}
|
||||
print("PASS: sandbox IPv4/IPv6, known/unknown scopes, mapped addresses, empty buffer and canonical equivalence for", count, "live addresses (values omitted)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user