CPSplitView: Hide collapsed subviews and preserve their size per AppKit

Previously, a collapsed subview remained visible with a size under one
point. Its pre-collapse size was lost, requiring a position table keyed
by divider index to restore it—a table that went stale whenever subviews
were inserted or removed.

Collapsed subviews are now arranged subviews that are hidden and retain
their frame size, while layout, -adjustSubviews, and divider constraints
allocate zero space to them. Dragging a divider to either limit collapses
the subview permitted by the delegate, moving a divider away unhides it,
and double-clicking restores the preserved size. Autosave now records
collapsed flags instead of divider positions, and legacy frame autosave
data restores a zero-sized subview as collapsed.
This commit is contained in:
David Richardson committed 2026-11-23 16:30:49 -06:00
1 parent c7b2f93266
commit 10e4497fa4
6 files changed
+155 -106

No files matched your search

+24 -25
View File
@@ -62,20 +62,27 @@
var userDefaults = [CPUserDefaults standardUserDefaults],
autosaveName = [self _framesKeyForAutosaveName:[self autosaveName]],
autosavePrecollapseName = [self _precollapseKeyForAutosaveName:[self autosaveName]],
autosaveCollapsedName = [self _collapsedKeyForAutosaveName:[self autosaveName]],
count = [_arrangedSubviews count],
positions = [CPMutableArray new],
preCollapseArray = [CPMutableArray new];
collapsed = [CPMutableArray new];
/*
A collapsed subview keeps its size, so the frames hold the sizes to restore and the
collapsed flags say which subviews are hidden.
2026-11-23: the split view saved the frames and a table of positions from before each
collapse, keyed by divider index. That table went stale when subviews were inserted or
removed, and the collapsed state itself was not saved.
*/
for (var i = 0; i < count; i++)
{
var frame = [_arrangedSubviews[i] frame];
[positions addObject:CGStringFromRect(frame)];
[preCollapseArray addObject:[_preCollapsePositions objectForKey:"" + i]];
[positions addObject:CGStringFromRect([_arrangedSubviews[i] frame])];
[collapsed addObject:!![_arrangedSubviews[i] isHidden]];
}
[userDefaults setObject:positions forKey:autosaveName];
[userDefaults setObject:preCollapseArray forKey:autosavePrecollapseName];
[userDefaults setObject:collapsed forKey:autosaveCollapsedName];
}
/*!
@@ -87,10 +94,10 @@
return;
var autosaveName = [self _framesKeyForAutosaveName:[self autosaveName]],
autosavePrecollapseName = [self _precollapseKeyForAutosaveName:[self autosaveName]],
autosaveCollapsedName = [self _collapsedKeyForAutosaveName:[self autosaveName]],
userDefaults = [CPUserDefaults standardUserDefaults],
frames = [userDefaults objectForKey:autosaveName],
preCollapseArray = [userDefaults objectForKey:autosavePrecollapseName];
collapsed = [userDefaults objectForKey:autosaveCollapsedName];
if (frames)
{
@@ -110,6 +117,13 @@
frame.size[_otherSizeComponent] = myOtherSize;
/*
Frames saved without collapsed flags come from the earlier format, where a
collapsed subview had a size of zero.
*/
var isCollapsed = collapsed ? !!collapsed[i] : frame.size[_sizeComponent] < 1;
[_arrangedSubviews[i] setHidden:isCollapsed];
[_arrangedSubviews[i] setFrame:frame];
}
@@ -124,21 +138,6 @@
_shouldAutosave = YES;
}
if (preCollapseArray)
{
_preCollapsePositions = [CPMutableDictionary new];
for (var i = 0, count = [preCollapseArray count]; i < count; i++)
{
var item = preCollapseArray[i];
if (item == nil)
[_preCollapsePositions removeObjectForKey:String(i)];
else
[_preCollapsePositions setObject:item forKey:String(i)];
}
}
}
/*!
@@ -155,12 +154,12 @@
/*!
@ignore
*/
- (CPString)_precollapseKeyForAutosaveName:(CPString)theAutosaveName
- (CPString)_collapsedKeyForAutosaveName:(CPString)theAutosaveName
{
if (!theAutosaveName)
return nil;
return @"CPSplitView Subview Precollapse Positions " + theAutosaveName;
return @"CPSplitView Subview Collapsed " + theAutosaveName;
}
@end
-1
View File
@@ -76,7 +76,6 @@ var CPSplitViewDelegateKey = @"CPSplitViewDelegateKey",
[self _init];
_suppressResizeNotificationsMask = 0;
_preCollapsePositions = [CPMutableDictionary new];
_currentDivider = CPNotFound;
_shouldAutosave = NO;
+67 -59
View File
@@ -32,13 +32,18 @@
// MARK: -
/*!
Returns YES if the supplied subview is collapsed, otherwise NO.
Returns YES if the supplied subview is collapsed, otherwise NO. As in AppKit, a collapsed
subview is an arranged subview that is hidden. It keeps its size and takes no space.
2026-11-23: a subview counted as collapsed when its size was below one point. A collapse
set the size to zero and kept the subview visible, so the size from before the collapse
was lost, and a separate table of positions keyed by divider index tried to restore it.
@param aSubview - the subview you are interested in.
@return BOOL - YES if the subview is collapsed, otherwise NO.
*/
- (BOOL)isSubviewCollapsed:(CPView)subview
{
return ([subview frame].size[_sizeComponent] < 1);
return [subview isHidden] && [_arrangedSubviews containsObjectIdenticalTo:subview];
}
/*!
@@ -133,28 +138,37 @@
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
_currentDivider = [self _dividerAtPoint:point];
var frame = [_arrangedSubviews[_currentDivider] frame],
startPosition = frame.origin[_originComponent] + frame.size[_sizeComponent];
var startPosition = [_arrangedSubviews[_currentDivider] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:_currentDivider];
if ([anEvent clickCount] == 2 &&
[self _delegateRespondsToSplitViewCanCollapseSubview] &&
[self _delegateRespondsToSplitViewshouldCollapseSubviewForDoubleClickOnDividerAtIndex])
{
/*
A collapsed subview keeps its size, so the expansion restores that size. A
subview with no size to restore opens to the middle of the available range.
*/
var minPosition = [self minPossiblePositionOfDividerAtIndex:_currentDivider],
maxPosition = [self maxPossiblePositionOfDividerAtIndex:_currentDivider],
preCollapsePosition = [_preCollapsePositions objectForKey:"" + _currentDivider] || 0;
middlePosition = minPosition + (maxPosition - minPosition) / 2,
viewA = _arrangedSubviews[_currentDivider],
viewB = _arrangedSubviews[_currentDivider + 1];
if ([self _sendDelegateSplitViewCanCollapseSubview:_arrangedSubviews[_currentDivider]] && [self _sendDelegateSplitViewShouldCollapseSubview:_arrangedSubviews[_currentDivider] forDoubleClickOnDividerAtIndex:_currentDivider])
if ([self _sendDelegateSplitViewCanCollapseSubview:viewA] && [self _sendDelegateSplitViewShouldCollapseSubview:viewA forDoubleClickOnDividerAtIndex:_currentDivider])
{
if ([self isSubviewCollapsed:_arrangedSubviews[_currentDivider]])
[self setPosition:preCollapsePosition ? preCollapsePosition : (minPosition + (maxPosition - minPosition) / 2) ofDividerAtIndex:_currentDivider];
var keptSizeA = [viewA frameSize][_sizeComponent];
if ([self isSubviewCollapsed:viewA])
[self setPosition:keptSizeA >= 1 ? minPosition + keptSizeA : middlePosition ofDividerAtIndex:_currentDivider];
else
[self setPosition:minPosition ofDividerAtIndex:_currentDivider];
}
else if ([self _sendDelegateSplitViewCanCollapseSubview:_arrangedSubviews[_currentDivider + 1]] && [self _sendDelegateSplitViewShouldCollapseSubview:_arrangedSubviews[_currentDivider + 1] forDoubleClickOnDividerAtIndex:_currentDivider])
else if ([self _sendDelegateSplitViewCanCollapseSubview:viewB] && [self _sendDelegateSplitViewShouldCollapseSubview:viewB forDoubleClickOnDividerAtIndex:_currentDivider])
{
if ([self isSubviewCollapsed:_arrangedSubviews[_currentDivider + 1]])
[self setPosition:preCollapsePosition ? preCollapsePosition : (minPosition + (maxPosition - minPosition) / 2) ofDividerAtIndex:_currentDivider];
var keptSizeB = [viewB frameSize][_sizeComponent];
if ([self isSubviewCollapsed:viewB])
[self setPosition:keptSizeB >= 1 ? maxPosition - keptSizeB : middlePosition ofDividerAtIndex:_currentDivider];
else
[self setPosition:maxPosition ofDividerAtIndex:_currentDivider];
}
@@ -224,21 +238,17 @@
// If we are currently tracking, keep the resize cursor active even outside of hit areas.
if (_currentDivider === i || (_currentDivider == CPNotFound && [self cursorAtPoint:point hitDividerAtIndex:i]))
{
var frameA = [_arrangedSubviews[i] frame],
sizeA = frameA.size[_sizeComponent],
startPosition = frameA.origin[_originComponent] + sizeA,
frameB = [_arrangedSubviews[i + 1] frame],
sizeB = frameB.size[_sizeComponent],
var startPosition = [_arrangedSubviews[i] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:i],
canShrink = [self _realPositionForPosition:startPosition - 1 ofDividerAtIndex:i] < startPosition,
canGrow = [self _realPositionForPosition:startPosition + 1 ofDividerAtIndex:i] > startPosition,
cursor = [CPCursor arrowCursor];
if (sizeA === 0)
if ([self isSubviewCollapsed:_arrangedSubviews[i]])
canGrow = YES; // Subview is collapsed.
else if (!canShrink && [self _sendDelegateSplitViewCanCollapseSubview:_arrangedSubviews[i]])
canShrink = YES; // Subview is collapsible.
if (sizeB === 0)
if ([self isSubviewCollapsed:_arrangedSubviews[i + 1]])
// Right/lower subview is collapsed.
canGrow = NO;
else if (!canGrow && [self _sendDelegateSplitViewCanCollapseSubview:_arrangedSubviews[i + 1]])
@@ -272,10 +282,8 @@
*/
- (float)maxPossiblePositionOfDividerAtIndex:(int)dividerIndex
{
var frame = [_arrangedSubviews[dividerIndex + 1] frame];
if (dividerIndex + 1 < [_arrangedSubviews count] - 1)
return frame.origin[_originComponent] + frame.size[_sizeComponent] - [self dividerThickness];
return [_arrangedSubviews[dividerIndex + 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex + 1] - [self dividerThickness];
else
return [self frame].size[_sizeComponent] - [self dividerThickness];
}
@@ -289,9 +297,7 @@
{
if (dividerIndex > 0)
{
var frame = [_arrangedSubviews[dividerIndex - 1] frame];
return frame.origin[_originComponent] + frame.size[_sizeComponent] + [self dividerThickness];
return [_arrangedSubviews[dividerIndex - 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex - 1] + [self dividerThickness];
}
else
return 0;
@@ -349,45 +355,23 @@
[self _setResizeNotificationsSuppressed:YES];
/*
A divider at the start of its range collapses the subview before it, and a divider at
the end of its range collapses the subview after it, when the delegate lets them
collapse. A collapsed subview is hidden and keeps its size; a divider that moves away
shows it again with the size the new position gives it.
*/
var realPosition = [self _realPositionForPosition:position ofDividerAtIndex:dividerIndex],
viewA = _arrangedSubviews[dividerIndex],
frameA = [viewA frame],
viewB = _arrangedSubviews[dividerIndex + 1],
frameB = [viewB frame],
preCollapsePosition = 0,
preSize = frameA.size[_sizeComponent];
thickness = [self dividerThickness],
startA = [self minPossiblePositionOfDividerAtIndex:dividerIndex],
endB = [self maxPossiblePositionOfDividerAtIndex:dividerIndex] + thickness,
collapsesA = realPosition <= startA && [self _sendDelegateSplitViewCanCollapseSubview:viewA],
collapsesB = !collapsesA && realPosition + thickness >= endB && [self _sendDelegateSplitViewCanCollapseSubview:viewB];
frameA.size[_sizeComponent] = realPosition - frameA.origin[_originComponent];
if (preSize !== 0 && frameA.size[_sizeComponent] === 0)
preCollapsePosition = preSize;
if (preSize !== frameA.size[_sizeComponent])
{
[self _maybePostWillResizeNotification];
[_arrangedSubviews[dividerIndex] setFrame:frameA];
[self _maybePostDidResizeNotification];
}
preSize = frameB.size[_sizeComponent];
var preOrigin = frameB.origin[_originComponent];
frameB.size[_sizeComponent] = frameB.origin[_originComponent] + frameB.size[_sizeComponent] - realPosition - [self dividerThickness];
if (preSize !== 0 && frameB.size[_sizeComponent] === 0)
preCollapsePosition = frameB.origin[_originComponent];
frameB.origin[_originComponent] = realPosition + [self dividerThickness];
if (preSize !== frameB.size[_sizeComponent] || preOrigin !== frameB.origin[_originComponent])
{
[self _maybePostWillResizeNotification];
[_arrangedSubviews[dividerIndex + 1] setFrame:frameB];
[self _maybePostDidResizeNotification];
}
if (preCollapsePosition)
[_preCollapsePositions setObject:preCollapsePosition forKey:"" + dividerIndex];
[self _placeArrangedSubview:viewA from:startA to:realPosition collapsed:collapsesA];
[self _placeArrangedSubview:viewB from:realPosition + thickness to:endB collapsed:collapsesB];
// Update the divider position
var dividerOrigin = [_dividerSubviews[dividerIndex] frameOrigin];
@@ -402,6 +386,30 @@
[self _setResizeNotificationsSuppressed:NO];
}
/*
Places an arranged subview between two positions along the split. A collapsed subview is
hidden at the start position and keeps its size; otherwise the subview is shown and fills
the space between the positions. The resize notifications are posted only for a change.
*/
- (void)_placeArrangedSubview:(CPView)aSubview from:(float)start to:(float)end collapsed:(BOOL)shouldCollapse
{
var oldFrame = [aSubview frame],
frame = CGRectMakeCopy(oldFrame);
frame.origin[_originComponent] = start;
if (!shouldCollapse)
frame.size[_sizeComponent] = end - start;
if (CGRectEqualToRect(frame, oldFrame) && !![aSubview isHidden] === !!shouldCollapse)
return;
[self _maybePostWillResizeNotification];
[aSubview setHidden:shouldCollapse];
[aSubview setFrame:frame];
[self _maybePostDidResizeNotification];
}
/*!
Set the button bar who's resize control should act as a control for this splitview.
Each divider can have at most one button bar assigned to it, and that button bar must be
+62 -17
View File
@@ -119,15 +119,40 @@
{
[self _maybePostWillResizeNotification];
// 2 possibilities : we have, or not, enough flexible space to accomodate fixed size subviews
/*
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.
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.
*/
var sizeToFit = [self frame].size[_sizeComponent] - _dividerSubviews.length * [self dividerThickness],
fixedSize = 0,
nbSubviews = _arrangedSubviews.length;
nbSubviews = _arrangedSubviews.length,
flexibleRatios = 0,
fixedRatios = 0,
flexibleCount = 0,
fixedCount = 0;
for (var i = 0; i < nbSubviews; i++)
if (!_isFlexible[i])
{
if ([_arrangedSubviews[i] isHidden])
continue;
if (_isFlexible[i])
{
flexibleRatios += _ratios[i];
flexibleCount++;
}
else
{
fixedSize += [_arrangedSubviews[i] frameSize][_sizeComponent];
fixedRatios += _ratios[i];
fixedCount++;
}
}
if (fixedSize < sizeToFit)
{
@@ -135,18 +160,15 @@
// So we only deal with flexible subviews, resizing them while keeping all ratios
var newFlexibleSize = sizeToFit - fixedSize,
remainingSpace = newFlexibleSize,
flexibleCount = 0;
remainingSpace = newFlexibleSize;
for (var i = 0, size, floatSize, intSize, cumulativeFloatSize = 0.0, cumulativeIntSize = 0; i < nbSubviews; i++)
if (_isFlexible[i])
if (_isFlexible[i] && ![_arrangedSubviews[i] isHidden])
{
flexibleCount++;
// 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 * _ratios[i]), remainingSpace);
floatSize = MIN((newFlexibleSize * [self _shareOfRatio:_ratios[i] inTotal:flexibleRatios count:flexibleCount]), remainingSpace);
cumulativeFloatSize += floatSize;
intSize = ROUND(cumulativeFloatSize) - cumulativeIntSize;
@@ -167,22 +189,22 @@
// 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,
fixedCount = 0;
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
{
fixedCount++;
// 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 * _ratios[i]), remainingSpace);
floatSize = MIN((sizeToFit * [self _shareOfRatio:_ratios[i] inTotal:fixedRatios count:fixedCount]), remainingSpace);
cumulativeFloatSize += floatSize;
intSize = ROUND(cumulativeFloatSize) - cumulativeIntSize;
@@ -212,7 +234,7 @@
{
origin = CGPointMakeZero();
origin[_originComponent] = position;
position += [_arrangedSubviews[i] frame].size[_sizeComponent];
position += [self _sizeOfArrangedSubviewAtIndex:i];
[_arrangedSubviews[i] setFrameOrigin:origin];
@@ -234,6 +256,29 @@
// MARK: - Private layout utilities
/*
Returns the space that the arranged subview at an index takes along the split. A collapsed
subview takes no space, but it keeps its frame size for its expansion.
*/
- (float)_sizeOfArrangedSubviewAtIndex:(CPInteger)anIndex
{
var subview = _arrangedSubviews[anIndex];
return [subview isHidden] ? 0 : [subview frameSize][_sizeComponent];
}
/*
Returns the part of the space that one ratio gets when a group of ratios share it. When the
ratios of the group add up to zero, each member gets an equal part.
*/
- (float)_shareOfRatio:(float)aRatio inTotal:(float)aTotal count:(CPInteger)aCount
{
if (aTotal > 0)
return aRatio / aTotal;
return aCount > 0 ? 1 / aCount : 0;
}
/*
Places each divider view after the subview before it, from that subview's frame. The divider
keeps its thickness and fills the split view in the other direction.
@@ -249,7 +294,7 @@
divider = _dividerSubviews[i],
frame = CGRectMakeZero();
frame.origin[_originComponent] = subviewFrame.origin[_originComponent] + subviewFrame.size[_sizeComponent];
frame.origin[_originComponent] = subviewFrame.origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:i];
frame.size[_sizeComponent] = [divider frameSize][_sizeComponent];
frame.size[_otherSizeComponent] = myOtherSize;
@@ -264,7 +309,7 @@
- (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)
if (_isFlexible[i] == onFlexible && ![_arrangedSubviews[i] isHidden])
{
size = CGSizeMakeCopy([_arrangedSubviews[i] frameSize]);
remainingSpace -= supplement = CEIL(remainingSpace / count);
-2
View File
@@ -92,7 +92,6 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
int _currentDivider;
float _initialOffset;
CPDictionary _preCollapsePositions;
CPString _originComponent;
CPString _otherOriginComponent;
@@ -153,7 +152,6 @@ var CPThemeStatesForSplitViewDivider = @[@"dummy one as CPSplitViewDividerStyle
- (void)_init
{
_suppressResizeNotificationsMask = 0;
_preCollapsePositions = @{};
_currentDivider = CPNotFound;
_buttonBars = [];
+2 -2
View File
@@ -56,10 +56,10 @@
{
var userDefaults = [CPUserDefaults standardUserDefaults],
autosaveName = [self _framesKeyForAutosaveName:[self autosaveName]],
autosavePrecollapseName = [self _precollapseKeyForAutosaveName:[self autosaveName]];
autosaveCollapsedName = [self _collapsedKeyForAutosaveName:[self autosaveName]];
[userDefaults setObject:nil forKey:autosaveName];
[userDefaults setObject:nil forKey:autosavePrecollapseName];
[userDefaults setObject:nil forKey:autosaveCollapsedName];
// Prevent a new autosave from being made.
[self setAutosaveName:nil];