Skip to content

Commit

Permalink
Add the ability to set a minimum label width on containers (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyarnold authored Sep 2, 2021
1 parent ac4c171 commit 5cef6c8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/Preferences/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension Preferences {
public struct Container: View {
private let sectionBuilder: () -> [Section]
private let contentWidth: Double
private let minimumLabelWidth: Double
@State private var maxLabelWidth: CGFloat = 0.0

/**
Expand All @@ -27,14 +28,17 @@ extension Preferences {
- Parameters:
- contentWidth: A fixed width of the container's content (excluding paddings).
- minimumLabelWidth: A minimum width for labels within this container. By default, it will fit to the largest label.
- builder: A view builder that creates `Preferences.Section`'s of this container.
*/
public init(
contentWidth: Double,
minimumLabelWidth: Double = 0,
@SectionBuilder builder: @escaping () -> [Section]
) {
self.sectionBuilder = builder
self.contentWidth = contentWidth
self.minimumLabelWidth = minimumLabelWidth
}

public var body: some View {
Expand All @@ -58,7 +62,7 @@ extension Preferences {
Divider()
// Strangely doesn't work without width being specified. Probably because of custom alignment.
.frame(width: CGFloat(contentWidth), height: 20.0)
.alignmentGuide(.preferenceSectionLabel) { $0[.leading] + maxLabelWidth }
.alignmentGuide(.preferenceSectionLabel) { $0[.leading] + max(minimumLabelWidth, maxLabelWidth) }
}
}
}
Expand Down

0 comments on commit 5cef6c8

Please sign in to comment.