Reputation: 2213
I have an status menu that uses NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
. I am porting a MenuBarExtra that has many Views and state attributes. I want to avoid using NSMenuItem
due to an ObservedObject
I depend on inside the SwiftUI view I am porting. The status menu itself is managed by an class StatusItemManager: ObservableObject
.
let statusItem: NSStatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
...
let statusMenuContent = NSHostingView(rootView: MyViewWithButtons())
statusMenuContent.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
menuItem.view = statusMenuContent
menu.addItem(menuItem)
This is the "gist" of it but I can post more code. This is more of a knowledge question about whether SwiftUI provides something to do what I'm asking or if I have to do it the manual way using menu.addItem for each View I have.
What I see:
What I want (previous behaviour using MenuBarExtra).
If I do
// inside MyViewWithButtons
var body: some View {
Menu("") {
// rest of views
}
}
I get the following undesired behaviour:
Upvotes: 0
Views: 111
Reputation: 2793
You don’t need NSMenu
.
Wrap your MyViewWithButtons
body in SwiftUI’s Menu
, everything will have a menu style.
struct MyViewWithButtons: View {
// your existing code
var body: some View {
Menu("SBApp") {
// your existing view code
}
}
}
Upvotes: 1