diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/NavigationBarSnapshotTests.swift b/SnapshotTests/Tests/DashUIKitSnapshotTests/NavigationBarSnapshotTests.swift index e249a0c..ea32054 100644 --- a/SnapshotTests/Tests/DashUIKitSnapshotTests/NavigationBarSnapshotTests.swift +++ b/SnapshotTests/Tests/DashUIKitSnapshotTests/NavigationBarSnapshotTests.swift @@ -46,5 +46,38 @@ final class NavigationBarSnapshotTests: XCTestCase { } try assertComponentSnapshots(of: view, named: "variants") } + + private func longTitle() -> some View { + Text("A title far too long to fit between the controls") + .dashFont(.subheadMedium) + .foregroundColor(.dash.primaryText) + } + + /// A long title ends in an ellipsis clear of the wider side: with both + /// sides, with a back button only, with two views in one slot (measured + /// together), and with no sides, where it spans the full width. + func testLongTitles() throws { + let view = VStack(spacing: 12) { + NavigationBar( + leading: { NavigationBarElement.back.button {} }, + central: { longTitle() }, + trailing: { NavigationBarElement.info.button {} } + ) + NavigationBar( + leading: { NavigationBarElement.back.button {} }, + central: { longTitle() } + ) + NavigationBar( + leading: { NavigationBarElement.back.button {} }, + central: { longTitle() }, + trailing: { + NavigationBarCircleButton(.system("arrow.clockwise"), accessibilityLabel: Text("Refresh")) {} + NavigationBarCircleButton(.system("ellipsis"), accessibilityLabel: Text("More")) {} + } + ) + NavigationBar(central: { longTitle() }) + } + try assertComponentSnapshots(of: view, named: "long-titles") + } } #endif diff --git a/Sources/DashUIKit/Components/NavigationBar.swift b/Sources/DashUIKit/Components/NavigationBar.swift index cd6c6b1..07fba7a 100644 --- a/Sources/DashUIKit/Components/NavigationBar.swift +++ b/Sources/DashUIKit/Components/NavigationBar.swift @@ -33,19 +33,76 @@ public struct NavigationBar: View } public var body: some View { - ZStack { - HStack { - leading + // Each slot is exactly one subview, however many views its builder + // returns (none for an omitted slot), so a slot with two buttons is + // measured as both. + NavigationBarSlotsLayout { + HStack { leading } + ZStack { + central + .lineLimit(1) + .truncationMode(.tail) + } + HStack { trailing } + } + } +} - Spacer() +// MARK: - Layout - trailing - } - .padding(.horizontal, 20) +/// Places the leading and trailing slots at the edges and centres the central +/// slot over the full width, inset on both sides by the wider side plus a gap: +/// a long title ends in an ellipsis before it reaches either control. With no +/// side slots, the central slot spans the full width. +private struct NavigationBarSlotsLayout: Layout { + static let edgePadding: CGFloat = 20 + /// The minimum gap between the title and a control. + static let minimumSpacing: CGFloat = 10 + static let minimumHeight: CGFloat = 74 - central - } - .frame(maxWidth: .infinity, minHeight: 74) + func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { + assert(subviews.count == 3, "NavigationBar passes leading, central and trailing") + guard subviews.count == 3 else { return .zero } + + let leading = subviews[0].sizeThatFits(.unspecified) + let trailing = subviews[2].sizeThatFits(.unspecified) + let width = proposal.width ?? { + let central = subviews[1].sizeThatFits(.unspecified).width + return 2 * centralInset(leading: leading.width, trailing: trailing.width) + central + }() + let central = subviews[1].sizeThatFits(ProposedViewSize( + width: centralWidth(in: width, leading: leading.width, trailing: trailing.width), + height: nil)) + let height = max(Self.minimumHeight, leading.height, trailing.height, central.height) + return CGSize(width: width, height: height) + } + + func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { + guard subviews.count == 3 else { return } + + let leading = subviews[0].sizeThatFits(.unspecified) + let trailing = subviews[2].sizeThatFits(.unspecified) + + subviews[0].place(at: CGPoint(x: bounds.minX + Self.edgePadding, y: bounds.midY), + anchor: .leading, proposal: ProposedViewSize(leading)) + subviews[2].place(at: CGPoint(x: bounds.maxX - Self.edgePadding, y: bounds.midY), + anchor: .trailing, proposal: ProposedViewSize(trailing)) + + let width = centralWidth(in: bounds.width, leading: leading.width, trailing: trailing.width) + subviews[1].place(at: CGPoint(x: bounds.midX, y: bounds.midY), + anchor: .center, proposal: ProposedViewSize(width: width, height: bounds.height)) + } + + private func centralInset(leading: CGFloat, trailing: CGFloat) -> CGFloat { + let side = max(leading, trailing) + guard side > 0 else { return 0 } + return Self.edgePadding + side + Self.minimumSpacing + } + + /// Never negative, so side slots wider than the bar leave no room for the + /// central slot instead of pushing it past the bar's edges. + private func centralWidth(in width: CGFloat, leading: CGFloat, trailing: CGFloat) -> CGFloat { + max(0, width - 2 * centralInset(leading: leading, trailing: trailing)) } } @@ -237,3 +294,35 @@ public enum NavigationBarElement: String, CaseIterable { trailing: { NavigationBarElement.close.button { } } ) } + +#Preview("NavigationBar Long Titles") { + let title = Text("A title far too long to fit between the controls") + .dashFont(.subheadMedium) + .foregroundStyle(Color.dash.primaryText) + + VStack { + NavigationBar( + leading: { NavigationBarElement.back.button { } }, + central: { title }, + trailing: { NavigationBarElement.info.button { } } + ) + + NavigationBar( + leading: { NavigationBarElement.back.button { } }, + central: { title } + ) + + NavigationBar( + leading: { NavigationBarElement.back.button { } }, + central: { title }, + trailing: { + NavigationBarCircleButton(.system("arrow.clockwise"), accessibilityLabel: Text("Refresh")) { } + NavigationBarCircleMenu(.system("ellipsis"), accessibilityLabel: Text("More")) { + Button("Action") { } + } + } + ) + + NavigationBar(central: { title }) + } +} diff --git a/docs/navigation-and-containers.md b/docs/navigation-and-containers.md index 7496890..2b3c6ae 100644 --- a/docs/navigation-and-containers.md +++ b/docs/navigation-and-containers.md @@ -9,9 +9,17 @@ Structural chrome — nav bars, bottom sheets, and card styling. File `Components/NavigationBar.swift` A custom three-slot top bar: **leading**, **central**, **trailing**, each a -`@ViewBuilder`. The leading/trailing pair is laid out in an `HStack` with a `Spacer` -between; the central content is overlaid centered (so a long title stays centered -regardless of side content). Min height 74. +`@ViewBuilder`. A small `Layout` places the leading slot at the left edge and the +trailing slot at the right, 20pt in, and centres the central slot over the full width +(so a long title stays centered regardless of side content). Each slot is measured as +one row, so two buttons in one slot count as their combined width. + +The central slot is inset on both sides by the wider side slot plus the 20pt edge +padding and a 10pt gap. Text there is one line by default and ends in an ellipsis +before it reaches either control; a host's own `.lineLimit` still wins, and non-Text +content is only offered the narrower width. With no side slots the central slot spans +the full width, so content that pads itself (a search field, a segmented control) keeps +its own margins. Min height 74. ```swift NavigationBar(