AppKit: Draw window frame border and corner radii in window view

Give window view sole ownership of window shape. Write corner radii at each layout, draw frame border as one ring above subviews, and round content view corners inside border.

* _CPWindowView.j: Add frame-border-color and frame-border-width theme attributes; write corner radii in -layoutSubviews; draw ring above subviews, one border width beyond bounds, so window view clip forms its outer edge; round content view corners concentric with window corners; clear radii from removed content view.

* _CPStandardWindowView.j: Take frame shape of document-modal window in sheet mode.

* CPWindow.j: Remove one-time corner radius code from initializer.

* Aristo3 ThemeDescriptors.j: Remove borders from head and body colours; give standard window view 6px top and 7px bottom radii and 1px frame border.

* CPWindowViewCornerTest.j: Add tests for inner and ring corner radii, corner insets, sheet frame shape and content view clip.
This commit is contained in:
David Richardson committed 2026-11-11 09:54:50 -06:00
1 parent 774f1381d7
commit c397f4b649
5 files changed
+349 -39

No files matched your search

-18
View File
@@ -386,24 +386,6 @@ CPTexturedBackgroundWindowMask
[_windowView _setWindow:self];
[_windowView setNextResponder:self];
// CSS Styling
if (CPDOMAvailable)
{
var radius;
if (radius = [_windowView actualValueForThemeAttribute:@"border-top-left-radius"])
_windowView._DOMElement.style.borderTopLeftRadius = radius;
if (radius = [_windowView actualValueForThemeAttribute:@"border-top-right-radius"])
_windowView._DOMElement.style.borderTopRightRadius = radius;
if (radius = [_windowView actualValueForThemeAttribute:@"border-bottom-left-radius"])
_windowView._DOMElement.style.borderBottomLeftRadius = radius;
if (radius = [_windowView actualValueForThemeAttribute:@"border-bottom-right-radius"])
_windowView._DOMElement.style.borderBottomRightRadius = radius;
}
// Size calculation needs _windowView
_minSize = [self _calculateMinSizeForProposedSize:CGSizeMake(0.0, 0.0)];
_maxSize = CGSizeMake(1000000.0, 1000000.0);
+9
View File
@@ -387,6 +387,15 @@ var _CPStandardWindowViewDividerViewHeight = 1.0;
return [super couldBeMoveEvent:anEvent];
}
/*
A sheet takes the frame shape of a document-modal window, as it takes the body colour of
that class: its top edge lies under the parent window's title bar.
*/
- (Class)_frameClass
{
return _isSheet ? _CPDocModalWindowView : [super _frameClass];
}
- (void)_enableSheet:(BOOL)enable inWindow:(CPWindow)parentWindow
{
[super _enableSheet:enable inWindow:parentWindow];
+217
View File
@@ -29,6 +29,7 @@
@class CPPlatformWindow
@global CPApp
@global document
var _CPWindowViewCornerResizeRectWidth = 10,
_CPWindowViewMinContentHeight = 2,
@@ -45,6 +46,43 @@ var _CPWindowViewCornerResizeRectWidth = 10,
_CPWindowViewResizeSlop = 3;
/*
Returns the CSS radius for the corner of a view that lies inside a window view corner of
radius anOuterRadius, aHorizontalInset and aVerticalInset points from it. Each component is
the outer radius less the inset, never less than zero, so the two curves have one centre.
An empty anOuterRadius gives an empty string, which removes the radius.
*/
function _CPWindowViewInnerCornerRadius(anOuterRadius, aHorizontalInset, aVerticalInset)
{
if (!anOuterRadius)
return "";
return "max(0px, " + anOuterRadius + " - " + aHorizontalInset + "px) max(0px, " + anOuterRadius + " - " + aVerticalInset + "px)";
}
/*
Returns the CSS radius for a corner of the frame border, which extends aWidth points beyond
a window view corner of radius anOuterRadius. The radius is the outer radius plus the width,
so the two curves have one centre.
*/
function _CPWindowViewFrameBorderRadius(anOuterRadius, aWidth)
{
return "calc(" + (anOuterRadius || "0px") + " + " + aWidth + "px)";
}
/*
Writes four corner radii into the style of aDOMElement. Nil radii remove them.
*/
function _CPWindowViewSetCornerRadii(aDOMElement, someRadii)
{
var style = aDOMElement.style;
style.borderTopLeftRadius = someRadii ? someRadii.topLeft : "";
style.borderTopRightRadius = someRadii ? someRadii.topRight : "";
style.borderBottomLeftRadius = someRadii ? someRadii.bottomLeft : "";
style.borderBottomRightRadius = someRadii ? someRadii.bottomRight : "";
}
@implementation _CPWindowView : CPView
{
@@ -64,6 +102,8 @@ _CPWindowViewResizeSlop = 3;
CGRect _cachedScreenFrame;
CPView _sheetShadowView;
CPView _cornerClippedView;
DOMElement _frameBorderElement;
BOOL _isTracking @accessors(getter=_isTracking);
}
@@ -145,6 +185,8 @@ _CPWindowViewResizeSlop = 3;
@"zoom-image-origin": [CPNull null],
@"zoom-image-size": [CPNull null],
@"frame-outset": CGInsetMakeZero(),
@"frame-border-color": [CPNull null],
@"frame-border-width": 0,
@"sheet-vertical-offset": -1
};
}
@@ -975,12 +1017,23 @@ _CPWindowViewResizeSlop = 3;
- (void)didAddSubview:(CPView)aView
{
if (aView === [[self window] contentView])
[self setNeedsLayout];
if (!_resizeIndicator || aView === _resizeIndicator)
return;
[self addSubview:_resizeIndicator];
}
- (void)willRemoveSubview:(CPView)aView
{
[super willRemoveSubview:aView];
if (aView === _cornerClippedView)
[self _setCornerClippedView:nil];
}
- (void)_enableSheet:(BOOL)enable inWindow:(CPWindow)parentWindow
{
if (enable)
@@ -1030,6 +1083,170 @@ _CPWindowViewResizeSlop = 3;
[_resizeIndicator setFrame:CGRectMake(boundsSize.width - size.width - _resizeIndicatorOffset.width, boundsSize.height - size.height - _resizeIndicatorOffset.height, size.width, size.height)];
[_resizeIndicator setImage:[self valueForThemeAttribute:@"resize-indicator"]];
}
[self _updateFrameShape];
}
/*
The window frame had three defects. CPWindow wrote the corner radii only when it made the
window, so a theme change or a new window view kept the old radii. The window border lay in
views behind the content view, and the content view, inside the border with square corners,
covered the border at each rounded corner. When the content corners follow the window
corners, the border and the content meet on one curve, and the browser smooths each edge
separately, so the border shows as a beaded line. The window view now writes its radii at
each layout, gives the content view corners that follow its own, and draws the frame border
itself, above its subviews.
*/
- (void)_updateFrameShape
{
var contentView = [[self window] contentView];
if ([contentView superview] !== self)
contentView = nil;
[self _setCornerClippedView:contentView];
if (!CPDOMAvailable)
return;
_CPWindowViewSetCornerRadii(_DOMElement, [self _cornerRadii]);
if (contentView)
_CPWindowViewSetCornerRadii(contentView._DOMElement, [self _cornerRadiiForSubviewFrame:[contentView frame]]);
[self _updateFrameBorder];
}
/*
Draws the frame border as one ring above the subviews, with the receiver's corner radii.
The ring extends one border width beyond the bounds and is twice the border width, so the
receiver's clip cuts off its outer half. The browser then smooths the outer edge once, at
the clip, and the inner edge once, over the content.
*/
- (void)_updateFrameBorder
{
var width = [self _frameValueForThemeAttribute:@"frame-border-width"],
color = [self _frameValueForThemeAttribute:@"frame-border-color"];
if (!(width > 0) || ![color isKindOfClass:[CPColor class]])
{
if (_frameBorderElement)
{
_DOMElement.removeChild(_frameBorderElement);
_frameBorderElement = nil;
}
return;
}
if (!_frameBorderElement)
{
_frameBorderElement = document.createElement("div");
_frameBorderElement.style.position = "absolute";
_frameBorderElement.style.boxSizing = "border-box";
_frameBorderElement.style.borderStyle = "solid";
_frameBorderElement.style.pointerEvents = "none";
/*
Each view element has z-index 0, so the ring lies above all subviews.
*/
_frameBorderElement.style.zIndex = 1;
_DOMElement.appendChild(_frameBorderElement);
}
var style = _frameBorderElement.style,
radii = [self _cornerRadii],
offset = -width + "px";
style.top = offset;
style.left = offset;
style.right = offset;
style.bottom = offset;
style.borderWidth = (2 * width) + "px";
style.borderColor = [color cssString];
style.borderTopLeftRadius = _CPWindowViewFrameBorderRadius(radii.topLeft, width);
style.borderTopRightRadius = _CPWindowViewFrameBorderRadius(radii.topRight, width);
style.borderBottomLeftRadius = _CPWindowViewFrameBorderRadius(radii.bottomLeft, width);
style.borderBottomRightRadius = _CPWindowViewFrameBorderRadius(radii.bottomRight, width);
}
/*
Returns the class whose theme entries give the frame shape: the corner radii and the frame
border.
*/
- (Class)_frameClass
{
return [self class];
}
/*
Returns the value of a frame attribute for the receiver's theme state, from the theme entry
of the frame class or of its nearest superclass that has one.
*/
- (id)_frameValueForThemeAttribute:(CPString)aName
{
var theme = [self actualTheme],
state = [self themeState];
for (var aClass = [self _frameClass]; aClass && aClass !== CPResponder; aClass = [aClass superclass])
{
var attribute = [theme attributeWithName:aName forClass:aClass];
if (attribute)
return [attribute valueForState:state];
}
return nil;
}
/*
Returns the CSS radii of the receiver's corners, from the theme.
*/
- (JSObject)_cornerRadii
{
return {
topLeft: [self _frameValueForThemeAttribute:@"border-top-left-radius"] || "",
topRight: [self _frameValueForThemeAttribute:@"border-top-right-radius"] || "",
bottomLeft: [self _frameValueForThemeAttribute:@"border-bottom-left-radius"] || "",
bottomRight: [self _frameValueForThemeAttribute:@"border-bottom-right-radius"] || ""
};
}
/*
Returns the CSS radii that make the corners of a subview with frame aFrame follow the
receiver's corners.
*/
- (JSObject)_cornerRadiiForSubviewFrame:(CGRect)aFrame
{
var radii = [self _cornerRadii],
bounds = [self bounds],
left = CGRectGetMinX(aFrame) - CGRectGetMinX(bounds),
top = CGRectGetMinY(aFrame) - CGRectGetMinY(bounds),
right = CGRectGetMaxX(bounds) - CGRectGetMaxX(aFrame),
bottom = CGRectGetMaxY(bounds) - CGRectGetMaxY(aFrame);
return {
topLeft: _CPWindowViewInnerCornerRadius(radii.topLeft, left, top),
topRight: _CPWindowViewInnerCornerRadius(radii.topRight, right, top),
bottomLeft: _CPWindowViewInnerCornerRadius(radii.bottomLeft, left, bottom),
bottomRight: _CPWindowViewInnerCornerRadius(radii.bottomRight, right, bottom)
};
}
/*
Makes aView the view whose corners follow the receiver's corners, and removes the radii
from the view that held them before.
*/
- (void)_setCornerClippedView:(CPView)aView
{
if (_cornerClippedView === aView)
return;
if (_cornerClippedView && CPDOMAvailable)
_CPWindowViewSetCornerRadii(_cornerClippedView._DOMElement, nil);
_cornerClippedView = aView;
}
- (void)_adjustShadowViewSize
+12 -21
View File
@@ -9097,17 +9097,12 @@ function A3MaskIcon(aMaskImage, aColor)
var standardWindowView = [[_CPStandardWindowView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) styleMask:CPClosableWindowMask],
// --- Colors ---
/*
The window view draws the frame border (frame-border-color below), so the head and
body colours give only the fill.
*/
bezelHeadCssColor = [CPColor colorWithCSSDictionary:@{
@"background-color": A3ColorBackgroundHighlighted,
@"border-top-color": A3ColorWindowBorder,
@"border-top-style": @"solid",
@"border-top-width": @"1px",
@"border-left-color": A3ColorWindowBorder,
@"border-left-style": @"solid",
@"border-left-width": @"1px",
@"border-right-color": A3ColorWindowBorder,
@"border-right-style": @"solid",
@"border-right-width": @"1px",
@"border-top-left-radius": @"6px",
@"border-top-right-radius": @"6px",
@"border-bottom-left-radius": @"0px",
@@ -9117,15 +9112,6 @@ function A3MaskIcon(aMaskImage, aColor)
inactiveBezelHeadCssColor = [CPColor colorWithCSSDictionary:@{
@"background-color": A3ColorWindowHeadInactive,
@"border-top-color": A3ColorWindowBorder,
@"border-top-style": @"solid",
@"border-top-width": @"1px",
@"border-left-color": A3ColorWindowBorder,
@"border-left-style": @"solid",
@"border-left-width": @"1px",
@"border-right-color": A3ColorWindowBorder,
@"border-right-style": @"solid",
@"border-right-width": @"1px",
@"border-top-left-radius": @"6px",
@"border-top-right-radius": @"6px",
@"border-bottom-left-radius": @"0px",
@@ -9139,9 +9125,6 @@ function A3MaskIcon(aMaskImage, aColor)
bezelCssColor = [CPColor colorWithCSSDictionary:@{
@"background-color": A3ColorBackground,
@"border-color": A3ColorWindowBorder,
@"border-style": @"solid",
@"border-width": @"1px",
@"border-top-left-radius": @"0px",
@"border-top-right-radius": @"0px",
@"border-bottom-left-radius": @"7px",
@@ -9272,6 +9255,14 @@ function A3MaskIcon(aMaskImage, aColor)
[@"title-margin", 4],
[@"frame-outset", CGInsetMake(1, 1, 1, 1)],
// --- FRAME SHAPE (window view corners, and frame border above the content) ---
[@"border-top-left-radius", @"6px"],
[@"border-top-right-radius", @"6px"],
[@"border-bottom-left-radius", @"7px"],
[@"border-bottom-right-radius", @"7px"],
[@"frame-border-color", [CPColor colorWithCSSString:A3ColorWindowBorder]],
[@"frame-border-width", 1],
// --- REGISTER BUTTON IMAGES ---
// 1. Grey in an inactive window and for a disabled button; coloured in the key and
+111
View File
@@ -0,0 +1,111 @@
/*
* CPWindowViewCornerTest.j
* AppKit
*
* Created by David Richardson on 2026-11-10.
* Copyright David Richardson 2026.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <AppKit/AppKit.j>
@implementation CPWindowViewCornerTest : OJTestCase
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
}
- (CPWindow)window
{
return [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 300.0, 200.0) styleMask:CPTitledWindowMask];
}
- (void)testAnInnerCornerHasTheOuterRadiusLessEachInset
{
[self assert:@"max(0px, 7px - 1px) max(0px, 7px - 2px)" equals:_CPWindowViewInnerCornerRadius(@"7px", 1, 2)];
}
- (void)testAnEmptyOuterRadiusGivesNoInnerRadius
{
[self assert:@"" equals:_CPWindowViewInnerCornerRadius(@"", 1, 1)];
[self assert:@"" equals:_CPWindowViewInnerCornerRadius(nil, 1, 1)];
}
- (void)testAFrameBorderCornerHasTheOuterRadiusPlusTheWidth
{
[self assert:@"calc(7px + 1px)" equals:_CPWindowViewFrameBorderRadius(@"7px", 1)];
[self assert:@"calc(0px + 1px)" equals:_CPWindowViewFrameBorderRadius(@"", 1)];
}
- (void)testASheetTakesTheFrameShapeOfADocumentModalWindow
{
var parent = [self window],
sheet = [self window],
windowView = [sheet _windowView];
[self assert:[windowView class] same:[windowView _frameClass] message:@"A window takes the frame shape of its own window view class"];
[windowView _enableSheet:YES inWindow:parent];
[self assert:_CPDocModalWindowView same:[windowView _frameClass] message:@"A sheet takes the frame shape of a document-modal window"];
[windowView _enableSheet:NO inWindow:parent];
[self assert:[windowView class] same:[windowView _frameClass] message:@"A former sheet takes its own frame shape again"];
}
/*
The subview frame has the insets left 1, top 30, right 2 and bottom 4. Each corner of the
subview takes the insets of its own two sides.
*/
- (void)testEachSubviewCornerTakesTheInsetsOfItsOwnSides
{
var windowView = [[self window] _windowView],
bounds = [windowView bounds],
frame = CGRectMake(1.0, 30.0, CGRectGetWidth(bounds) - 3.0, CGRectGetHeight(bounds) - 34.0),
radii = [windowView _cornerRadii],
subviewRadii = [windowView _cornerRadiiForSubviewFrame:frame];
[self assert:_CPWindowViewInnerCornerRadius(radii.topLeft, 1, 30) equals:subviewRadii.topLeft];
[self assert:_CPWindowViewInnerCornerRadius(radii.topRight, 2, 30) equals:subviewRadii.topRight];
[self assert:_CPWindowViewInnerCornerRadius(radii.bottomLeft, 1, 4) equals:subviewRadii.bottomLeft];
[self assert:_CPWindowViewInnerCornerRadius(radii.bottomRight, 2, 4) equals:subviewRadii.bottomRight];
}
- (void)testTheWindowViewRoundsTheCornersOfTheCurrentContentView
{
var window = [self window],
windowView = [window _windowView],
contentView = [window contentView],
otherView = [[CPView alloc] initWithFrame:CGRectMakeZero()];
[windowView layoutIfNeeded];
[self assert:contentView same:windowView._cornerClippedView message:@"The first layout rounds the content view"];
[window setContentView:otherView];
[self assertNull:windowView._cornerClippedView message:@"The old content view loses its corners when it leaves the window view"];
[self assertTrue:[windowView needsLayout] message:@"A new content view marks the window view for layout"];
[windowView layoutIfNeeded];
[self assert:otherView same:windowView._cornerClippedView message:@"The next layout rounds the new content view"];
}
@end