Cocoa/TabControl: exttabRemoveTabViewItem() refactored

This commit is contained in:
rich2014 2023-10-12 22:59:15 +08:00
parent cbb27a8f6d
commit c34f708ae4

View File

@ -868,50 +868,21 @@ begin
removedIndex := exttabIndexOfTabViewItem( removedTabPage ); removedIndex := exttabIndexOfTabViewItem( removedTabPage );
isRemovingCurrentPage:= (removedIndex=currentIndex); isRemovingCurrentPage:= (removedIndex=currentIndex);
if fulltabs.count=1 then begin fulltabs.removeObjectAtIndex( removedIndex );
// there is only one Page, so must be removing current Page,
// the selectedItem must be lost if isRemovingCurrentPage then begin
fulltabs.removeObjectAtIndex( removedIndex ); // removing current page
removeTabViewItem( removedTabPage ); attachAllTabs;
end else if isRemovingCurrentPage then begin if currentIndex = fulltabs.count then begin
// removing current Page, and there is at least one other Page.
// to keep the Page position stable and reduce selection changed events:
// 1. if the current page is on the rightmost of the visible TabView,
// try add the Next Page to the visible TabView first
// 2. then look at the TabView again and if it still only has one page,
// that means there is no Next Page, then there must be a prev Page,
// insert it into the TabView
// 3. if there is the next Page, select it, thne removedPage will be auto
// removed in UpdateTabAndArrowVisibility(), otherwise remove the
// removedPage directly, and the prev Page will be auto selected.
// 4. finally remove the Page to be removed in fulltabs
if currentIndex < fulltabs.count-1 then
nextTabPage:= NSTabViewItem( fulltabs.objectAtIndex(currentIndex+1) )
else
nextTabPage:= nil;
if (currentIndex=visibleRightIndex) and Assigned(nextTabPage) then begin // 1
addTabViewItem( nextTabPage );
inc( visibleRightIndex );
end;
if numberOfTabViewItems=1 then begin // 2
prevTabPage:= NSTabViewItem( fulltabs.objectAtIndex(currentIndex-1) );
insertTabViewItem_atIndex( prevTabPage, 0 );
end;
if Assigned(nextTabPage) then begin // 3
selectTabViewItem( nextTabPage );
end else begin
removeTabViewItem( removedTabPage );
end;
fulltabs.removeObjectAtIndex( removedIndex ); // 4
if removedIndex <= currentIndex then begin
dec( currentIndex ); dec( currentIndex );
dec( leftKeepAmount ); removeTabViewItem( removedTabPage );
end else begin
selectTabViewItemAtIndex( currentIndex );
end; end;
end else begin end else begin
// not removing current page // not removing current page
// only fulltabs need to be changed, // only fulltabs need to be changed,
// visible TabView auto updated in UpdateTabAndArrowVisibility() // visible TabView auto updated in UpdateTabAndArrowVisibility()
fulltabs.removeObjectAtIndex( removedIndex );
if removedIndex < currentIndex then begin if removedIndex < currentIndex then begin
dec( currentIndex ); dec( currentIndex );
if (removedIndex=visibleLeftIndex) and (removedIndex=currentIndex) then if (removedIndex=visibleLeftIndex) and (removedIndex=currentIndex) then