The following example displays in the StatusBar whether the menu is currently opened or closed. I have tested it successfully on MacOS 10.14.6 'Mojave' with PB 5.71 x64:
Code: Select all
EnableExplicit
ProcedureC NotificationCallback(Object.I, Selector.I, Notification.I)
Protected NotificationName.S
NotificationName = PeekS(CocoaMessage(0,
CocoaMessage(0, Notification, "name"), "UTF8String"), -1, #PB_UTF8)
Select NotificationName
Case "NSMenuDidBeginTrackingNotification"
StatusBarText(0, 0, "Menu is opened", #PB_StatusBar_Center)
Case "NSMenuDidEndTrackingNotification"
StatusBarText(0, 0, "Menu is closed", #PB_StatusBar_Center)
EndSelect
EndProcedure
Define AppDelegate.I = CocoaMessage(0, CocoaMessage(0, 0,
"NSApplication sharedApplication"), "delegate")
Define DelegateClass.I = CocoaMessage(0, AppDelegate, "class")
Define Menu.I
Define NotificationCenter.I = CocoaMessage(0, 0,
"NSNotificationCenter defaultCenter")
Define Selector.I = sel_registerName_("DetectMenuAction")
Define SharedApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
OpenWindow(0, 270, 100, 320, 100, "Detect opening and closing of menu")
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 0, "Menu is closed", #PB_StatusBar_Center)
CreateMenu(0, WindowID(0))
MenuTitle("Program")
MenuItem(0, "Info")
MenuItem(1, "Quit")
SharedApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
Menu = CocoaMessage(0, SharedApp, "mainMenu")
class_addMethod_(DelegateClass, Selector, @NotificationCallback(), "v@:@")
CocoaMessage(0, NotificationCenter,
"addObserver:", AppDelegate,
"selector:", Selector,
"name:$", @"NSMenuDidBeginTrackingNotification",
"object:", Menu)
CocoaMessage(0, NotificationCenter,
"addObserver:", AppDelegate,
"selector:", Selector,
"name:$", @"NSMenuDidEndTrackingNotification",
"object:", Menu)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Menu
Select EventMenu()
Case 0
MessageRequester("Info",
"This program demonstrates how to detect opening and closing" +
" of the menu.",
#PB_MessageRequester_Info)
Case 1
Break
EndSelect
EndSelect
ForEver
CocoaMessage(0, NotificationCenter, "removeObserver:", AppDelegate)