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.

Put back an uninstall's Dock tiles at once, where they were

Țugui Dragoș381297d7e254
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift b/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
index 30c941de..0abe391e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
@@ -116,32 +116,38 @@ public struct DockTiles: Sendable {
return true
}
- /// Puts back the tiles taken out for the app at `app`, each in its own list, unless that list holds one for it
- /// again.
- public func putBack(_ app: URL) async {
- guard let removed = FileLock.whileHeld(beside: memory, { read()?[key(app)] }) else { return }
- var settled: Set<DockList> = []
+ /// Puts back the tiles taken out for `apps`, each in its own list unless that list holds one for its app again,
+ /// and restarts the Dock once. Each list takes its tiles in the order of the places they had, so apps taken out
+ /// together come back as they were, whatever order they are put back in.
+ public func putBack(_ apps: [URL]) async {
+ guard let remembered = FileLock.whileHeld(beside: memory, { read() }) else { return }
+ let removed = Set(apps.map(key)).compactMap { key in remembered[key].map { (key: key, entries: $0) } }
+ var settled: [String: Set<DockList>] = [:]
var changed = false
for list in DockList.allCases {
- let entries = removed.filter { $0.list == list }
- guard !entries.isEmpty, var tiles = store.tiles(in: list) else { continue }
- if indices(of: app, in: tiles).isEmpty {
- for entry in entries.sorted(by: { $0.index < $1.index }) {
- if let tile = try? PropertyListSerialization.propertyList(from: entry.tile, format: nil) {
- tiles.insert(tile, at: min(entry.index, tiles.count))
- }
+ let wanted = removed.filter { $0.entries.contains { $0.list == list } }
+ guard !wanted.isEmpty, var tiles = store.tiles(in: list) else { continue }
+ let missing = wanted.filter { indices(ofKey: $0.key, in: tiles).isEmpty }
+ let entries = missing.flatMap { $0.entries.filter { $0.list == list } }
+ for entry in entries.sorted(by: { $0.index < $1.index }) {
+ if let tile = try? PropertyListSerialization.propertyList(from: entry.tile, format: nil) {
+ tiles.insert(tile, at: min(entry.index, tiles.count))
}
+ }
+ if !entries.isEmpty {
guard store.setTiles(tiles, in: list) else { continue }
changed = true
}
- settled.insert(list)
+ for app in wanted { settled[app.key, default: []].insert(list) }
}
if changed {
await store.restartTheDock()
}
_ = change { entries in
- let left = entries[key(app)]?.filter { !settled.contains($0.list) } ?? []
- entries[key(app)] = left.isEmpty ? nil : left
+ for (key, lists) in settled {
+ let left = entries[key]?.filter { !lists.contains($0.list) } ?? []
+ entries[key] = left.isEmpty ? nil : left
+ }
}
}
@@ -157,7 +163,11 @@ public struct DockTiles: Sendable {
}
private func indices(of app: URL, in tiles: [Any]) -> [Int] {
- tiles.indices.filter { Self.path(of: tiles[$0]) == key(app) }
+ indices(ofKey: key(app), in: tiles)
+ }
+
+ private func indices(ofKey key: String, in tiles: [Any]) -> [Int] {
+ tiles.indices.filter { Self.path(of: tiles[$0]) == key }
}
private func key(_ app: URL) -> String {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index ce019454..9bf2a5a0 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -254,7 +254,7 @@ public struct TrashService: Sendable {
typealias StartJob = @Sendable (LaunchdCleanup.Job, _ canUseHelper: Bool) async -> Void
typealias ForgetDomains = @Sendable ([URL], _ owner: String?) async -> Void
typealias MoveThroughHelper = @Sendable ([URL]) async -> TrashResult
- typealias PutBackDockTiles = @Sendable (URL) async -> Void
+ typealias PutBackDockTiles = @Sendable (_ items: [URL]) async -> Void
typealias RunningInside = @Sendable (_ app: URL) async -> [LaunchdCleanup.Running]
typealias StopRunning = @Sendable ([LaunchdCleanup.Running], _ canUseHelper: Bool) async -> [LaunchdCleanup.Running]
typealias RegistrationAtLogin = @Sendable (_ app: URL) async -> StartsAtLogin.Registration?
@@ -724,15 +724,20 @@ public struct TrashService: Sendable {
}
/// Puts `items` back in the order given, each as `restore(_:canUseHelper:)` does, and answers for each why it
- /// stayed, or nil. Launch Services leaves out an app in the Trash and isn't told it moved back, so the apps among
- /// them are registered again, but only once every item is back: a registered app starts its extensions, which
- /// would make their containers again before the ones in the Trash went back.
+ /// stayed, or nil. The Dock tiles an uninstall took out for them go back then, all at once. Launch Services
+ /// leaves out an app in the Trash and isn't told it moved back, so the apps among them are registered again, but
+ /// only once every item is back: a registered app starts its extensions, which would make their containers again
+ /// before the ones in the Trash went back.
@concurrent
public func restore(_ items: [TrashedItem], canUseHelper: Bool = false) async -> [RestoreFailure?] {
var answers: [RestoreFailure?] = []
for item in items {
answers.append(await restore(item, canUseHelper: canUseHelper))
}
+ let back = zip(items, answers).filter { $0.1 == nil }.map(\.0.originalURL)
+ if !back.isEmpty {
+ await putBackDockTiles(back)
+ }
for (item, answer) in zip(items, answers)
where answer == nil && item.originalURL.pathExtension.caseInsensitiveCompare("app") == .orderedSame {
await registerApp(item.originalURL)
@@ -741,12 +746,11 @@ public struct TrashService: Sendable {
}
/// Moves an item from the Trash back where it came from, through the helper when the folder needs
- /// administrator rights, and puts back the Dock tiles an uninstall took out for it.
+ /// administrator rights.
@concurrent
func restore(_ item: TrashedItem, canUseHelper: Bool = false) async -> RestoreFailure? {
let failure = await moveBack(item, canUseHelper: canUseHelper)
if failure == nil {
- await putBackDockTiles(item.originalURL)
// The label comes from History, which any process of the user can rewrite, so the job starts only as the
// file now back in place declares it.
if let label = item.stoppedJob,
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
index 74ca478d..a230cb0b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
@@ -65,14 +65,14 @@ struct DockTilesTests {
])
#expect(await tiles.holding([studio]).isEmpty)
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths() == [
"/System/Applications/Mail.app", "/Applications/org.example.Studio.app",
"/Volumes/Other/org.example.Studio.app", "/Applications/org.example.Notes.app",
])
#expect(store.restarts.load(ordering: .relaxed) == 2)
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths().count == 4, "a tile was put back twice")
}
@@ -104,21 +104,41 @@ struct DockTilesTests {
#expect(store.paths() == ["/System/Applications/Mail.app", "/Applications/org.example.Notes.app"])
#expect(store.restarts.load(ordering: .relaxed) == 1)
- await tiles.putBack(studio)
- await tiles.putBack(tools)
+ await tiles.putBack([studio])
+ await tiles.putBack([tools])
#expect(store.paths() == [
"/System/Applications/Mail.app", "/Applications/org.example.Studio.app",
"/Applications/org.example.Notes.app", "/Applications/org.example.Tools.app",
])
}
+ @Test func putsBackSeveralAppsTilesInTheirOrderWithOneRestart() async throws {
+ let directory = try TemporaryDirectory()
+ let tools = URL(filePath: "/Applications/org.example.Tools.app", directoryHint: .isDirectory)
+ let store = Store([
+ Self.tile("file:///System/Applications/Mail.app/"),
+ Self.tile("file:///Applications/org.example.Studio.app/"),
+ Self.tile("file:///Applications/org.example.Notes.app/"),
+ Self.tile("file:///Applications/org.example.Tools.app/"),
+ Self.tile("file:///System/Applications/Music.app/"),
+ ])
+ let tiles = DockTiles(store: store, memory: directory.url.appending(path: "dock-tiles.json"))
+ let original = store.paths()
+
+ #expect(await tiles.takeOut([studio, tools]))
+ await tiles.putBack([tools, studio])
+
+ #expect(store.paths() == original)
+ #expect(store.restarts.load(ordering: .relaxed) == 2)
+ }
+
@Test func leavesTheDockAsItIsForAnAppWithNoTile() async throws {
let directory = try TemporaryDirectory()
let store = Store([Self.tile("file:///Applications/org.example.Notes.app/")])
let tiles = DockTiles(store: store, memory: directory.url.appending(path: "dock-tiles.json"))
#expect(!(await tiles.takeOut([studio])))
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths() == ["/Applications/org.example.Notes.app"])
#expect(store.restarts.load(ordering: .relaxed) == 0)
}
@@ -131,7 +151,7 @@ struct DockTilesTests {
#expect(await tiles.takeOut([studio]))
_ = store.setTiles([Self.tile("file:///Applications/org.example.Studio.app/")], in: .persistent)
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths() == ["/Applications/org.example.Studio.app"])
}
@@ -152,7 +172,7 @@ struct DockTilesTests {
#expect(store.paths(in: .recent) == ["/Applications/org.example.Notes.app"])
#expect(store.paths() == ["/System/Applications/Mail.app"])
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths(in: .recent) == [
"/Applications/org.example.Notes.app", "/Applications/org.example.Studio.app",
])
@@ -172,7 +192,7 @@ struct DockTilesTests {
#expect(store.paths(in: .recent).isEmpty)
#expect(store.restarts.load(ordering: .relaxed) == 1)
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths() == ["/Applications/org.example.Studio.app"])
#expect(store.paths(in: .recent) == ["/Applications/org.example.Studio.app"])
#expect(store.restarts.load(ordering: .relaxed) == 2)
@@ -188,7 +208,7 @@ struct DockTilesTests {
try JSONSerialization.data(withJSONObject: [PathPattern.comparablePath(of: studio): [entry]]).write(to: memory)
let store = Store([Self.tile("file:///System/Applications/Mail.app/")], recent: [])
- await DockTiles(store: store, memory: memory).putBack(studio)
+ await DockTiles(store: store, memory: memory).putBack([studio])
#expect(store.paths() == ["/Applications/org.example.Studio.app", "/System/Applications/Mail.app"])
#expect(store.paths(in: .recent).isEmpty)
@@ -209,7 +229,7 @@ struct DockTilesTests {
store.refusing.withLock { $0 = [] }
_ = store.setTiles([], in: .recent)
- await tiles.putBack(studio)
+ await tiles.putBack([studio])
#expect(store.paths() == ["/Applications/org.example.Studio.app"])
#expect(store.paths(in: .recent).isEmpty, "a tile that never left the Dock was put back")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 2e522bb0..2008db13 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -540,7 +540,7 @@ struct TrashServiceTests {
homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
),
- putBackDockTiles: { url in asked.withLock { $0.append(url.lastPathComponent) } }
+ putBackDockTiles: { urls in asked.withLock { $0.append(contentsOf: urls.map(\.lastPathComponent)) } }
) { url in
let destination = trash.appending(path: url.lastPathComponent)
try FileManager.default.moveItem(at: url, to: destination)
@@ -549,8 +549,8 @@ struct TrashServiceTests {
let moved = try #require(await service.trash([app]).trashed.first)
#expect(asked.withLock { $0 }.isEmpty)
- #expect(await service.restore(moved) == nil)
- #expect(await service.restore(moved) != nil)
+ #expect(await service.restore([moved]) == [nil])
+ #expect(await service.restore([moved]) != [nil])
#expect(asked.withLock { $0 } == ["org.example.Studio.app"])
}