CocoaExtension // Allow overriding stack spacing.

This commit is contained in:
ShikiSuen 2024-02-14 22:36:54 +08:00
parent 169902db19
commit 5090c7e6d4
2 changed files with 7 additions and 3 deletions

View File

@ -148,6 +148,7 @@ public extension NSStackView {
static func buildSection( static func buildSection(
_ orientation: NSUserInterfaceLayoutOrientation = .vertical, _ orientation: NSUserInterfaceLayoutOrientation = .vertical,
spacing: CGFloat? = nil,
width: CGFloat? = nil, width: CGFloat? = nil,
withDividers: Bool = true, withDividers: Bool = true,
@ArrayBuilder<NSView?> views: () -> [NSView?] @ArrayBuilder<NSView?> views: () -> [NSView?]
@ -166,7 +167,7 @@ public extension NSStackView {
itemWidth = (width - splitterDelta) / CGFloat(viewsRendered.count) - 6 itemWidth = (width - splitterDelta) / CGFloat(viewsRendered.count) - 6
} }
func giveViews() -> [NSView?] { viewsRendered } func giveViews() -> [NSView?] { viewsRendered }
let result = build(orientation, divider: withDividers, width: itemWidth, views: giveViews)? let result = build(orientation, divider: withDividers, spacing: spacing, width: itemWidth, views: giveViews)?
.withInsets(.new(all: 4)) .withInsets(.new(all: 4))
return result return result
} }
@ -174,6 +175,7 @@ public extension NSStackView {
static func build( static func build(
_ orientation: NSUserInterfaceLayoutOrientation, _ orientation: NSUserInterfaceLayoutOrientation,
divider: Bool = false, divider: Bool = false,
spacing: CGFloat? = nil,
width: CGFloat? = nil, width: CGFloat? = nil,
height: CGFloat? = nil, height: CGFloat? = nil,
insets: NSEdgeInsets? = nil, insets: NSEdgeInsets? = nil,
@ -185,7 +187,7 @@ public extension NSStackView {
.makeSimpleConstraint(.height, relation: .equal, value: height) .makeSimpleConstraint(.height, relation: .equal, value: height)
} }
guard !result.isEmpty else { return nil } guard !result.isEmpty else { return nil }
return result.stack(orientation, divider: divider)?.withInsets(insets) return result.stack(orientation, divider: divider, spacing: spacing)?.withInsets(insets)
} }
func withInsets(_ newValue: NSEdgeInsets?) -> NSStackView { func withInsets(_ newValue: NSEdgeInsets?) -> NSStackView {
@ -198,6 +200,7 @@ public extension Array where Element == NSView {
func stack( func stack(
_ orientation: NSUserInterfaceLayoutOrientation, _ orientation: NSUserInterfaceLayoutOrientation,
divider: Bool = false, divider: Bool = false,
spacing: CGFloat? = nil,
insets: NSEdgeInsets? = nil insets: NSEdgeInsets? = nil
) -> NSStackView? { ) -> NSStackView? {
guard !isEmpty else { return nil } guard !isEmpty else { return nil }
@ -212,6 +215,7 @@ public extension Array where Element == NSView {
if #unavailable(macOS 10.10) { if #unavailable(macOS 10.10) {
outerStack.spacing = Swift.max(1, outerStack.spacing) - 1 outerStack.spacing = Swift.max(1, outerStack.spacing) - 1
} }
outerStack.spacing = spacing ?? outerStack.spacing
outerStack.setHuggingPriority(.fittingSizeCompression, for: .horizontal) outerStack.setHuggingPriority(.fittingSizeCompression, for: .horizontal)
outerStack.setHuggingPriority(.fittingSizeCompression, for: .vertical) outerStack.setHuggingPriority(.fittingSizeCompression, for: .vertical)

View File

@ -65,7 +65,7 @@ public extension SettingsPanesCocoa {
NSStackView.build(.vertical, insets: .new(all: 14)) { NSStackView.build(.vertical, insets: .new(all: 14)) {
NSStackView.buildSection(width: contentWidth) { NSStackView.buildSection(width: contentWidth) {
NSStackView.build(.vertical) { NSStackView.build(.vertical) {
NSStackView.build(.horizontal) { NSStackView.build(.horizontal, spacing: 4) {
cmbPEInputModeMenu cmbPEInputModeMenu
cmbPEDataTypeMenu cmbPEDataTypeMenu
NSView() NSView()