Difference between revisions of "Show Badge on Application Icon in Dock"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎External links: Add another category)
(→‎Overview: Update warning info)
 
(6 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
== Overview ==
 
== Overview ==
  
Badges are used to notify a user of something new in an application. The badge is a counter that appears on the application's dock tile (icon) as a white number on a red circular or oval, depending on the number of digits, background. What the number represents depends on the application - it might be the number of new emails in an email application, new iMessages, unfinished Reminders, missed FaceTime calls, the number of outstanding application updates, etc.
+
{{Warning2|For unknown reasons this does not always work in macOS 12 (Monterey) and does not log any error in the system console log - it has been tested working in macOS 10.6 (Snow Leopard) through 11 (Big Sur). It has also been tested working on one 2018 Intel Mac mini macOS 12 system, but not working on one 2018 Intel Mac mini macOS 12 system and one 2020 M1 Mac mini macOS 12 system.}}
 +
 
 +
Badges are used to notify a user of something new in an application. The badge is a counter that appears on the application's dock tile (icon) as a white number on a red circular or oval, depending on the number of digits, background. What the number represents depends on the application - it might be the number of new emails in an email application, new iMessages, unfinished Reminders, missed FaceTime calls, the number of outstanding application updates, etc.
  
 
== Example code ==
 
== Example code ==
Line 31: Line 33:
 
     procedure Button1Click(Sender: TObject);
 
     procedure Button1Click(Sender: TObject);
 
     procedure Button2Click(Sender: TObject);
 
     procedure Button2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
 
 
   private
 
   private
  
Line 46: Line 47:
  
 
{ TForm1 }
 
{ TForm1 }
 
procedure TForm1.FormActivate(Sender: TObject);
 
begin
 
  // NSApplication.sharedApplication
 
  // - Returns the application instance, creating it if it doesn’t exist yet.
 
  // NSApp
 
  // - The global variable for the shared application instance.
 
  NSApp := NSApplication.sharedApplication;
 
end;
 
  
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
Line 65: Line 57:
 
begin
 
begin
 
   // Remove badge text from the dock tile (icon)
 
   // Remove badge text from the dock tile (icon)
   NSApp.dockTile.setBadgeLabel(NSStr(''));
+
   NSApp.dockTile.setBadgeLabel(Nil);
 
end;
 
end;
  
Line 73: Line 65:
 
== See also ==
 
== See also ==
  
* [[Bounce Application Icon in Dock]].
+
* [[Application disable resize over Dock]]
 +
* [[Bounce Application Icon in Dock]]
 +
* [[Hiding a macOS app from the Dock]]
 +
* [[macOS Application Dock Menu]]
  
 
== External links ==
 
== External links ==
  
 
* [https://developer.apple.com/documentation/appkit/nsapplication Apple: NSApplication].
 
* [https://developer.apple.com/documentation/appkit/nsapplication Apple: NSApplication].
 +
* [https://developer.apple.com/documentation/appkit/nsdocktile Apple: NSDockTile].
  
 
[[Category:macOS]]
 
[[Category:macOS]]

Latest revision as of 06:43, 15 May 2022

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)

Overview

<translate> Warning: </translate> Warning For unknown reasons this does not always work in macOS 12 (Monterey) and does not log any error in the system console log - it has been tested working in macOS 10.6 (Snow Leopard) through 11 (Big Sur). It has also been tested working on one 2018 Intel Mac mini macOS 12 system, but not working on one 2018 Intel Mac mini macOS 12 system and one 2020 M1 Mac mini macOS 12 system.

Badges are used to notify a user of something new in an application. The badge is a counter that appears on the application's dock tile (icon) as a white number on a red circular or oval, depending on the number of digits, background. What the number represents depends on the application - it might be the number of new emails in an email application, new iMessages, unfinished Reminders, missed FaceTime calls, the number of outstanding application updates, etc.

Example code

This example shows you how to implement notification badges in an application.

unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch objectivec2}

interface

uses
  Classes, SysUtils, Forms, Dialogs, StdCtrls,
  CocoaAll;

type

  { TForm1 }

  TForm1 = Class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Display badge text on the dock tile (icon)
  NSApp.dockTile.setBadgeLabel(NSStr('1'));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Remove badge text from the dock tile (icon)
  NSApp.dockTile.setBadgeLabel(Nil);
end;

end.

See also

External links