Recent

Author Topic: NSApp.dockTile.setBadgeLabel issue in Monterey  (Read 2961 times)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
NSApp.dockTile.setBadgeLabel issue in Monterey
« on: December 19, 2021, 01:44:44 pm »
This code works up to and including Big Sur, but fails to do anything with no system log errors in Monterey (12.0.1, 12.1 and 12.2).

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch objectivec2}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Dialogs, StdCtrls,
  10.   CocoaAll;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = Class(TForm)
  17.     Button1: TButton;
  18.     Button2: TButton;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. begin
  38.   // Display badge text on the dock tile (icon)
  39.   NSApp.dockTile.setBadgeLabel(NSStr('1'));
  40. end;
  41.  
  42. procedure TForm1.Button2Click(Sender: TObject);
  43. begin
  44.   // Remove badge text from the dock tile (icon)
  45.   NSApp.dockTile.setBadgeLabel(Nil);
  46. end;
  47.  
  48. end.

This pure FPC code still works in Monterey:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch objectivec1}
  3.  
  4. uses
  5. CocoaAll;
  6.  
  7. type
  8.    TMyDelegate = objcclass(NSObject)
  9.    public
  10.       procedure BadgeButtonClick(sender :  id); message 'BadgeButtonClick:';
  11.       procedure UnBadgeButtonClick(sender :  id); message 'UnBadgeButtonClick:';
  12.    end;
  13.  
  14. var
  15.    appName    : NSString;
  16.    window     : NSWindow;
  17.    myBadgeButton   : NSButton;
  18.    myUnBadgeButton : NSButton;
  19.    myDelegate : TMyDelegate;
  20.  
  21. procedure TMyDelegate.BadgeButtonClick(sender :  id);
  22. begin
  23.    NSApp.dockTile.setBadgeLabel(NSStr('16'));
  24. end;
  25.  
  26. procedure TMyDelegate.UnBadgeButtonClick(sender :  id);
  27. begin
  28.    NSApp.dockTile.setBadgeLabel(Nil);
  29. end;
  30.  
  31. begin
  32.    // app and window creation
  33.    NSApp := NSApplication.sharedApplication;
  34.    NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  35.    appName := NSStr('Badger');
  36.    window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
  37.                    NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
  38.                    NSBackingStoreBuffered, False);
  39.    myDelegate := TMyDelegate.alloc.init;
  40.  
  41.    // badge button
  42.    myBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 100, 50));
  43.    window.contentView.addSubview(myBadgeButton);
  44.    myBadgeButton.setTitle(NSSTR('Badge'));
  45.    myBadgeButton.setButtonType(NSMomentaryLightButton);
  46.    myBadgeButton.setBezelStyle(NSRoundedBezelStyle);
  47.  
  48.    // unbadge button
  49.    myUnBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 60, 100, 50));
  50.    window.contentView.addSubview(myUnBadgeButton);
  51.    myUnBadgeButton.setTitle(NSSTR('UnBadge'));
  52.    myUnBadgeButton.setButtonType(NSMomentaryLightButton);
  53.    myUnBadgeButton.setBezelStyle(NSRoundedBezelStyle);
  54.  
  55.    // badge button event handler
  56.    myBadgeButton.setTarget(myDelegate);
  57.    myBadgeButton.setAction(ObjCSelector(myDelegate.BadgeButtonClick));
  58.  
  59.    // unbadge button event handler
  60.    myUnBadgeButton.setTarget(myDelegate);
  61.    myUnBadgeButton.setAction(ObjCSelector(myDelegate.UnBadgeButtonClick));
  62.  
  63.    // Window showing and app running
  64.    window.center;
  65.    window.setTitle(appName);
  66.    window.makeKeyAndOrderFront(Nil);
  67.    NSApp.activateIgnoringOtherApps(True);
  68.    NSApp.run;
  69. end.

Any ideas why the Lazarus version of the code now fails?
« Last Edit: February 05, 2022, 12:20:09 pm by trev »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: NSApp.dockTile.setBadgeLabel issue in Monterey
« Reply #1 on: May 17, 2022, 01:58:52 am »
Curiouser and curiouser.

I have two more or less identical (internal SSD size differs) 2018 Intel Mac minis running Monterey 12.3.1. On one system the badge appears, on the other system it does not. The same app was copied from the working one to the non-working one.

The badge also does not appear on my 2020 M1 Mac mini.

Comparing the two Intel systems is problematical because the working system is located 500Km away and there is no remote access.


 

TinyPortal © 2005-2018