Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X - TTrayIcon, Added items to popup menu not visible

3 Posts
1 Users
0 Reactions
1,920 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

I was toying with TTrayIcon on a Mac and found that when you add items dynamically (at runtime) to the popupmenu of the trayicon, that changes are not made visible when you click the trayicon (placed on the Mac in the top Menu Bar). So far this seems "normal" as I have gotten no response if this should be this way or not.

No matter what you try: add/remove/change items, even destroy the entire popupmenu - it remains as was at startup of your application.

After a lot of fiddling I found a trick that makes it that changes are visible. Simply hide the trayicon, make your changes and show it again.

trayicon1.Hide;
// add/remove/modify popupmenu items
trayicon1.Show;

For those interested, this is how you add items to the menu:

...
var tmpMenuItem : TMenuItem;
...
          tmpMenuItem := TMenuItem.Create(PopupMenu1);
          tmpMenuItem.Caption:='new menu item'; // Optional: // tmpMenuItem.Name := 'MyMenuItem'; // Give it a name so you can later "talk" to this item // Optional, but pretty much required, otherwise the menu doesn't do anything when you click it
          // tmpMenuItem.OnClick:=@MenuItem_Click; // so the click event is handled
...
          PopupMenu1.Items.Add(tmpMenuItem); // insert at the beginning or at a given position //  PopupMenu1.Items.Insert(0,tmpMenuItem);  // Alternative: insert at the beginning or at a given position
         
...

The procedure MenuItem_Click could be defined as:

...
procedure MenuItem_Click(Sender: TObject);
...
procedure TConnectMeNowForm.MenuItem_ServerShareClick(Sender: TObject);
var tmpMenuItem:TMenuItem;
begin
  tmpMenuItem:=TMenuItem(Sender); if tmpMenuItem='MyMenuItem' then ... // do something
  ...
end;                             

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Notes:

Carbon: Instead of hiding and showing, after updating a menu, use: 

TrayIcon.InternalUpdate;

Cocoa: Using Hide and Show (Lazarus 2.1.0 r59646M) will actually draw 2 trayicons, I haven't found a good fix yet.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

For Cocoa this issue has been fixed! Thanks Dmitry at the Lazarus Forum!


   
ReplyQuote
Share: