perf(menubar): 隔离卡片动效并推进 1.1 低开销版本

This commit is contained in:
2026-11-07 12:33:18 +08:00
parent 668bdab0b8
commit 5787193754
19 changed files with 362 additions and 34 deletions
@@ -0,0 +1,32 @@
import AppKit
import SwiftUI
/// Keeps local animation rendering inside a fixed-size child host.
struct NativeContentHost<Content: View>: NSViewRepresentable {
let content: Content
func makeNSView(context: Context) -> NSHostingView<AnyView> {
let host = NSHostingView(rootView: root(context))
// The parent owns the card frame, including its responsive column width.
host.sizingOptions = []
return host
}
func updateNSView(_ host: NSHostingView<AnyView>, context: Context) {
host.rootView = root(context)
}
func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSHostingView<AnyView>, context: Context) -> CGSize? {
CGSize(width: proposal.width.flatMap { $0.isFinite ? max(0, $0) : nil } ?? 10,
height: proposal.height.flatMap { $0.isFinite ? max(0, $0) : nil } ?? 10)
}
static func dismantleNSView(_ host: NSHostingView<AnyView>, coordinator: ()) {
// AppKit may cache a detached view. Release its mounted content immediately.
host.rootView = AnyView(EmptyView())
}
private func root(_ context: Context) -> AnyView {
AnyView(content.environment(\.self, context.environment))
}
}