Is it possible for an NSMenu object to notify BEFORE it'll close, not after? Its delegate has method didClose(_:) but I want to update its items before it actually closes, since the disappearing animation is too long and the eye can see the change.
I've tried to monitor NSEvents, but it's useless because NSMenu hasn't public property containing its NSWindow object.
It's theoretically possible to achieve by creating a custom NSViews for each menu items. But I don't like this because then I'll have to draw all the drawing of the items, including selection and click animation.
UPDATE: I've tried to subclass the NSPopUpButton to track menu updates:
class CustomPopUpButton: NSPopUpButton {
var isMenuShown: Bool = false
var onClosingMenu: ((NSMenu)->())?
override var needsDisplay: Bool {
willSet {
if let menu = self.menu, isMenuShown, newValue {
onClosingMenu?(menu)
isMenuShown = false
}
}
}
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
isMenuShown = true
super.willOpenMenu(menu, with: event)
}
}
I'm not proud of that piece of code but it works in general. Yet the 'onClosingMenu' method is being called just after the menu closing animation is finished. Not before.
Video of what I want to achieve: https://drive.google.com/file/d/1GAceKp-fTlurxSybdB3h0epVZrtQjthm/view?usp=sharing
NSMenu.willSendActionNotification
handler, because menu is closed once any item action sent. – Asperi