Cocoa: Fix the APP openning files issue on startup (including Lazarus IDE)

This commit is contained in:
rich2014 2023-09-02 19:06:40 +08:00
parent 6c5322e24c
commit e234155cdc
2 changed files with 46 additions and 13 deletions

View File

@ -128,6 +128,7 @@ type
FNSApp: TCocoaApplication;
FNSApp_Delegate: TAppDelegate;
FCaptureControl: HWND;
FWaitingDropFiles: NSMutableArray;
protected
FStockNullBrush: HBRUSH;
@ -161,6 +162,8 @@ type
function nextEventBeforeRunLoop(const eventExpDate: NSDate): NSEvent;
procedure SyncClipboard();
procedure DropWaitingFiles;
procedure DropFiles(filenames: NSArray);
function PromptUser(const DialogCaption, DialogMessage: String;
DialogType: longint; Buttons: PLongint; ButtonCount, DefaultIndex,

View File

@ -73,6 +73,7 @@ begin
if Assigned(ALoop) then
begin
TCocoaApplication(NSApp).aloop:=ALoop;
DropWaitingFiles;
NSApp.run();
end;
end;
@ -171,6 +172,7 @@ begin
inherited Create;
FTerminating := False;
FCaptureControl:= 0;
FWaitingDropFiles := NSMutableArray.alloc.init;
NSMessageWnd := NSStringUTF8('HWND');
NSMessageMsg := NSStringUTF8('MSG');
@ -203,6 +205,8 @@ begin
ReleaseToCollect(0);
inherited Destroy;
FWaitingDropFiles.release;
FreeStockItems;
ScreenContext.Free;
@ -520,6 +524,44 @@ begin
end;
procedure TCocoaWidgetSet.DropWaitingFiles;
var
lFiles: array of string;
lNSStr: NSString;
i: Integer;
begin
if FWaitingDropFiles.count = 0 then
exit;
SetLength(lFiles, FWaitingDropFiles.count);
for i := 0 to FWaitingDropFiles.count-1 do
begin
lNSStr := NSString(FWaitingDropFiles.objectAtIndex(i));
lFiles[i] := NSStringToString(lNSStr);
end;
Application.IntfDropFiles(lFiles);
if Application.MainForm<>nil then
Application.MainForm.IntfDropFiles(lFiles);
FWaitingDropFiles.removeAllObjects;
end;
// on MacOS, the system notifies the APP to open the files by calling
// TAppDelegate.application_openFiles(). for example, double-click
// the associated files in Finder to open the APP.
// at this time, the MainForm may not have been created, and the notifies
// information about the files will be lost.
// including Lazarus IDE itself will also be affected by this issue on startup.
// so save it in FWaitingDropFiles first, DropWaitingFiles() will be called
// in TCocoaWidgetSet.AppRun().
procedure TCocoaWidgetSet.DropFiles(filenames: NSArray);
begin
FWaitingDropFiles.addObjectsFromArray(filenames);
if Assigned(TCocoaApplication(NSApp).aloop) then
DropWaitingFiles;
end;
{------------------------------------------------------------------------------
Method: TCocoaWidgetSet.LCLPlatform
Returns: lpCocoa - enum value for Cocoa widgetset
@ -543,20 +585,8 @@ begin
end;
procedure TAppDelegate.application_openFiles(sender: NSApplication; filenames: NSArray);
var
lFiles: array of string;
lNSStr: NSString;
i: Integer;
begin
SetLength(lFiles, filenames.count);
for i := 0 to filenames.count-1 do
begin
lNSStr := NSString(filenames.objectAtIndex(i));
lFiles[i] := NSStringToString(lNSStr);
end;
Application.IntfDropFiles(lFiles);
if Application.MainForm<>nil then
Application.MainForm.IntfDropFiles(lFiles);
TCocoaWidgetSet(WidgetSet).DropFiles(filenames);
end;
procedure TAppDelegate.applicationDidHide(notification: NSNotification);