CPSplitView: Add holding priorities and preserve arrangement on orientation change

Previously, CPSplitView had no holding priorities, so every subview the
delegate permitted to resize absorbed part of each size change, causing
sidebars to grow and shrink with the window. Additionally,
-setVertical: sized subviews from ratios summing to one per subview
kind—overflowing the split view when both kinds were present—and removed
and re-added every subview.

Each arranged subview now carries a holding priority (defaulting to
250) preserved across insertion, removal, orientation changes, and
archiving. -adjustSubviews distributes size changes first to subviews
the delegate allows to resize and then to others, ordered by ascending
priority; equal-priority subviews share changes proportionally to their
ratios, passing any remainder past zero to the next group.
-setVertical: now preserves each subview's share along the split axis
and keeps subviews arranged.
This commit is contained in:
David Richardson committed 2026-11-23 17:01:48 -06:00
1 parent 1938847271
commit b792073dcf
4 files changed
+437 -133

No files matched your search

+24 -1
View File
@@ -34,7 +34,8 @@ var CPSplitViewDelegateKey = @"CPSplitViewDelegateKey",
CPSplitViewDividerSubviewsKey = @"CPSplitViewDividerSubviewsKey",
CPSplitViewArrangedSubviewsKey = @"CPSplitViewArrangedSubviewsKey",
CPSplitViewArrangesAllSubviewsKey = @"CPSplitViewArrangesAllSubviewsKey",
CPSplitViewRealSubviewsKey = @"CPSplitViewRealSubviewsKey";
CPSplitViewRealSubviewsKey = @"CPSplitViewRealSubviewsKey",
CPSplitViewHoldingPrioritiesKey = @"CPSplitViewHoldingPrioritiesKey";
@implementation CPSplitView (CPCoding)
@@ -91,6 +92,17 @@ var CPSplitViewDelegateKey = @"CPSplitViewDelegateKey",
_arrangedSubviews = [aCoder decodeObjectForKey:CPSplitViewArrangedSubviewsKey] || @[];
_realSubviews = [aCoder decodeObjectForKey:CPSplitViewRealSubviewsKey] || @[];
/*
A cib has no arranged subviews yet: its priorities are kept as archived until
-_cibInstantiate arranges the subviews.
*/
var holdingPriorities = [aCoder decodeObjectForKey:CPSplitViewHoldingPrioritiesKey];
if (_arrangedSubviews.length > 0)
[self _setHoldingPrioritiesFromArchive:holdingPriorities];
else
_holdingPriorities = holdingPriorities ? [holdingPriorities mutableCopy] : @[];
[self setArrangesAllSubviews:[aCoder decodeBoolForKey:CPSplitViewArrangesAllSubviewsKey]];
[self setDividerStyle:[aCoder decodeIntForKey:CPSplitViewDividerStyleKey]];
@@ -106,12 +118,22 @@ var CPSplitViewDelegateKey = @"CPSplitViewDelegateKey",
{
// Subviews received from Xcode IB are in fact arranged subviews
/*
A cib has no arranged subviews when it is decoded, so the archived holding priorities
are applied once the subviews are arranged.
*/
var archivedPriorities = [_holdingPriorities copy];
_holdingPriorities = @[];
_subviewsManagementDisabled = YES;
for (var i = 0, subviews = [self subviews], count = subviews.length; i < count; i++)
[self _addArrangedSubview:subviews[i]];
_subviewsManagementDisabled = NO;
[self _setHoldingPrioritiesFromArchive:archivedPriorities];
_shouldAutosave = YES;
if (_autosaveName)
@@ -141,6 +163,7 @@ var CPSplitViewDelegateKey = @"CPSplitViewDelegateKey",
[aCoder encodeObject:_dividerSubviews forKey:CPSplitViewDividerSubviewsKey];
[aCoder encodeObject:_arrangedSubviews forKey:CPSplitViewArrangedSubviewsKey];
[aCoder encodeObject:_realSubviews forKey:CPSplitViewRealSubviewsKey];
[aCoder encodeObject:_holdingPriorities forKey:CPSplitViewHoldingPrioritiesKey];
[aCoder encodeBool: _arrangesAllSubviews forKey:CPSplitViewArrangesAllSubviewsKey];
[aCoder encodeObject:_autosaveName forKey:CPSplitViewAutosaveNameKey];
}
+136 -103
View File
@@ -103,10 +103,10 @@
/*!
Adjusts the sizes of the split view’s subviews so they (plus the dividers) fill the split view.
When you call this method, the split view’s subviews are resized proportionally; the relative sizes of the subviews do not change.
The default implementation of this method resizes subviews proportionally so that the ratio of heights (when using horizontal dividers)
or widths (when using vertical dividers) does not change, even though the absolute sizes change.
The change in size goes first to the subviews with the lowest holding priority. When they cannot shrink further, the rest of the
change goes to the subviews with the next priority. Subviews with the same holding priority share the change proportionally, so the
ratio of their heights (when using horizontal dividers) or widths (when using vertical dividers) does not change. Subviews for which
the delegate returns NO from splitView:shouldAdjustSizeOfSubview: change only when the other subviews cannot take the change.
Call this method on split views from which subviews have been added or removed, to reestablish the consistency of subview placement.
@@ -117,104 +117,42 @@
[self _maybePostWillResizeNotification];
/*
Two cases: there is enough space for the fixed subviews, or there is not. A collapsed
subview takes no space and keeps its size, so it is left out of both cases. The ratios
of the other subviews are scaled so that they share all the space.
The change in size goes to groups of subviews in turn. Subviews that the delegate lets
the split view resize come first, then the others; within each, the subviews with the
lowest holding priority come first. A group takes the whole change, and when it cannot
shrink past zero the rest of the change goes to the next group. In a group, the
subviews share the new size by their ratios. A collapsed subview takes no space and
keeps its size, so it belongs to no group.
2026-11-23: a collapsed subview was a visible subview of size zero, so it needed no
special case. Now that it keeps its size, counting it would leave a gap.
The first group always gets its sizes from its ratios, even when the change is zero, so
a call reestablishes the proportions of the subviews that resize first. A later group
changes only when the change reaches it.
2026-11-23: the split view had no holding priorities. Every subview that the delegate
let it resize took a part of each change, so a sidebar grew and shrank with the window.
*/
var sizeToFit = MAX(0, [self frame].size[_sizeComponent] - [self _totalDividerThickness]),
groups = [self _resizeGroups],
change = sizeToFit;
var sizeToFit = [self frame].size[_sizeComponent] - [self _totalDividerThickness],
fixedSize = 0,
nbSubviews = _arrangedSubviews.length,
flexibleRatios = 0,
fixedRatios = 0,
flexibleCount = 0,
fixedCount = 0;
for (var i = 0, count = _arrangedSubviews.length; i < count; i++)
change -= [self _sizeOfArrangedSubviewAtIndex:i];
for (var i = 0; i < nbSubviews; i++)
for (var g = 0, groupCount = groups.length; g < groupCount; g++)
{
if ([_arrangedSubviews[i] isHidden])
continue;
if (g > 0 && change === 0)
break;
if (_isFlexible[i])
{
flexibleRatios += _ratios[i];
flexibleCount++;
}
else
{
fixedSize += [_arrangedSubviews[i] frameSize][_sizeComponent];
fixedRatios += _ratios[i];
fixedCount++;
}
}
var group = groups[g],
groupSize = 0;
if (fixedSize < sizeToFit)
{
// There's enough space to maintain fixed and flexible subviews
// So we only deal with flexible subviews, resizing them while keeping all ratios
for (var i = 0, count = group.length; i < count; i++)
groupSize += [self _sizeOfArrangedSubviewAtIndex:group[i]];
var newFlexibleSize = sizeToFit - fixedSize,
remainingSpace = newFlexibleSize;
var newGroupSize = MAX(0, groupSize + change);
for (var i = 0, size, floatSize, intSize, cumulativeFloatSize = 0.0, cumulativeIntSize = 0; i < nbSubviews; i++)
if (_isFlexible[i] && ![_arrangedSubviews[i] isHidden])
{
// Cocoa doesn't seem to round arranged subviews sizes.
// So the next computation ensures we get the same results but with rounded sizes.
size = CGSizeMakeCopy([_arrangedSubviews[i] frameSize]);
floatSize = MIN((newFlexibleSize * [self _shareOfRatio:_ratios[i] inTotal:flexibleRatios count:flexibleCount]), remainingSpace);
cumulativeFloatSize += floatSize;
intSize = ROUND(cumulativeFloatSize) - cumulativeIntSize;
cumulativeIntSize += intSize;
remainingSpace -= size[_sizeComponent] = intSize;
[_arrangedSubviews[i] setFrameSize:size];
}
if (remainingSpace > 0)
// Due to roundings, some pixels remain to attribute. We do this in reverse order of views.
// For example, if we have 9px to distribute on 4 views, we do : +2 +2 +2 +3
[self _distribute:remainingSpace amoung:flexibleCount onFlexible:YES fromIndex:0 toIndex:nbSubviews-1];
}
else
{
// There's not enough space to maintain fixed and flexible subviews
// So we collapse all flexible subviews and shrink evenly fixed subviews
var remainingSpace = sizeToFit;
for (var i = 0, size, floatSize, intSize, cumulativeFloatSize = 0.0, cumulativeIntSize = 0; i < nbSubviews; i++)
{
if ([_arrangedSubviews[i] isHidden])
continue;
size = CGSizeMakeCopy([_arrangedSubviews[i] frameSize]);
if (_isFlexible[i])
size[_sizeComponent] = 0;
else
{
// Cocoa doesn't seem to round arranged subviews sizes.
// So the next computation ensures we get the same results but with rounded sizes.
floatSize = MIN((sizeToFit * [self _shareOfRatio:_ratios[i] inTotal:fixedRatios count:fixedCount]), remainingSpace);
cumulativeFloatSize += floatSize;
intSize = ROUND(cumulativeFloatSize) - cumulativeIntSize;
cumulativeIntSize += intSize;
remainingSpace -= size[_sizeComponent] = intSize;
}
[_arrangedSubviews[i] setFrameSize:size];
}
if (remainingSpace > 0)
[self _distribute:remainingSpace amoung:fixedCount onFlexible:NO fromIndex:0 toIndex:nbSubviews-1];
[self _setSize:newGroupSize ofResizeGroup:group];
change -= newGroupSize - groupSize;
}
[self _maybePostDidResizeNotification];
@@ -248,6 +186,44 @@
[self updateTrackingAreas];
}
// MARK: - Holding priorities
/*!
Returns the holding priority of the arranged subview at an index. The default is 250.
@param subviewIndex - The index of the arranged subview.
@return float - The holding priority.
*/
- (float)holdingPriorityForSubviewAtIndex:(CPInteger)subviewIndex
{
[self _checkArrangedSubviewIndex:subviewIndex];
return _holdingPriorities[subviewIndex];
}
/*!
Sets the holding priority of the arranged subview at an index. When the split view changes size, the subviews with the lowest
holding priority take the change first. The priority takes effect at the next change in size.
@param priority - The holding priority.
@param subviewIndex - The index of the arranged subview.
*/
- (void)setHoldingPriority:(float)priority forSubviewAtIndex:(CPInteger)subviewIndex
{
[self _checkArrangedSubviewIndex:subviewIndex];
_holdingPriorities[subviewIndex] = priority;
}
/*
Raises a range exception when an index is not the index of an arranged subview.
*/
- (void)_checkArrangedSubviewIndex:(CPInteger)anIndex
{
if (anIndex < 0 || anIndex >= _arrangedSubviews.length)
[CPException raise:CPRangeException reason:@"CPSplitView: index " + anIndex + " is out of range of the " + _arrangedSubviews.length + " arranged subviews"];
}
// MARK: - Private layout utilities
/*
@@ -345,18 +321,75 @@
}
- (void)_distribute:(CPInteger)remainingSpace amoung:(CPInteger)count onFlexible:(BOOL)onFlexible fromIndex:(CPInteger)fromIndex toIndex:(CPInteger)toIndex
{
for (var i = toIndex, size, supplement; i >= fromIndex; i--)
if (_isFlexible[i] == onFlexible && ![_arrangedSubviews[i] isHidden])
{
size = CGSizeMakeCopy([_arrangedSubviews[i] frameSize]);
remainingSpace -= supplement = CEIL(remainingSpace / count);
size[_sizeComponent] += supplement;
[_arrangedSubviews[i] setFrameSize:size];
count--;
/*
Returns the indexes of the visible arranged subviews in the groups that take a change in
size, in the order in which they take it: first the subviews that the delegate lets the
split view resize, then the others, and in each by ascending holding priority. Subviews of
a group have the same holding priority.
*/
- (CPArray)_resizeGroups
{
var indexes = [];
for (var i = 0, count = _arrangedSubviews.length; i < count; i++)
if (![_arrangedSubviews[i] isHidden])
indexes.push(i);
var tier = function(anIndex) { return _isFlexible[anIndex] ? 0 : 1; };
indexes.sort(function(a, b)
{
return (tier(a) - tier(b)) || (_holdingPriorities[a] - _holdingPriorities[b]) || (a - b);
});
var groups = [];
for (var i = 0, count = indexes.length, group = nil; i < count; i++)
{
var index = indexes[i];
if (!group || tier(index) !== tier(group[0]) || _holdingPriorities[index] !== _holdingPriorities[group[0]])
groups.push(group = []);
group.push(index);
}
return groups;
}
/*
Gives the subviews of a resize group their shares of a size, by their ratios. The sizes are
rounded so that their running total stays on the running total of the shares; the last
subview takes what is left, so the sizes add up to the given size.
*/
- (void)_setSize:(float)aSize ofResizeGroup:(CPArray)aGroup
{
var totalRatio = 0,
count = aGroup.length;
for (var i = 0; i < count; i++)
totalRatio += _ratios[aGroup[i]];
for (var i = 0, cumulativeFloatSize = 0, cumulativeIntSize = 0; i < count; i++)
{
var subview = _arrangedSubviews[aGroup[i]],
size = CGSizeMakeCopy([subview frameSize]),
intSize;
if (i === count - 1)
intSize = aSize - cumulativeIntSize;
else
{
cumulativeFloatSize += aSize * [self _shareOfRatio:_ratios[aGroup[i]] inTotal:totalRatio count:count];
intSize = ROUND(cumulativeFloatSize) - cumulativeIntSize;
}
cumulativeIntSize += intSize;
size[_sizeComponent] = intSize;
[subview setFrameSize:size];
}
}
- (void)_updateRatios
+83 -24
View File
@@ -51,6 +51,11 @@
CPSplitViewDidResizeSubviewsNotification = @"CPSplitViewDidResizeSubviewsNotification";
CPSplitViewWillResizeSubviewsNotification = @"CPSplitViewWillResizeSubviewsNotification";
/*
The holding priority of a new arranged subview, as in AppKit (NSLayoutPriorityDefaultLow).
*/
var CPSplitViewDefaultHoldingPriority = 250;
// FIXME: Previous implementation (before October 2018 and Aristo3) uses direct DOM elements manipulations to render dividers.
//
@@ -121,6 +126,7 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
CPMutableArray _initialSizes;
CPMutableArray _ratios;
CPMutableArray _isFlexible;
CPMutableArray _holdingPriorities; // One per arranged subview, in the same order
}
+ (CPString)defaultThemeClass
@@ -165,6 +171,7 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
_initialSizes = @[];
_ratios = @[];
_isFlexible = @[];
_holdingPriorities = @[];
_arrangesAllSubviews = YES; // Default for previous behavior compatibility
[self setDividerStyle:CPSplitViewDividerStyleThick]; // Default of Xcode IB
@@ -235,43 +242,73 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
*/
- (void)setVertical:(BOOL)shouldBeVertical
{
var oldSizeComponent = _sizeComponent;
if (![self _setVertical:shouldBeVertical])
return;
// Just re-adjust evenly.
var frame = [self frame],
dividerThickness = [self dividerThickness],
previousArrangedSubviews = [_arrangedSubviews copy],
nbArrangedSubviews = previousArrangedSubviews.length,
previousTotalSize = frame.size[_otherSizeComponent],
previousRatios = [_ratios copy],
totalSize = frame.size[_sizeComponent] - (nbArrangedSubviews - 1) * dividerThickness;
/*
Each arranged subview keeps its share of the space along the split, and fills the split
view in the other direction. A collapsed subview keeps its size, scaled in the same way,
for its expansion.
2026-11-23: the sizes came from the ratios, which add up to one for the subviews that
the delegate lets the split view resize and to one again for the others. With both
kinds, the subviews took more than the split view. The method also removed and added
each subview again, so it lost the holding priorities.
*/
var frameSize = [self frame].size,
count = _arrangedSubviews.length,
weights = [],
totalWeight = 0,
lastVisible = CPNotFound,
oldTotal = 0;
[self _postNotificationWillResize];
_subviewsManagementDisabled = YES;
for (var i = 0; i < count; i++)
if (![_arrangedSubviews[i] isHidden])
{
oldTotal += [_arrangedSubviews[i] frameSize][oldSizeComponent];
lastVisible = i;
}
var i = _arrangedSubviews.length;
while (i > 0)
[self removeArrangedSubview:_arrangedSubviews[--i]];
for (var i = 0, theView, theViewSize; i < nbArrangedSubviews; i++)
for (var i = 0; i < count; i++)
{
theView = previousArrangedSubviews[i];
theViewSize = CGSizeMakeCopy([theView frameSize]);
theViewSize[_sizeComponent] = totalSize * previousRatios[i];
theViewSize[_otherSizeComponent] = frame.size[_otherSizeComponent];
[theView setFrameSize:theViewSize];
weights[i] = oldTotal > 0 ? [_arrangedSubviews[i] frameSize][oldSizeComponent] : 1;
[self _addArrangedSubview:theView];
if (![_arrangedSubviews[i] isHidden])
totalWeight += weights[i];
}
_subviewsManagementDisabled = NO;
var newTotal = MAX(0, frameSize[_sizeComponent] - [self _totalDividerThickness]);
for (var i = 0, cumulativeFloatSize = 0, cumulativeIntSize = 0; i < count; i++)
{
var subview = _arrangedSubviews[i],
size = CGSizeMakeCopy([subview frameSize]),
share = totalWeight > 0 ? weights[i] / totalWeight * newTotal : 0;
if ([subview isHidden])
size[_sizeComponent] = oldTotal > 0 ? ROUND(share) : size[oldSizeComponent];
else
{
cumulativeFloatSize += share;
var intSize = (i === lastVisible ? newTotal : ROUND(cumulativeFloatSize)) - cumulativeIntSize;
cumulativeIntSize += intSize;
size[_sizeComponent] = intSize;
}
size[_otherSizeComponent] = frameSize[_otherSizeComponent];
[subview setFrameSize:size];
}
[self _layoutSubviewsInEffectiveAppearance];
[self _updateRatios];
[self _postNotificationDidResize];
return;
}
- (BOOL)_setVertical:(BOOL)shouldBeVertical
@@ -403,6 +440,7 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
[_arrangedSubviews insertObject:view atIndex:index];
[_realSubviews addObject:view];
[_initialSizes insertObject:newViewFrame.size[_sizeComponent] atIndex:index];
[_holdingPriorities insertObject:CPSplitViewDefaultHoldingPriority atIndex:index];
// We have to reinitialize this cache as flexibility can change over time (determined by the delegate)
_isFlexible = @[];
@@ -448,6 +486,26 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
}
}
/*
Gives each arranged subview a holding priority. Archived priorities are kept when there is
one for each arranged subview; otherwise each subview gets the default priority.
*/
- (void)_setHoldingPrioritiesFromArchive:(CPArray)priorities
{
var count = _arrangedSubviews.length;
if (priorities && priorities.length === count)
{
_holdingPriorities = [priorities mutableCopy];
return;
}
_holdingPriorities = @[];
for (var i = 0; i < count; i++)
[_holdingPriorities addObject:CPSplitViewDefaultHoldingPriority];
}
- (void)removeArrangedSubview:(CPView)view
{
// The view remains a subview, keeping its size and position at the moment of the removal.
@@ -462,6 +520,7 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
}
[_arrangedSubviews removeObjectAtIndex:arrangedIndex];
[_holdingPriorities removeObjectAtIndex:arrangedIndex];
if (_dividerSubviews.length > 0)
{
+194 -5
View File
@@ -28,11 +28,11 @@
/*
These tests state how a split view behaves as AppKit's NSSplitView does: divider drags and
their delegate constraints, collapsing, double-clicks, hidden dividers, notifications, hit
testing, button bars, autosave, orientation, arranged subviews and archiving. They come
before a refactoring and a rework of CPSplitView, so they test through public behaviour
only. The one exception is -_restoreFromAutosave: today only a cib load starts a restore,
so the autosave tests call it, as CPSplitViewTest does.
their delegate constraints, collapsing, double-clicks, hidden dividers, holding priorities,
notifications, hit testing, button bars, autosave, orientation, arranged subviews and
archiving. They come before a refactoring and a rework of CPSplitView, so they test through
public behaviour only. The one exception is -_restoreFromAutosave: today only a cib load
starts a restore, so the autosave tests call it, as CPSplitViewTest does.
Each test uses a vertical split view 300 points wide and 100 points high, with thin dividers
(1 point under ojtest, where no theme is loaded). The first subview is 100 points wide; the
@@ -354,6 +354,126 @@
[self assert:1 equals:CGRectGetWidth([splitView rectOfDividerAtIndex:0]) message:@"the divider has its thickness again"];
}
// MARK: - Holding priorities
- (void)testTheDefaultHoldingPriorityIs250
{
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:0] message:@"the first subview"];
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:1] message:@"the second subview"];
}
- (void)testAHoldingPriorityIsKept
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[self assert:260 equals:[splitView holdingPriorityForSubviewAtIndex:0] message:@"the first subview"];
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:1] message:@"the second subview"];
}
- (void)testAnIndexOutOfRangeRaises
{
[self assertThrows:function() { [splitView holdingPriorityForSubviewAtIndex:2]; }];
[self assertThrows:function() { [splitView setHoldingPriority:260 forSubviewAtIndex:-1]; }];
}
- (void)testGrowingGivesTheSpaceToTheLowestHoldingPriority
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView setFrame:CGRectMake(0, 0, 400, 100)];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:viewA named:@"the subview that holds"];
[self assertFrame:CGRectMake(101, 0, 299, 100) of:viewB named:@"the subview that grows"];
}
- (void)testAHigherPriorityOnTheSecondSubviewMakesTheFirstGrow
{
[splitView setHoldingPriority:260 forSubviewAtIndex:1];
[splitView setFrame:CGRectMake(0, 0, 400, 100)];
[self assertFrame:CGRectMake(0, 0, 200, 100) of:viewA named:@"the subview that grows"];
[self assertFrame:CGRectMake(201, 0, 199, 100) of:viewB named:@"the subview that holds"];
}
- (void)testShrinkingTakesTheSpaceFromTheLowestHoldingPriority
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView setFrame:CGRectMake(0, 0, 200, 100)];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:viewA named:@"the subview that holds"];
[self assertFrame:CGRectMake(101, 0, 99, 100) of:viewB named:@"the subview that shrinks"];
}
/*
When the subviews with the lowest priority reach zero, the rest of the change goes to the
subviews with the next priority.
*/
- (void)testShrinkingPastZeroMovesToTheNextHoldingPriority
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView setFrame:CGRectMake(0, 0, 60, 100)];
[self assertFrame:CGRectMake(0, 0, 59, 100) of:viewA named:@"the subview with the higher priority"];
[self assertFrame:CGRectMake(60, 0, 0, 100) of:viewB named:@"the subview with the lower priority"];
}
- (void)testEqualHoldingPrioritiesShareTheChangeBySize
{
[splitView setFrame:CGRectMake(0, 0, 400, 100)];
[self assertFrame:CGRectMake(0, 0, 133, 100) of:viewA named:@"the first subview"];
[self assertFrame:CGRectMake(134, 0, 266, 100) of:viewB named:@"the second subview"];
}
/*
A subview that the delegate does not let the split view resize changes only when the
others cannot take the change, whatever its holding priority.
*/
- (void)testASubviewTheDelegateHoldsComesAfterEveryPriority
{
[testDelegate setFixedSubviews:[viewA]];
[splitView setDelegate:testDelegate];
[splitView setHoldingPriority:100 forSubviewAtIndex:0];
[splitView setFrame:CGRectMake(0, 0, 400, 100)];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:viewA named:@"the subview the delegate holds"];
[self assertFrame:CGRectMake(101, 0, 299, 100) of:viewB named:@"the other subview"];
}
- (void)testACollapsedSubviewKeepsItsSizeWhenTheSplitViewGrows
{
[testDelegate setMinimum:80];
[testDelegate setCollapsibleSubviews:[viewA]];
[splitView setDelegate:testDelegate];
[self dragDivider:0 toX:30];
[splitView setHoldingPriority:260 forSubviewAtIndex:1];
[splitView setFrame:CGRectMake(0, 0, 400, 100)];
[self assert:100 equals:CGRectGetWidth([viewA frame]) message:@"the collapsed subview keeps its width"];
[self assertFrame:CGRectMake(1, 0, 399, 100) of:viewB named:@"the visible subview"];
}
- (void)testAHoldingPriorityFollowsItsSubviewWhenASubviewIsInserted
{
var viewC = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 50, 100)];
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView insertArrangedSubview:viewC atIndex:0];
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:0] message:@"the inserted subview"];
[self assert:260 equals:[splitView holdingPriorityForSubviewAtIndex:1] message:@"the first subview"];
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:2] message:@"the second subview"];
}
- (void)testRemovingASubviewRemovesItsHoldingPriority
{
[splitView setHoldingPriority:260 forSubviewAtIndex:1];
[viewA removeFromSuperview];
[self assert:260 equals:[splitView holdingPriorityForSubviewAtIndex:0] message:@"the remaining subview"];
[self assertThrows:function() { [splitView holdingPriorityForSubviewAtIndex:1]; }];
}
// MARK: - Notifications
- (void)testDraggingTheDividerPostsTheResizeNotifications
@@ -475,6 +595,27 @@
[self assert:140 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the divider stays at the restored position"];
}
/*
A restore happens before the window gives the split view its final size. A subview with a
higher holding priority keeps its restored width through that resize.
*/
- (void)testARestoredSidebarKeepsItsWidthWhenTheSplitViewResizes
{
[self useAutosaveStore];
[splitView setAutosaveName:@"CPSplitViewInteractionTest.Sidebar"];
[self dragDivider:0 toX:140];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self setUp];
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView setAutosaveName:@"CPSplitViewInteractionTest.Sidebar"];
[splitView _restoreFromAutosave];
[splitView setFrame:CGRectMake(0, 0, 500, 100)];
[self assertFrame:CGRectMake(0, 0, 140, 100) of:viewA named:@"the sidebar"];
[self assertFrame:CGRectMake(141, 0, 359, 100) of:viewB named:@"the content"];
}
/*
Saved state belongs to the subviews it was saved for. A split view that has gained a subview
since the save keeps its current layout.
@@ -547,6 +688,37 @@
[self assert:100 equals:CGRectGetHeight(frameA) + 1 + CGRectGetHeight(frameB) message:@"the subviews and the divider fill the height"];
}
- (void)testChangingTheOrientationKeepsTheProportions
{
[splitView setVertical:NO];
[self assertFrame:CGRectMake(0, 0, 300, 33) of:viewA named:@"the first subview"];
[self assertFrame:CGRectMake(0, 34, 300, 66) of:viewB named:@"the second subview"];
}
/*
The subviews share the space by their sizes, whether or not the delegate lets the split
view resize them.
*/
- (void)testChangingTheOrientationWithASubviewTheDelegateHoldsFillsTheSplitView
{
[testDelegate setFixedSubviews:[viewA]];
[splitView setDelegate:testDelegate];
[splitView setVertical:NO];
[self assertFrame:CGRectMake(0, 0, 300, 33) of:viewA named:@"the subview the delegate holds"];
[self assertFrame:CGRectMake(0, 34, 300, 66) of:viewB named:@"the other subview"];
}
- (void)testChangingTheOrientationKeepsTheHoldingPriorities
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
[splitView setVertical:NO];
[self assert:260 equals:[splitView holdingPriorityForSubviewAtIndex:0] message:@"the first subview"];
[self assert:250 equals:[splitView holdingPriorityForSubviewAtIndex:1] message:@"the second subview"];
}
- (void)testASubviewAddedWhileNotArrangingAllSubviewsIsNotArranged
{
var viewC = [[CPView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
@@ -589,6 +761,16 @@
[self assert:140 equals:CGRectGetWidth([arranged[0] frame]) message:@"the first subview of the copy keeps its width"];
}
- (void)testArchivingKeepsTheHoldingPriorities
{
[splitView setHoldingPriority:260 forSubviewAtIndex:0];
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:splitView]];
[self assert:260 equals:[copy holdingPriorityForSubviewAtIndex:0] message:@"the first subview of the copy"];
[self assert:250 equals:[copy holdingPriorityForSubviewAtIndex:1] message:@"the second subview of the copy"];
}
@end
// MARK: -
@@ -602,6 +784,7 @@
float _maximum @accessors(property=maximum);
float _snap @accessors(property=snap);
CPArray _collapsibleSubviews @accessors(property=collapsibleSubviews);
CPArray _fixedSubviews @accessors(property=fixedSubviews);
BOOL _collapsesOnDoubleClick @accessors(property=collapsesOnDoubleClick);
CGRect _additionalRect @accessors(property=additionalRect);
CPArray _hiddenDividerIndexes @accessors(property=hiddenDividerIndexes);
@@ -617,6 +800,7 @@
if (self = [super init])
{
_collapsibleSubviews = [];
_fixedSubviews = [];
_hiddenDividerIndexes = [];
_didResizeCount = 0;
}
@@ -639,6 +823,11 @@
return _snap ? ROUND(proposedPosition / _snap) * _snap : proposedPosition;
}
- (BOOL)splitView:(CPSplitView)aSplitView shouldAdjustSizeOfSubview:(CPView)aSubview
{
return ![_fixedSubviews containsObject:aSubview];
}
- (BOOL)splitView:(CPSplitView)aSplitView canCollapseSubview:(CPView)aSubview
{
return [_collapsibleSubviews containsObject:aSubview];