Difference between revisions of "macOS Application Dock Menu"
From Lazarus wiki
Jump to navigationJump to search (macOS new content - WIP) |
(→See also: + →External links: New sections) |
||
Line 130: | Line 130: | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | * [[Apple-specific UI elements]] | ||
+ | |||
+ | == External links == | ||
+ | |||
+ | * [https://developer.apple.com/documentation/appkit/nsapplication Apple: NSApplication]. | ||
+ | * [https://developer.apple.com/documentation/appkit/nsmenu Apple: NSMenu]. | ||
+ | * [https://developer.apple.com/documentation/appkit/nsmenuitem Apple: NSMenuItem]. | ||
+ | * [https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428564-applicationdockmenu Apple: applicationDockMenu:]. |
Revision as of 11:30, 2 March 2021
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.