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
CocoaConfigApplication: TCocoaConfigApplication = (
event: (
events: (
// highest priority event handler
highestHandler: nil;
// Event to call when there is no MainForm with MainForm.close
onQuitApp: nil;
);
);

View File

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

View File

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

View File

@ -90,6 +90,9 @@ procedure NSMenuAddItemsFromLCLMenu(menu: NSMenu; lclMenu: TMenuItem);
implementation
uses
CocoaInt;
type
TCocoaMenuItem_HideApp = objcclass(NSMenuItem)
public
@ -703,7 +706,12 @@ begin
{$endif}
// Should be used instead of Application.Terminate when possible
// 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
else
Application.Terminate;

View File

@ -757,7 +757,10 @@ begin
end;
// Try to quit
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
else
Application.Terminate;