How to use a SwiftUI touchbar with a NSWindow

Inside the app delegate I uses a simple SwiftUI view as the root for my macOS main window.


func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView().touchBar(myTouchbar)

        // 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.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }


This contentView has a touchbar asigned to it. If the contentView is a simple view that can hold a focus (like a Textield) then the touchbar becomes visible on my MacBook. But in my application the contentView (the root view of the NSWindow) is a layout container like a HSplitView. In this case the window appears but the touchbar is not visible. How can I use a SwiftUI touchbar with a window without focusable or input elements so that the touchbar is always visible together with the window.

The only way I have found for this so far is to use the focusable() modifier:


var body: some View {
  myView()
    .focusable()
    .touchBar() { … }
}
How to use a SwiftUI touchbar with a NSWindow
 
 
Q