Page 1 of 1

Lazarus – MacOS X – TTrayIcon, Added items to popup menu not visible

Lazarus – MacOS X – TTrayIcon, Added items to popup menu not visible

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.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Software Development Delphi, Lazarus, Free Pascal Lazarus – MacOS X – TTrayIcon, Added items to popup menu not visible

This topic contains 2 replies, has 1 voice, and was last updated by  hans 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • 4917

    hans
    Keymaster

    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;                             
    11164

    hans
    Keymaster

    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.

    11353

    hans
    Keymaster

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

Viewing 3 posts - 1 through 3 (of 3 total)



You must be logged in to reply to this topic.