diff --git a/Demos/FluentUIDemo_macOS/FluentUITestViewControllers/TestButtonViewController.swift b/Demos/FluentUIDemo_macOS/FluentUITestViewControllers/TestButtonViewController.swift index 1c01ef7b76..8a5b09c569 100644 --- a/Demos/FluentUIDemo_macOS/FluentUITestViewControllers/TestButtonViewController.swift +++ b/Demos/FluentUIDemo_macOS/FluentUITestViewControllers/TestButtonViewController.swift @@ -95,6 +95,7 @@ class TestButtonViewController: NSViewController, NSMenuDelegate { let widthPopup = NSPopUpButton(frame: .zero, pullsDown: false) var widthConstraints: [NSLayoutConstraint] = [] let heightPopup = NSPopUpButton(frame: .zero, pullsDown: false) + let capsuleToggle = NSSwitch() var heightConstraints: [NSLayoutConstraint] = [] let scrollView = VibrantScrollView() let materialPane = NSVisualEffectView() @@ -144,13 +145,20 @@ class TestButtonViewController: NSViewController, NSMenuDelegate { heightPopup.action = #selector(TestButtonViewController.heightConstraintsChanged) heightPopup.setAccessibilityLabel(heightPopupLabel) + let capsuleToggleLabel = "Use Capsule Appearance" + capsuleToggle.controlSize = .small + capsuleToggle.target = self + capsuleToggle.action = #selector(TestButtonViewController.capsuleAppearanceChanged) + capsuleToggle.setAccessibilityLabel(capsuleToggleLabel) + let tools = [ [NSTextField(labelWithString: "\(materialPopupLabel):"), materialsPopup], [NSTextField(labelWithString: "\(backgroundColorsPopupLabel):"), backgroundColorsPopup], [NSTextField(labelWithString: "\(imagePositionsPopupLabel):"), imagePositionsPopup], [NSTextField(labelWithString: "\(buttonStatesPopupLabel):"), buttonStatesPopup], [NSTextField(labelWithString: "\(widthPopupLabel):"), widthPopup], - [NSTextField(labelWithString: "\(heightPopupLabel):"), heightPopup] + [NSTextField(labelWithString: "\(heightPopupLabel):"), heightPopup], + [NSTextField(labelWithString: "\(capsuleToggleLabel):"), capsuleToggle] ] let toolsGrid = NSGridView(views: tools) @@ -431,6 +439,13 @@ class TestButtonViewController: NSViewController, NSMenuDelegate { NSLayoutConstraint.activate(heightConstraints) } + @objc func capsuleAppearanceChanged() { + let capsuleAppearanceEnabled = capsuleToggle.state == .on ? true : false + for button in fluentButtons { + button.usesCapsuleAppearance = capsuleAppearanceEnabled + } + } + @objc func buttonPressed() { print("Button pressed") } diff --git a/Sources/FluentUI_macOS/Components/Button/Button.swift b/Sources/FluentUI_macOS/Components/Button/Button.swift index d8d9b97396..8574a7fa77 100644 --- a/Sources/FluentUI_macOS/Components/Button/Button.swift +++ b/Sources/FluentUI_macOS/Components/Button/Button.swift @@ -545,7 +545,15 @@ open class Button: NSButton { } } - private var cornerRadius: CGFloat = ButtonSizeParameters.large.cornerRadius + private var _cornerRadius: CGFloat = ButtonSizeParameters.large.cornerRadius + private var cornerRadius: CGFloat { + get { + usesCapsuleAppearance ? (bounds.size.height / 2.0) - 1.0 : _cornerRadius + } + set { + _cornerRadius = newValue + } + } private static let borderWidth: CGFloat = 1 @@ -579,6 +587,17 @@ open class Button: NSButton { } } + @objc public var usesCapsuleAppearance: Bool = false { + didSet { + guard oldValue != usesCapsuleAppearance else { + return + } + setSizeParameters(forSize: size) + invalidateIntrinsicContentSize() + needsDisplay = true + } + } + open override var intrinsicContentSize: CGSize { let superSize = super.intrinsicContentSize let trailingImageAdjustment: CGFloat