import AppKit import SwiftUI /// 菜单命令的纯模型:标题、键、修饰键。 public struct MenuCommand { public let title: String public let key: String public let modifiers: NSEvent.ModifierFlags public let enabled: Bool public init( title: String, key: String = "", modifiers: NSEvent.ModifierFlags = [], enabled: Bool = true ) { self.title = title self.key = key self.modifiers = modifiers self.enabled = enabled } } public enum MenuBuilder { /// 构建 NSMenu,仅设置展示属性;动作由宿主注入。 public static func build(_ commands: [MenuCommand]) -> NSMenu { let menu = NSMenu() for command in commands { if command.title == "-" { menu.addItem(.separator()) continue } let item = NSMenuItem( title: command.title, action: nil, keyEquivalent: command.key ) item.keyEquivalentModifierMask = command.modifiers item.isEnabled = command.enabled menu.addItem(item) } return menu } } /// 窗口配置:桥接时需要覆盖 SwiftUI 无法直接设置的属性。 public struct WindowConfig: Sendable { public let title: String public let level: NSWindow.Level public let isMovableByBackground: Bool public let styleMask: NSWindow.StyleMask public init( title: String, level: NSWindow.Level = .normal, isMovableByBackground: Bool = true, styleMask: NSWindow.StyleMask = [.titled, .closable, .resizable] ) { self.title = title self.level = level self.isMovableByBackground = isMovableByBackground self.styleMask = styleMask } public var isValid: Bool { !title.isEmpty && styleMask.contains(.titled) } public func apply(to window: NSWindow) { window.title = title window.level = level window.isMovableByWindowBackground = isMovableByBackground window.styleMask = styleMask } } /// 统计 `updateNSView` 调用次数,验证桥接的生命周期。 public final class BridgeCounter: @unchecked Sendable { public private(set) var updates = 0 public private(set) var makes = 0 public init() {} public func markMake() { makes += 1 } public func markUpdate() { updates += 1 } } /// 包一个 NSTextField:最小 `NSViewRepresentable`。 public struct LabelField: NSViewRepresentable { public let text: String public let counter: BridgeCounter public init(text: String, counter: BridgeCounter) { self.text = text self.counter = counter } public func makeNSView(context: Context) -> NSTextField { counter.markMake() let field = NSTextField(labelWithString: text) field.isBezeled = false field.drawsBackground = false return field } public func updateNSView(_ nsView: NSTextField, context: Context) { counter.markUpdate() nsView.stringValue = text } } /// 截图:桥接板。 public struct BridgingBoard: View { public init() {} private let rows: [(String, String)] = [ ("NSViewRepresentable", "把 AppKit 视图塞进 SwiftUI"), ("Coordinator", "委托与回调的桥"), ("NSHostingView", "把 SwiftUI 塞进 AppKit"), ("NSWindow", "标题、层级、样式"), ("NSMenu / NSToolbar", "命令与工具栏"), ("生命周期", "make / update / dismantle"), ] public var body: some View { VStack(alignment: .leading, spacing: 9) { Text("AppKit 桥接").font(.system(size: 17, weight: .semibold)) ForEach(rows, id: \.0) { row in HStack(spacing: 10) { Text(row.0) .font(.system(size: 9, design: .monospaced)) .frame(width: 150, alignment: .leading) Text(row.1) .font(.system(size: 11)) .foregroundStyle(.secondary) Spacer() } } } .padding(18) .frame(width: 380, alignment: .leading) .background(Color.white) } }