Post

Replies

Boosts

Views

Activity

Reply to How to open a window in SwiftUI?
There is a hacky way to do this. You can create a View that acts as your app's root view. The body of this view depends on the value of a specific variable. For example: @main struct TestApp: App {     var body: some Scene {         WindowGroup {             RootView()         }     } } var useWindow1: Bool = true struct RootView: View {     var body: some View {         Group {             if useWindow1 {                 Window1()             } else {                 Window2()             }         }     } } You can then use the NSWindowController on the MainWindow to open a new window.  NSApp.mainWindow?.windowController?.newWindowForTab(nil) Though this does require that at least one window is visible. There could be another way to trigger the "New Window" command, but I haven't found one that works. One upshot of using this instead of creating a new NSWindow with an NSHostingController is that toolbars won't display their items if you just put them in a Hosting Controller, since the NSWindow thinks it should manage the toolbar. Also, you may have to override some of macOS' window restoration behavior, since if you have two windows of two separate views when you quit the app, it may relaunch with two of the "default" view.
Sep ’20