Cocoa: Add an OnQuitApp event to use when there is no Main Form

This commit is contained in:
David Jenkins 2024-12-19 14:19:20 +00:00 committed by rich2014
parent c0a982f5ef
commit 9bde82e965
5 changed files with 24 additions and 8 deletions

View File

@ -34,9 +34,11 @@ var
var var
CocoaConfigApplication: TCocoaConfigApplication = ( CocoaConfigApplication: TCocoaConfigApplication = (
event: ( events: (
// highest priority event handler // highest priority event handler
highestHandler: nil; highestHandler: nil;
// Event to call when there is no MainForm with MainForm.close
onQuitApp: nil;
); );
); );

View File

@ -8,7 +8,7 @@ unit CocoaConfig;
interface interface
uses uses
SysUtils, Forms, SysUtils,
Menus, Menus,
CocoaAll, Cocoa_Extra, CocoaConst; CocoaAll, Cocoa_Extra, CocoaConst;
@ -146,13 +146,16 @@ type
type type
// return True for event Handled, bypass post system processing // return True for event Handled, bypass post system processing
TCocoaApplicationEventHandler = function( event: NSEvent ): Boolean of object; TCocoaApplicationEventHandler = function( event: NSEvent ): Boolean of object;
// Need to pass to QueueAsyncCall which takes TDataEvent
TCocoaApplicationOnAppQuitEvent = TDataEvent;
TCocoaConfigApplicationEvent = record TCocoaConfigApplicationEvents = record
highestHandler: TCocoaApplicationEventHandler; highestHandler: TCocoaApplicationEventHandler;
onQuitApp: TCocoaApplicationOnAppQuitEvent;
end; end;
TCocoaConfigApplication = record TCocoaConfigApplication = record
event: TCocoaConfigApplicationEvent; events: TCocoaConfigApplicationEvents;
end; end;
type type

View File

@ -672,8 +672,8 @@ begin
Application.OnUserInput doesn't allow filtering messages. Application.OnUserInput doesn't allow filtering messages.
see also !351 see also !351
} }
if Assigned(Result) and Assigned(CocoaConfigApplication.event.highestHandler) then begin if Assigned(Result) and Assigned(CocoaConfigApplication.events.highestHandler) then begin
if CocoaConfigApplication.event.highestHandler(Result) then if CocoaConfigApplication.events.highestHandler(Result) then
Exit; Exit;
end; end;

View File

@ -90,6 +90,9 @@ procedure NSMenuAddItemsFromLCLMenu(menu: NSMenu; lclMenu: TMenuItem);
implementation implementation
uses
CocoaInt;
type type
TCocoaMenuItem_HideApp = objcclass(NSMenuItem) TCocoaMenuItem_HideApp = objcclass(NSMenuItem)
public public
@ -703,7 +706,12 @@ begin
{$endif} {$endif}
// Should be used instead of Application.Terminate when possible // Should be used instead of Application.Terminate when possible
// to allow events to be sent, see bug 32148 // to allow events to be sent, see bug 32148
if Assigned(Application.MainForm) then if Assigned(CocoaConfigApplication.events.onQuitApp) then
{ Don't call directly since key/mouse events need to unwind in case
OnQuitApp frees the caller }
Application.QueueAsyncCall(
TDataEvent(CocoaConfigApplication.events.onQuitApp), PtrInt(nil))
else if Assigned(Application.MainForm) then
Application.MainForm.Close Application.MainForm.Close
else else
Application.Terminate; Application.Terminate;

View File

@ -757,7 +757,10 @@ begin
end; end;
// Try to quit // Try to quit
if not Cancel then if not Cancel then
if Assigned(Application.MainForm) then begin
if Assigned(CocoaConfigApplication.events.onQuitApp) then
CocoaConfigApplication.events.onQuitApp(PtrInt(nil))
else if Assigned(Application.MainForm) then
Application.MainForm.Close Application.MainForm.Close
else else
Application.Terminate; Application.Terminate;