AppKit: Lay out hidden CPScrollView scrollers

-tile laid out a scroller only when the scroller was visible. A hidden
scroller kept its creation frame, so after a theme change it retained the
content inset from the previous theme.

Layout now assigns each scroller its edge frame, and visibility only controls whether the scroller is
hidden.
This commit is contained in:
David Richardson committed 2026-12-02 17:41:31 -06:00
1 parent 082d9e24bd
commit 7dcc38bf10
2 files changed
+153 -30

No files matched your search

+76 -30
View File
@@ -641,7 +641,6 @@ Notifies the delegate when the scroll view has finished scrolling.
var bounds = [self _insetBounds];
[self setHorizontalScroller:[[CPScroller alloc] initWithFrame:CGRectMake(0.0, 0.0, MAX(CGRectGetWidth(bounds), [_horizontalScroller scrollerWidth] + 1), [_horizontalScroller scrollerWidth])]];
[[self horizontalScroller] setFrameSize:CGSizeMake(CGRectGetWidth(bounds), [_horizontalScroller scrollerWidth])];
}
[self reflectScrolledClipView:_contentView];
@@ -706,7 +705,6 @@ Notifies the delegate when the scroll view has finished scrolling.
var bounds = [self _insetBounds];
[self setVerticalScroller:[[CPScroller alloc] initWithFrame:CGRectMake(0.0, 0.0, [_verticalScroller scrollerWidth], MAX(CGRectGetHeight(bounds), [_verticalScroller scrollerWidth] + 1))]];
[[self verticalScroller] setFrameSize:CGSizeMake([_verticalScroller scrollerWidth], CGRectGetHeight(bounds))];
}
[self reflectScrolledClipView:_contentView];
@@ -1154,6 +1152,61 @@ Notifies the delegate when the scroll view has finished scrolling.
return [[self class] _insetBounds:[self bounds] borderType:_borderType];
}
/*
@ignore
Lays out the scrollers in aFrame, the area left for the content and the scrollers,
and returns the frame left for the content. Each scroller gets its frame at its edge
whether it is shown or hidden. In the legacy style, a shown scroller takes its width
from the content; in the overlay style, it lies over the content. A gap keeps an
overlay scroller clear of the other scroller.
*/
- (CGRect)_layoutScrollersInFrame:(CGRect)aFrame
showingVertical:(BOOL)showVertical
showingHorizontal:(BOOL)showHorizontal
verticalGap:(BOOL)verticalGap
horizontalGap:(BOOL)horizontalGap
{
var isLegacy = _scrollerStyle === CPScrollerStyleLegacy,
overlay = isLegacy ? 0.0 : [CPScroller scrollerOverlay],
verticalScrollerWidth = [_verticalScroller scrollerWidth],
horizontalScrollerHeight = [_horizontalScroller scrollerWidth],
contentFrame = CGRectMakeCopy(aFrame);
if (isLegacy)
{
if (showVertical)
contentFrame.size.width -= verticalScrollerWidth;
if (showHorizontal)
contentFrame.size.height -= horizontalScrollerHeight;
}
if (_verticalScroller)
{
var verticalScrollerX = CGRectGetMaxX(aFrame) - (isLegacy ? verticalScrollerWidth : overlay),
verticalScrollerY = MAX(CGRectGetMinY(contentFrame), MAX(CGRectGetMaxY([self _cornerViewFrame]), CGRectGetMaxY([self _headerClipViewFrame]))),
verticalScrollerHeight = CGRectGetMaxY(contentFrame) - verticalScrollerY;
if (verticalGap)
verticalScrollerHeight -= horizontalScrollerHeight;
[_verticalScroller setFrame:CGRectMake(verticalScrollerX, verticalScrollerY, verticalScrollerWidth, verticalScrollerHeight)];
}
if (_horizontalScroller)
{
var horizontalScrollerY = CGRectGetMaxY(aFrame) - (isLegacy ? horizontalScrollerHeight : overlay),
horizontalScrollerWidth = CGRectGetWidth(contentFrame);
if (horizontalGap)
horizontalScrollerWidth -= verticalScrollerWidth;
[_horizontalScroller setFrame:CGRectMake(CGRectGetMinX(contentFrame), horizontalScrollerY, horizontalScrollerWidth, horizontalScrollerHeight)];
}
return contentFrame;
}
/* @ignore */
- (void)_updateCornerAndHeaderView
{
@@ -1439,6 +1492,12 @@ Notifies the delegate when the scroll view has finished scrolling.
[_horizontalScroller setHidden:YES];
}
[self _layoutScrollersInFrame:[self _insetBounds]
showingVertical:_verticalScroller && ![_verticalScroller isHidden]
showingHorizontal:_horizontalScroller && ![_horizontalScroller isHidden]
verticalGap:NO
horizontalGap:NO];
[_contentView setFrame:[self _insetBounds]];
[_headerClipView setFrame:CGRectMakeZero()];
@@ -1522,37 +1581,30 @@ Notifies the delegate when the scroll view has finished scrolling.
[_horizontalScroller setHidden:!shouldShowHorizontalScroller];
[_horizontalScroller setEnabled:hasHorizontalScroll];
var overlay = [CPScroller scrollerOverlay];
if (_scrollerStyle === CPScrollerStyleLegacy)
{
// We can thus appropriately account for them changing the content size.
if (shouldShowVerticalScroller)
contentFrame.size.width -= verticalScrollerWidth;
if (shouldShowHorizontalScroller)
contentFrame.size.height -= horizontalScrollerHeight;
overlay = 0;
}
var scrollPoint = [_contentView bounds].origin,
wasShowingVerticalScroller = ![_verticalScroller isHidden],
wasShowingHorizontalScroller = ![_horizontalScroller isHidden];
/*
2026-12-02: Layout set the frame of a scroller only while the scroller was shown,
so a hidden scroller kept the frame from its creation. -tile must lay out the
scrollers, and a hidden scroller is still a component of the scroll view. After a
theme change, a hidden scroller kept the old theme's content inset. Layout now
gives each scroller its frame; the hidden state only decides whether the content
gives up room for it.
*/
contentFrame = [self _layoutScrollersInFrame:contentFrame
showingVertical:shouldShowVerticalScroller
showingHorizontal:shouldShowHorizontalScroller
verticalGap:_scrollerStyle === CPScrollerStyleOverlay && hasHorizontalScroll
horizontalGap:_scrollerStyle === CPScrollerStyleOverlay && hasVerticalScroll];
if (shouldShowVerticalScroller)
{
var verticalScrollerY =
MAX(CGRectGetMinY(contentFrame), MAX(CGRectGetMaxY([self _cornerViewFrame]), CGRectGetMaxY(headerClipViewFrame)));
var verticalScrollerHeight = CGRectGetMaxY(contentFrame) - verticalScrollerY;
// Make a gap at the bottom of the vertical scroller so that the horizontal and vertical can't overlap.
if (_scrollerStyle === CPScrollerStyleOverlay && hasHorizontalScroll)
verticalScrollerHeight -= horizontalScrollerHeight;
var documentHeight = CGRectGetHeight(documentFrame);
[_verticalScroller setFloatValue:(difference.height <= 0.0) ? 0.0 : scrollPoint.y / difference.height];
[_verticalScroller setKnobProportion:documentHeight > 0 ? CGRectGetHeight(contentFrame) / documentHeight : 1.0];
[_verticalScroller setFrame:CGRectMake(CGRectGetMaxX(contentFrame) - overlay, verticalScrollerY, verticalScrollerWidth, verticalScrollerHeight)];
}
else if (wasShowingVerticalScroller)
{
@@ -1562,16 +1614,10 @@ Notifies the delegate when the scroll view has finished scrolling.
if (shouldShowHorizontalScroller)
{
var horizontalScrollerWidth = CGRectGetWidth(contentFrame);
// Make a gap at the bottom of the vertical scroller so that the horizontal and vertical can't overlap.
if (_scrollerStyle === CPScrollerStyleOverlay && hasVerticalScroll)
horizontalScrollerWidth -= verticalScrollerWidth;
var documentWidth = CGRectGetWidth(documentFrame);
[_horizontalScroller setFloatValue:(difference.width <= 0.0) ? 0.0 : scrollPoint.x / difference.width];
[_horizontalScroller setKnobProportion:documentWidth > 0 ? CGRectGetWidth(contentFrame) / documentWidth : 1.0];
[_horizontalScroller setFrame:CGRectMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame) - overlay, horizontalScrollerWidth, horizontalScrollerHeight)];
}
else if (wasShowingHorizontalScroller)
{
+77
View File
@@ -615,6 +615,83 @@
message:@"content frame " + CPStringFromRect([[scrollView contentView] frame]) + " should be {{5, 5}, {90, 90}}"];
}
/*
Returns a bezeled scroll view without a document view, in the legacy scroller style,
whose scrollers hide automatically, so that both scrollers exist and are hidden.
*/
- (CPScrollView)scrollViewWithHiddenScrollers
{
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
[scrollView setScrollerStyle:CPScrollerStyleLegacy];
[scrollView setAutohidesScrollers:YES];
[scrollView setBorderType:CPBezelBorder];
[scrollView layoutIfNeeded];
[self assertTrue:[[scrollView verticalScroller] isHidden] message:@"precondition: the vertical scroller is hidden"];
[self assertTrue:[[scrollView horizontalScroller] isHidden] message:@"precondition: the horizontal scroller is hidden"];
return scrollView;
}
/*
Asserts that each scroller of aScrollView has its frame at its edge of anInsetFrame,
the legacy layout for a scroll view whose scrollers are both hidden.
*/
- (void)assertScrollersOf:(CPScrollView)aScrollView areLaidOutAtTheEdgesOf:(CGRect)anInsetFrame
{
var verticalScroller = [aScrollView verticalScroller],
horizontalScroller = [aScrollView horizontalScroller],
verticalWidth = [verticalScroller scrollerWidth],
horizontalHeight = [horizontalScroller scrollerWidth],
expectedVerticalFrame = CGRectMake(CGRectGetMaxX(anInsetFrame) - verticalWidth, CGRectGetMinY(anInsetFrame), verticalWidth, CGRectGetHeight(anInsetFrame)),
expectedHorizontalFrame = CGRectMake(CGRectGetMinX(anInsetFrame), CGRectGetMaxY(anInsetFrame) - horizontalHeight, CGRectGetWidth(anInsetFrame), horizontalHeight);
[self assertTrue:CGRectEqualToRect([verticalScroller frame], expectedVerticalFrame)
message:@"vertical scroller frame " + CPStringFromRect([verticalScroller frame]) + " should be " + CPStringFromRect(expectedVerticalFrame)];
[self assertTrue:CGRectEqualToRect([horizontalScroller frame], expectedHorizontalFrame)
message:@"horizontal scroller frame " + CPStringFromRect([horizontalScroller frame]) + " should be " + CPStringFromRect(expectedHorizontalFrame)];
}
/*
-tile lays out the scrollers, as in Cocoa. A hidden scroller is still a component of
the scroll view, so layout places it at its edge, as it places a shown one.
*/
- (void)testLayoutPlacesHiddenScrollersAtTheirEdges
{
[CPTheme setDefaultTheme:[self themeNamed:@"CPScrollViewTestInsetOne" withBezelContentInset:CGInsetMake(1.0, 1.0, 1.0, 1.0)]];
var scrollView = [self scrollViewWithHiddenScrollers];
[self assertScrollersOf:scrollView areLaidOutAtTheEdgesOf:CGRectMake(1, 1, 98, 98)];
[scrollView setFrameSize:CGSizeMake(200.0, 150.0)];
[scrollView layoutIfNeeded];
[self assertScrollersOf:scrollView areLaidOutAtTheEdgesOf:CGRectMake(1, 1, 198, 148)];
}
/*
The content inset is a theme value, so after a theme change the hidden scrollers must
be laid out in the new theme's inset bounds, as in a scroll view made under the new
theme.
*/
- (void)testAThemeChangeAppliesTheNewContentInsetToHiddenScrollers
{
var firstTheme = [self themeNamed:@"CPScrollViewTestInsetOne" withBezelContentInset:CGInsetMake(1.0, 1.0, 1.0, 1.0)],
secondTheme = [self themeNamed:@"CPScrollViewTestInsetFive" withBezelContentInset:CGInsetMake(5.0, 5.0, 5.0, 5.0)];
[CPTheme setDefaultTheme:firstTheme];
var scrollView = [self scrollViewWithHiddenScrollers];
[CPTheme setDefaultTheme:secondTheme];
[scrollView _setThemeIncludingDescendants:secondTheme];
[scrollView layoutIfNeeded];
[self assertScrollersOf:scrollView areLaidOutAtTheEdgesOf:CGRectMake(5, 5, 90, 90)];
}
@end