macOS Application Dock Menu

From Lazarus wiki
Revision as of 10:44, 2 March 2021 by Trev (talk | contribs) (macOS new content - WIP)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)



Code example

unit Unit1;

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

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  CocoaAll;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private

  public

  end;

  TMyAppDelegate = objcclass(NSObject)
    private

    public
      function applicationDockMenu (sender: NSApplication): NSMenu;
        message 'applicationDockMenu:';
      procedure menuItemHello1;
        message 'menuItemHello1';
      procedure menuItemHello2;
        message 'menuItemHello2';
    end;

var
  Form1: TForm1;
  myAppDelegate: TMyAppDelegate;

implementation

{$R *.lfm}

{ TForm1 }

procedure TMyAppDelegate.menuItemHello1;
begin
  NSLog(NSStr('hello menu item 1'));
end;

procedure TMyAppDelegate.menuItemHello2;
begin
  NSLog(NSStr('hello menu item 2'));
end;

function TMyAppDelegate.applicationDockMenu(sender: NSApplication): NSMenu;
var
  aSel, bSel: SEL;
  myMenu: NSMenu;
  myMenuItem0: NSMenuItem;
  myMenuItem1: NSMenuItem;
  myMenuItem2: NSMenuItem;
begin
  NSLog(NSStr('dock menu'));

  aSel := ObjCSelector(TMyAppDelegate.menuItemHello1);
  bSel := ObjCSelector(TMyAppDelegate.menuItemHello2);

  myMenu := NSMenu.alloc.init.autorelease;
  myMenuItem0 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('My Dock Menu'), Nil, NSStr('')).autorelease;
  myMenuItem1 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('Hello Item 1'), aSel, NSStr('')).autorelease;
  myMenuItem2 := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(NSStr('Hello Item 2'), bSel, NSStr('')).autorelease;
  myMenu.addItem(myMenuItem0);
  myMenu.addItem(myMenuItem1);
  myMenu.addItem(myMenuItem2);
  Result := myMenu;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  NSLog(NSStr('Closing'));
  Close;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  NSLog(NSStr('Leaving...'));

  if(NSApp.isActive) then
    begin
      NSLog(NSStr('exiting'));
      exit;
    end
  else
    // needed if any part of form is occluded or does not quit
    // until dock icon clicked when quitting via dock menu
    begin
      NSLog(NSStr('activating'));
      Application.BringToFront;
      CloseAction := TCloseAction.caFree;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // NSApp
  // - The global variable for the shared application instance.
  // NSApplication.sharedApplication
  // - Returns the application instance, creating it if it doesn’t exist yet.
  NSApp := NSApplication.sharedApplication;
  NSApp.setDelegate(myAppDelegate);
end;

Initialization
  myAppDelegate := TMyAppDelegate.alloc.init;

Finalization
  myAppDelegate.Release;
end.