From 21932166de1689cc86fb35b4d01836b7f75818e3 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 6 Mar 2023 08:52:24 +0800 Subject: [PATCH] SSPreferences // Add certain customized settings back. --- .../Sources/SSPreferences/Container.swift | 26 +++++++++-------- .../Sources/SSPreferences/Pane.swift | 5 ++-- .../Sources/SSPreferences/Section.swift | 28 +++++++++++++++++-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Container.swift b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Container.swift index a0a8a2ed..c2eb34fe 100644 --- a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Container.swift +++ b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Container.swift @@ -50,7 +50,20 @@ public extension Settings { return VStack(alignment: .settingsSectionLabel) { ForEach(0 ..< sections.count, id: \.self) { index in - viewForSection(sections, index: index) + let labelWidth = max(minimumLabelWidth, maximumLabelWidth) + if sections[index].label != nil { + sections[index] + .frame(width: contentWidth, alignment: .leading) + } else { + sections[index].content + .alignmentGuide(.settingsSectionLabel) { $0[.leading] + labelWidth } + .frame(width: contentWidth, alignment: .leading) + } + if sections[index].bottomDivider, index < sections.count - 1 { + Divider() + .frame(height: 10) + .alignmentGuide(.settingsSectionLabel) { $0[.leading] + labelWidth } + } } } .modifier(Section.LabelWidthModifier(maximumWidth: $maximumLabelWidth)) @@ -58,17 +71,6 @@ public extension Settings { .padding(.vertical, 20) .padding(.horizontal, 30) } - - @ViewBuilder - private func viewForSection(_ sections: [Section], index: Int) -> some View { - sections[index] - if index != sections.count - 1, sections[index].bottomDivider { - Divider() - // Strangely doesn't work without width being specified. Probably because of custom alignment. - .frame(width: contentWidth, height: 20) - .alignmentGuide(.settingsSectionLabel) { $0[.leading] + max(minimumLabelWidth, maximumLabelWidth) } - } - } } } diff --git a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Pane.swift b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Pane.swift index eb943ba1..c39d7c34 100644 --- a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Pane.swift +++ b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Pane.swift @@ -90,8 +90,9 @@ public extension View { /** Applies font and color for a label used for describing a setting. */ - func preferenceDescription() -> some View { - font(.system(size: 11.0)) + func preferenceDescription(maxWidth: CGFloat? = nil) -> some View { + controlSize(.small) + .frame(maxWidth: maxWidth, alignment: .leading) // TODO: Use `.foregroundStyle` when targeting macOS 12. .foregroundColor(.secondary) } diff --git a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Section.swift b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Section.swift index c71f2a97..35df7aab 100644 --- a/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Section.swift +++ b/Packages/Sindresorhus_SSPreferences/Sources/SSPreferences/Section.swift @@ -54,7 +54,7 @@ public extension Settings { } } - public let label: AnyView + public private(set) var label: AnyView? public let content: AnyView public let bottomDivider: Bool public let verticalAlignment: VerticalAlignment @@ -83,6 +83,27 @@ public extension Settings { self.content = stack.eraseToAnyView() } + /** + A section is responsible for controlling a single setting without title label. + + - Parameters: + - bottomDivider: Whether to place a `Divider` after the section content. Default is `false`. + - verticalAlignement: The vertical alignment of the section content. + - label: A view describing the setting handled by this section. + - content: A content view. + */ + public init( + bottomDivider: Bool = false, + verticalAlignment: VerticalAlignment = .firstTextBaseline, + @ViewBuilder content: @escaping () -> some View + ) { + label = nil + self.bottomDivider = bottomDivider + self.verticalAlignment = verticalAlignment + let stack = VStack(alignment: .leading) { content() } + self.content = stack.eraseToAnyView() + } + /** Creates instance of section, responsible for controling a single setting with `Text` as a `Label`. @@ -115,8 +136,9 @@ public extension Settings { public var body: some View { HStack(alignment: verticalAlignment) { - label - .alignmentGuide(.settingsSectionLabel) { $0[.trailing] } + if let label = label { + label.alignmentGuide(.settingsSectionLabel) { $0[.trailing] } + } content Spacer() }