Here's another good one that would work better with multiple windows:
Use a NSViewRepresentable to get binding for NSWindow as in this StackOverflow answer: https://stackoverflow.com/a/63439982
And in order for contentAspectRatio
to exclude the title bar, you must also call window.styleMask.remove(.fullSizeContentView)
Ending up with something like:
struct Example: App {
@State private var window: NSWindow?
var body: some Scene {
WindowGroup {
ContentView()
.background(WindowAccessor(window: $window))
.onChange(of: window) { newWindow in
newWindow?.styleMask.remove(.fullSizeContentView) // Exclude title bar from content area
newWindow?.contentAspectRatio = desiredAspectRatio
}
}
}