From 9cfd95df9f50f03a1b14974d3d0f3cbcd6245ca6 Mon Sep 17 00:00:00 2001 From: Rostislav Gorbachenko Date: Tue, 10 Nov 2026 14:40:20 +0100 Subject: [PATCH 1/3] fix(navigation-bar): keep the title clear of the side controls The central slot was overlaid at full width, so a long single-line title truncated at the bar's edges and drew over the back/close or trailing circles. The bar now measures both side slots and insets the central one on both sides by the wider of them, plus the edge padding and a 10pt gap, keeping the title centered. Co-Authored-By: Claude Opus 5.5 --- .../DashUIKit/Components/NavigationBar.swift | 49 ++++++++++++++++++- docs/navigation-and-containers.md | 4 +- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Sources/DashUIKit/Components/NavigationBar.swift b/Sources/DashUIKit/Components/NavigationBar.swift index cd6c6b1..2ea58bb 100644 --- a/Sources/DashUIKit/Components/NavigationBar.swift +++ b/Sources/DashUIKit/Components/NavigationBar.swift @@ -22,6 +22,9 @@ public struct NavigationBar: View private let central: Central private let trailing: Trailing + @State private var leadingWidth: CGFloat = 0 + @State private var trailingWidth: CGFloat = 0 + public init( @ViewBuilder leading: () -> Leading, @ViewBuilder central: () -> Central, @@ -36,16 +39,60 @@ public struct NavigationBar: View ZStack { HStack { leading + .background(NavigationBarWidthReader()) Spacer() trailing + .background(NavigationBarWidthReader()) } - .padding(.horizontal, 20) + .padding(.horizontal, NavigationBarLayout.edgePadding) + // Centered over the full width, and inset by the wider side on both + // sides so a long title truncates before it reaches either control. central + .padding(.horizontal, centralInset) } .frame(maxWidth: .infinity, minHeight: 74) + .onPreferenceChange(NavigationBarLeadingWidthKey.self) { leadingWidth = $0 } + .onPreferenceChange(NavigationBarTrailingWidthKey.self) { trailingWidth = $0 } + } + + private var centralInset: CGFloat { + let side = max(leadingWidth, trailingWidth) + guard side > 0 else { return NavigationBarLayout.edgePadding } + return NavigationBarLayout.edgePadding + side + NavigationBarLayout.minimumSpacing + } +} + +// MARK: - Layout + +private enum NavigationBarLayout { + static let edgePadding: CGFloat = 20 + /// The minimum gap between the title and a control. + static let minimumSpacing: CGFloat = 10 +} + +private struct NavigationBarLeadingWidthKey: PreferenceKey { + static let defaultValue: CGFloat = 0 + static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { + value = max(value, nextValue()) + } +} + +private struct NavigationBarTrailingWidthKey: PreferenceKey { + static let defaultValue: CGFloat = 0 + static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { + value = max(value, nextValue()) + } +} + +/// Reports the laid-out width of the slot it backs. +private struct NavigationBarWidthReader: View where Key.Value == CGFloat { + var body: some View { + GeometryReader { proxy in + Color.clear.preference(key: Key.self, value: proxy.size.width) + } } } diff --git a/docs/navigation-and-containers.md b/docs/navigation-and-containers.md index 7496890..71ac3ba 100644 --- a/docs/navigation-and-containers.md +++ b/docs/navigation-and-containers.md @@ -11,7 +11,9 @@ 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. +regardless of side content). The central slot is inset on both sides by the wider of the +two side slots plus the 20pt edge padding and a 10pt gap, so a long single-line title +(`.lineLimit(1)`) truncates before it reaches either control. Min height 74. ```swift NavigationBar( From 4b4274fdda0789c291146411b6de830f91c29c88 Mon Sep 17 00:00:00 2001 From: Rostislav Gorbachenko Date: Tue, 10 Nov 2026 14:44:21 +0100 Subject: [PATCH 2/3] fix(navigation-bar): keep the title to one line with a tail ellipsis Co-Authored-By: Claude Opus 5.5 --- Sources/DashUIKit/Components/NavigationBar.swift | 7 +++++-- docs/navigation-and-containers.md | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/DashUIKit/Components/NavigationBar.swift b/Sources/DashUIKit/Components/NavigationBar.swift index 2ea58bb..8dd68cf 100644 --- a/Sources/DashUIKit/Components/NavigationBar.swift +++ b/Sources/DashUIKit/Components/NavigationBar.swift @@ -48,9 +48,12 @@ public struct NavigationBar: View } .padding(.horizontal, NavigationBarLayout.edgePadding) - // Centered over the full width, and inset by the wider side on both - // sides so a long title truncates before it reaches either control. + // One line, centered over the full width, and inset by the wider side + // on both sides: a long title ends in an ellipsis before it reaches + // either control. central + .lineLimit(1) + .truncationMode(.tail) .padding(.horizontal, centralInset) } .frame(maxWidth: .infinity, minHeight: 74) diff --git a/docs/navigation-and-containers.md b/docs/navigation-and-containers.md index 71ac3ba..f757da7 100644 --- a/docs/navigation-and-containers.md +++ b/docs/navigation-and-containers.md @@ -11,9 +11,10 @@ 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). The central slot is inset on both sides by the wider of the -two side slots plus the 20pt edge padding and a 10pt gap, so a long single-line title -(`.lineLimit(1)`) truncates before it reaches either control. Min height 74. +regardless of side content). Text in the central slot is always one line: the slot is +inset on both sides by the wider of the two side slots plus the 20pt edge padding and a +10pt gap, and whatever does not fit ends in an ellipsis before it reaches either control. +Min height 74. ```swift NavigationBar( From aca5caf64cc37aec4683d3f5f8263a8c0c056a75 Mon Sep 17 00:00:00 2001 From: Rostislav Gorbachenko Date: Mon, 16 Nov 2026 14:25:17 +0100 Subject: [PATCH 3/3] fix(navigation-bar): lay out the slots in one pass, measure each as a row A small Layout places the three slots, replacing the width preferences: - a slot with several views is measured as one row, not as its widest view; - the title gets its inset in the first layout pass, with no jump once measurements arrive; - side slots wider than the bar leave the title no room instead of pushing it past the bar's edges; - with no side slots the central slot spans the full width again. Adds a long-title snapshot (both sides, back only, two views in one slot, no sides) and preview, and says "one line by default" in the docs. Co-Authored-By: Claude Opus 5.5 --- .../NavigationBarSnapshotTests.swift | 33 +++++ .../DashUIKit/Components/NavigationBar.swift | 135 +++++++++++------- docs/navigation-and-containers.md | 17 ++- 3 files changed, 131 insertions(+), 54 deletions(-) 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 8dd68cf..07fba7a 100644 --- a/Sources/DashUIKit/Components/NavigationBar.swift +++ b/Sources/DashUIKit/Components/NavigationBar.swift @@ -22,9 +22,6 @@ public struct NavigationBar: View private let central: Central private let trailing: Trailing - @State private var leadingWidth: CGFloat = 0 - @State private var trailingWidth: CGFloat = 0 - public init( @ViewBuilder leading: () -> Leading, @ViewBuilder central: () -> Central, @@ -36,66 +33,76 @@ public struct NavigationBar: View } public var body: some View { - ZStack { - HStack { - leading - .background(NavigationBarWidthReader()) - - Spacer() - - trailing - .background(NavigationBarWidthReader()) + // 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) } - .padding(.horizontal, NavigationBarLayout.edgePadding) - - // One line, centered over the full width, and inset by the wider side - // on both sides: a long title ends in an ellipsis before it reaches - // either control. - central - .lineLimit(1) - .truncationMode(.tail) - .padding(.horizontal, centralInset) + HStack { trailing } } - .frame(maxWidth: .infinity, minHeight: 74) - .onPreferenceChange(NavigationBarLeadingWidthKey.self) { leadingWidth = $0 } - .onPreferenceChange(NavigationBarTrailingWidthKey.self) { trailingWidth = $0 } - } - - private var centralInset: CGFloat { - let side = max(leadingWidth, trailingWidth) - guard side > 0 else { return NavigationBarLayout.edgePadding } - return NavigationBarLayout.edgePadding + side + NavigationBarLayout.minimumSpacing } } // MARK: - Layout -private enum NavigationBarLayout { +/// 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 + + 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) -private struct NavigationBarLeadingWidthKey: PreferenceKey { - static let defaultValue: CGFloat = 0 - static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { - value = max(value, nextValue()) + 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 struct NavigationBarTrailingWidthKey: PreferenceKey { - static let defaultValue: CGFloat = 0 - static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { - value = max(value, nextValue()) + 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 } -} -/// Reports the laid-out width of the slot it backs. -private struct NavigationBarWidthReader: View where Key.Value == CGFloat { - var body: some View { - GeometryReader { proxy in - Color.clear.preference(key: Key.self, value: proxy.size.width) - } + /// 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)) } } @@ -287,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 f757da7..2b3c6ae 100644 --- a/docs/navigation-and-containers.md +++ b/docs/navigation-and-containers.md @@ -9,12 +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). Text in the central slot is always one line: the slot is -inset on both sides by the wider of the two side slots plus the 20pt edge padding and a -10pt gap, and whatever does not fit ends in an ellipsis before it reaches either control. -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(