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.

A check in Duplicates stops reading files on the main thread

Țugui Dragoș9710a423c9eb
diff --git a/Peel/Views/Duplicates/DuplicateGroupView.swift b/Peel/Views/Duplicates/DuplicateGroupView.swift
index 782d9a52..9446154f 100644
--- a/Peel/Views/Duplicates/DuplicateGroupView.swift
+++ b/Peel/Views/Duplicates/DuplicateGroupView.swift
@@ -4,7 +4,7 @@ import SwiftUI
struct DuplicateGroupView: View {
@Environment(DuplicateLibrary.self) private var duplicates
- @State private var previewURL: URL?
+ @State private var preview = QuickLookTarget()
let group: DuplicateGroup
var body: some View {
@@ -17,12 +17,12 @@ struct DuplicateGroupView: View {
ForEach(Array(group.files.enumerated()), id: \.element.id) { index, file in
DuplicateFileRow(
duplicates: duplicates,
+ preview: preview,
file: file,
group: group,
isSelected: duplicates.isSelected(file),
canChangeSelection: duplicates.canChange(file, in: group),
- isFirst: index == 0,
- onPreview: { previewURL = file.url }
+ isFirst: index == 0
)
}
.listRowSeparator(.hidden)
@@ -37,7 +37,7 @@ struct DuplicateGroupView: View {
.fadesInColumn(whenRowsChange: group.files.map(\.id))
.navigationTitle(Text(verbatim: group.files[0].url.lastPathComponent))
.toolbar(removing: .title)
- .quickLookPreview($previewURL, in: group.files.map(\.url))
+ .quickLookPreview(Bindable(preview).url, in: group.files.map(\.url))
}
private var header: some View {
@@ -97,12 +97,14 @@ struct DuplicateGroupView: View {
private struct DuplicateFileRow: View {
let duplicates: DuplicateLibrary
+ /// An object, as `duplicates` is, so the row compares equal from one change of the page to the next: a closure
+ /// made in the page's body never would, and every row would be built again at every check (`RowSelection`).
+ let preview: QuickLookTarget
let file: DuplicateFile
let group: DuplicateGroup
let isSelected: Bool
let canChangeSelection: Bool
var isFirst = false
- let onPreview: () -> Void
var body: some View {
HStack(spacing: 10) {
@@ -134,7 +136,9 @@ private struct DuplicateFileRow: View {
.disabled(!canChangeSelection)
.help(canChangeSelection ? Text(verbatim: file.url.path(percentEncoded: false)) : Text("Peel always keeps at least one copy."))
- Button(action: onPreview) {
+ Button {
+ preview.url = file.url
+ } label: {
Label("Quick Look", systemImage: "eye")
.minimumTarget()
}
@@ -144,12 +148,18 @@ private struct DuplicateFileRow: View {
}
.tableRow(isFirst: isFirst)
.contextMenu {
- Button("Quick Look", systemImage: "eye", action: onPreview)
+ Button("Quick Look", systemImage: "eye") { preview.url = file.url }
ItemMenu(url: file.url)
}
}
}
+/// The copy Quick Look shows, held by the page.
+@Observable
+private final class QuickLookTarget {
+ var url: URL?
+}
+
/// A group's total to free, which follows the copies selected in it.
struct DuplicateSelectedTotal: View {
let bytes: Int64
diff --git a/Peel/Views/Duplicates/DuplicateStatusViews.swift b/Peel/Views/Duplicates/DuplicateStatusViews.swift
index 8d62c444..b3e13dcb 100644
--- a/Peel/Views/Duplicates/DuplicateStatusViews.swift
+++ b/Peel/Views/Duplicates/DuplicateStatusViews.swift
@@ -132,7 +132,6 @@ struct FileThumbnail: View {
init(url: URL) {
self.url = url
- _made = State(initialValue: ThumbnailCache.image(for: url).map { (url, $0) })
}
private var thumbnail: NSImage? {
@@ -166,7 +165,8 @@ struct FileThumbnail: View {
.motion(value: thumbnail == nil)
.task(id: url) {
guard thumbnail == nil else { return }
- if let cached = ThumbnailCache.image(for: url) {
+ let key = await ThumbnailCache.key(for: url)
+ if let key, let cached = ThumbnailCache.image(forKey: key) {
made = (url, cached)
return
}
@@ -181,8 +181,8 @@ struct FileThumbnail: View {
for await image in Self.representations(of: request) {
made = (url, NSImage(cgImage: image, size: CGSize(width: 64, height: 64)))
}
- if let thumbnail {
- ThumbnailCache.keep(thumbnail, for: url)
+ if let thumbnail, let key {
+ ThumbnailCache.keep(thumbnail, forKey: key)
}
}
}
@@ -198,21 +198,22 @@ enum ThumbnailCache {
}()
@MainActor
- static func image(for url: URL) -> NSImage? {
- key(for: url).flatMap(images.object(forKey:))
+ static func image(forKey key: String) -> NSImage? {
+ images.object(forKey: key as NSString)
}
@MainActor
- static func keep(_ image: NSImage, for url: URL) {
- guard let key = key(for: url) else { return }
- images.setObject(image, forKey: key)
+ static func keep(_ image: NSImage, forKey key: String) {
+ images.setObject(image, forKey: key as NSString)
}
- /// The path with the item's device, inode, size and modification time. Nil for an item that is gone.
- private static func key(for url: URL) -> NSString? {
+ /// The path with the item's device, inode, size and modification time, or nil for an item that is gone. Read
+ /// off the main actor, since a network volume can be slow to answer.
+ @concurrent
+ static func key(for url: URL) async -> String? {
let path = url.path(percentEncoded: false)
var info = stat()
guard lstat(path, &info) == 0 else { return nil }
- return "\(path)\n\(info.st_dev) \(info.st_ino) \(info.st_size) \(info.st_mtimespec.tv_sec) \(info.st_mtimespec.tv_nsec)" as NSString
+ return "\(path)\n\(info.st_dev) \(info.st_ino) \(info.st_size) \(info.st_mtimespec.tv_sec) \(info.st_mtimespec.tv_nsec)"
}
}