Recent

Author Topic: TTrayIcon.PopupMenu.OnPopup on MacOS  (Read 562 times)

CCRDude

  • Hero Member
  • *****
  • Posts: 541
TTrayIcon.PopupMenu.OnPopup on MacOS
« on: November 09, 2022, 02:41:49 pm »
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

  • Hero Member
  • *****
  • Posts: 4096
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #1 on: November 09, 2022, 02:52:53 pm »
You could try Application.OnIdle to avoid introducing a timer.

CCRDude

  • Hero Member
  • *****
  • Posts: 541
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #2 on: November 09, 2022, 05:19:37 pm »
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

  • Hero Member
  • *****
  • Posts: 4096
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #3 on: November 09, 2022, 06:51:11 pm »
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

  • Hero Member
  • *****
  • Posts: 541
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #4 on: November 09, 2022, 09:09:33 pm »
Well, it's updating the UI when the menu opens, the "algorithm" is clear, see my initial post.
Quote
Usually 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

  • Hero Member
  • *****
  • Posts: 4096
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #5 on: November 10, 2022, 09:36:10 am »
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.
« Last Edit: November 10, 2022, 01:57:53 pm by howardpc »

dbannon

  • Hero Member
  • *****
  • Posts: 2280
    • tomboy-ng, a rewrite of the classic Tomboy
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #6 on: November 10, 2022, 12:29:25 pm »
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
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2004
  • Former Delphi 1-7, 10.2 user
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #7 on: November 11, 2022, 12:31:16 am »
The OP may find my macOS-specific Wiki article on NSStatusBar useful. 
Lazarus 2.3, FPC 3.3.1 macOS 12.6.1 x86_64 Xcode 14.1
Lazarus 2.3, FPC 3.3.1 macOS 12.6.1 aarch64 Xcode 14.1

dbannon

  • Hero Member
  • *****
  • Posts: 2280
    • tomboy-ng, a rewrite of the classic Tomboy
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #8 on: November 11, 2022, 01:29:47 am »
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
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

CCRDude

  • Hero Member
  • *****
  • Posts: 541
Re: TTrayIcon.PopupMenu.OnPopup on MacOS
« Reply #9 on: November 11, 2022, 07:59:21 am »
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

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 :) )

The 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.

 

TinyPortal © 2005-2018