Cocoa: Fix #40623, TCustomComboBox is not referenced to avoid the issue of APP directly calling TControl.Free() in event handler without using Application.ReleaseComponent()

This commit is contained in:
rich2014 2023-12-03 10:40:34 +08:00
parent b78b524b5b
commit 1898a84c34
2 changed files with 45 additions and 23 deletions

View File

@ -1379,7 +1379,7 @@ begin
begin
// ComboBox.Text will be set to the List Item value after comboBox_indexOfItemWithStringValue()
// so if cbactRetainPrefixCase set, ComboBox.Text should be reset Async
TComboBoxAsyncHelper.ResetTextIfNecessary(lclCmb, lclString);
TComboBoxAsyncHelper.ResetTextIfNecessary(self, lclString);
Result := idx;
end;
end;

View File

@ -61,16 +61,16 @@ type
TComboBoxAsyncHelper = class
private
cmb: TCustomComboBox;
newText: String;
_cocoaCmb: NSObject;
_newText: String;
private
procedure AsyncResetText(Data:PtrInt);
procedure AsyncSetLastIndex(Data:PtrInt);
public
constructor Create(ACmb:TCustomComboBox);
constructor Create(cocoaCmb:NSObject);
public
class procedure ResetTextIfNecessary(AObject:TObject; ANewText:String);
class procedure SetLastIndex(AObject:TObject);
class procedure ResetTextIfNecessary(cocoaCmb:NSObject; ANewText:String);
class procedure SetLastIndex(cocoaCmb:NSObject);
end;
{ TLCLComboboxCallback }
@ -722,42 +722,64 @@ end;
{ TComboBoxAsyncHelper }
constructor TComboBoxAsyncHelper.Create(ACmb:TCustomComboBox);
constructor TComboBoxAsyncHelper.Create(cocoaCmb:NSObject);
begin
cmb:= ACmb;
_cocoaCmb:= cocoaCmb;
_cocoaCmb.retain;
end;
procedure TComboBoxAsyncHelper.AsyncResetText(Data:PtrInt);
var
cmb: TCustomComboBox;
begin
TCocoaWSCustomComboBox.SetText(cmb, newText);
cmb.SelStart:= UTF8Length(newText);
cmb.SelLength:= 0;
Free;
try
cmb:= TCustomComboBox(_cocoaCmb.lclGetTarget);
if not Assigned(cmb) then
exit;
TCocoaWSCustomComboBox.SetText(cmb, _newText);
cmb.SelStart:= UTF8Length(_newText);
cmb.SelLength:= 0;
finally
_cocoaCmb.release;
Free;
end;
end;
procedure TComboBoxAsyncHelper.AsyncSetLastIndex(Data:PtrInt);
var
cmb: TCustomComboBox;
begin
TCocoaWSCustomComboBox.SetItemIndex(cmb, TCocoaReadOnlyComboBox(cmb.Handle).lastSelectedItemIndex);
Free;
try
cmb:= TCustomComboBox(_cocoaCmb.lclGetTarget);
if not Assigned(cmb) then
exit;
TCocoaWSCustomComboBox.SetItemIndex(cmb, TCocoaReadOnlyComboBox(_cocoaCmb).lastSelectedItemIndex);
finally
_cocoaCmb.release;
Free;
end;
end;
class procedure TComboBoxAsyncHelper.ResetTextIfNecessary(AObject:TObject; ANewText:String);
class procedure TComboBoxAsyncHelper.ResetTextIfNecessary(cocoaCmb:NSObject; ANewText:String);
var
helper: TComboBoxAsyncHelper;
ACmb: TCustomComboBox absolute AObject;
cmb: TCustomComboBox;
begin
if not (cbactRetainPrefixCase in ACmb.AutoCompleteText) then exit;
helper:= TComboBoxAsyncHelper.Create(ACmb);
helper.newText:= ANewText;
cmb:= TCustomComboBox(cocoaCmb.lclGetTarget);
if not Assigned(cmb) then
exit;
if not (cbactRetainPrefixCase in cmb.AutoCompleteText) then
exit;
helper:= TComboBoxAsyncHelper.Create(cocoaCmb);
helper._newText:= ANewText;
Application.QueueAsyncCall(@helper.AsyncResetText, 0);
end;
class procedure TComboBoxAsyncHelper.SetLastIndex(AObject:TObject);
class procedure TComboBoxAsyncHelper.SetLastIndex(cocoaCmb:NSObject);
var
helper: TComboBoxAsyncHelper;
ACmb: TCustomComboBox absolute AObject;
begin
helper:= TComboBoxAsyncHelper.Create(ACmb);
helper:= TComboBoxAsyncHelper.Create(cocoaCmb);
Application.QueueAsyncCall(@helper.AsyncSetLastIndex, 0);
end;
@ -1911,7 +1933,7 @@ begin
rocmb.setTarget(rocmb);
rocmb.setAction(objcselector('comboboxAction:'));
rocmb.lastSelectedItemIndex:= -1;
TComboBoxAsyncHelper.SetLastIndex(AWinControl);
TComboBoxAsyncHelper.SetLastIndex(rocmb);
rocmb.callback:=TLCLComboboxCallback.Create(rocmb, AWinControl);
Result:=TLCLHandle(rocmb);
rocmb.isOwnerDrawn := ComboBoxIsOwnerDrawn(TCustomComboBox(AWinControl).Style);