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 the sidebar's labels as a chosen row draws them on macOS 27

Țugui Dragoș6ac6a0b20f37
diff --git a/Peel/Navigation/ToolSidebar.swift b/Peel/Navigation/ToolSidebar.swift
index 07649d24..290d5039 100644
--- a/Peel/Navigation/ToolSidebar.swift
+++ b/Peel/Navigation/ToolSidebar.swift
@@ -218,6 +218,16 @@ struct ToolSidebar: View {
.fontWeight(.semibold)
}
+ /// A label's words as a chosen row draws them, semibold on macOS 27. The ruler measures every label so, which keeps
+ /// the chosen row's name whole and the column's width the same whichever row is chosen.
+ static func asChosen(_ words: Text) -> Text {
+ if #available(macOS 27, *) {
+ words.fontWeight(.semibold)
+ } else {
+ words
+ }
+ }
+
private func row(for tool: Tool) -> some View {
HStack(spacing: 0) {
Label {
@@ -247,14 +257,14 @@ struct ToolSidebar: View {
private var ruler: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(spacing: 8) {
- Label { Text(Tool.home.title) } icon: { icon(Tool.home.systemImage) }
+ Label { Self.asChosen(Text(Tool.home.title)) } icon: { icon(Tool.home.systemImage) }
// Stands in for the attention badge that can appear beside Home.
Color.clear.frame(width: 16, height: 1)
}
ForEach(shown(Tool.allCases.filter { $0 != .home })) { tool in
- Label { Text(tool.title) } icon: { icon(tool.systemImage) }
+ Label { Self.asChosen(Text(tool.title)) } icon: { icon(tool.systemImage) }
}
- Label { Text("Settings") } icon: { icon("gearshape") }
+ Label { Self.asChosen(Text("Settings")) } icon: { icon("gearshape") }
// The head's version line and the room the face and the icon take around it, so the head fits too.
HStack(spacing: 0) {
versionLine.font(.caption)
diff --git a/PeelTests/SidebarRulerTests.swift b/PeelTests/SidebarRulerTests.swift
new file mode 100644
index 00000000..4a741510
--- /dev/null
+++ b/PeelTests/SidebarRulerTests.swift
@@ -0,0 +1,52 @@
+import AppKit
+@testable import Peel
+import SwiftUI
+import Testing
+
+@MainActor
+struct SidebarRulerTests {
+ private final class Widths {
+ var chosen: CGFloat = 0
+ var measured: CGFloat = 0
+ }
+
+ @Test func theRulerMeasuresALabelAsWideAsAChosenRowDrawsIt() async {
+ let name = "Elementi in background"
+ let widths = Widths()
+ let sidebar = List(selection: .constant("chosen")) {
+ Label {
+ Text(verbatim: name)
+ } icon: {
+ Image(systemName: "gearshape.2")
+ }
+ .background {
+ ToolSidebar.asChosen(Text(verbatim: name))
+ .fixedSize()
+ .hidden()
+ .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { widths.measured = $0 }
+ }
+ .tag("other")
+ Label {
+ Text(verbatim: name)
+ .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { widths.chosen = $0 }
+ } icon: {
+ Image(systemName: "gearshape.2")
+ }
+ .tag("chosen")
+ }
+ .listStyle(.sidebar)
+ let window = NSWindow(
+ contentRect: NSRect(x: 0, y: 0, width: 600, height: 300), styleMask: [.titled],
+ backing: .buffered, defer: true
+ )
+ window.contentView = NSHostingView(rootView: sidebar)
+
+ for _ in 0..<200 where widths.chosen == 0 || widths.measured == 0 {
+ window.layoutIfNeeded()
+ try? await Task.sleep(for: .milliseconds(5))
+ }
+
+ #expect(widths.chosen > 0)
+ #expect(widths.measured >= widths.chosen)
+ }
+}