Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Measure each part of a section header once

Țugui Dragoș7c171af3075d
diff --git a/Peel/Views/SectionHeaderLine.swift b/Peel/Views/SectionHeaderLine.swift
index a66c01f3..ba1fe427 100644
--- a/Peel/Views/SectionHeaderLine.swift
+++ b/Peel/Views/SectionHeaderLine.swift
@@ -10,21 +10,18 @@ struct SectionHeaderLine<Heading: View, Count: View, Actions: View>: View {
@ViewBuilder let actions: Actions
var body: some View {
- ViewThatFits(in: .horizontal) {
- line(heading) {
- count
- actions
+ // Each part is measured once: on macOS 26 every sheet or alert measures each header again, and a `ViewThatFits`
+ // of whole lines would measure the heading's note and the buttons once per line.
+ SectionHeaderLayout {
+ HStack(spacing: SectionHeaderLayout.spacing) { heading }
+ .lineLimit(2)
+ .fixedSize(horizontal: false, vertical: true)
+ // A count left out is gone, from VoiceOver too, rather than drawn at no width.
+ ViewThatFits(in: .horizontal) {
+ HStack(spacing: SectionHeaderLayout.spacing) { count }
+ Color.clear.frame(width: 0, height: 0)
}
- line(heading) { actions }
- line(heading.lineLimit(2).fixedSize(horizontal: false, vertical: true)) { actions }
- }
- }
-
- private func line(_ heading: some View, @ViewBuilder trailing: () -> some View) -> some View {
- HStack(spacing: 8) {
- heading
- Spacer(minLength: 8)
- trailing()
+ HStack(spacing: SectionHeaderLayout.spacing) { actions }
}
}
}
@@ -34,3 +31,63 @@ extension SectionHeaderLine where Count == EmptyView {
self.init(heading: heading, count: { EmptyView() }, actions: actions)
}
}
+
+/// The heading, then the count and the buttons at the far end, centered on one line, with the room an eight point
+/// spacer leaves between them. The buttons keep their whole width: with too little room the count is left out, and
+/// then the heading wraps.
+private struct SectionHeaderLayout: Layout {
+ static let spacing: CGFloat = 8
+
+ private struct Line {
+ var heading: CGSize
+ var showsCount: Bool
+ }
+
+ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
+ let width = proposal.width ?? lineWidth(
+ heading: subviews[0].sizeThatFits(.unspecified).width, trailing: trailing(subviews, showingCount: true)
+ )
+ let line = line(in: width, subviews: subviews)
+ let count = line.showsCount ? subviews[1].sizeThatFits(.unspecified).height : 0
+ let actions = subviews[2].sizeThatFits(.unspecified).height
+ return CGSize(width: width, height: max(line.heading.height, count, actions))
+ }
+
+ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
+ let line = line(in: bounds.width, subviews: subviews)
+ let count = line.showsCount ? subviews[1].sizeThatFits(.unspecified) : .zero
+ let actions = subviews[2].sizeThatFits(.unspecified)
+ func place(_ index: Int, at x: CGFloat, _ size: CGSize) {
+ subviews[index].place(at: CGPoint(x: x, y: bounds.midY), anchor: .leading, proposal: ProposedViewSize(size))
+ }
+ place(0, at: bounds.minX, line.heading)
+ place(2, at: bounds.maxX - actions.width, actions)
+ place(1, at: bounds.maxX - actions.width - (actions.width > 0 ? Self.spacing : 0) - count.width, count)
+ }
+
+ /// What the line shows in `width`: everything, everything but the count, or the heading wrapped to the room left.
+ private func line(in width: CGFloat, subviews: Subviews) -> Line {
+ let heading = subviews[0].sizeThatFits(.unspecified)
+ if lineWidth(heading: heading.width, trailing: trailing(subviews, showingCount: true)) <= width {
+ return Line(heading: heading, showsCount: true)
+ }
+ let actions = trailing(subviews, showingCount: false)
+ if lineWidth(heading: heading.width, trailing: actions) <= width {
+ return Line(heading: heading, showsCount: false)
+ }
+ let room = max(width - lineWidth(heading: 0, trailing: actions), 0)
+ return Line(heading: subviews[0].sizeThatFits(ProposedViewSize(width: room, height: nil)), showsCount: false)
+ }
+
+ /// A heading followed by what is at the far end, with the spacing on either side of the spacer between them.
+ private func lineWidth(heading: CGFloat, trailing: CGFloat) -> CGFloat {
+ heading + 2 * Self.spacing + (trailing > 0 ? Self.spacing + trailing : 0)
+ }
+
+ /// The count, when shown, and the buttons, with the spacing between them.
+ private func trailing(_ subviews: Subviews, showingCount: Bool) -> CGFloat {
+ let widths = [showingCount ? subviews[1].sizeThatFits(.unspecified).width : 0,
+ subviews[2].sizeThatFits(.unspecified).width].filter { $0 > 0 }
+ return widths.reduce(0, +) + Self.spacing * CGFloat(max(widths.count - 1, 0))
+ }
+}
diff --git a/PeelTests/SectionHeaderLineTests.swift b/PeelTests/SectionHeaderLineTests.swift
new file mode 100644
index 00000000..ca374722
--- /dev/null
+++ b/PeelTests/SectionHeaderLineTests.swift
@@ -0,0 +1,76 @@
+import AppKit
+@testable import Peel
+import SwiftUI
+import Testing
+
+@MainActor
+struct SectionHeaderLineTests {
+ private nonisolated final class Counter {
+ var count = 0
+ }
+
+ /// Counts how often SwiftUI asks the view inside for its size.
+ private struct Counting: Layout {
+ let counter: Counter
+
+ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
+ counter.count += 1
+ return subviews.first?.sizeThatFits(proposal) ?? .zero
+ }
+
+ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
+ subviews.first?.place(at: bounds.origin, proposal: ProposedViewSize(bounds.size))
+ }
+ }
+
+ private struct Measured {
+ let heading = Counter(), buttons = Counter()
+
+ var measuredHeading: some View {
+ Counting(counter: heading) {
+ HStack(spacing: 5) {
+ Text(verbatim: "Review Before Removing")
+ Button(String("i")) {}
+ }
+ }
+ }
+
+ var measuredButtons: some View {
+ Counting(counter: buttons) { Button(String("Select")) {} }
+ }
+
+ func size(of view: some View, width: CGFloat) -> (heading: Int, buttons: Int) {
+ let host = NSHostingController(rootView: view)
+ heading.count = 0
+ buttons.count = 0
+ _ = host.sizeThatFits(in: CGSize(width: width, height: 200))
+ return (heading.count, buttons.count)
+ }
+ }
+
+ @Test(arguments: [CGFloat(600), 160, 40])
+ func measuresEachPartAsOftenAsAPlainRow(width: CGFloat) {
+ let header = Measured(), row = Measured()
+ let inHeader = header.size(
+ of: SectionHeaderLine {
+ header.measuredHeading
+ } count: {
+ Text(verbatim: "8 items")
+ } actions: {
+ header.measuredButtons
+ },
+ width: width
+ )
+ let inRow = row.size(
+ of: HStack(spacing: 8) {
+ row.measuredHeading
+ Spacer(minLength: 8)
+ Text(verbatim: "8 items")
+ row.measuredButtons
+ },
+ width: width
+ )
+ #expect(inHeader.heading <= inRow.heading)
+ #expect(inHeader.buttons <= inRow.buttons)
+ }
+}