0

I have a Mac app with an Appkit lifecycle. I would like to create a window that is entirely defined by SwiftUI, including the toolbar.

What I've tried:

  1. Programmatically create an NSWindow and set its view to be an NSHostingView to a SwiftUI view that uses .toolbar(...). The toolbar does not show up.

  2. Programmatically create an NSWindow and an NSToolbar, set up the toolbar using Appkit APIs and assign the toolbar to the window. The toolbar shows up as expected, but is not configured using SwiftUI and it is awkward to have the toolbar reference or interact with SwiftUI view state.

  3. Rewrite the entire app with a SwiftUI lifecycle. Works fine. Not really an option for me.

Primary question:

Is there a way to wire up the toolbar for a manually created NSWindow in such a way that SwiftUI views can use the .toolbar attributes to place/manage items in it?

If the answer is simply, "no":

  • Is there a way to create a full SwiftUI lifecycle window without the entire app being constructed that way? (I have many non-SwiftUI windows that can't/won't be converted)
  • Alternatively, is there a way for the Appkit toolbar code to less awkwardly reference View state in SwiftUI views and/or respond to published changes in SwiftUI state?
zaphoyd
  • 2,642
  • 1
  • 16
  • 22
  • Same here: I can't make `.toolbar` work with `NSHostingView`. [This](https://stackoverflow.com/a/60468060/16157878) is an approach that uses `NSWindow.addTitlebarAccessoryViewController` to get a toolbar, which would allow to keep all non-SwiftUI windows. – soundflix Aug 23 '23 at 18:09
  • There is also [https://github.com/billibala/SUIToolbarPlay], which creates an AppKit `NSToolbar` programmatically, which was probably something like your second attempt. – soundflix Aug 23 '23 at 18:42

0 Answers0