Cocoa/Form: adapt ModalForm and OnTopForm in applicationDidResignActive()

This commit is contained in:
rich2014 2023-11-16 21:41:41 +08:00
parent 95373db0f0
commit 5f37850234
2 changed files with 50 additions and 34 deletions

View File

@ -71,7 +71,6 @@ type
procedure applicationDidHide(notification: NSNotification); procedure applicationDidHide(notification: NSNotification);
procedure applicationDidUnhide(notification: NSNotification); procedure applicationDidUnhide(notification: NSNotification);
procedure applicationDidBecomeActive(notification: NSNotification); procedure applicationDidBecomeActive(notification: NSNotification);
procedure applicationWillResignActive(notification: NSNotification);
procedure applicationDidResignActive(notification: NSNotification); procedure applicationDidResignActive(notification: NSNotification);
procedure applicationDidChangeScreenParameters(notification: NSNotification); procedure applicationDidChangeScreenParameters(notification: NSNotification);
procedure applicationWillFinishLaunching(notification: NSNotification); procedure applicationWillFinishLaunching(notification: NSNotification);

View File

@ -642,50 +642,67 @@ begin
Application.IntfAppActivate; Application.IntfAppActivate;
end; end;
procedure TAppDelegate.applicationWillResignActive(notification: NSNotification procedure TAppDelegate.applicationDidResignActive(notification: NSNotification);
);
var var
baseWindowNumber: NSInteger; lastWindowNumber: NSInteger;
topWindowNumber: NSInteger;
windows: NSArray; windows: NSArray;
window: NSWindow; window: NSWindow;
form: TObject; form: TObject;
info: PWinLevelOrder; style: TFormStyle;
state: TFormState;
i: Integer;
begin begin
// no window in this space
if NSWindow.windowNumbersWithOptions(0).count = 0 then
Exit;
windows:= NSApp.orderedWindows; windows:= NSApp.orderedWindows;
baseWindowNumber:= 0; // reset fsStayOnTop form
lastWindowNumber:= 0;
for i:=windows.count-1 downto 0 do begin
window:= NSWindow( windows.objectAtIndex(i) );
if not window.isVisible then
continue;
form:= window.lclGetTarget;
if not (form is TCustomForm) then
continue;
style:= TCustomForm(form).FormStyle;
if style in fsAllNonSystemStayOnTop then begin
window.setLevel( NSNormalWindowLevel );
if lastWindowNumber<>0 then
window.orderWindow_relativeTo( NSWindowAbove, lastWindowNumber );
end;
lastWindowNumber:= window.windowNumber;
end;
// find top window of NSNormalWindowLevel
topWindowNumber:= 0;
for window in windows do begin for window in windows do begin
if window.level=NSNormalWindowLevel then begin if not window.isVisible then
baseWindowNumber:= window.windowNumber; continue;
if window.level <> NSNormalWindowLevel then
continue;
topWindowNumber:= window.windowNumber;
break; break;
end; end;
end;
orderArrayCount:= windows.count; // bring up modal form
orderArray:= GetMem(orderArrayCount * sizeof(TWinLevelOrder)); for i:=windows.count-1 downto 0 do begin
window:= NSWindow( windows.objectAtIndex(i) );
info:= orderArray^; if not window.isVisible then
for window in windows do begin continue;
info^.win := window;
info^.lvl := window.level;
info^.ord := window.orderedIndex;
info^.vis := window.isVisible;
inc( info );
end;
for window in windows do begin
form:= window.lclGetTarget; form:= window.lclGetTarget;
if form is TCustomForm then begin if not (form is TCustomForm) then
if TCustomForm(form).FormStyle=fsStayOnTop then begin continue;
window.setLevel( NSNormalWindowLevel ); state:= TCustomForm(form).FormState;
window.orderWindow_relativeTo( NSWindowAbove, baseWindowNumber ); if fsModal in state then begin
end; window.orderWindow_relativeTo( NSWindowAbove, topWindowNumber );
end; topWindowNumber:= window.windowNumber;
end; end;
end; end;
procedure TAppDelegate.applicationDidResignActive(notification: NSNotification);
begin
Application.IntfAppDeactivate; Application.IntfAppDeactivate;
Application.DoBeforeMouseMessage(nil); Application.DoBeforeMouseMessage(nil);
end; end;