Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: no reference for this snapshot. The snapshot job fails here with "No reference was found on disk" for long-titles-light and long-titles-dark. Fetch the references the job recorded with .github/scripts/fetch-snapshots.sh, check them by eye (a tail ellipsis clear of the wider side in the first three bars, full width in the last) and commit them.

}
}
#endif
109 changes: 99 additions & 10 deletions Sources/DashUIKit/Components/NavigationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,76 @@ public struct NavigationBar<Leading: View, Central: View, Trailing: View>: 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))
}
}

Expand Down Expand Up @@ -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 })
}
}
14 changes: 11 additions & 3 deletions docs/navigation-and-containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading