AppKit: Size CPTokenField document view to clip view width

-layoutSubviews set the document view's width to the scroll view's width
only when its height changed. Otherwise width came from autoresizing,
which began from a clip view with a negative width because the token
scroll view starts at a zero frame.

When the theme set a content inset on the scroll view,
the document view exceeded the clip view width and could scroll horizontally.

Layout now sets the document view's width to the clip view's width on each layout pass.
This commit is contained in:
David Richardson committed 2026-12-03 09:33:01 -06:00
1 parent 94a7525d0f
commit ae075b03b8
2 files changed
+46 -2

No files matched your search

+11 -2
View File
@@ -1137,10 +1137,19 @@ CPTokenFieldDeleteButtonType = 1;
if (textFieldContentView)
[textFieldContentView setHidden:[self stringValue] !== @""];
/*
The tokens wrap at the visible width of the scroll view, which is its clip
view's width. The document view took the scroll view's width, and only when
its height changed, so where the theme gives the scroll view a content inset
it was wider than the clip view and could scroll sideways.
*/
var frame = [self frame],
contentView = [_tokenScrollView documentView],
visibleWidth = CGRectGetWidth([[_tokenScrollView contentView] bounds]),
tokens = [self _tokens];
[contentView setFrameSize:CGSizeMake(visibleWidth, CGRectGetHeight([contentView bounds]))];
// Hack to make sure we are handling an array
if (![tokens isKindOfClass:[CPArray class]])
return;
@@ -1181,7 +1190,7 @@ CPTokenFieldDeleteButtonType = 1;
// Make sure the frame fits.
var scrollHeight = offset.y + tokenHeight + CEIL(spaceBetweenTokens.height / 2.0);
if (CGRectGetHeight([contentView bounds]) < scrollHeight)
[contentView setFrameSize:CGSizeMake(CGRectGetWidth([_tokenScrollView bounds]), scrollHeight)];
[contentView setFrameSize:CGSizeMake(visibleWidth, scrollHeight)];
offset.x += width + spaceBetweenTokens.width;
@@ -1263,7 +1272,7 @@ CPTokenFieldDeleteButtonType = 1;
// Trim off any excess height downwards (in case we shrank).
var scrollHeight = offset.y + tokenHeight;
if (CGRectGetHeight([contentView bounds]) > scrollHeight)
[contentView setFrameSize:CGSizeMake(CGRectGetWidth([_tokenScrollView bounds]), scrollHeight)];
[contentView setFrameSize:CGSizeMake(visibleWidth, scrollHeight)];
if (_shouldScrollTo !== CPScrollDestinationNone)
{
+35
View File
@@ -1,5 +1,7 @@
@import <AppKit/AppKit.j>
@import "BuiltThemes.j"
@class TestDelegateTokenField
@implementation CPTokenFieldTest : OJTestCase
@@ -57,6 +59,39 @@
}
/*
The tokens wrap at the visible width of the token scroll view. Where the theme gives
a scroll view without a border a content inset, the visible width is the clip
view's width, so the document view must not be wider.
*/
- (void)testTheDocumentViewHasTheVisibleWidthWhereTheScrollViewHasAContentInset
{
var theme = BuiltThemeWithValue(@"Aristo3", CPScrollView, @"content-inset-no-border", CGInsetMake(1.0, 1.0, 1.0, 1.0)),
previousTheme = [CPTheme defaultTheme];
[self assertNotNull:theme message:@"The built blends hold no Aristo3 theme. Build the themes first."];
[CPTheme setDefaultTheme:theme];
try
{
var tokenField = [[CPTokenField alloc] initWithFrame:CGRectMake(10, 10, 100, 28)];
[[_theWindow contentView] addSubview:tokenField];
[tokenField setNeedsLayout];
[tokenField layoutIfNeeded];
var scrollView = tokenField._tokenScrollView,
visibleWidth = CGRectGetWidth([[scrollView contentView] bounds]);
[self assert:CGRectGetWidth([scrollView bounds]) - 2 equals:visibleWidth message:@"The clip view leaves the content inset free"];
[self assert:visibleWidth equals:CGRectGetWidth([[scrollView documentView] frame]) message:@"The document view has the visible width"];
}
finally
{
[CPTheme setDefaultTheme:previousTheme];
}
}
// MARK: -
// MARK: Test objectValue and stringValue