Cocoa/TabControl: improve Robustness

This commit is contained in:
rich2014 2023-12-16 18:09:06 +08:00
parent 917e9d3ae8
commit 1666b45e46
2 changed files with 14 additions and 4 deletions

View File

@ -281,6 +281,8 @@ begin
if aview.fulltabs.count=0 then exit; if aview.fulltabs.count=0 then exit;
tryToKeepIndex:= aview.currentIndex - aview.leftKeepAmount; tryToKeepIndex:= aview.currentIndex - aview.leftKeepAmount;
if tryToKeepIndex < 0 then
tryToKeepIndex:= 0;
// AttachAllTabs() has been modified to not remove the selectedTabViewItem first, // AttachAllTabs() has been modified to not remove the selectedTabViewItem first,
// and no longer trigger tabView_didSelectTabViewItem // and no longer trigger tabView_didSelectTabViewItem
@ -817,10 +819,13 @@ begin
if fulltabs.count=0 then if fulltabs.count=0 then
Exit; Exit;
OldIndex := exttabIndexOfTabViewItem( lTabPage );
if OldIndex < 0 then
Exit;
if NewIndex > fulltabs.count then if NewIndex > fulltabs.count then
NewIndex:= fulltabs.count; NewIndex:= fulltabs.count;
OldIndex := exttabIndexOfTabViewItem( lTabPage );
isMovingCurrentPage := (OldIndex=currentIndex); isMovingCurrentPage := (OldIndex=currentIndex);
fulltabs.removeObjectAtIndex( OldIndex ); fulltabs.removeObjectAtIndex( OldIndex );
@ -861,6 +866,8 @@ begin
triggeringByLCL:= true; triggeringByLCL:= true;
try try
removedIndex := exttabIndexOfTabViewItem( removedTabPage ); removedIndex := exttabIndexOfTabViewItem( removedTabPage );
if removedIndex < 0 then
Exit;
isRemovingCurrentPage:= (removedIndex=currentIndex); isRemovingCurrentPage:= (removedIndex=currentIndex);
fulltabs.removeObjectAtIndex( removedIndex ); fulltabs.removeObjectAtIndex( removedIndex );
@ -895,6 +902,8 @@ function TCocoaTabControl.exttabIndexOfTabViewItem(lTabPage: NSTabViewItem
): NSInteger; ): NSInteger;
begin begin
Result := fulltabs.indexOfObject(lTabPage); Result := fulltabs.indexOfObject(lTabPage);
if Result = NSNotFound then
Result:= -1;
end; end;
{ TCocoaTabControlArrow } { TCocoaTabControlArrow }

View File

@ -901,10 +901,11 @@ begin
tb := TCocoaTabPageView(ATabControl.Page[AIndex].Handle); tb := TCocoaTabPageView(ATabControl.Page[AIndex].Handle);
if not Assigned(tb) then Exit; if not Assigned(tb) then Exit;
i := TCocoaTabControl(ATabControl.Handle).fulltabs.indexOfObject( tb.tabPage ); i := TCocoaTabControl(ATabControl.Handle).exttabIndexOfTabViewItem(tb.tabPage);
if (i = NSNotFound) then Exit; if i < 0 then
Exit;
TCocoaTabControl(ATabControl.Handle).extselectTabViewItemAtIndex(NSInteger(i)); TCocoaTabControl(ATabControl.Handle).extselectTabViewItemAtIndex(i);
end; end;
class procedure TCocoaWSCustomTabControl.SetTabPosition(const ATabControl: TCustomTabControl; const ATabPosition: TTabPosition); class procedure TCocoaWSCustomTabControl.SetTabPosition(const ATabControl: TCustomTabControl; const ATabPosition: TTabPosition);