CPSplitView: Hide dividers the delegate asks to hide, per AppKit

The split view did not ask the delegate whether to hide a divider, so every
divider took its full thickness and a collapsed sidebar left a divider line at
the edge.

The split view now sends splitView:shouldHideDividerAtIndex:. A
hidden divider has zero thickness, its view is hidden, layout and the divider
limits give it no space, and it has no drag area of its own; the delegate's
additional effective rect and button bar resize controls still apply.

When a
collapse or expansion changes the answer, the two subviews are placed again
with the new thickness.
This commit is contained in:
David Richardson committed 2026-11-23 16:52:30 -06:00
1 parent d2d9bf8c79
commit 1938847271
5 files changed
+209 -34

No files matched your search

+23 -1
View File
@@ -35,7 +35,8 @@ var CPSplitViewDelegate_splitView_canCollapseSubview_
CPSplitViewDelegate_splitView_constrainSplitPosition_ofSubviewAt_ = 1 << 7,
CPSplitViewDelegate_splitView_resizeSubviewsWithOldSize_ = 1 << 8,
CPSplitViewDelegate_splitViewDidResizeSubviews_ = 1 << 9,
CPSplitViewDelegate_splitViewWillResizeSubviews_ = 1 << 10;
CPSplitViewDelegate_splitViewWillResizeSubviews_ = 1 << 10,
CPSplitViewDelegate_splitView_shouldHideDividerAtIndex_ = 1 << 11;
@implementation CPSplitView (CPSplitViewDelegate)
@@ -102,6 +103,12 @@ var CPSplitViewDelegate_splitView_canCollapseSubview_
- (void)splitView:(CPSplitView)aSplitView resizeSubviewsWithOldSize:(CGSize)oldSize;
@endcode
Lets the delegate hide a divider. A hidden divider has zero thickness and takes no space, and
the user cannot drag it by its own rect.
@code
- (BOOL)splitView:(CPSplitView)aSplitView shouldHideDividerAtIndex:(int)dividerIndex;
@endcode
@param delegate - The delegate of the splitview.
*/
- (void)setDelegate:(id <CPSplitViewDelegate>)aDelegate
@@ -145,6 +152,9 @@ var CPSplitViewDelegate_splitView_canCollapseSubview_
if ([_delegate respondsToSelector:@selector(splitView:resizeSubviewsWithOldSize:)])
_implementedDelegateMethods |= CPSplitViewDelegate_splitView_resizeSubviewsWithOldSize_;
if ([_delegate respondsToSelector:@selector(splitView:shouldHideDividerAtIndex:)])
_implementedDelegateMethods |= CPSplitViewDelegate_splitView_shouldHideDividerAtIndex_;
// We have to recompute flexible/fixed caches as the new delegate could consider things differently...
[self _updateRatios];
}
@@ -218,6 +228,18 @@ var CPSplitViewDelegate_splitView_canCollapseSubview_
return [_delegate splitView:self shouldCollapseSubview:aView forDoubleClickOnDividerAtIndex:anIndex];
}
/*!
@ignore
Call the delegate splitView:shouldHideDividerAtIndex:
*/
- (BOOL)_sendDelegateSplitViewShouldHideDividerAtIndex:(CPInteger)anIndex
{
if (!(_implementedDelegateMethods & CPSplitViewDelegate_splitView_shouldHideDividerAtIndex_))
return NO;
return !![_delegate splitView:self shouldHideDividerAtIndex:anIndex];
}
/*!
@ignore
Call the delegate splitView:additionalEffectiveRectOfDividerAtIndex:
+29 -10
View File
@@ -76,10 +76,15 @@
rightButtonBarRect.origin = [self convertPoint:rightButtonBarRect.origin fromView:rightButtonBar];
}
effectiveRect = [self _sendDelegateSplitViewEffectiveRect:effectiveRect forDrawnRect:effectiveRect ofDividerAtIndex:anIndex];
// A hidden divider has no drag area of its own; see -effectiveRectOfDividerAtIndex:.
if ([self _isDividerHiddenAtIndex:anIndex])
effectiveRect = nil;
else
effectiveRect = [self _sendDelegateSplitViewEffectiveRect:effectiveRect forDrawnRect:effectiveRect ofDividerAtIndex:anIndex];
additionalRect = [self _sendDelegateSplitViewAdditionalEffectiveRectOfDividerAtIndex:anIndex];
return CGRectContainsPoint(effectiveRect, aPoint) ||
return (effectiveRect && CGRectContainsPoint(effectiveRect, aPoint)) ||
(additionalRect && CGRectContainsPoint(additionalRect, aPoint)) ||
(leftButtonBarRect && CGRectContainsPoint(leftButtonBarRect, aPoint)) ||
(rightButtonBarRect && CGRectContainsPoint(rightButtonBarRect, aPoint));
@@ -283,9 +288,9 @@
- (float)maxPossiblePositionOfDividerAtIndex:(int)dividerIndex
{
if (dividerIndex + 1 < [_arrangedSubviews count] - 1)
return [_arrangedSubviews[dividerIndex + 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex + 1] - [self dividerThickness];
return [_arrangedSubviews[dividerIndex + 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex + 1] - [self _thicknessOfDividerAtIndex:dividerIndex];
else
return [self frame].size[_sizeComponent] - [self dividerThickness];
return [self frame].size[_sizeComponent] - [self _thicknessOfDividerAtIndex:dividerIndex];
}
/*!
@@ -297,7 +302,7 @@
{
if (dividerIndex > 0)
{
return [_arrangedSubviews[dividerIndex - 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex - 1] + [self dividerThickness];
return [_arrangedSubviews[dividerIndex - 1] frame].origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:dividerIndex - 1] + [self _thicknessOfDividerAtIndex:dividerIndex - 1];
}
else
return 0;
@@ -360,7 +365,7 @@
var realPosition = [self _realPositionForPosition:position ofDividerAtIndex:dividerIndex],
viewA = _arrangedSubviews[dividerIndex],
viewB = _arrangedSubviews[dividerIndex + 1],
thickness = [self dividerThickness],
thickness = [self _thicknessOfDividerAtIndex:dividerIndex],
startA = [self minPossiblePositionOfDividerAtIndex:dividerIndex],
endB = [self maxPossiblePositionOfDividerAtIndex:dividerIndex] + thickness,
collapsesA = realPosition <= startA && [self _sendDelegateSplitViewCanCollapseSubview:viewA],
@@ -369,10 +374,24 @@
[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];
dividerOrigin[_originComponent] = realPosition;
[_dividerSubviews[dividerIndex] setFrameOrigin:dividerOrigin];
/*
The delegate can hide the divider next to a collapsed subview, so the collapse or the
expansion can change the thickness of the divider. The subviews are then placed again
with the new thickness. A divider that collapses the subview after it stays at the end
of its range.
*/
var newThickness = [self _thicknessOfDividerAtIndex:dividerIndex];
if (newThickness !== thickness)
{
if (collapsesB)
realPosition = endB - newThickness;
[self _placeArrangedSubview:viewA from:startA to:realPosition collapsed:collapsesA];
[self _placeArrangedSubview:viewB from:realPosition + newThickness to:endB collapsed:collapsesB];
}
[self _placeDividerAtIndex:dividerIndex at:realPosition];
[self _updateRatios];
+67 -17
View File
@@ -45,6 +45,14 @@
var realRect = [self rectOfDividerAtIndex:aDivider],
padding = 2;
/*
A hidden divider has no drag area of its own: its rect has zero thickness and gets no
padding. The additional effective rect from the delegate and the resize controls of
button bars still apply.
*/
if ([self _isDividerHiddenAtIndex:aDivider])
return realRect;
realRect.size[_sizeComponent] += padding * 2;
realRect.origin[_originComponent] -= padding;
@@ -117,7 +125,7 @@
special case. Now that it keeps its size, counting it would leave a gap.
*/
var sizeToFit = [self frame].size[_sizeComponent] - _dividerSubviews.length * [self dividerThickness],
var sizeToFit = [self frame].size[_sizeComponent] - [self _totalDividerThickness],
fixedSize = 0,
nbSubviews = _arrangedSubviews.length,
flexibleRatios = 0,
@@ -229,14 +237,11 @@
if (i < count - 1)
{
origin = CGPointMakeZero();
origin[_originComponent] = position;
position += [_dividerSubviews[i] frame].size[_sizeComponent];
// Apply the color to the specific divider view
[_dividerSubviews[i] setBackgroundColor:currentDividerColor];
[_dividerSubviews[i] setFrameOrigin:origin];
[self _placeDividerAtIndex:i at:position];
position += [self _thicknessOfDividerAtIndex:i];
}
}
@@ -245,6 +250,58 @@
// MARK: - Private layout utilities
/*
Returns YES when the delegate hides the divider at an index. As in AppKit, a hidden divider
has zero thickness and takes no space.
2026-11-23: the split view did not ask the delegate, so every divider took its full
thickness. A collapsed sidebar left a divider line at the edge of the window.
*/
- (BOOL)_isDividerHiddenAtIndex:(CPInteger)anIndex
{
return [self _sendDelegateSplitViewShouldHideDividerAtIndex:anIndex];
}
/*
Returns the space that the divider at an index takes along the split.
*/
- (float)_thicknessOfDividerAtIndex:(CPInteger)anIndex
{
return [self _isDividerHiddenAtIndex:anIndex] ? 0 : [self dividerThickness];
}
/*
Returns the space that all the dividers take along the split.
*/
- (float)_totalDividerThickness
{
var total = 0;
for (var i = 0, count = _dividerSubviews.length; i < count; i++)
total += [self _thicknessOfDividerAtIndex:i];
return total;
}
/*
Places the divider view at an index at a position along the split. The view has the
thickness of the divider, fills the split view in the other direction, and is hidden when
the divider is hidden.
*/
- (void)_placeDividerAtIndex:(CPInteger)anIndex at:(float)aPosition
{
var divider = _dividerSubviews[anIndex],
isHidden = [self _isDividerHiddenAtIndex:anIndex],
frame = CGRectMakeZero();
frame.origin[_originComponent] = aPosition;
frame.size[_sizeComponent] = isHidden ? 0 : [self dividerThickness];
frame.size[_otherSizeComponent] = [self frame].size[_otherSizeComponent];
[divider setFrame:frame];
[divider setHidden:isHidden];
}
/*
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.
@@ -274,21 +331,14 @@
*/
- (void)_layoutDividersFromArrangedSubviews
{
var myOtherSize = [self frame].size[_otherSizeComponent],
dividerColor = [self dividerColor];
var dividerColor = [self dividerColor];
for (var i = 0, count = MIN(_dividerSubviews.length, _arrangedSubviews.length - 1); i < count; i++)
{
var subviewFrame = [_arrangedSubviews[i] frame],
divider = _dividerSubviews[i],
frame = CGRectMakeZero();
var subviewFrame = [_arrangedSubviews[i] frame];
frame.origin[_originComponent] = subviewFrame.origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:i];
frame.size[_sizeComponent] = [divider frameSize][_sizeComponent];
frame.size[_otherSizeComponent] = myOtherSize;
[divider setBackgroundColor:dividerColor];
[divider setFrame:frame];
[_dividerSubviews[i] setBackgroundColor:dividerColor];
[self _placeDividerAtIndex:i at:subviewFrame.origin[_originComponent] + [self _sizeOfArrangedSubviewAtIndex:i]];
}
[self updateTrackingAreas];
+1 -1
View File
@@ -44,7 +44,7 @@
- (void)splitView:(CPSplitView)splitView resizeSubviewsWithOldSize:(CGSize)oldSize;
- (void)splitViewDidResizeSubviews:(CPNotification)aNotification;
- (void)splitViewWillResizeSubviews:(CPNotification)aNotification;
// FIXME: missing splitView:shouldHideDividerAtIndex:
- (BOOL)splitView:(CPSplitView)splitView shouldHideDividerAtIndex:(CPInteger)dividerIndex;
@end
+89 -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, 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, 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
@@ -286,6 +286,74 @@
[self assertFrame:CGRectMake(0, 0, 100, 100) of:viewA named:@"the first subview has its size from before"];
}
// MARK: - Hidden dividers
/*
AppKit asks the delegate whether to hide each divider. A hidden divider has zero thickness
and takes no space, so the subviews share the whole split view.
*/
- (void)testAHiddenDividerTakesNoSpace
{
[testDelegate setHiddenDividerIndexes:[0]];
[splitView setDelegate:testDelegate];
[splitView adjustSubviews];
[self assertFrame:CGRectMake(0, 0, 100, 100) of:viewA named:@"the first subview"];
[self assertFrame:CGRectMake(100, 0, 200, 100) of:viewB named:@"the second subview"];
[self assert:0 equals:CGRectGetWidth([splitView rectOfDividerAtIndex:0]) message:@"the hidden divider has zero thickness"];
}
/*
A hidden divider has no drag area of its own.
*/
- (void)testAHiddenDividerCannotBeDragged
{
[testDelegate setHiddenDividerIndexes:[0]];
[splitView setDelegate:testDelegate];
[splitView adjustSubviews];
[self dragDivider:0 toX:140];
[self assert:100 equals:CGRectGetWidth([viewA frame]) message:@"the first subview keeps its width"];
}
/*
A delegate that hides the divider next to a collapsed subview gives the space of the divider
to the other subview when the collapse happens.
*/
- (void)testCollapsingASubviewWhoseDividerHidesGivesTheSpaceToTheOtherSubview
{
[testDelegate setMinimum:80];
[testDelegate setCollapsibleSubviews:[viewA]];
[testDelegate setHidesDividersOfCollapsedSubviews:YES];
[splitView setDelegate:testDelegate];
[self dragDivider:0 toX:30];
[self assertTrue:[splitView isSubviewCollapsed:viewA] message:@"the first subview is collapsed"];
[self assertFrame:CGRectMake(0, 0, 300, 100) of:viewB named:@"the second subview"];
[self assert:0 equals:CGRectGetWidth([splitView rectOfDividerAtIndex:0]) message:@"the hidden divider has zero thickness"];
}
/*
The divider of an expanded subview is shown again and takes its thickness.
*/
- (void)testExpandingASubviewShowsItsHiddenDivider
{
[testDelegate setMinimum:80];
[testDelegate setCollapsibleSubviews:[viewA]];
[testDelegate setHidesDividersOfCollapsedSubviews:YES];
[splitView setDelegate:testDelegate];
[self dragDivider:0 toX:30];
[splitView setPosition:140 ofDividerAtIndex:0];
[self assertFalse:[splitView isSubviewCollapsed:viewA] message:@"the first subview is expanded"];
[self assertFrame:CGRectMake(0, 0, 140, 100) of:viewA named:@"the first subview"];
[self assertFrame:CGRectMake(141, 0, 159, 100) of:viewB named:@"the second subview"];
[self assert:1 equals:CGRectGetWidth([splitView rectOfDividerAtIndex:0]) message:@"the divider has its thickness again"];
}
// MARK: - Notifications
- (void)testDraggingTheDividerPostsTheResizeNotifications
@@ -536,6 +604,8 @@
CPArray _collapsibleSubviews @accessors(property=collapsibleSubviews);
BOOL _collapsesOnDoubleClick @accessors(property=collapsesOnDoubleClick);
CGRect _additionalRect @accessors(property=additionalRect);
CPArray _hiddenDividerIndexes @accessors(property=hiddenDividerIndexes);
BOOL _hidesDividersOfCollapsedSubviews @accessors(property=hidesDividersOfCollapsedSubviews);
int _didResizeCount @accessors(readonly, property=didResizeCount);
id _lastNotificationObject @accessors(readonly, property=lastNotificationObject);
@@ -547,6 +617,7 @@
if (self = [super init])
{
_collapsibleSubviews = [];
_hiddenDividerIndexes = [];
_didResizeCount = 0;
}
@@ -583,6 +654,19 @@
return _additionalRect || CGRectMakeZero();
}
- (BOOL)splitView:(CPSplitView)aSplitView shouldHideDividerAtIndex:(int)anIndex
{
if ([_hiddenDividerIndexes containsObject:anIndex])
return YES;
if (!_hidesDividersOfCollapsedSubviews)
return NO;
var subviews = [aSplitView arrangedSubviews];
return [aSplitView isSubviewCollapsed:subviews[anIndex]] || [aSplitView isSubviewCollapsed:subviews[anIndex + 1]];
}
- (void)splitViewDidResizeSubviews:(CPNotification)aNotification
{
var splitView = [aNotification object];