AppKit: Add border and divider to content rect when computing frame

_CPStandardWindowView insets a frame by its 1-point border and 1-point
divider when converting to a content rect, but it inherited the reverse
conversion without adding those insets back.

Each content-to-frame conversion therefore shrank the content area by 2 points
in width and height: a window created from a content rect received a smaller content view,
and each toolbar update shrank the window further.

_CPStandardWindowView now adds the border and divider when converting a
content rect to a frame, making the two conversions inverses as in
NSWindow.
This commit is contained in:
David Richardson committed 2026-11-30 09:34:53 -06:00
1 parent 545faeb6db
commit f825aa5f7a
2 files changed
+56

No files matched your search

+20
View File
@@ -172,6 +172,26 @@ var _CPStandardWindowViewDividerViewHeight = 1.0;
return contentRect;
}
/*
2026-11-30: a content rect converted to a frame and back lost 2 points in width and
2 in height, so a window made with a content rect had a smaller content view, and each
toolbar change shrank the window again. Cause: this class insets the frame by the border
and the divider above, but did not add them back in the reverse conversion, which it
inherited unchanged. The two conversions are now inverses, as in NSWindow.
*/
+ (CGRect)frameRectForContentRect:(CGRect)aContentRect
{
var frameRect = [super frameRectForContentRect:aContentRect];
frameRect.origin.x -= 1;
frameRect.size.width += 2;
frameRect.size.height += 1;
frameRect.origin.y -= _CPStandardWindowViewDividerViewHeight;
frameRect.size.height += _CPStandardWindowViewDividerViewHeight;
return frameRect;
}
- (id)initWithFrame:(CGRect)aFrame styleMask:(unsigned)aStyleMask
{
self = [super initWithFrame:aFrame styleMask:aStyleMask];
+36
View File
@@ -525,6 +525,42 @@
[self assert:@"Redo Delete" equals:[redoItem title]];
}
/*
A titled window converts between its frame and its content rect through its window
view. The two conversions must be inverses, or every conversion changes the content
size. These tests use a titled window, whose window view draws a border and a divider.
*/
- (void)testATitledWindowGivesItsContentViewTheContentRectSize
{
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(196.0, 540.0, 480.0, 270.0)
styleMask:CPTitledWindowMask | CPClosableWindowMask];
[self assertTrue:CGSizeEqualToSize([[window contentView] frameSize], CGSizeMake(480.0, 270.0))
message:@"content view size " + CPStringFromSize([[window contentView] frameSize]) + " should be {480, 270}"];
}
- (void)testATitledWindowConvertsAContentRectToAFrameAndBack
{
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(196.0, 540.0, 480.0, 270.0)
styleMask:CPTitledWindowMask | CPClosableWindowMask],
contentRect = CGRectMake(10.0, 20.0, 300.0, 200.0),
roundTrip = [window contentRectForFrameRect:[window frameRectForContentRect:contentRect]];
[self assertTrue:CGRectEqualToRect(roundTrip, contentRect)
message:@"round trip " + CPStringFromRect(roundTrip) + " should be " + CPStringFromRect(contentRect)];
}
- (void)testAddingAToolbarToATitledWindowKeepsTheContentSize
{
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(196.0, 540.0, 480.0, 270.0)
styleMask:CPTitledWindowMask | CPClosableWindowMask];
[window setToolbar:[[CPToolbar alloc] initWithIdentifier:@"CPWindowTestToolbar"]];
[self assertTrue:CGSizeEqualToSize([[window contentView] frameSize], CGSizeMake(480.0, 270.0))
message:@"content view size " + CPStringFromSize([[window contentView] frameSize]) + " should be {480, 270}"];
}
@end
/*