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.

Give a switch row's title its whole width

Țugui Dragoșc49048b628a0
diff --git a/Peel/Views/RowSwitch.swift b/Peel/Views/RowSwitch.swift
index 987e00e4..3540e8f4 100644
--- a/Peel/Views/RowSwitch.swift
+++ b/Peel/Views/RowSwitch.swift
@@ -1,14 +1,27 @@
import SwiftUI
-@available(macOS 27, *)
+/// The switch at the end of a Tweaks or Terminal row, named for VoiceOver by the row's title.
struct RowSwitch: View {
let title: Text
@Binding var isOn: Bool
var body: some View {
- // An empty label rather than a hidden one: SwiftUI then shows VoiceOver AppKit's own switch rather than a
- // stand-in, which leaves AppKit's switch outside the accessibility tree.
- Toggle(isOn: $isOn) { Text(verbatim: "") }
- .accessibilityLabel(title)
+ Group {
+ if #available(macOS 27, *) {
+ // An empty label rather than a hidden one: SwiftUI then shows VoiceOver AppKit's own switch rather
+ // than a stand-in, which leaves AppKit's switch outside the accessibility tree.
+ Toggle(isOn: $isOn) { Text(verbatim: "") }
+ .accessibilityLabel(title)
+ } else {
+ Toggle(isOn: $isOn) { EmptyView() }
+ .labelsHidden()
+ .accessibilityRepresentation {
+ Toggle(isOn: $isOn) { title }
+ }
+ }
+ }
+ // The row's title takes its room first: with a label, even an empty one, a switch asks for far more width than
+ // it draws.
+ .layoutPriority(-1)
}
}
diff --git a/Peel/Views/Terminal/TerminalRows.swift b/Peel/Views/Terminal/TerminalRows.swift
index c6f3defe..10d1de38 100644
--- a/Peel/Views/Terminal/TerminalRows.swift
+++ b/Peel/Views/Terminal/TerminalRows.swift
@@ -32,17 +32,8 @@ struct TerminalSwitchRow: View {
}
}
Spacer(minLength: 8)
- if #available(macOS 27, *) {
- RowSwitch(title: Text(title), isOn: isOn)
- .disabled(isDisabled)
- } else {
- Toggle(isOn: isOn) { EmptyView() }
- .labelsHidden()
- .accessibilityRepresentation {
- Toggle(isOn: isOn) { Text(title) }
- }
- .disabled(isDisabled)
- }
+ RowSwitch(title: Text(title), isOn: isOn)
+ .disabled(isDisabled)
}
.padding(.vertical, 4)
}
diff --git a/Peel/Views/Tweaks/TweakDetailView.swift b/Peel/Views/Tweaks/TweakDetailView.swift
index e39e332c..ae728afb 100644
--- a/Peel/Views/Tweaks/TweakDetailView.swift
+++ b/Peel/Views/Tweaks/TweakDetailView.swift
@@ -145,17 +145,8 @@ struct TweakRow: View {
get: { tweaks.isOn(tweak, state: state) },
set: { tweaks.set(tweak, on: $0) }
)
- if #available(macOS 27, *) {
- RowSwitch(title: Text(tweak.words.title), isOn: isOn)
- .disabled(isHeld || !works)
- } else {
- Toggle(isOn: isOn) { EmptyView() }
- .labelsHidden()
- .accessibilityRepresentation {
- Toggle(isOn: isOn) { Text(tweak.words.title) }
- }
- .disabled(isHeld || !works)
- }
+ RowSwitch(title: Text(tweak.words.title), isOn: isOn)
+ .disabled(isHeld || !works)
case .folder:
// The folder row has no switch to turn off, so Put Back is how the user undoes Peel's change.
// It fades in and out, since it takes room in the row as it comes and goes.
diff --git a/PeelTests/RowSwitchTests.swift b/PeelTests/RowSwitchTests.swift
new file mode 100644
index 00000000..2c71d409
--- /dev/null
+++ b/PeelTests/RowSwitchTests.swift
@@ -0,0 +1,45 @@
+import AppKit
+@testable import Peel
+import SwiftUI
+import Testing
+
+@MainActor
+struct RowSwitchTests {
+ private final class Width {
+ var value: CGFloat = 0
+ }
+
+ private static let title = "A switch whose title runs long, as titles do in German and Romanian"
+
+ @Test func theSwitchAtTheEndOfARowLeavesItsTitleWhole() async {
+ let font = NSFont.systemFont(ofSize: NSFont.systemFontSize, weight: .semibold)
+ let needed = (Self.title as NSString).size(withAttributes: [.font: font]).width.rounded(.up)
+ let width = Width()
+ let form = Form {
+ HStack(spacing: 10) {
+ Text(verbatim: Self.title)
+ .font(.body.weight(.semibold))
+ .lineLimit(1)
+ .minimumScaleFactor(0.8)
+ .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width.value = $0 }
+ Spacer(minLength: 8)
+ RowSwitch(title: Text(verbatim: Self.title), isOn: .constant(false))
+ }
+ }
+ .formStyle(.grouped)
+ .frame(width: needed + 160)
+ let window = NSWindow(
+ contentRect: NSRect(x: 0, y: 0, width: needed + 200, height: 120), styleMask: [.titled],
+ backing: .buffered, defer: true
+ )
+ window.contentView = NSHostingView(rootView: form)
+
+ for _ in 0..<200 where width.value == 0 {
+ window.layoutIfNeeded()
+ try? await Task.sleep(for: .milliseconds(5))
+ }
+
+ #expect(width.value > 0)
+ #expect(width.value + 1 >= needed)
+ }
+}