CPScrollView: Take the content inset from the scroll view's own theme

-_insetBounds called +_insetBounds:borderType:, which reads the inset
from the default theme. A scroll view whose theme differed from the
default theme laid out its clip view and scrollers with the default
theme's inset.

The instance method now reads the inset from the scroll
view's own theme. The class method still reads the default theme for
the class-level size conversions, which have no scroll view. Shared
functions hold the code both use.
This commit is contained in:
David Richardson committed 2026-12-03 11:53:40 -06:00
1 parent 45d895323d
commit bce7f5d49b
2 files changed
+85 -27

No files matched your search

+60 -27
View File
@@ -103,6 +103,41 @@ var CPScrollerStyleGlobal = CPScrollerStyleOverlay,
var CPScrollViewBorderSuffixes = @[@"no-border", @"line-border", @"bezel-border", @"groove-border"];
/*
While a theme is compiled, the theme attributes cannot give the inset: the theme
that holds them is the one being made.
*/
function _CPScrollViewIsCompilingATheme()
{
return [[[CPBundle mainBundle] objectForInfoDictionaryKey:@"CPApplicationDelegateClass"] isEqualToString:@"BKShowcaseController"];
}
function _CPScrollViewHasBorderType(borderType)
{
return borderType >= 0 && borderType < CPScrollViewBorderSuffixes.length;
}
function _CPScrollViewInsetBoundsByContentInset(bounds, borderType, contentInset)
{
switch (borderType)
{
case CPNoBorder:
case CPLineBorder:
case CPBezelBorder:
return CGRectInsetByInset(bounds, contentInset);
case CPGrooveBorder:
// FIXME: Do something better with this
bounds = CGRectInsetByInset(bounds, contentInset);
++bounds.origin.y;
--bounds.size.height;
return bounds;
default:
return bounds;
}
}
// _CPScrollViews will hold all created CPScrollView's in order to propagate changes
// of scroller global style
var _CPScrollViews;
@@ -273,40 +308,25 @@ var CPScrollViewDefaultMinMagnification = 0.25,
return frameSize;
}
/*
The class method serves the class-level size conversions, which have no scroll view
and so take the inset from the default theme. A scroll view takes its inset from its
own theme, in -_insetBounds.
*/
+ (CGRect)_insetBounds:(CGRect)bounds borderType:(CPBorderType)borderType
{
// First, we have to check if we are compiling a theme or running an application because if working on a theme,
// we can't use theme attributes to determine the inset ! This would be a kind of circular reference...
var compilingATheme = [[[CPBundle mainBundle] objectForInfoDictionaryKey:@"CPApplicationDelegateClass"] isEqualToString:@"BKShowcaseController"];
if (compilingATheme)
if (_CPScrollViewIsCompilingATheme() || !_CPScrollViewHasBorderType(borderType))
return bounds;
var contentInset = [[CPTheme defaultTheme] valueForAttributeWithName:@"content-inset-"+CPScrollViewBorderSuffixes[borderType] forClass:CPScrollView];
var attributeName = @"content-inset-" + CPScrollViewBorderSuffixes[borderType],
contentInset = [[CPTheme defaultTheme] valueForAttributeWithName:attributeName forClass:CPScrollView];
// As this is a class method, we don't have object attributes, so if the theme doesn't declare content insets, we don't automatically get default value.
// Get it by hand.
if (!contentInset)
contentInset = [[self themeAttributes] objectForKey:@"content-inset-"+CPScrollViewBorderSuffixes[borderType]];
contentInset = [[self themeAttributes] objectForKey:attributeName];
switch (borderType)
{
case CPNoBorder:
case CPLineBorder:
case CPBezelBorder:
return CGRectInsetByInset(bounds, contentInset);
case CPGrooveBorder:
// FIXME: Do something better with this
bounds = CGRectInsetByInset(bounds, contentInset);
++bounds.origin.y;
--bounds.size.height;
return bounds;
default:
return bounds;
}
return _CPScrollViewInsetBoundsByContentInset(bounds, borderType, contentInset);
}
/*!
@@ -1146,10 +1166,23 @@ Notifies the delegate when the scroll view has finished scrolling.
[self reflectScrolledClipView:_contentView];
}
/* @ignore */
/*
@ignore
2026-12-03: the inset came from the class method, which reads the default theme. A
scroll view whose theme was not the default theme laid out its clip view and scrollers
with the default theme's inset. The cause was that a class method has no view and so
no theme of its own. The inset now comes from the scroll view's own theme.
*/
- (CGRect)_insetBounds
{
return [[self class] _insetBounds:[self bounds] borderType:_borderType];
var bounds = [self bounds];
if (_CPScrollViewIsCompilingATheme() || !_CPScrollViewHasBorderType(_borderType))
return bounds;
var contentInset = [self valueForThemeAttribute:@"content-inset-" + CPScrollViewBorderSuffixes[_borderType]];
return _CPScrollViewInsetBoundsByContentInset(bounds, _borderType, contentInset);
}
/*
+25
View File
@@ -615,6 +615,31 @@
message:@"content frame " + CPStringFromRect([[scrollView contentView] frame]) + " should be {{5, 5}, {90, 90}}"];
}
/*
The content inset is a value of the scroll view's own theme. A scroll view whose theme
is not the default theme must leave its own theme's inset free, not the default
theme's. The default theme stays unchanged throughout.
*/
- (void)testAScrollViewTakesTheContentInsetOfItsOwnTheme
{
var defaultTheme = [self themeNamed:@"CPScrollViewTestDefaultInsetOne" withBezelContentInset:CGInsetMake(1.0, 1.0, 1.0, 1.0)],
ownTheme = [self themeNamed:@"CPScrollViewTestOwnInsetFive" withBezelContentInset:CGInsetMake(5.0, 5.0, 5.0, 5.0)];
[CPTheme setDefaultTheme:defaultTheme];
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
[scrollView setTheme:ownTheme];
[scrollView setBorderType:CPBezelBorder];
[scrollView layoutIfNeeded];
[self assertTrue:[scrollView theme] === ownTheme message:@"precondition: the scroll view has its own theme"];
[self assertTrue:[CPTheme defaultTheme] === defaultTheme message:@"precondition: the default theme is unchanged"];
[self assertTrue:CGRectEqualToRect([[scrollView contentView] frame], CGRectMake(5, 5, 90, 90))
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.