I am building a MacOS application which shows a menubar icon with a popover. I am using SwiftUI and building for Monterey only. I would like to change some app settings and I enabled a button inside the popover. Now, then the user presses the button, this should open a standard window where the user can add/change settings. The window is designed using SwiftUI.
So in my code, I tried to do something similar to this
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
Button(action: {openWindow()}) { Text("SETTINGS")}
Spacer()
}
}
func openWindow() {
let window = NSWindow()
let contentView = ContentView()
window.aspectRatio = NSSize(width: 400, height: 400)
window.contentViewController = NSHostingController(rootView: contentView)
window.makeKey()
}
but nothing happen. So I am wondering few things:
- what did I miss with Cocoa here?
- shall I use Cocoa for this? Isn't anything more native ?