swiftmacosswiftuiappkit

SwiftUI on MacOS. - Opening a new Window


In my MacOS SwiftUI App I want to display some views in new Windows. The calls shall be possible from several parts of the code so I decided, do implement it as a static func of as special struct like this.
Works fine - there is something to be said against it?

struct Appwindows {
  
  static func newWindow(forSpecialView view: SpecialView, title: String = "new Window")
 { let newWindow = newWindowInternal(title: title)!
   
    newWindow.contentView = NSHostingView(rootView: view)
 }
  
  private static func newWindowInternal(title: String = "new Window") -> NSWindow!
 { var newWindow: NSWindow!
   newWindow = NSWindow(
     contentRect: NSRect(x: 20, y: 20, width: 680, height: 600),
     styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
     backing: .buffered,
     defer: false)
     newWindow.center()
     newWindow.isReleasedWhenClosed = false
     newWindow.title = title
     newWindow.makeKeyAndOrderFront(nil)
     return newWindow
 }
}

Is there a way to make it more generic, so that any kind of views could be passed to the func?


Solution

  • Make your function generic and add view constraint.

    static func newWindow<Content: View>(forSpecialView view: Content, title: String = "new Window") { // <-- Here
    

    Another good and easy solution is to use View extension.

    extension View {
        private func newWindowInternal(with title: String) -> NSWindow {
            let window = NSWindow(
                contentRect: NSRect(x: 20, y: 20, width: 680, height: 600),
                styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                backing: .buffered,
                defer: false)
            window.center()
            window.isReleasedWhenClosed = false
            window.title = title
            window.makeKeyAndOrderFront(nil)
            return window
        }
        
        func openNewWindow(with title: String = "new Window") {
            self.newWindowInternal(with: title).contentView = NSHostingView(rootView: self)
        }
    }
    

    Usage:

    struct ContentView: View {
        var body: some View {
            Button(action: {
                ContentViewNewWindow().openNewWindow()
            }) {
                Text("Open New Window")
            }
        }
    }