Repository Public License
1// ORIX Clean — App/Sources/Features/SimilarImages/SimilarImagesView.swift2// Description: Groups of visually similar images for manual review. Nothing is preselected.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67struct SimilarImagesView: View {8 @Environment(AppModel.self) private var model910 var body: some View {11 @Bindable var model = model12 if model.tree == nil {13 EmptyState(symbol: "photo.on.rectangle.angled", title: L("No scan yet"), message: L("Scan a folder that contains pictures first."),14 actionTitle: L("Scan Home Folder")) { model.startScan(path: model.home) }15 } else {16 VStack(spacing: 0) {17 HStack(spacing: ORIX.Space.m) {18 Picker(L("Method"), selection: $model.similarMode) {19 Text(L("Fast")).tag(SimilarImageOptions.Mode.fast)20 Text(L("Thorough (finds crops)")).tag(SimilarImageOptions.Mode.thorough)21 }.frame(width: 300)22 .help(L("Fast compares image structure, then confirms with Vision. Thorough compares every pair with Vision and is limited to 3,000 images."))23 Button(model.similar == nil ? L("Find Similar Images") : L("Search Again")) { model.findSimilar() }24 .buttonStyle(.borderedProminent).disabled(model.similarPhase == .running).accessibilityIdentifier("similar.find")25 Spacer()26 if let s = model.similar {27 Text(L("%@ images examined · %@ groups", ByteFormat.count(s.imagesExamined), ByteFormat.count(s.groups.count)))28 .font(.callout.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary)29 }30 }31 .padding(.horizontal, ORIX.Space.m).padding(.vertical, ORIX.Space.s).background(.bar)32 Divider()33 if model.similarPhase == .running {34 let p = model.similarProgress35 ProgressLine(title: p.stage == "fingerprint" ? L("Reading images…") : L("Comparing images…"),36 detail: L("%@ of %@", ByteFormat.count(p.done), ByteFormat.count(p.total)),37 fraction: p.total > 0 ? Double(p.done) / Double(p.total) : nil) { model.cancelSimilar() }38 .padding(ORIX.Space.m)39 }40 if let s = model.similar {41 if s.groups.isEmpty {42 EmptyState(symbol: "checkmark.seal", title: L("No similar images found"), message: L("%@ images were examined on this Mac.", ByteFormat.count(s.imagesExamined)))43 } else {44 ScrollView {45 LazyVStack(alignment: .leading, spacing: ORIX.Space.l) {46 InfoBanner(text: L("These images look alike to a local model. That is not proof they are the same picture: compare them before removing any. Nothing is selected for you."))47 if s.mode != model.similarMode, model.similarMode == .thorough {48 InfoBanner(text: L("More than 3,000 images: the fast method was used instead of the thorough one."))49 }50 ForEach(s.groups.prefix(200)) { g in group(g) }51 }52 .padding(ORIX.Space.l)53 }54 .accessibilityIdentifier("similar.list")55 }56 } else if model.similarPhase != .running {57 EmptyState(symbol: "photo.on.rectangle.angled", title: L("Find near-duplicate pictures"),58 message: L("Resized, recompressed or lightly edited copies are grouped using image fingerprints computed on this Mac with Vision. Photo libraries are not opened."),59 actionTitle: L("Find Similar Images")) { model.findSimilar() }60 }61 }62 }63 }6465 private func group(_ g: SimilarGroup) -> some View {66 Card(padding: ORIX.Space.m) {67 VStack(alignment: .leading, spacing: ORIX.Space.s) {68 HStack {69 Text(L("%d similar images", g.images.count)).font(.headline)70 Text(L("confidence %d%%", Int(g.confidence * 100))).font(.caption.monospacedDigit())71 .padding(.horizontal, 6).padding(.vertical, 1).background(ORIX.Palette.hairline, in: Capsule())72 .help(L("Derived from the largest measured difference between linked images. 100% would be structurally identical."))73 Spacer()74 Button(L("Compare in Quick Look")) { QuickLook.show(g.images.map(\.path)) }.controlSize(.small)75 }76 ScrollView(.horizontal) {77 HStack(alignment: .top, spacing: ORIX.Space.m) {78 ForEach(g.images) { img in79 let selected = model.isSelected(img.path)80 VStack(alignment: .leading, spacing: 4) {81 Thumbnail(path: img.path, side: 150)82 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.s).strokeBorder(selected ? ORIX.Palette.review : .clear, lineWidth: 3))83 .onTapGesture(count: 2) { QuickLook.show([img.path]) }84 HStack(spacing: 5) {85 Checkbox(isOn: selected, label: L("Select %@ for cleanup", (img.path as NSString).lastPathComponent)) { model.toggleSimilar(img, in: g) }86 Text((img.path as NSString).lastPathComponent).font(.caption).lineLimit(1).truncationMode(.middle)87 }88 Text("\(img.pixelWidth) × \(img.pixelHeight) · \(ByteFormat.string(img.logicalSize))").font(.caption2.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary)89 PathText(path: img.path)90 }91 .frame(width: 150)92 .contextMenu { FileMenu(path: img.path) }93 .accessibilityElement(children: .combine)94 }95 }96 .padding(.bottom, 4)97 }98 }99 }100 }101}102