TTrayIcon.PopupMenu.OnPopup on MacOS lazarus / 31-LCL ---------------------------------------------------------------------- CCRDude · 2022-11-09 13:41 OS: macOS Ventura 13.0 Lazarus: 2.2 with FPC 3.2.2, Revision "unknown", x86_64-darwin-cocoa I'm writing a small tool to handle a bunch of day-to-day stuff from a menubar icon (like auto-reconnecting a VPN or user-mounting through sshfs). This menubar icon (TTrayIcon) has PopupMenu assigned listing VPN connections with captions indicating their state. Usually I would now create a OnPopup event for the PopupMenu and update item captions based on their state. But OnPopup doesn't fire when the menubar icon is clicked. Neither does OnMeasureItem. As a workaround, I would use the OnMouseDown or OnClick handlers of the TTrayIcon, but they don't get fired. Aside from using a timer and updating regularly when it's not required (and my status updates are querying command line tools, which is much overhead to repeatedly do here), can you suggest any other events that may work here? ---------------------------------------------------------------------- howardpc · 2022-11-09 13:52 You could try Application.OnIdle to avoid introducing a timer. ---------------------------------------------------------------------- CCRDude · 2022-11-09 16:19 That wouldn't avoid the "updating regularly when it's not required" though (at some point, that will be required for the auto-reconnect, but only for those entries that need to auto-reconnect) ---------------------------------------------------------------------- howardpc · 2022-11-09 17:51 If you have an algorithm for determining when it's not required, you can disable the handler for those cases, and re-enable it when the algorithm tells it to get going again. ---------------------------------------------------------------------- CCRDude · 2022-11-09 20:09 Well, it's updating the UI when the menu opens, the "algorithm" is clear, see my initial post. QuoteUsually I would now create a OnPopup event for the PopupMenu and update item captions based on their state. An alternative would be a system event telling me a VPN state has changed. I don't know any, do you? (and then, a second for when a volume has been mounted or unmounted) ---------------------------------------------------------------------- howardpc · 2022-11-10 08:36 I only just took note that this is for MacOS, whose menu system differs significantly from Win/Linux with which I am familiar. So sorry, no experience there to help you. ---------------------------------------------------------------------- dbannon · 2022-11-10 11:29 OnMeasureItem is marked as Restricted on Mac in the Object Inspector. But I see no mention of OnPopup also being so. But the SystemTray is very OS dependent, it does no more that the OS permits and often quite a lot less. So, it could be a case of MacOS just does not fire an event before displaying the menu. Probably worth looking in the code. I guess you have tested that a popmemu does fire OnPopUp if its attached to just a button of a form rather than the SystemTray ? Davo ---------------------------------------------------------------------- trev · 2022-11-10 23:31 The OP may find my macOS-specific Wiki article on NSStatusBar useful. ---------------------------------------------------------------------- dbannon · 2022-11-11 00:29 Hmm, thats interesting trev, should we consider modifying the existing Lazarus System Tray Unit to work through the StatusBar instead ? My application is quite happy with a pure SystemTray approach but I am not suprised all functions are not available. They are not in any modern Linux for example .... D ---------------------------------------------------------------------- CCRDude · 2022-11-11 06:59 Quote from: dbannon on November 10, 2022, 12:29:25 pmI guess you have tested that a popmemu does fire OnPopUp if its attached to just a button of a form rather than the SystemTray I use it quite often to update Enabled states of actions available in the menu based on context, but I tried again. In this context, I found another "interesting" part: If I call .Popup for the same TPopupMenu that's associated with the TTrayIcon, the .OnPopup event is triggered, but afterwards, the tray icon won't open the menu any more! (I guess I need to test on trunk to be sure this hasn't been fixed, but I can't install a trunk version through fpcupdeluxe as I usually do, because of insufficient disk space for XCode. Will have to find another way or finally replace the SDD ) Quote from: trev on November 11, 2022, 12:31:16 amThe OP may find my macOS-specific Wiki article on NSStatusBar useful. Indeed, thanks a lot! Would need some more attention to Apples framework, since I use checked menu items, submenus etc, but sounds interesting.