Looks like I was heading down the wrong path. I find that I need to work with NSWindow, not NSWindowController, and among other things, its style mask. I found some code on line that lets me position the window on the screen. I made some modifications to it and added a function setStyleMask which is an extension to NSWndow. It allows me to make the modifications I want. Below is the updated code. My ContentView does not change with the exception of the elimination of @State private var winController = WinController().
import SwiftUI
@main
struct BouncingWheelApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.hostingWindowPosition(
vertical: .center,
horizontal: .center,
screen: .main)
}
}
}
import SwiftUI
import AppKit
extension NSWindow {
func setStyleMask() {
titlebarAppearsTransparent = true
titleVisibility = .hidden
backgroundColor = .gray
styleMask.remove(.resizable)
}
func setPosition(_ position: Position, in screen: NSScreen?) {
setStyleMask()
guard let visibleFrame = (screen ?? self.screen)?.visibleFrame else { return }
let origin = position.value(forWindow: frame, inScreen: visibleFrame)
let myRect = CGRect(x: origin.x, y: origin.y, width: 800.0, height: 800.0)
setFrame(myRect, display: true)
} // end function set position
struct Position {
static let defaultPadding: CGFloat = 16
var vertical: Vertical
var horizontal: Horizontal
var padding = Self.defaultPadding
enum Horizontal {
case left, center, right
}
enum Vertical {
case top, center, bottom
}
func value(forWindow windowRect: CGRect, inScreen screenRect: CGRect) -> CGPoint {
let xPosition = horizontal.valueFor(screenRange: screenRect.minX..<screenRect.maxX, width: windowRect.width, padding: padding)
let yPosition = vertical.valueFor(screenRange: screenRect.minY..<screenRect.maxY, height: windowRect.height, padding: padding)
return CGPoint(x: xPosition, y: yPosition)
}
} // end structure Position
}
extension NSWindow.Position.Horizontal {
func valueFor(screenRange: Range<CGFloat>, width: CGFloat, padding: CGFloat) -> CGFloat {
switch self {
case .left:
return screenRange.lowerBound + padding
case .center:
return (screenRange.upperBound + screenRange.lowerBound - width) / 2
case .right:
return screenRange.upperBound - width - padding
}
}
}
extension NSWindow.Position.Vertical {
func valueFor(screenRange: Range<CGFloat>, height: CGFloat, padding: CGFloat) -> CGFloat {
switch self {
case .top:
return screenRange.upperBound - height - padding
case .center:
return (screenRange.upperBound + screenRange.lowerBound - height) / 2
case .bottom:
return screenRange.lowerBound + padding
}
}
}
struct HostingWindowFinder: NSViewRepresentable {
var callback: (NSWindow?) -> ()
func makeNSView(context: Self.Context) -> NSView {
let view = NSView()
DispatchQueue.main.async { self.callback(view.window) }
return view
}
func updateNSView(_ nsView: NSView, context: Context) {
DispatchQueue.main.async { self.callback(nsView.window) }
}
}
private struct WindowPositionModifier: ViewModifier {
let position: NSWindow.Position
let screen: NSScreen?
func body(content: Content) -> some View {
content
.background(HostingWindowFinder {
$0?.setPosition(position, in: screen)
}
)
}
}
extension View {
func hostingWindowPosition(vertical: NSWindow.Position.Vertical, horizontal: NSWindow.Position.Horizontal, padding: CGFloat = NSWindow.Position.defaultPadding, screen: NSScreen? = nil) -> some View {
modifier(
WindowPositionModifier(
position: NSWindow.Position(vertical: vertical, horizontal: horizontal, padding: padding),
screen: screen
)
)
}
}