My app use modal window opened by NSApp.runModal(for: NSWindow).
This program works in macOS 13, but nothing happens when I press the button(Button("close ModalWindow")) in macOS 14.0 Sonoma.
Why is it not working in macOS 14 ?
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Button("runModal") {
let window = ModalWindow(
contentRect: NSRect(x: 20, y: 20, width: 500, height: 350),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false)
window.isReleasedWhenClosed = false
window.title = NSLocalizedString("subView", comment: "")
window.contentView = NSHostingView(rootView: SubView())
NSApp.runModal(for: window)
}
.buttonStyle(.borderedProminent)
.tint(.accentColor)
}
.padding(10)
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 60, maxHeight: 60, alignment: .center)
}
}
#Preview {
ContentView()
}
final class ModalWindow: NSWindow {
override func becomeKey() {
super.becomeKey()
level = .modalPanel
}
override func close() {
super.close()
NSApp.stopModal()
}
}
struct SubView: View {
var body: some View {
HStack {
Button("close ModalWindow") {
NSApplication.shared.keyWindow?.close()
}
}
.padding(10)
.frame(minWidth: 150, maxWidth: .infinity, minHeight: 60, maxHeight: 60, alignment: .center)
}
}