title: Update NSWindow content size to fit SwiftUI view
body: <p>I have a SwiftUI view that do not know the size of because of variable length strings (e.g. due to localisation). I need to have the containing <code>NSWindow</code> resize to fix the content. I can provide a fixed/minimum width.</p>
  <p>Creating a new Mac app with an AppKit delegate yields a fairly simple AppDelegate:</p>
  <pre class="lang-swift prettyprint-override"><code>import Cocoa
  import SwiftUI
  
  @main
  class AppDelegate: NSObject, NSApplicationDelegate {
      func applicationDidFinishLaunching(_ aNotification: Notification) {
          // Create the SwiftUI view that provides the window contents.
          let contentView = ContentView()
  
          // Create the window and set the content view.
          window = NSWindow(
              contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
              styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
              backing: .buffered, defer: false)
          window.isReleasedWhenClosed = false
          window.center()
          window.setFrameAutosaveName(&quot;Main Window&quot;)
          window.contentView = NSHostingView(rootView: contentView)
          window.makeKeyAndOrderFront(nil)
      }
  }
  </code></pre>
  <p>I then have a SwiftUI with a button that adds to the label's text to simulate changing the text:</p>
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  
  struct ContentView: View {
      @State var text = &quot;Hello, World!&quot;
  
      var body: some View {
          VStack {
              Text(text)
                  .frame(maxWidth: .infinity, maxHeight: .infinity)
              Button(&quot;Add Some Text&quot;) {
                  text += &quot;\nHello, World!&quot;
              }
          }
      }
  }
  </code></pre>
  <p>Clicking the button eventually causes too many lines of text to be added and the label truncates.</p>
  <p>How can I update the container <code>NSWindow</code>'s content size when the size of the SwiftUI view changes?</p>