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.

Lay out the buttons at the foot of a page by one rule

Țugui Dragoș34b3f167b7ae
diff --git a/AGENTS.md b/AGENTS.md
index ab11ed3f..b9cd3dbf 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -386,6 +386,10 @@ Peel runs on macOS 26 and later, and the two versions are treated differently in
accent color read under the 4.5:1 text needs on a light background. The color goes on a symbol beside the words,
which keep the style of the text around them: `StatusLabel` for a warning or a state, a `Badge`'s `tint`, and a
note's `caution`.
+- **The buttons at the foot of a page go in `ActionRow`:** of two, what leads elsewhere stands at the left end and the
+ main action at the right, one alone is centered, and `keepsTogether` holds the ones that act on the item itself in
+ the middle. It gives each button a line of its own when a language's words leave no room, so never lay such a row
+ out by hand. A sheet's footer stays on the right, and buttons under a title stay in a `FlowLayout`.
- Rows of a long list stay cheap to build. A checkbox in a row is `NativeCheckbox` beside the item, with
`checkboxTitleLine()` on the first line of its title and `checkboxTitle()` on the title, rather than a checkbox
`Toggle` whose label is the row; Duplicates' rows keep their `Toggle`, where the AppKit checkbox costs as much. A
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c6d5727..c1c5b2d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -302,6 +302,9 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
search, and under File Search's count, so an entry left behind by an app you no longer use can be found.
- The divider between a list and its page stays where it is and can't be dragged: without the sidebar, the list takes
the room the sidebar covered.
+- The buttons at the foot of a page follow one rule in every language: two stand at the ends of the row, one is
+ centered, and those that act on the item itself stay together in the middle. Where a language's words leave no room
+ on one line, each takes a line of its own rather than having its title cut.
#### `peel`
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/ActionRowPlan.swift b/Packages/PeelCore/Sources/PeelCore/Support/ActionRowPlan.swift
new file mode 100644
index 00000000..143c5b7b
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/ActionRowPlan.swift
@@ -0,0 +1,39 @@
+public import CoreGraphics
+
+/// Where the buttons at the foot of a page go. One is centered, and two stand at the ends of the row, unless they are
+/// kept together in the middle. When a language's words leave no room on one line, each is centered on a line of its
+/// own, so no title is cut.
+public struct ActionRowPlan: Equatable, Sendable {
+ public let frames: [CGRect]
+ public let size: CGSize
+
+ /// - Parameter sizes: Each button's own size. What has no width, a group with nothing to show, takes no room.
+ public init(sizes: [CGSize], width: CGFloat?, keepsTogether: Bool = false, spacing: CGFloat = 8) {
+ let shown = sizes.indices.filter { sizes[$0].width > 0 }
+ let needed = shown.reduce(0) { $0 + sizes[$1].width } + spacing * CGFloat(max(shown.count - 1, 0))
+ let room = width.flatMap { $0.isFinite ? $0 : nil } ?? needed
+ var frames = [CGRect](repeating: .zero, count: sizes.count)
+ var height: CGFloat = 0
+
+ if needed <= room {
+ height = shown.reduce(0) { max($0, sizes[$1].height) }
+ let atTheEnds = shown.count == 2 && !keepsTogether
+ var x = (room - needed) / 2
+ for index in shown {
+ let size = sizes[index]
+ if atTheEnds { x = index == shown[0] ? 0 : room - size.width }
+ frames[index] = CGRect(origin: CGPoint(x: x, y: (height - size.height) / 2), size: size)
+ x += size.width + spacing
+ }
+ } else {
+ for index in shown {
+ let width = min(sizes[index].width, room)
+ frames[index] = CGRect(x: (room - width) / 2, y: height, width: width, height: sizes[index].height)
+ height += sizes[index].height + spacing
+ }
+ height -= spacing
+ }
+ self.frames = frames
+ size = CGSize(width: room, height: height)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ActionRowPlanTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ActionRowPlanTests.swift
new file mode 100644
index 00000000..ef21c538
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ActionRowPlanTests.swift
@@ -0,0 +1,77 @@
+import CoreGraphics
+@testable import PeelCore
+import Testing
+
+struct ActionRowPlanTests {
+ private let small = CGSize(width: 100, height: 28)
+ private let wide = CGSize(width: 300, height: 28)
+
+ @Test func oneActionIsCentered() {
+ let plan = ActionRowPlan(sizes: [small], width: 400)
+ #expect(plan.frames == [CGRect(x: 150, y: 0, width: 100, height: 28)])
+ #expect(plan.size == CGSize(width: 400, height: 28))
+ }
+
+ @Test func twoActionsStandAtTheEndsOfTheRow() {
+ let plan = ActionRowPlan(sizes: [small, small], width: 400)
+ #expect(plan.frames[0] == CGRect(x: 0, y: 0, width: 100, height: 28))
+ #expect(plan.frames[1] == CGRect(x: 300, y: 0, width: 100, height: 28))
+ #expect(plan.size.height == 28)
+ }
+
+ @Test func twoActionsWithoutRoomTakeACenteredLineEach() {
+ let plan = ActionRowPlan(sizes: [wide, small], width: 400, spacing: 8)
+ #expect(plan.frames[0] == CGRect(x: 50, y: 0, width: 300, height: 28))
+ #expect(plan.frames[1] == CGRect(x: 150, y: 36, width: 100, height: 28))
+ #expect(plan.size == CGSize(width: 400, height: 64))
+ }
+
+ @Test func twoActionsThatJustFitStayOnOneLine() {
+ let plan = ActionRowPlan(sizes: [wide, small], width: 408, spacing: 8)
+ #expect(plan.frames[0].minX == 0)
+ #expect(plan.frames[1].maxX == 408)
+ #expect(plan.size.height == 28)
+ }
+
+ @Test func actionsKeptTogetherStayInTheMiddleHoweverManyTheyAre() {
+ let two = ActionRowPlan(sizes: [small, small], width: 400, keepsTogether: true, spacing: 8)
+ #expect(two.frames.map(\.minX) == [96, 204])
+ let three = ActionRowPlan(sizes: [small, small, small], width: 400, keepsTogether: true, spacing: 8)
+ #expect(three.frames.map(\.minX) == [42, 150, 258])
+ }
+
+ @Test func actionsKeptTogetherWithoutRoomTakeACenteredLineEach() {
+ let plan = ActionRowPlan(sizes: [wide, wide], width: 400, keepsTogether: true, spacing: 8)
+ #expect(plan.frames.map(\.minX) == [50, 50])
+ #expect(plan.frames.map(\.minY) == [0, 36])
+ }
+
+ @Test func whatHasNoWidthTakesNoRoom() {
+ let plan = ActionRowPlan(sizes: [.zero, small], width: 400)
+ #expect(plan.frames[1] == CGRect(x: 150, y: 0, width: 100, height: 28))
+ #expect(plan.frames[0].size == .zero)
+ #expect(plan.size.height == 28)
+ }
+
+ @Test func aRowWithNothingInItHasNoHeight() {
+ #expect(ActionRowPlan(sizes: [.zero], width: 400).size.height == 0)
+ #expect(ActionRowPlan(sizes: [], width: 400).size.height == 0)
+ }
+
+ @Test func anActionWiderThanTheRowIsGivenTheWholeRow() {
+ let plan = ActionRowPlan(sizes: [CGSize(width: 500, height: 28)], width: 400)
+ #expect(plan.frames == [CGRect(x: 0, y: 0, width: 400, height: 28)])
+ }
+
+ @Test func aRowOfTallerActionsCentersTheShorterOnTheLine() {
+ let plan = ActionRowPlan(sizes: [CGSize(width: 100, height: 40), small], width: 400)
+ #expect(plan.frames[1].minY == 6)
+ #expect(plan.size.height == 40)
+ }
+
+ @Test func withoutAWidthTheRowIsAsWideAsItsActions() {
+ let plan = ActionRowPlan(sizes: [small, small], width: nil, spacing: 8)
+ #expect(plan.size == CGSize(width: 208, height: 28))
+ #expect(ActionRowPlan(sizes: [small, small], width: .infinity, spacing: 8).size.width == 208)
+ }
+}
diff --git a/Peel/Views/ActionRow.swift b/Peel/Views/ActionRow.swift
new file mode 100644
index 00000000..912f202f
--- /dev/null
+++ b/Peel/Views/ActionRow.swift
@@ -0,0 +1,34 @@
+import PeelCore
+import SwiftUI
+
+/// The buttons at the foot of a page, laid out by `ActionRowPlan`: one is centered, and of two, what leads elsewhere
+/// goes at the left end and the main action at the right. `keepsTogether` is for buttons that all act on one thing.
+struct ActionRow: Layout {
+ var keepsTogether = false
+
+ func makeCache(subviews: Subviews) -> [CGSize] {
+ subviews.map { $0.sizeThatFits(.unspecified) }
+ }
+
+ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout [CGSize]) -> CGSize {
+ plan(subviews, cache, width: proposal.width).size
+ }
+
+ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout [CGSize]) {
+ for (subview, frame) in zip(subviews, plan(subviews, cache, width: bounds.width).frames) {
+ subview.place(
+ at: CGPoint(x: bounds.minX + frame.minX, y: bounds.minY + frame.minY),
+ proposal: ProposedViewSize(frame.size)
+ )
+ }
+ }
+
+ /// What is wider than the row, such as a sentence beside a button, is asked how tall it gets at the row's width.
+ private func plan(_ subviews: Subviews, _ sizes: [CGSize], width: CGFloat?) -> ActionRowPlan {
+ let fitted = zip(subviews, sizes).map { subview, size in
+ guard let width, size.width > width else { return size }
+ return subview.sizeThatFits(ProposedViewSize(width: width, height: nil))
+ }
+ return ActionRowPlan(sizes: fitted, width: width, keepsTogether: keepsTogether)
+ }
+}
diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
index d009e35e..16547ba2 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
@@ -407,22 +407,15 @@ struct BackgroundItemDetailView: View {
private var hasOwnControls: Bool { item.offersControls }
+ /// A row for what acts on the job, and a row for what acts on its file.
+ @ViewBuilder
private var controls: some View {
- let hasFileControls = finderURL != nil || item.canMoveToTrash
- // With controls on one side only, there is no spacer, so the row is only as wide as its buttons and is
- // centered. When the buttons don't fit on one line, `FlowLayout` wraps them onto the next.
- return ViewThatFits(in: .horizontal) {
- HStack {
- if hasOwnControls { ownControls }
- if hasOwnControls, hasFileControls { Spacer() }
- fileControls
- }
- FlowLayout {
- if hasOwnControls { ownControls }
- fileControls
- }
+ if hasOwnControls {
+ ActionRow(keepsTogether: true) { ownControls }
+ }
+ if finderURL != nil || item.canMoveToTrash {
+ ActionRow { fileControls }
}
- .frame(maxWidth: .infinity)
}
private var ownControls: some View {
diff --git a/Peel/Views/CenteredInRow.swift b/Peel/Views/CenteredInRow.swift
index 85900dce..53fe5dad 100644
--- a/Peel/Views/CenteredInRow.swift
+++ b/Peel/Views/CenteredInRow.swift
@@ -3,10 +3,6 @@ import SwiftUI
extension View {
/// A control alone in its box, centered in the row rather than left against one edge.
func centeredInRow() -> some View {
- HStack {
- Spacer(minLength: 0)
- self
- Spacer(minLength: 0)
- }
+ ActionRow { self }
}
}
diff --git a/Peel/Views/FileSearch/FileSearchFileView.swift b/Peel/Views/FileSearch/FileSearchFileView.swift
index 99f336c7..8f4142fc 100644
--- a/Peel/Views/FileSearch/FileSearchFileView.swift
+++ b/Peel/Views/FileSearch/FileSearchFileView.swift
@@ -59,12 +59,10 @@ struct FileSearchFileView: View {
}
Section {
- HStack {
- Spacer()
+ ActionRow {
Button("Show in Finder", systemImage: "folder") {
NSWorkspace.shared.activateFileViewerSelecting([file.url])
}
- Spacer()
}
}
}
diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swift
index 5b8e3478..84761c4a 100644
--- a/Peel/Views/Homebrew/HomebrewDetailView.swift
+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift
@@ -130,51 +130,7 @@ struct HomebrewDetailView: View {
if package.isMissingWhatItInstalled {
RemovalsHeldBanner()
}
- HStack {
- if package.isMissingWhatItInstalled {
- Button("Forget Cask") { casksToForget = [package] }
- .buttonStyle(.borderedProminent)
- .disabled(!history.mayMove)
- .help(Text(verbatim: history.holdingReason?.explanation ?? ""))
- } else if package.waitsForAnUpgrade {
- if package.isPeelItself {
- Button("Check for Updates…", systemImage: "arrow.down.circle") {
- updater.checkForUpdates()
- }
- .buttonStyle(.borderedProminent)
- .disabled(!updater.canCheckForUpdates)
- } else if upgradeRefusal == nil,
- package.upgradeNeedsAnAdministrator || homebrew.overrides.contains(.cleansUp) {
- let kind = package.kind == .cask ? "--cask" : "--formula"
- CopyButton(
- text: Homebrew.terminalCommand(
- "brew upgrade \(kind) \(package.fullName)", keeping: homebrew.keptFormulaeNow
- ),
- title: "Copy Upgrade Command"
- )
- } else {
- Button("Upgrade", systemImage: "arrow.down.circle") {
- isConfirmingUpgrade = true
- }
- .buttonStyle(.borderedProminent)
- // A cask's apps, which have to quit first, are known only once they are matched.
- .disabled(
- package.isPinned || package.kind == .cask && !library.knowsHomebrewsApps
- || upgradeRefusal != nil
- )
- .help(upgradeRefusal.map { Text($0) } ?? Text(verbatim: ""))
- }
- }
- if isRunning {
- ProgressView()
- .controlSize(.small)
- Text("Running Homebrew…")
- .foregroundStyle(.secondary)
- } else if let uninstallRefusal {
- Label { Text(uninstallRefusal) } icon: { Image(systemName: "hand.raised.fill") }
- .foregroundStyle(.secondary)
- }
- Spacer()
+ ActionRow {
if isUninstalledInTerminal {
CopyButton(
text: Homebrew.terminalCommand(
@@ -191,6 +147,7 @@ struct HomebrewDetailView: View {
}
.disabled(uninstallRefusal != nil)
}
+ stateAndUpgrade
}
.disabled(
homebrew.runningCommand != nil || homebrew.isForgetting || homebrew.quittingToUpgrade.isWaiting
@@ -266,6 +223,55 @@ struct HomebrewDetailView: View {
}
}
+ /// The right end of the row: what is under way or in the way, and the step that brings the package up to date.
+ private var stateAndUpgrade: some View {
+ HStack {
+ if isRunning {
+ ProgressView()
+ .controlSize(.small)
+ Text("Running Homebrew…")
+ .foregroundStyle(.secondary)
+ } else if let uninstallRefusal {
+ Label { Text(uninstallRefusal) } icon: { Image(systemName: "hand.raised.fill") }
+ .foregroundStyle(.secondary)
+ }
+ if package.isMissingWhatItInstalled {
+ Button("Forget Cask") { casksToForget = [package] }
+ .buttonStyle(.borderedProminent)
+ .disabled(!history.mayMove)
+ .help(Text(verbatim: history.holdingReason?.explanation ?? ""))
+ } else if package.waitsForAnUpgrade {
+ if package.isPeelItself {
+ Button("Check for Updates…", systemImage: "arrow.down.circle") {
+ updater.checkForUpdates()
+ }
+ .buttonStyle(.borderedProminent)
+ .disabled(!updater.canCheckForUpdates)
+ } else if upgradeRefusal == nil,
+ package.upgradeNeedsAnAdministrator || homebrew.overrides.contains(.cleansUp) {
+ let kind = package.kind == .cask ? "--cask" : "--formula"
+ CopyButton(
+ text: Homebrew.terminalCommand(
+ "brew upgrade \(kind) \(package.fullName)", keeping: homebrew.keptFormulaeNow
+ ),
+ title: "Copy Upgrade Command"
+ )
+ } else {
+ Button("Upgrade", systemImage: "arrow.down.circle") {
+ isConfirmingUpgrade = true
+ }
+ .buttonStyle(.borderedProminent)
+ // A cask's apps, which have to quit first, are known only once they are matched.
+ .disabled(
+ package.isPinned || package.kind == .cask && !library.knowsHomebrewsApps
+ || upgradeRefusal != nil
+ )
+ .help(upgradeRefusal.map { Text($0) } ?? Text(verbatim: ""))
+ }
+ }
+ }
+ }
+
private func readWhatUsesIt() async {
guard package.kind == .formula, let cellar = homebrew.installation?.cellar else {
inUse = nil
diff --git a/Peel/Views/Intel/IntelDetailView.swift b/Peel/Views/Intel/IntelDetailView.swift
index b74a37b4..f0ffedc9 100644
--- a/Peel/Views/Intel/IntelDetailView.swift
+++ b/Peel/Views/Intel/IntelDetailView.swift
@@ -115,15 +115,10 @@ struct IntelDetailView: View {
}
Section {
- HStack {
- // Show in Finder is centered when alone, and goes first when a Review button is beside it.
- if app == nil, !finding.isOnThePlugInsPage, finding.job == nil, receipt == nil {
- Spacer()
- }
+ ActionRow {
Button("Show in Finder", systemImage: "folder") {
NSWorkspace.shared.activateFileViewerSelecting([finding.url])
}
- Spacer()
if let app {
let review = Button("Review in Applications", systemImage: "square.grid.2x2") {
library.selection = [app.id]
diff --git a/Peel/Views/Plugins/PluginDetailView.swift b/Peel/Views/Plugins/PluginDetailView.swift
index 21188941..094773c1 100644
--- a/Peel/Views/Plugins/PluginDetailView.swift
+++ b/Peel/Views/Plugins/PluginDetailView.swift
@@ -83,25 +83,26 @@ struct PluginDetailView: View {
.font(.callout)
.foregroundStyle(.secondary)
}
- HStack {
+ ActionRow {
Button("Show in Finder", systemImage: "folder") {
NSWorkspace.shared.activateFileViewerSelecting([plugin.url])
}
- Spacer()
- BusyShown(isBusy: plugins.isRemoving) { isShown in
- if isShown {
- ProgressView()
- .controlSize(.small)
+ HStack {
+ BusyShown(isBusy: plugins.isRemoving) { isShown in
+ if isShown {
+ ProgressView()
+ .controlSize(.small)
+ }
}
+ Button("Move to Trash", systemImage: "trash") {
+ isConfirmingRemoval = true
+ }
+ .buttonStyle(.borderedProminent)
+ .disabled(
+ isLocked || plugin.refusal != nil || plugins.isRemoving || plugins.isScanning
+ || !history.mayMove
+ )
}
- Button("Move to Trash", systemImage: "trash") {
- isConfirmingRemoval = true
- }
- .buttonStyle(.borderedProminent)
- .disabled(
- isLocked || plugin.refusal != nil || plugins.isRemoving || plugins.isScanning
- || !history.mayMove
- )
}
}
}
diff --git a/Peel/Views/ResetSheet.swift b/Peel/Views/ResetSheet.swift
index da72b984..e5ca4f13 100644
--- a/Peel/Views/ResetSheet.swift
+++ b/Peel/Views/ResetSheet.swift
@@ -320,7 +320,11 @@ struct ResetSheet: View {
.buttonStyle(.bordered)
}
}
- HStack(spacing: 10) {
+ ActionRow {
+ Button("Show in Finder", systemImage: "folder") {
+ NSWorkspace.shared.activateFileViewerSelecting([backup])
+ }
+ .buttonStyle(.bordered)
Button("Put Settings Back") {
isPuttingBack = true
Task {
@@ -335,10 +339,6 @@ struct ResetSheet: View {
}
.buttonStyle(.borderedProminent)
.disabled(isPuttingBack || plan.isAppRunning || plan.putBackRefusal != nil)
- Button("Show in Finder", systemImage: "folder") {
- NSWorkspace.shared.activateFileViewerSelecting([backup])
- }
- .buttonStyle(.bordered)
}
if let refusal = plan.putBackRefusal {
putBackResult(refusal)
diff --git a/Peel/Views/Settings/AppFoldersSection.swift b/Peel/Views/Settings/AppFoldersSection.swift
index 66de817a..9c1d2eae 100644
--- a/Peel/Views/Settings/AppFoldersSection.swift
+++ b/Peel/Views/Settings/AppFoldersSection.swift
@@ -55,11 +55,10 @@ struct AppFoldersSection: View {
if library.couldNotSaveFolders {
StatusLabel(title: Text("Peel couldn’t save the app folders."))
}
- HStack {
+ ActionRow {
Button("Add Folder…", systemImage: "plus") {
isChoosing = true
}
- Spacer()
Button("Remove", systemImage: "minus") { removeSelected() }
.disabled(selected.isEmpty)
}
diff --git a/Peel/Views/Settings/ExclusionsSettingsView.swift b/Peel/Views/Settings/ExclusionsSettingsView.swift
index daadbb1d..788a89c4 100644
--- a/Peel/Views/Settings/ExclusionsSettingsView.swift
+++ b/Peel/Views/Settings/ExclusionsSettingsView.swift
@@ -81,11 +81,10 @@ struct ExclusionsSettingsView: View {
Task { await exclusions.remove(paths: Array(removed)) }
}
}
- HStack {
+ ActionRow {
Button("Add Files or Folders…", systemImage: "plus") {
isChoosingPaths = true
}
- Spacer()
Button("Remove", systemImage: "minus") {
let removed = selectedPaths
selectedPaths = []
@@ -131,7 +130,7 @@ struct ExclusionsSettingsView: View {
Task { await exclusions.remove(bundleIdentifiers: Array(removed)) }
}
}
- HStack {
+ ActionRow {
Menu {
// An app with no identifier is left alone by where it is, as its Control-click menu does.
ForEach(
@@ -155,7 +154,6 @@ struct ExclusionsSettingsView: View {
}
.menuStyle(.button)
.fixedSize()
- Spacer()
Button("Remove", systemImage: "minus") {
let removed = selectedApps
selectedApps = []
@@ -205,8 +203,7 @@ struct ExclusionsSettingsView: View {
} else {
Text("Peel can’t read this list, so Orphaned Files lists these files again. Starting over keeps the old list beside a new one.")
.foregroundStyle(.secondary)
- HStack {
- Spacer()
+ ActionRow {
StartOverButton(
file: OrphanOwners.defaultURL,
startedOver: "Started the list of orphaned files that belong to an app over."
diff --git a/Peel/Views/Settings/HomebrewSection.swift b/Peel/Views/Settings/HomebrewSection.swift
index f1a77772..8f83d49b 100644
--- a/Peel/Views/Settings/HomebrewSection.swift
+++ b/Peel/Views/Settings/HomebrewSection.swift
@@ -42,9 +42,8 @@ struct HomebrewSection: View {
if couldNotSave {
StatusLabel(title: Text("Peel couldn’t save which `brew` to run."))
}
- HStack {
+ ActionRow {
Button("Choose…") { isChoosing = true }
- Spacer()
if choice != nil {
Button("Use the Usual Places") { use(nil) }
}