I started down this rabbit hole because a SwiftUI button, designated to pop up a menu, with a .buttonStyle(BorderlessButtonStyle())
style wasn't behaving correctly when switching from a light to a dark mode.
Frustrated, I decided to just make my own button. I noticed the SwiftUI button would sort of toggle when popping up the menu and so I want this behaviour for my button as well. Therefore, I need to find out when an NSMenu is closed.
I've tried the answer suggested here ⤵︎
class NSMenuDelegateModified: NSObject, NSMenuDelegate, ObservableObject {
var menu: NSMenu
@Published var isVisible: Bool?
internal init(menu: NSMenu) {
self.menu = menu
super.init()
self.menu.delegate = self
}
func menuDidClose(_ menu: NSMenu) {
isVisible = false
}
func menuWillOpen(_ menu: NSMenu) {
isVisible = true
}
}
Now this will tell me if the menu is visible from within the class, but when I try to print out .isVisible
on an instantiated object of the class, it only returns false.
Is there a way to figure this out?