Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/OpenSwiftUI/View/Control/Button/AppKitButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ extension NSButton {
idealContentInsets: &content, maxContentInsets: nil
)
#endif
/* OpenSwiftUI Addition Begin */
// AppKit can report no intrinsic width and rectangular content insets for a circular bezel.
// The cell's title rect gives the horizontal inset of the native content view.
if #available(macOS 26.0, *), bezelStyle == .circular,
size.width == NSView.noIntrinsicMetric, size.height > 0 {
size.width = size.height
let bounds = CGRect(origin: .zero, size: size)
let titleRect = Graph.withoutUpdate { cell?.titleRect(forBounds: bounds) } ?? .zero
let inset = titleRect.isEmpty ? content.top : titleRect.minX
content.left = inset
content.right = inset
}
/* OpenSwiftUI Addition End */
}
}
let alignmentInsets = EdgeInsets(top: alignment.top, leading: alignment.left, bottom: alignment.bottom, trailing: alignment.right)
Expand Down
42 changes: 42 additions & 0 deletions Tests/OpenSwiftUITests/View/Control/AppKitButtonTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// AppKitButtonTests.swift
// OpenSwiftUITests

#if os(macOS)
import AppKit
import COpenSwiftUI
import OpenAttributeGraphShims
@testable import OpenSwiftUI
import Testing

@MainActor
@Suite(.disabled(if: attributeGraphVendor == .oag))
struct AppKitButtonTests {
// On macOS 26, AppKit reported no intrinsic artwork width for Game2048's circular
// New Game button, and its bezel collapsed to 1 pt. The rectangular content insets
// also shifted the icon left when the bezel was given its full width.
@Test(arguments: [ControlSize.small, .regular])
func circularButtonCentersItsIcon(controlSize: ControlSize) throws {
let rootView = Button("New Game", systemImage: "arrow.clockwise") {}
.labelStyle(.iconOnly)
.font(.title3.weight(.bold))
.buttonStyle(BorderedButtonStyle_CarMac(tint: nil, isProminent: true))
.buttonBorderShape(.circle)
.controlSize(controlSize)
let host = NSHostingView(rootView: rootView)
for width: CGFloat in [160, 200] {
host.setFrameSize(CGSize(width: width, height: 120))
host._renderForTest(interval: 0)
host.layoutSubtreeIfNeeded()

let button = try #require(host.subviews.compactMap { $0 as? NSButton }.first)
let icon = try #require(button.contentView?.subviews.first)
let iconFrame = icon.convert(icon.bounds, to: button)
#expect(button.bounds.width == button.bounds.height)
#expect(iconFrame.width > 0)
#expect(button.bounds.contains(iconFrame))
#expect(abs(iconFrame.midX - button.bounds.midX) <= 0.5)
}
}
}
#endif
Loading