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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down
21 changes: 20 additions & 1 deletion Sources/FluentUI_macOS/Components/Button/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading