SwiftUI 2.0 can't remove .titled from styleMask on NSWindow using NSViewRepresentable

294 views Asked by At

I'm reworking my app for SwiftUI 2.0 but have come across a problem when replicating what I could do with AppDelegate.

I'm using NSViewRepresentable to get access to NSWindow so I can remove the titlebar of the window (I know it's not in the guidelines but this will never be submitted). When removing .titled from styleMask, the app crashes.

struct WindowAccessor: NSViewRepresentable {
    @Binding var window: NSWindow?
    
    func makeNSView(context: Context) -> NSView {
        let view = NSView()
        DispatchQueue.main.async {
            self.window = view.window
            self.window?.isOpaque = false
            self.window?.titlebarAppearsTransparent = true
            self.window?.backgroundColor = NSColor.clear
            self.window?.styleMask = [.fullSizeContentView]
            self.window?.isMovableByWindowBackground = true
            self.window?.backingType = .buffered
        }
        return view
    }
    
    func updateNSView(_ nsView: NSView, context: Context) {}
}

@main
struct MyApp_App: App {
    @State private var window: NSWindow?
    var body: some Scene {
        WindowGroup {
            ContentView().background(WindowAccessor(window: $window))
        }
    }
}

struct ContentView: View {
    var body: some View {
        Text("Hello, world!").padding().background(Color(NSColor.windowBackgroundColor))
    }
}

When I run the app I get Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

All I'm trying to achieve with my app is a Menu Bar application that looks exactly like Spotlight. No dock icon, no title bar, all preferences to be handled by a popover or another window.

EDIT:

Is this something to do with the canBecomeKey property?

0

There are 0 answers