CPTableView: Distribute user column resize to trailing columns per autoresizing style

This commit is contained in:
David Richardson committed 2026-11-26 15:58:27 -06:00
1 parent 5a247365f3
commit 0a669c840b
3 files changed
+196

No files matched your search

+5
View File
@@ -994,8 +994,13 @@ var CPTableHeaderViewResizeZone = 3.0,
if ((delta > 0 && aPoint.x > columnMaxX) || (delta < 0 && aPoint.x < columnMaxX))
{
var oldWidth = [tableColumn width];
[tableColumn setWidth:newWidth - spacing];
// The columns after it take the opposite change, by the column autoresizing style.
[_tableView _autoresizeColumnsAfterColumn:aColumnIndex byWidth:oldWidth - [tableColumn width]];
[self setNeedsLayout];
[self setNeedsDisplay:YES];
}
+78
View File
@@ -444,6 +444,84 @@
}
}
/*
Gives aDelta points of width to the autoresizable columns after the column at anIndex, by
the column autoresizing style, and returns the part of aDelta they could not take. The
header view calls it while the user resizes the column at anIndex, with the opposite of
that column's change, so the columns keep their total width where they can:
- no column autoresizing: no column changes;
- uniform: the columns after it take equal shares;
- sequential: the last column first, then the one before it, back to the resized column;
- reverse sequential: the column after it first, then the next;
- last column only: the last column, unless it is the resized column;
- first column only: the first column after it.
2026-11-26: a column resized by the user took the whole change, whatever the style. The
other columns kept their widths, so the change became a gap after the last column, or an
overflow, which every later change of the table's width kept, since autoresizing applies
only the change. In People, making Salary narrower left a gap after Email with the last
column only style. As in AppKit, the columns after the resized column now take the change.
*/
- (float)_autoresizeColumnsAfterColumn:(CPInteger)anIndex byWidth:(float)aDelta
{
if (!aDelta || _columnAutoResizingStyle === CPTableViewNoColumnAutoresizing)
return aDelta;
var autoresizableColumns = [],
lastVisibleColumn = nil;
for (var i = anIndex + 1, count = [self _numberOfColumns]; i < count; i++)
{
var column = _tableColumns[i];
if ([column isHidden])
continue;
lastVisibleColumn = column;
if ([column resizingMask] & CPTableColumnAutoresizingMask)
autoresizableColumns.push(column);
}
var oldWidths = 0;
for (var i = 0; i < autoresizableColumns.length; i++)
oldWidths += [autoresizableColumns[i] width];
switch (_columnAutoResizingStyle)
{
case CPTableViewUniformColumnAutoresizingStyle:
[self _resizeColumns:autoresizableColumns uniformlyByWidth:aDelta];
break;
case CPTableViewSequentialColumnAutoresizingStyle:
[self _resizeColumns:autoresizableColumns.slice().reverse() sequentiallyByWidth:aDelta];
break;
case CPTableViewReverseSequentialColumnAutoresizingStyle:
[self _resizeColumns:autoresizableColumns sequentiallyByWidth:aDelta];
break;
case CPTableViewLastColumnOnlyAutoresizingStyle:
if (lastVisibleColumn && [autoresizableColumns containsObjectIdenticalTo:lastVisibleColumn])
[self _resizeColumns:[lastVisibleColumn] sequentiallyByWidth:aDelta];
break;
case CPTableViewFirstColumnOnlyAutoresizingStyle:
if (autoresizableColumns.length)
[self _resizeColumns:[autoresizableColumns[0]] sequentiallyByWidth:aDelta];
break;
}
var newWidths = 0;
for (var i = 0; i < autoresizableColumns.length; i++)
newWidths += [autoresizableColumns[i] width];
return aDelta - (newWidths - oldWidths);
}
/*
Gives aDelta points to someColumns in order. Each column takes what it can within its
minimum and maximum widths, and the next column takes the rest.
@@ -39,6 +39,11 @@
A column is autoresizable when it is visible and its resizing mask includes
CPTableColumnAutoresizingMask. The default style is last column only.
When the user resizes a column, the columns after it take the opposite change by the same
style, so the columns keep their total width where they can. What they cannot take changes
the total width. The tests resize the column as the header view does, then give the change
to the columns after it.
-sizeLastColumnToFit makes the last column end at the edge of the clip view, and
-sizeToFit gives the columns equal widths, within their maximum widths, so that all of
them are visible.
@@ -136,6 +141,19 @@
return width;
}
/*
Resizes the column at anIndex to aWidth as the user does with the header view: the columns
after it take the opposite change.
*/
- (void)userResizeColumn:(CPInteger)anIndex toWidth:(float)aWidth
{
var oldWidth = [columns[anIndex] width];
[columns[anIndex] setWidth:aWidth];
[tableView _autoresizeColumnsAfterColumn:anIndex byWidth:oldWidth - [columns[anIndex] width]];
[tableView load];
}
- (void)assertWidths:(CPArray)someWidths message:(CPString)aMessage
{
for (var i = 0; i < 3; i++)
@@ -418,6 +436,101 @@
[self assert:280.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as its columns"];
}
// MARK: - Resizing a column by the user
- (void)testAUserResizeWithNoColumnAutoresizingChangesTheTotalWidth
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewNoColumnAutoresizing];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width, width] message:@"after the first column shrinks by 20 points"];
[self assert:280.0 equals:[self maxXOfColumn:2] message:@"the columns end 20 points before the edge"];
}
- (void)testAUserResizeWithUniformResizingSharesTheChangeAfterTheColumn
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width + 10.0, width + 10.0] message:@"after the first column shrinks by 20 points"];
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
}
- (void)testAUserResizeWithSequentialResizingStartsAtTheLastColumn
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewSequentialColumnAutoresizingStyle];
[columns[2] setMaxWidth:width + 5.0];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width + 15.0, width + 5.0] message:@"after the first column shrinks by 20 points"];
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
}
- (void)testAUserResizeWithReverseSequentialResizingStartsAtTheNextColumn
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewReverseSequentialColumnAutoresizingStyle];
[columns[1] setMaxWidth:width + 5.0];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width + 5.0, width + 15.0] message:@"after the first column shrinks by 20 points"];
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
}
- (void)testAUserResizeWithLastColumnOnlyResizingGivesTheChangeToTheLastColumn
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[self userResizeColumn:1 toWidth:width - 20.0];
[self assertWidths:[width, width - 20.0, width + 20.0] message:@"after the middle column shrinks by 20 points"];
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
}
- (void)testAUserResizeOfTheLastColumnChangesTheTotalWidth
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[self userResizeColumn:2 toWidth:width - 20.0];
[self assertWidths:[width, width, width - 20.0] message:@"after the last column shrinks by 20 points"];
[self assert:280.0 equals:[self maxXOfColumn:2] message:@"the columns end 20 points before the edge"];
}
- (void)testAUserResizeLeavesWhatTheLastColumnCannotTake
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[columns[2] setMaxWidth:width + 5.0];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width, width + 5.0] message:@"after the first column shrinks by 20 points"];
[self assert:285.0 equals:[self maxXOfColumn:2] message:@"the columns end 15 points before the edge"];
}
- (void)testAUserResizeWithFirstColumnOnlyResizingGivesTheChangeToTheNextColumn
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewFirstColumnOnlyAutoresizingStyle];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width + 20.0, width] message:@"after the first column shrinks by 20 points"];
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
}
- (void)testAUserResizeSkipsAColumnWithoutTheAutoresizingMask
{
var width = [self fillWithEqualColumnsForStyle:CPTableViewFirstColumnOnlyAutoresizingStyle];
[columns[1] setResizingMask:CPTableColumnUserResizingMask];
[self userResizeColumn:0 toWidth:width - 20.0];
[self assertWidths:[width - 20.0, width, width + 20.0] message:@"after the first column shrinks by 20 points"];
}
// MARK: - Size to fit
- (void)testSizeToFitGivesTheColumnsEqualWidthsThatFillTheClipView