Page 1 of 1

Lazarus – macOS – Set Dock Icon text and bouncing Dock Icon

Lazarus – macOS – Set Dock Icon text and bouncing Dock Icon

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 – Set Dock Icon text and bouncing Dock Icon

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

Viewing 1 post (of 1 total)
  • Author
    Posts
  • 11857

    hans
    Keymaster

    So one of the nice things of macOS is the Dock.
    From an application perspective, the bouncing icon asking for the attention of the user is cool, and having a badge (a number or % or whatever you want) tagged to the dock icon helps with giving the user feedback even when the application is not visible.

    So how do we do this in Lazarus Pascal?
    I found this post in the Lazarus Forum and after some tinkering managed to get both to work in a very easy way.

    First of all, the compiler needs to be set in ObjectiveC1 mode. You can place this somewhere at the top of your unit.

    {$modeswitch ObjectiveC1}

    Next we will need the unit MacOSAll and CocoaAll.

    uses ... ,MacOSAll, CocoaAll ...;

    Bouncing Dock Icon …

    1. Bounce once:

    NSApplication.sharedApplication.requestUserAttention(NSInformationalRequest);

    2. Bounce until the application (form) gets focus (only works when the application does not have focus!):

    NSApplication.sharedApplication.requestUserAttention(NSCriticalRequest);

    3. Manually stop the bouncing: 

    For this we will need to store the request ID, so we can reference it when asking for the bouncing to stop.
    Because of this we start the bouncing as such:

    AttentionRequestID : NSInteger;
    ...
    AttentionRequestID := NSApplication.sharedApplication.requestUserAttention(NSCriticalRequest);
    ...

    and later on we stop it with:

    NSApplication.sharedApplication.cancelUserAttentionRequest(AttentionRequestID);

    Add Text to Dock Icon (eg. a number or a percentage) …

    In this example I set the text to “100%“, but this can be anything – just keep it short, there is not much space!

    NSApplication.sharedApplication.dockTile.setBadgeLabel(NSStr(Pchar('100%')));

    The tekst is removed again with:

    NSApplication.sharedApplication.dockTile.setBadgeLabel(nil);
Viewing 1 post (of 1 total)



You must be logged in to reply to this topic.