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 a Dock icon back beside the icons it sat between, not at its old index

Țugui Dragoș0345434c88bb
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift b/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
index 0abe391e..10c36342 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/DockTiles.swift
@@ -43,11 +43,14 @@ public struct DockTiles: Sendable {
let list: DockList
let index: Int
let tile: Data
+ /// The paths of every tile in its list when it was taken out, nil for one with no path, such as a spacer.
+ let around: [String?]?
- init(list: DockList, index: Int, tile: Data) {
+ init(list: DockList, index: Int, tile: Data, around: [String?]) {
self.list = list
self.index = index
self.tile = tile
+ self.around = around
}
/// A tile remembered before Peel took out recent ones names no list: it came from the persistent one.
@@ -56,6 +59,42 @@ public struct DockTiles: Sendable {
list = try container.decodeIfPresent(DockList.self, forKey: .list) ?? .persistent
index = try container.decode(Int.self, forKey: .index)
tile = try container.decode(Data.self, forKey: .tile)
+ around = try container.decodeIfPresent([String?].self, forKey: .around)
+ }
+
+ /// Where the tile goes back among tiles leading to `paths`: right after the nearest tile that was before it and
+ /// is still there, past the spacers that were between them, else right before the nearest that was after it,
+ /// else at its old index.
+ func place(among paths: [String?]) -> Int {
+ guard let around, around.indices.contains(index) else { return min(index, paths.count) }
+ var spacers = 0
+ for path in around[..<index].reversed() {
+ guard let path else {
+ spacers += 1
+ continue
+ }
+ guard var place = paths.firstIndex(of: path) else { continue }
+ place += 1
+ while spacers > 0, place < paths.count, paths[place] == nil {
+ place += 1
+ spacers -= 1
+ }
+ return place
+ }
+ spacers = 0
+ for path in around[(index + 1)...] {
+ guard let path else {
+ spacers += 1
+ continue
+ }
+ guard var place = paths.firstIndex(of: path) else { continue }
+ while spacers > 0, place > 0, paths[place - 1] == nil {
+ place -= 1
+ spacers -= 1
+ }
+ return place
+ }
+ return min(index, paths.count)
}
}
@@ -84,12 +123,13 @@ public struct DockTiles: Sendable {
let lists = Dictionary(uniqueKeysWithValues: DockList.allCases.compactMap { list in
store.tiles(in: list).map { (list, $0) }
})
+ let paths = lists.mapValues { $0.map(Self.path(of:)) }
var removed: [String: [Removed]] = [:]
for app in apps {
let found = lists.flatMap { list, tiles in indices(of: app, in: tiles).map { (list, tiles[$0], $0) } }
let kept = found.compactMap { list, tile, index in
(try? PropertyListSerialization.data(fromPropertyList: tile, format: .binary, options: 0))
- .map { Removed(list: list, index: index, tile: $0) }
+ .map { Removed(list: list, index: index, tile: $0, around: paths[list] ?? []) }
}
if !found.isEmpty, kept.count == found.count { removed[key(app)] = kept }
}
@@ -117,8 +157,8 @@ public struct DockTiles: Sendable {
}
/// 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.
+ /// and restarts the Dock once. A tile goes back beside the tiles it sat between, so apps taken out together come
+ /// back where they were, together or one at a time in any order, even after the person added other tiles.
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) } }
@@ -131,7 +171,7 @@ public struct DockTiles: Sendable {
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))
+ tiles.insert(tile, at: entry.place(among: tiles.map(Self.path(of:))))
}
}
if !entries.isEmpty {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
index a230cb0b..246296c1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DockTilesTests.swift
@@ -37,12 +37,26 @@ struct DockTilesTests {
func paths(in list: DockList = .persistent) -> [String] {
(tiles(in: list) ?? []).compactMap { DockTiles.path(of: $0) }
}
+
+ func layout(in list: DockList = .persistent) -> [String?] {
+ (tiles(in: list) ?? []).map { DockTiles.path(of: $0) }
+ }
+
+ func add(_ tile: Any, at index: Int = 0, in list: DockList = .persistent) {
+ var tiles = tiles(in: list) ?? []
+ tiles.insert(tile, at: index)
+ _ = setTiles(tiles, in: list)
+ }
}
private static func tile(_ url: String, type: Int = 15) -> [String: Any] {
["tile-data": ["file-data": ["_CFURLString": url, "_CFURLStringType": type]], "tile-type": "file-tile"]
}
+ private static func spacer() -> [String: Any] {
+ ["tile-data": [String: Any](), "tile-type": "spacer-tile"]
+ }
+
private let studio = URL(filePath: "/Applications/org.example.Studio.app", directoryHint: .isDirectory)
/// The Dock keeps a gone app's tile as a question mark. Peel takes out only the tiles of that copy, and History
@@ -132,6 +146,64 @@ struct DockTilesTests {
#expect(store.restarts.load(ordering: .relaxed) == 2)
}
+ @Test func appsTakenOutTogetherComeBackInTheirPlacesOneAtATime() 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.Tools.app/"),
+ Self.tile("file:///Applications/org.example.Notes.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])
+ await tiles.putBack([studio])
+
+ #expect(store.paths() == original)
+ }
+
+ @Test func aTileThePersonAddedBeforeItMeanwhileMovesNothing() async throws {
+ let directory = try TemporaryDirectory()
+ 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/"),
+ ])
+ let tiles = DockTiles(store: store, memory: directory.url.appending(path: "dock-tiles.json"))
+
+ #expect(await tiles.takeOut([studio]))
+ store.add(Self.tile("file:///System/Applications/Music.app/"))
+ await tiles.putBack([studio])
+
+ #expect(store.paths() == [
+ "/System/Applications/Music.app", "/System/Applications/Mail.app",
+ "/Applications/org.example.Studio.app", "/Applications/org.example.Notes.app",
+ ])
+ }
+
+ @Test func aTileGoesBackOnItsSideOfASpacer() async throws {
+ for spacerFirst in [true, false] {
+ let directory = try TemporaryDirectory()
+ let studioTile = Self.tile("file:///Applications/org.example.Studio.app/")
+ let store = Store(
+ [Self.tile("file:///System/Applications/Mail.app/")]
+ + (spacerFirst ? [Self.spacer(), studioTile] : [studioTile, Self.spacer()])
+ + [Self.tile("file:///Applications/org.example.Notes.app/")]
+ )
+ let tiles = DockTiles(store: store, memory: directory.url.appending(path: "dock-tiles.json"))
+ let original = store.layout()
+
+ #expect(await tiles.takeOut([studio]))
+ store.add(Self.tile("file:///System/Applications/Music.app/"))
+ await tiles.putBack([studio])
+
+ #expect(store.layout() == ["/System/Applications/Music.app"] + original, "spacer first: \(spacerFirst)")
+ }
+ }
+
@Test func leavesTheDockAsItIsForAnAppWithNoTile() async throws {
let directory = try TemporaryDirectory()
let store = Store([Self.tile("file:///Applications/org.example.Notes.app/")])