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

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

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

   
ReplyQuote
Share: