65 lines
2.7 KiB
Swift
65 lines
2.7 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
import Observation
|
|
|
|
@MainActor @Observable private final class TabState {
|
|
var selected = 0
|
|
var locale = Locale(identifier: "en")
|
|
}
|
|
|
|
private struct TabHost: View {
|
|
@Bindable var state: TabState
|
|
var body: some View {
|
|
NeutralTabPicker(title: "panel.page", selection: $state.selected,
|
|
options: [(0, "panel.overview"), (1, "panel.processes")])
|
|
.environment(\.locale, state.locale)
|
|
.frame(width: 164, height: 28)
|
|
}
|
|
}
|
|
|
|
@main struct TabPickerChecks {
|
|
@MainActor static func main() {
|
|
_ = NSApplication.shared
|
|
NSApp.setActivationPolicy(.accessory)
|
|
Task { @MainActor in
|
|
let state = TabState()
|
|
let window = NSWindow(contentRect: NSRect(x: 40, y: 40, width: 220, height: 70),
|
|
styleMask: [.titled], backing: .buffered, defer: false)
|
|
window.isReleasedWhenClosed = false
|
|
let host = NSHostingView(rootView: TabHost(state: state))
|
|
window.contentView = host
|
|
window.makeKeyAndOrderFront(nil)
|
|
await until { find(host) != nil }
|
|
let control = find(host)!
|
|
await until { control.label(forSegment: 0) == "Overview" }
|
|
precondition(control.selectedSegment == 0)
|
|
state.selected = 1
|
|
await until { control.selectedSegment == 1 }
|
|
control.selectedSegment = 0
|
|
_ = control.sendAction(control.action, to: control.target)
|
|
await until { state.selected == 0 }
|
|
for (locale, title) in [("zh-Hans", "概览"), ("zh-Hant", "概覽"), ("en", "Overview")] {
|
|
state.locale = Locale(identifier: locale)
|
|
await until { control.label(forSegment: 0) == title }
|
|
precondition(find(host) === control, "Locale updates must retain the mounted native control")
|
|
precondition(control.selectedSegment == 0)
|
|
}
|
|
window.orderOut(nil); window.close()
|
|
print("PASS: native tab binding in both directions, localized labels in place and retained selection")
|
|
fflush(stdout); NSApp.terminate(nil)
|
|
}
|
|
NSApp.run()
|
|
}
|
|
|
|
@MainActor private static func until(_ condition: () -> Bool) async {
|
|
let deadline = ContinuousClock.now.advanced(by: .seconds(3))
|
|
while !condition() && ContinuousClock.now < deadline { try? await Task.sleep(for: .milliseconds(20)) }
|
|
precondition(condition(), "Timed out waiting for native tab update")
|
|
}
|
|
|
|
@MainActor private static func find(_ view: NSView) -> NSSegmentedControl? {
|
|
if let control = view as? NSSegmentedControl { return control }
|
|
return view.subviews.lazy.compactMap { find($0) }.first
|
|
}
|
|
}
|