mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPTableView: Resize columns by column autoresizing style
Previously, CPTableView guessed whether columns had filled the clip view only for the uniform style, while the first- and last-column-only styles ignored the heuristic and called _autoResizeFirstColumn or sizeLastColumnToFit—forcing the columns to the clip view edge rather than applying the width delta and collapsing trailing gaps (e.g., a last column of 291 instead of 150). Sequential styles did nothing, uniform missed the clip view by the intercell spacing, programmatic table views had no default autoresizing style, and -tile left row views at their prior width. Following NSTableView, width changes now distribute across autoresizable columns according to the column autoresizing style: uniform distributes whole-point shares and redistributes shares from columns hitting min/max bounds, sequential starts from the last column, reverse sequential from the first, and first/last column only resize their respective single column. The default style is now last column only. Added -sizeToFit to size visible columns equally to fill the clip view within each column's min/max bounds, and updated -tile to refresh row view frames after setting the table view width. Tests: CPTableViewColumnAutoresizingTest (29 tests).
This commit is contained in:
1 parent
309b7cda97
commit
4f33729e69
4 files changed
+682
-147
No files matched your search
@@ -281,17 +281,22 @@
|
||||
/*!
|
||||
Sets the column autoresizing style of the receiver to a given style.
|
||||
|
||||
When the width of the receiver changes, the change goes to its autoresizable columns: the
|
||||
visible columns whose resizing mask includes CPTableColumnAutoresizingMask.
|
||||
|
||||
@param aStyle the column autoresizing style for the receiver. Valid values are:
|
||||
<pre>
|
||||
CPTableViewNoColumnAutoresizing
|
||||
CPTableViewUniformColumnAutoresizingStyle
|
||||
CPTableViewLastColumnOnlyAutoresizingStyle
|
||||
CPTableViewFirstColumnOnlyAutoresizingStyle
|
||||
CPTableViewNoColumnAutoresizing no column changes
|
||||
CPTableViewUniformColumnAutoresizingStyle each column takes an equal share
|
||||
CPTableViewSequentialColumnAutoresizingStyle the last column first, then the one before it
|
||||
CPTableViewReverseSequentialColumnAutoresizingStyle the first column first, then the one after it
|
||||
CPTableViewLastColumnOnlyAutoresizingStyle the last column only (the default)
|
||||
CPTableViewFirstColumnOnlyAutoresizingStyle the first column only
|
||||
</pre>
|
||||
A column takes the change until it reaches its minimum or maximum width.
|
||||
*/
|
||||
- (void)setColumnAutoresizingStyle:(unsigned)style
|
||||
{
|
||||
//FIX ME: CPTableViewSequentialColumnAutoresizingStyle and CPTableViewReverseSequentialColumnAutoresizingStyle are not yet implemented
|
||||
_columnAutoResizingStyle = style;
|
||||
}
|
||||
|
||||
@@ -308,8 +313,6 @@
|
||||
*/
|
||||
- (void)sizeLastColumnToFit
|
||||
{
|
||||
_lastColumnShouldSnap = YES;
|
||||
|
||||
var superview = [self superview];
|
||||
|
||||
if (!superview)
|
||||
@@ -336,6 +339,60 @@
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
|
||||
/*!
|
||||
Gives the visible columns equal widths that fill the enclosing clip view, so that all of
|
||||
them are visible. A column whose minimum or maximum width the equal width passes keeps that
|
||||
limit, and the other columns share the rest.
|
||||
*/
|
||||
- (void)sizeToFit
|
||||
{
|
||||
var superview = [self superview],
|
||||
width = [superview isKindOfClass:[CPClipView class]] ? CGRectGetWidth([superview bounds]) : CGRectGetWidth([self bounds]),
|
||||
open = [];
|
||||
|
||||
for (var i = 0, count = [self _numberOfColumns]; i < count; i++)
|
||||
if (![_tableColumns[i] isHidden])
|
||||
open.push(_tableColumns[i]);
|
||||
|
||||
var remainingWidth = width - open.length * _intercellSpacing.width;
|
||||
|
||||
/*
|
||||
Each pass gives the open columns an equal width. The columns that the width puts
|
||||
outside their limits take their limits and leave the pass, until no column does.
|
||||
*/
|
||||
while (open.length)
|
||||
{
|
||||
var equalWidth = remainingWidth / open.length,
|
||||
stillOpen = [];
|
||||
|
||||
for (var i = 0; i < open.length; i++)
|
||||
{
|
||||
var column = open[i],
|
||||
limit = equalWidth > [column maxWidth] ? [column maxWidth] : (equalWidth < [column minWidth] ? [column minWidth] : nil);
|
||||
|
||||
if (limit === nil)
|
||||
stillOpen.push(column);
|
||||
else
|
||||
{
|
||||
[column setWidth:limit];
|
||||
remainingWidth -= limit;
|
||||
}
|
||||
}
|
||||
|
||||
if (stillOpen.length === open.length)
|
||||
{
|
||||
for (var i = 0; i < open.length; i++)
|
||||
[open[i] setWidth:equalWidth];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
open = stillOpen;
|
||||
}
|
||||
|
||||
[self tile];
|
||||
}
|
||||
|
||||
/*!
|
||||
Informs the receiver that the number of records in the datasource has changed.
|
||||
*/
|
||||
|
||||
@@ -358,168 +358,145 @@
|
||||
|
||||
/*!
|
||||
@ignore
|
||||
|
||||
Gives the change in the width of the receiver to its columns, by its column autoresizing
|
||||
style. In a clip view the receiver is as wide as the clip view, so the change is the new
|
||||
width of the clip view less the width of the receiver before the resize.
|
||||
|
||||
The method resized the columns to fill the clip view, except with the uniform style, and
|
||||
only when a heuristic guessed that the columns had filled it before. A table view with a
|
||||
gap after its columns lost the gap, the sequential styles did nothing, and the uniform
|
||||
style missed the clip view by the intercell spacing. 2026-11-24
|
||||
*/
|
||||
- (void)resizeWithOldSuperviewSize:(CGSize)aSize
|
||||
{
|
||||
var oldWidth = CGRectGetWidth([self frame]);
|
||||
|
||||
[super resizeWithOldSuperviewSize:aSize];
|
||||
|
||||
// Ensure all row views stretch to fill the new width
|
||||
[self _updateAllVisibleRowViewsFrames];
|
||||
|
||||
if (_disableAutomaticResizing)
|
||||
return;
|
||||
|
||||
var mask = _columnAutoResizingStyle;
|
||||
|
||||
// should we actually do some resizing?
|
||||
if (!_lastColumnShouldSnap)
|
||||
{
|
||||
// did the clip view intersect the old tablesize?
|
||||
var superview = [self superview];
|
||||
|
||||
if (!superview || ![superview isKindOfClass:[CPClipView class]])
|
||||
return;
|
||||
|
||||
var superviewWidth = [superview bounds].size.width,
|
||||
lastColumnMaxX = CGRectGetMaxX([self rectOfColumn:[self numberOfColumns] -1]);
|
||||
|
||||
// Fix me: this fires on the table setup at times
|
||||
if (lastColumnMaxX >= superviewWidth && lastColumnMaxX <= aSize.width || lastColumnMaxX <= superviewWidth && lastColumnMaxX >= aSize.width)
|
||||
_lastColumnShouldSnap = YES;
|
||||
else if (mask === CPTableViewUniformColumnAutoresizingStyle)
|
||||
return;
|
||||
}
|
||||
|
||||
if (mask === CPTableViewUniformColumnAutoresizingStyle)
|
||||
[self _resizeAllColumnUniformlyWithOldSize:aSize];
|
||||
else if (mask === CPTableViewLastColumnOnlyAutoresizingStyle)
|
||||
[self sizeLastColumnToFit];
|
||||
else if (mask === CPTableViewFirstColumnOnlyAutoresizingStyle)
|
||||
[self _autoResizeFirstColumn];
|
||||
}
|
||||
|
||||
/*!
|
||||
@ignore
|
||||
*/
|
||||
- (void)_autoResizeFirstColumn
|
||||
{
|
||||
var superview = [self superview];
|
||||
|
||||
if (!superview)
|
||||
if (_disableAutomaticResizing || ![superview isKindOfClass:[CPClipView class]])
|
||||
return;
|
||||
|
||||
[self _updateColumnRangesIfNecessary];
|
||||
var clipWidth = CGRectGetWidth([superview bounds]);
|
||||
|
||||
var count = [self _numberOfColumns],
|
||||
columnToResize = nil,
|
||||
totalWidth = 0,
|
||||
i = 0;
|
||||
if (clipWidth === aSize.width)
|
||||
return;
|
||||
|
||||
for (; i < count; i++)
|
||||
/*
|
||||
Before its first tile the receiver can be narrower than the clip view was. It was then
|
||||
as wide as the clip view for the purpose of the change.
|
||||
*/
|
||||
[self _autoresizeColumnsByWidth:clipWidth - MAX(oldWidth, aSize.width)];
|
||||
}
|
||||
|
||||
/*
|
||||
Gives aDelta points of width to the autoresizable columns by the column autoresizing style.
|
||||
*/
|
||||
- (void)_autoresizeColumnsByWidth:(float)aDelta
|
||||
{
|
||||
if (!aDelta || _columnAutoResizingStyle === CPTableViewNoColumnAutoresizing)
|
||||
return;
|
||||
|
||||
var visibleColumns = [],
|
||||
autoresizableColumns = [];
|
||||
|
||||
for (var i = 0, count = [self _numberOfColumns]; i < count; i++)
|
||||
{
|
||||
var column = _tableColumns[i];
|
||||
|
||||
if (![column isHidden])
|
||||
{
|
||||
if (!columnToResize)
|
||||
columnToResize = column;
|
||||
totalWidth += [column width] + _intercellSpacing.width;
|
||||
}
|
||||
if ([column isHidden])
|
||||
continue;
|
||||
|
||||
visibleColumns.push(column);
|
||||
|
||||
if ([column resizingMask] & CPTableColumnAutoresizingMask)
|
||||
autoresizableColumns.push(column);
|
||||
}
|
||||
|
||||
// If there is a visible column
|
||||
if (columnToResize)
|
||||
var firstColumn = visibleColumns[0],
|
||||
lastColumn = visibleColumns[visibleColumns.length - 1];
|
||||
|
||||
switch (_columnAutoResizingStyle)
|
||||
{
|
||||
var superviewSize = [superview bounds].size,
|
||||
newWidth = superviewSize.width - totalWidth;
|
||||
case CPTableViewUniformColumnAutoresizingStyle:
|
||||
[self _resizeColumns:autoresizableColumns uniformlyByWidth:aDelta];
|
||||
break;
|
||||
|
||||
newWidth += [columnToResize width];
|
||||
[columnToResize _tryToResizeToWidth:newWidth];
|
||||
}
|
||||
case CPTableViewSequentialColumnAutoresizingStyle:
|
||||
[self _resizeColumns:autoresizableColumns.reverse() sequentiallyByWidth:aDelta];
|
||||
break;
|
||||
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
case CPTableViewReverseSequentialColumnAutoresizingStyle:
|
||||
[self _resizeColumns:autoresizableColumns sequentiallyByWidth:aDelta];
|
||||
break;
|
||||
|
||||
case CPTableViewLastColumnOnlyAutoresizingStyle:
|
||||
if (lastColumn && [autoresizableColumns containsObjectIdenticalTo:lastColumn])
|
||||
[self _resizeColumns:[lastColumn] sequentiallyByWidth:aDelta];
|
||||
break;
|
||||
|
||||
/*!
|
||||
@ignore
|
||||
FIX ME: this can be a lot faster
|
||||
*/
|
||||
- (void)_resizeAllColumnUniformlyWithOldSize:(CGSize)oldSize
|
||||
{
|
||||
// what we care about is the superview clip rect
|
||||
// FIX ME: if it's not in a scrollview this doesn't really work
|
||||
var superview = [self superview];
|
||||
|
||||
if (!superview || ![superview isKindOfClass:[CPClipView class]])
|
||||
return;
|
||||
|
||||
[self _updateColumnRangesIfNecessary];
|
||||
|
||||
var superviewWidth = [superview bounds].size.width,
|
||||
count = [self _numberOfColumns],
|
||||
resizableColumns = [CPIndexSet indexSet],
|
||||
remainingSpace = 0.0,
|
||||
i = 0;
|
||||
|
||||
// find resizable columns
|
||||
// FIX ME: we could cache resizableColumns after this loop and reuse it during the resize
|
||||
for (; i < count; i++)
|
||||
{
|
||||
var tableColumn = _tableColumns[i];
|
||||
if (![tableColumn isHidden] && ([tableColumn resizingMask] & CPTableColumnAutoresizingMask))
|
||||
[resizableColumns addIndex:i];
|
||||
}
|
||||
|
||||
var maxXofColumns = CGRectGetMaxX([self rectOfColumn:[resizableColumns lastIndex]]),
|
||||
remainingSpace = superviewWidth - maxXofColumns,
|
||||
resizeableColumnsCount = [resizableColumns count],
|
||||
proportionate = 0;
|
||||
|
||||
while (remainingSpace && resizeableColumnsCount)
|
||||
{
|
||||
// Divy out the space.
|
||||
proportionate += remainingSpace / resizeableColumnsCount;
|
||||
|
||||
// Reset the remaining space to 0
|
||||
remainingSpace = 0.0;
|
||||
|
||||
var index = CPNotFound;
|
||||
|
||||
while ((index = [resizableColumns indexGreaterThanIndex:index]) !== CPNotFound)
|
||||
{
|
||||
var item = _tableColumns[index],
|
||||
proposedWidth = [item width] + proportionate,
|
||||
resizeLeftovers = [item _tryToResizeToWidth:proposedWidth];
|
||||
|
||||
if (resizeLeftovers)
|
||||
{
|
||||
[resizableColumns removeIndex:index];
|
||||
|
||||
remainingSpace += resizeLeftovers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now that we've reached the end we know there are likely rounding errors
|
||||
// so we should size the last resized to fit
|
||||
|
||||
// find the last visisble column
|
||||
while (count--)
|
||||
{
|
||||
var hidden = [_tableColumns[count] isHidden];
|
||||
if (!hidden)
|
||||
case CPTableViewFirstColumnOnlyAutoresizingStyle:
|
||||
if (firstColumn && [autoresizableColumns containsObjectIdenticalTo:firstColumn])
|
||||
[self _resizeColumns:[firstColumn] sequentiallyByWidth:aDelta];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// find the max x, but subtract a single pixel since the spacing isn't applicable here.
|
||||
if (count >= 0)
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
- (void)_resizeColumns:(CPArray)someColumns sequentiallyByWidth:(float)aDelta
|
||||
{
|
||||
var remaining = aDelta;
|
||||
|
||||
for (var i = 0; i < someColumns.length && remaining; i++)
|
||||
{
|
||||
var item = _tableColumns[count],
|
||||
delta = superviewWidth - CGRectGetMaxX([self rectOfColumn:count]) - ([self intercellSpacing].width || 1),
|
||||
newSize = [item width] + delta;
|
||||
var column = someColumns[i],
|
||||
oldWidth = [column width];
|
||||
|
||||
[item _tryToResizeToWidth:newSize];
|
||||
[column _tryToResizeToWidth:oldWidth + remaining];
|
||||
remaining -= [column width] - oldWidth;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Gives aDelta points to someColumns in equal shares at the same time. The shares are whole
|
||||
points that add up to the change. A column that reaches its minimum or maximum width leaves,
|
||||
and the columns still open share what it could not take.
|
||||
*/
|
||||
- (void)_resizeColumns:(CPArray)someColumns uniformlyByWidth:(float)aDelta
|
||||
{
|
||||
var remaining = aDelta,
|
||||
open = someColumns.slice();
|
||||
|
||||
while (Math.abs(remaining) >= 0.5 && open.length)
|
||||
{
|
||||
var count = open.length,
|
||||
given = 0,
|
||||
stillOpen = [];
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var column = open[i],
|
||||
share = Math.round(remaining * (i + 1) / count) - Math.round(remaining * i / count),
|
||||
oldWidth = [column width];
|
||||
|
||||
[column _tryToResizeToWidth:oldWidth + share];
|
||||
given += [column width] - oldWidth;
|
||||
|
||||
if (remaining > 0 ? [column width] < [column maxWidth] : [column width] > [column minWidth])
|
||||
stillOpen.push(column);
|
||||
}
|
||||
|
||||
remaining -= given;
|
||||
|
||||
if (!given && stillOpen.length === count)
|
||||
break;
|
||||
|
||||
open = stillOpen;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -566,6 +543,13 @@
|
||||
|
||||
[self setFrameSize:CGSizeMake(width, height)];
|
||||
|
||||
/*
|
||||
The row views span the new width. They took the width in -resizeWithOldSuperviewSize:,
|
||||
before this method changed it, so they kept the width of the table view before a
|
||||
resize. 2026-11-24
|
||||
*/
|
||||
[self _updateAllVisibleRowViewsFrames];
|
||||
|
||||
[self setNeedsLayout];
|
||||
[self _markDrawingPassForDisplay];
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ _CPTableViewDrawingLayerGrid = 1 << 2;
|
||||
_CPTableViewDrawingLayerRows = 1 << 3;
|
||||
|
||||
CPTableViewNoColumnAutoresizing = 0;
|
||||
CPTableViewUniformColumnAutoresizingStyle = 1; // FIX ME: This is FUBAR
|
||||
CPTableViewUniformColumnAutoresizingStyle = 1;
|
||||
CPTableViewSequentialColumnAutoresizingStyle = 2;
|
||||
CPTableViewReverseSequentialColumnAutoresizingStyle = 3;
|
||||
CPTableViewLastColumnOnlyAutoresizingStyle = 4;
|
||||
@@ -303,7 +303,6 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
|
||||
CPInteger _dropTargetRow;
|
||||
|
||||
BOOL _disableAutomaticResizing @accessors(property=disableAutomaticResizing);
|
||||
BOOL _lastColumnShouldSnap;
|
||||
BOOL _isViewBased;
|
||||
BOOL _contentBindingExplicitlySet;
|
||||
|
||||
@@ -378,6 +377,12 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
|
||||
_allowsColumnSelection = NO;
|
||||
_disableAutomaticResizing = NO;
|
||||
|
||||
/*
|
||||
The default style is last column only, as in AppKit. The style was left unset, so
|
||||
a table view made in code did not autoresize its columns. 2026-11-24
|
||||
*/
|
||||
_columnAutoResizingStyle = CPTableViewLastColumnOnlyAutoresizingStyle;
|
||||
|
||||
//Setting Display Attributes
|
||||
_selectionHighlightStyle = CPTableViewSelectionHighlightStyleRegular;
|
||||
[self setBackgroundColor:nil];
|
||||
@@ -432,8 +437,6 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
|
||||
_dropOperationFeedbackView = [[_CPDropOperationDrawingView alloc] initWithFrame:CGRectMakeZero()];
|
||||
[_dropOperationFeedbackView setTableView:self];
|
||||
|
||||
_lastColumnShouldSnap = NO;
|
||||
|
||||
if (!_alternatingRowBackgroundColors)
|
||||
_alternatingRowBackgroundColors = [[CPColor whiteColor], [CPColor colorWithHexString:@"e4e7ff"]];
|
||||
|
||||
|
||||
@@ -0,0 +1,491 @@
|
||||
/*
|
||||
* CPTableViewColumnAutoresizingTest.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-24.
|
||||
* Copyright David Richardson 2026.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <AppKit/AppKit.j>
|
||||
|
||||
/*
|
||||
A table view in a scroll view resizes its columns by its column autoresizing style when its
|
||||
own width changes, as NSTableView does. The table view is as wide as the clip view, or as
|
||||
wide as its columns when they are wider, and the change in that width goes to the columns:
|
||||
|
||||
- no column autoresizing: no column changes;
|
||||
- uniform: each autoresizable column takes an equal share at the same time;
|
||||
- sequential: the last autoresizable column takes the change until it reaches its minimum
|
||||
or maximum width, then the column before it, and so on to the first;
|
||||
- reverse sequential: the same from the first autoresizable column to the last;
|
||||
- last column only: the last column takes the change until it reaches its minimum or
|
||||
maximum width, then autoresizing stops;
|
||||
- first column only: the same with the first column.
|
||||
|
||||
A column is autoresizable when it is visible and its resizing mask includes
|
||||
CPTableColumnAutoresizingMask. The default style is last column only.
|
||||
|
||||
-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.
|
||||
|
||||
The row views span the table view after every change. The scroll view has no scrollers and
|
||||
no border, so the clip view is as wide as the scroll view.
|
||||
*/
|
||||
@implementation CPTableViewColumnAutoresizingTest : OJTestCase
|
||||
{
|
||||
CPWindow theWindow;
|
||||
CPScrollView scrollView;
|
||||
CPTableView tableView;
|
||||
CPArray columns;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
// This will init the global var CPApp which are used internally in the AppKit
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 600.0, 400.0) styleMask:CPWindowNotSizable];
|
||||
|
||||
scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 200.0)];
|
||||
[scrollView setBorderType:CPNoBorder];
|
||||
[scrollView setHasVerticalScroller:NO];
|
||||
[scrollView setHasHorizontalScroller:NO];
|
||||
|
||||
tableView = [[CPTableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 200.0)];
|
||||
columns = [];
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var column = [[CPTableColumn alloc] initWithIdentifier:@"Column" + i];
|
||||
|
||||
[column setWidth:50.0];
|
||||
[tableView addTableColumn:column];
|
||||
columns.push(column);
|
||||
}
|
||||
|
||||
[tableView setDataSource:[[_ColumnAutoresizingTestDataSource alloc] init]];
|
||||
[tableView setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
|
||||
|
||||
[scrollView setDocumentView:tableView];
|
||||
[[theWindow contentView] addSubview:scrollView];
|
||||
|
||||
[self layOut];
|
||||
}
|
||||
|
||||
/*
|
||||
Reloads the table view and lays it out, as the display cycle does.
|
||||
*/
|
||||
- (void)layOut
|
||||
{
|
||||
[tableView reloadData];
|
||||
[tableView load];
|
||||
}
|
||||
|
||||
- (void)setScrollViewWidth:(float)aWidth
|
||||
{
|
||||
[scrollView setFrameSize:CGSizeMake(aWidth, CGRectGetHeight([scrollView frame]))];
|
||||
[tableView load];
|
||||
}
|
||||
|
||||
- (float)clipWidth
|
||||
{
|
||||
return CGRectGetWidth([[scrollView contentView] bounds]);
|
||||
}
|
||||
|
||||
- (float)spacing
|
||||
{
|
||||
return [tableView intercellSpacing].width;
|
||||
}
|
||||
|
||||
- (float)maxXOfColumn:(CPInteger)anIndex
|
||||
{
|
||||
return CGRectGetMaxX([tableView rectOfColumn:anIndex]);
|
||||
}
|
||||
|
||||
/*
|
||||
Sets the style and gives the columns equal widths that together fill the clip view, 97
|
||||
points each with the default spacing.
|
||||
*/
|
||||
- (float)fillWithEqualColumnsForStyle:(unsigned)aStyle
|
||||
{
|
||||
[tableView setColumnAutoresizingStyle:aStyle];
|
||||
|
||||
var width = [self clipWidth] / 3 - [self spacing];
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
[columns[i] setWidth:width];
|
||||
|
||||
[tableView tile];
|
||||
[tableView load];
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
- (void)assertWidths:(CPArray)someWidths message:(CPString)aMessage
|
||||
{
|
||||
for (var i = 0; i < 3; i++)
|
||||
[self assert:someWidths[i] equals:[columns[i] width] message:aMessage + @": width of column " + i];
|
||||
}
|
||||
|
||||
- (void)assertRowViewsSpanTheTableView:(CPString)aMessage
|
||||
{
|
||||
var tableWidth = CGRectGetWidth([tableView bounds]);
|
||||
|
||||
for (var row = 0; row < 5; row++)
|
||||
{
|
||||
var rowView = [tableView rowViewAtRow:row makeIfNecessary:NO];
|
||||
|
||||
[self assertTrue:rowView !== nil message:aMessage + @": row " + row + @" has a row view"];
|
||||
[self assert:tableWidth equals:CGRectGetWidth([rowView frame]) message:aMessage + @": width of the row view of row " + row];
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Default
|
||||
|
||||
- (void)testTheDefaultStyleIsLastColumnOnly
|
||||
{
|
||||
[self assert:CPTableViewLastColumnOnlyAutoresizingStyle equals:[[[CPTableView alloc] initWithFrame:CGRectMakeZero()] columnAutoresizingStyle]];
|
||||
}
|
||||
|
||||
// MARK: - The width of the table view
|
||||
|
||||
- (void)testTheTableViewFillsTheClipViewWhenItsColumnsAreNarrower
|
||||
{
|
||||
[self assert:[self clipWidth] equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as the clip view"];
|
||||
[self assertRowViewsSpanTheTableView:@"with columns narrower than the clip view"];
|
||||
}
|
||||
|
||||
/*
|
||||
Autoresizing applies the change in width; it does not fill the clip view. The last column
|
||||
of columns narrower than the clip view grows by the change, and the space after it stays.
|
||||
*/
|
||||
- (void)testAutoresizingAppliesTheChangeInWidth
|
||||
{
|
||||
[self setScrollViewWidth:400.0];
|
||||
|
||||
[self assertWidths:[50.0, 50.0, 150.0] message:@"after the clip view grows by 100 points"];
|
||||
}
|
||||
|
||||
// MARK: - No column autoresizing
|
||||
|
||||
- (void)testNoColumnAutoresizingKeepsTheColumnWidths
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewNoColumnAutoresizing];
|
||||
|
||||
[self setScrollViewWidth:360.0];
|
||||
[self assertWidths:[width, width, width] message:@"after the clip view grows"];
|
||||
[self assert:360.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as the clip view"];
|
||||
|
||||
[self setScrollViewWidth:240.0];
|
||||
[self assertWidths:[width, width, width] message:@"after the clip view shrinks"];
|
||||
[self assert:300.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as its columns"];
|
||||
}
|
||||
|
||||
// MARK: - Uniform
|
||||
|
||||
- (void)testUniformResizingSharesAGrowthEqually
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
|
||||
|
||||
[self setScrollViewWidth:330.0];
|
||||
|
||||
[self assertWidths:[width + 10.0, width + 10.0, width + 10.0] message:@"after the clip view grows by 30 points"];
|
||||
[self assert:330.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testUniformResizingSharesAShrinkEqually
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
|
||||
|
||||
[self setScrollViewWidth:270.0];
|
||||
|
||||
[self assertWidths:[width - 10.0, width - 10.0, width - 10.0] message:@"after the clip view shrinks by 30 points"];
|
||||
[self assert:270.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testUniformResizingGivesTheShareOfAColumnAtItsMaximumToTheOthers
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
|
||||
|
||||
[columns[0] setMaxWidth:width + 6.0];
|
||||
[self setScrollViewWidth:336.0];
|
||||
|
||||
[self assertWidths:[width + 6.0, width + 15.0, width + 15.0] message:@"after the clip view grows by 36 points"];
|
||||
[self assert:336.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testUniformResizingLeavesAColumnWithoutTheAutoresizingMask
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
|
||||
|
||||
[columns[1] setResizingMask:CPTableColumnUserResizingMask];
|
||||
[self setScrollViewWidth:330.0];
|
||||
|
||||
[self assertWidths:[width + 15.0, width, width + 15.0] message:@"after the clip view grows by 30 points"];
|
||||
}
|
||||
|
||||
- (void)testUniformResizingThroughNoWidthRestoresTheColumns
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewUniformColumnAutoresizingStyle];
|
||||
|
||||
[self setScrollViewWidth:0.0];
|
||||
[self setScrollViewWidth:300.0];
|
||||
|
||||
[self assertWidths:[width, width, width] message:@"after a resize through no width"];
|
||||
}
|
||||
|
||||
// MARK: - Sequential
|
||||
|
||||
- (void)testSequentialResizingGrowsFromTheLastColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewSequentialColumnAutoresizingStyle];
|
||||
|
||||
[columns[2] setMaxWidth:width + 20.0];
|
||||
[self setScrollViewWidth:360.0];
|
||||
|
||||
[self assertWidths:[width, width + 40.0, width + 20.0] message:@"after the clip view grows by 60 points"];
|
||||
[self assert:360.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testSequentialResizingShrinksFromTheLastColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewSequentialColumnAutoresizingStyle];
|
||||
|
||||
[columns[2] setMinWidth:width - 20.0];
|
||||
[self setScrollViewWidth:240.0];
|
||||
|
||||
[self assertWidths:[width, width - 40.0, width - 20.0] message:@"after the clip view shrinks by 60 points"];
|
||||
[self assert:240.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testSequentialResizingSkipsAHiddenColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewSequentialColumnAutoresizingStyle];
|
||||
|
||||
[columns[2] setHidden:YES];
|
||||
[tableView tile];
|
||||
[self setScrollViewWidth:360.0];
|
||||
|
||||
[self assertWidths:[width, width + 60.0, width] message:@"after the clip view grows by 60 points, with the last column hidden"];
|
||||
}
|
||||
|
||||
// MARK: - Reverse sequential
|
||||
|
||||
- (void)testReverseSequentialResizingGrowsFromTheFirstColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewReverseSequentialColumnAutoresizingStyle];
|
||||
|
||||
[columns[0] setMaxWidth:width + 20.0];
|
||||
[self setScrollViewWidth:360.0];
|
||||
|
||||
[self assertWidths:[width + 20.0, width + 40.0, width] message:@"after the clip view grows by 60 points"];
|
||||
[self assert:360.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testReverseSequentialResizingShrinksFromTheFirstColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewReverseSequentialColumnAutoresizingStyle];
|
||||
|
||||
[columns[0] setMinWidth:width - 20.0];
|
||||
[self setScrollViewWidth:240.0];
|
||||
|
||||
[self assertWidths:[width - 20.0, width - 40.0, width] message:@"after the clip view shrinks by 60 points"];
|
||||
[self assert:240.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
// MARK: - Last column only
|
||||
|
||||
- (void)testSizeLastColumnToFitFillsTheClipView
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
|
||||
[self assert:[self clipWidth] equals:[self maxXOfColumn:2] message:@"the last column ends at the edge of the clip view"];
|
||||
[self assert:50.0 equals:[columns[0] width] message:@"the first column keeps its width"];
|
||||
[self assert:50.0 equals:[columns[1] width] message:@"the second column keeps its width"];
|
||||
[self assert:[self clipWidth] equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as the clip view"];
|
||||
}
|
||||
|
||||
- (void)testSizeLastColumnToFitUsesTheLastVisibleColumn
|
||||
{
|
||||
[columns[2] setHidden:YES];
|
||||
[tableView sizeLastColumnToFit];
|
||||
|
||||
[self assert:[self clipWidth] equals:[self maxXOfColumn:1] message:@"the second column ends at the edge of the clip view"];
|
||||
[self assert:50.0 equals:[columns[0] width] message:@"the first column keeps its width"];
|
||||
}
|
||||
|
||||
- (void)testTheLastColumnGrowsWithTheClipView
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:400.0];
|
||||
|
||||
[self assert:400.0 equals:[self maxXOfColumn:2] message:@"the last column ends at the edge of the clip view"];
|
||||
[self assert:50.0 equals:[columns[0] width] message:@"the first column keeps its width"];
|
||||
[self assert:50.0 equals:[columns[1] width] message:@"the second column keeps its width"];
|
||||
}
|
||||
|
||||
- (void)testTheLastColumnShrinksWithTheClipView
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:250.0];
|
||||
|
||||
[self assert:250.0 equals:[self maxXOfColumn:2] message:@"the last column ends at the edge of the clip view"];
|
||||
[self assert:50.0 equals:[columns[0] width] message:@"the first column keeps its width"];
|
||||
[self assert:50.0 equals:[columns[1] width] message:@"the second column keeps its width"];
|
||||
}
|
||||
|
||||
- (void)testTheLastColumnStopsAtItsMinimumWidth
|
||||
{
|
||||
[columns[2] setMinWidth:40.0];
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:100.0];
|
||||
|
||||
[self assert:40.0 equals:[columns[2] width] message:@"the last column keeps its minimum width"];
|
||||
[self assert:50.0 equals:[columns[0] width] message:@"the first column keeps its width"];
|
||||
[self assert:[self maxXOfColumn:2] equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as its columns, wider than the clip view"];
|
||||
}
|
||||
|
||||
- (void)testTheLastColumnStopsAtItsMaximumWidth
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
|
||||
|
||||
[columns[2] setMaxWidth:width + 20.0];
|
||||
[self setScrollViewWidth:360.0];
|
||||
|
||||
[self assertWidths:[width, width, width + 20.0] message:@"after the clip view grows by 60 points"];
|
||||
[self assert:360.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as the clip view"];
|
||||
}
|
||||
|
||||
- (void)testTheLastColumnFillsTheClipViewAgainAfterItsMinimumWidth
|
||||
{
|
||||
[columns[2] setMinWidth:40.0];
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:100.0];
|
||||
[self setScrollViewWidth:350.0];
|
||||
|
||||
[self assert:350.0 equals:[self maxXOfColumn:2] message:@"the last column ends at the edge of the clip view"];
|
||||
[self assert:350.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as the clip view"];
|
||||
}
|
||||
|
||||
- (void)testAResizeThroughNoWidthRestoresTheColumns
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
|
||||
var widths = [[columns[0] width], [columns[1] width], [columns[2] width]];
|
||||
|
||||
[self setScrollViewWidth:0.0];
|
||||
[self setScrollViewWidth:300.0];
|
||||
|
||||
[self assertWidths:widths message:@"after a resize through no width"];
|
||||
[self assert:300.0 equals:[self maxXOfColumn:2] message:@"the last column ends at the edge of the clip view"];
|
||||
}
|
||||
|
||||
// MARK: - First column only
|
||||
|
||||
- (void)testFirstColumnOnlyResizingGivesTheGrowthToTheFirstColumn
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewFirstColumnOnlyAutoresizingStyle];
|
||||
|
||||
[self setScrollViewWidth:360.0];
|
||||
|
||||
[self assertWidths:[width + 60.0, width, width] message:@"after the clip view grows by 60 points"];
|
||||
[self assert:360.0 equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testTheFirstColumnStopsAtItsMinimumWidth
|
||||
{
|
||||
var width = [self fillWithEqualColumnsForStyle:CPTableViewFirstColumnOnlyAutoresizingStyle];
|
||||
|
||||
[columns[0] setMinWidth:width - 20.0];
|
||||
[self setScrollViewWidth:240.0];
|
||||
|
||||
[self assertWidths:[width - 20.0, width, width] message:@"after the clip view shrinks by 60 points"];
|
||||
[self assert:280.0 equals:CGRectGetWidth([tableView frame]) message:@"the table view is as wide as its columns"];
|
||||
}
|
||||
|
||||
// MARK: - Size to fit
|
||||
|
||||
- (void)testSizeToFitGivesTheColumnsEqualWidthsThatFillTheClipView
|
||||
{
|
||||
[columns[0] setWidth:30.0];
|
||||
[columns[1] setWidth:120.0];
|
||||
[tableView sizeToFit];
|
||||
|
||||
var width = [self clipWidth] / 3 - [self spacing];
|
||||
|
||||
[self assertWidths:[width, width, width] message:@"after sizeToFit"];
|
||||
[self assert:[self clipWidth] equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
- (void)testSizeToFitKeepsAColumnWithinItsMaximumWidth
|
||||
{
|
||||
[columns[0] setMaxWidth:41.0];
|
||||
[tableView sizeToFit];
|
||||
|
||||
[self assert:41.0 equals:[columns[0] width] message:@"the first column keeps its maximum width"];
|
||||
[self assert:[columns[1] width] equals:[columns[2] width] message:@"the other columns have equal widths"];
|
||||
[self assert:[self clipWidth] equals:[self maxXOfColumn:2] message:@"the columns fill the clip view"];
|
||||
}
|
||||
|
||||
// MARK: - Row views
|
||||
|
||||
- (void)testTheRowViewsSpanTheTableViewAfterTheClipViewGrows
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:400.0];
|
||||
|
||||
[self assertRowViewsSpanTheTableView:@"after the clip view grows"];
|
||||
}
|
||||
|
||||
- (void)testTheRowViewsSpanTheTableViewAfterTheClipViewShrinks
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:200.0];
|
||||
|
||||
[self assertRowViewsSpanTheTableView:@"after the clip view shrinks"];
|
||||
}
|
||||
|
||||
- (void)testTheRowViewsSpanTheTableViewAfterAResizeThroughNoWidth
|
||||
{
|
||||
[tableView sizeLastColumnToFit];
|
||||
[self setScrollViewWidth:0.0];
|
||||
[self setScrollViewWidth:300.0];
|
||||
|
||||
[self assertRowViewsSpanTheTableView:@"after a resize through no width"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
Five rows of text.
|
||||
*/
|
||||
@implementation _ColumnAutoresizingTestDataSource : CPObject
|
||||
{
|
||||
}
|
||||
|
||||
- (CPInteger)numberOfRowsInTableView:(CPTableView)aTableView
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
|
||||
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aTableColumn row:(CPInteger)aRow
|
||||
{
|
||||
return @"Row " + aRow;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user