Cocoa: Fix #40581 #40535 , Merge branch 'cocoa/message'

keep LM_SETFOCUS/LM_KILLFOCUS/LM_DESTROY consistent with Win32
This commit is contained in:
rich2014 2024-01-19 22:26:20 +08:00
commit 7b75e8ba5d
4 changed files with 38 additions and 5 deletions

View File

@ -170,6 +170,9 @@ type
procedure DoSetMainMenu(AMenu: NSMenu; ALCLMenu: TMenu);
public
KeyWindow: NSWindow;
KillingFocus: Boolean;
// modal session
Modals : TList;
ModalCounter: Integer; // the cheapest way to determine if modal window was called

View File

@ -2148,8 +2148,15 @@ var
lclobj : TObject;
begin
Result := 0;
if KillingFocus then
Exit;
win := NSApp.keyWindow;
if not Assigned(win) then Exit;
if not Assigned(win) then
win := CocoaWidgetSet.KeyWindow;
if not Assigned(win) then
Exit;
// assuming that that the content view of Window
// is the focused handle and return it, by default
Result := HWND(win.contentView);

View File

@ -1376,7 +1376,12 @@ end;
procedure TLCLCommonCallback.ResignFirstResponder;
begin
if not Assigned(Target) then Exit;
LCLSendKillFocusMsg(Target);
CocoaWidgetSet.KillingFocus:= true;
try
LCLSendKillFocusMsg(Target);
finally
CocoaWidgetSet.KillingFocus:= false;
end;
end;
procedure TLCLCommonCallback.DidBecomeKeyNotification;
@ -1621,6 +1626,12 @@ begin
CocoaWidgetSet.ReleaseCapture;
obj := NSObject(AWinControl.Handle);
Callback := obj.lclGetCallback;
if AWinControl.Focused and Assigned(Callback) then
Callback.ResignFirstResponder; // dont' call LCLSendKillFocusMsg
LCLSendDestroyMsg( AWinControl );
if obj.isKindOfClass_(NSView) then
begin
// no need to "retain" prior to "removeFromSuperview"
@ -1633,7 +1644,6 @@ begin
NSWindow(obj).close;
// destroy the callback
Callback := obj.lclGetCallback;
if Assigned(Callback) then
begin
if Callback.HasCaret then DestroyCaret(nil);

View File

@ -348,7 +348,10 @@ procedure TLCLWindowCallback.Activate;
var
ACustForm: TCustomForm;
isDesign: Boolean;
focusedCb: ICommonCallback;
begin
CocoaWidgetSet.KeyWindow:= window;
if not IsActivating then
begin
IsActivating:=True;
@ -379,7 +382,9 @@ begin
end;
LCLSendActivateMsg(Target, WA_ACTIVE, false);
LCLSendSetFocusMsg(Target);
focusedCb := window.firstResponder.lclGetCallback;
if Assigned(focusedCb) then
focusedCb.BecomeFirstResponder;
// The only way to update Forms.ActiveCustomForm for the main form
// is calling TCustomForm.SetFocusedControl, see bug 31056
ACustForm.SetFocusedControl(ACustForm.ActiveControl);
@ -392,9 +397,17 @@ begin
end;
procedure TLCLWindowCallback.Deactivate;
var
focusedCb: ICommonCallback;
begin
CocoaWidgetSet.KeyWindow:= nil;
focusedCb:= window.firstResponder.lclGetCallback;
if Assigned(focusedCb) then begin
if not (csDestroying in TComponent(focusedCb.GetTarget).ComponentState) then
focusedCb.ResignFirstResponder;
end;
LCLSendActivateMsg(Target, WA_INACTIVE, false);
LCLSendKillFocusMsg(Target);
end;
procedure TLCLWindowCallback.CloseQuery(var CanClose: Boolean);