import SwiftUI import LichtkastenCore struct ContactSheetView: View { @EnvironmentObject var state: AppState var body: some View { GeometryReader { geo in let size = CGFloat(state.settings.thumbnailSize) let spacing: CGFloat = 10 let cols = max(1, Int((geo.size.width - 20 + spacing) / (size + spacing))) ScrollViewReader { proxy in ScrollView { LazyVGrid(columns: Array(repeating: GridItem(.fixed(size), spacing: spacing), count: cols), spacing: spacing) { ForEach(state.visible) { item in ThumbnailCell(item: item, size: size, selected: state.selection.contains(item.id), isCursor: state.cursorID == item.id, imageURL: state.previewURL(for: item), onTagClick: { tagClick(item) }) .equatable() .id(item.id) .contentShape(Rectangle()) // Single tap reacts immediately; a double click (clickCount 2) opens the preview. .onTapGesture { click(item) } .contextMenu { ItemContextMenu(item: item) } } } .padding(10) } .background(Color(nsColor: .underPageBackgroundColor)) .onChange(of: state.cursorID) { _, id in if let id { proxy.scrollTo(id) } } .onAppear { state.gridColumns = cols } .onChange(of: cols) { _, c in state.gridColumns = c } } } } private func click(_ item: PhotoItem) { let event = NSApp.currentEvent if (event?.clickCount ?? 1) >= 2 { state.cursorID = item.id state.selection = [item.id] state.previewMode = true } else { state.click(item.id, modifiers: event?.modifierFlags ?? []) } } private func tagClick(_ item: PhotoItem) { if !state.selection.contains(item.id) { state.selection = [item.id]; state.cursorID = item.id } state.toggleTag() } } struct ItemContextMenu: View { @EnvironmentObject var state: AppState let item: PhotoItem var body: some View { Button("IPTC-Info …") { ensureSelected(); state.openWindow("iptc") } Button("Tag umschalten") { ensureSelected(); state.toggleTag() } Menu("Bewertung") { ForEach(0...5, id: \.self) { n in Button(n == 0 ? "Keine" : String(repeating: "★", count: n)) { ensureSelected(); state.setRating(n) } } } Menu("Farbmarkierung") { ForEach(ColorLabel.allCases, id: \.self) { l in Button(l.germanName) { ensureSelected(); state.setLabel(l) } } } Divider() Button("In Lightroom bearbeiten (⌘E)") { ensureSelected(); state.sendToLightroom(export: false, develop: true) } Button("Im Finder zeigen") { NSWorkspace.shared.activateFileViewerSelecting([item.primary]) } Button("Als Vorlage übernehmen") { state.templates.append(IPTCTemplate(name: item.baseName, values: item.iptc)) } } private func ensureSelected() { if !state.selection.contains(item.id) { state.selection = [item.id]; state.cursorID = item.id } } } /// No environment object on purpose: with `.equatable()` a cell only re-renders when its own /// item / selection state changes, which keeps clicking and arrow keys fast with thousands of images. struct ThumbnailCell: View, Equatable { let item: PhotoItem let size: CGFloat let selected: Bool let isCursor: Bool let imageURL: URL let onTagClick: () -> Void @State private var image: NSImage? static func == (a: ThumbnailCell, b: ThumbnailCell) -> Bool { a.item == b.item && a.size == b.size && a.selected == b.selected && a.isCursor == b.isCursor && a.imageURL == b.imageURL } private static let captionField = IPTCField.byID["caption"]! var body: some View { VStack(spacing: 4) { ZStack { Rectangle().fill(Color.black.opacity(0.25)) if let image { Image(nsImage: image).resizable().interpolation(.medium).scaledToFit() } else { ProgressView().controlSize(.small) } } .frame(width: size - 12, height: (size - 12) * 0.72) .clipped() HStack(spacing: 4) { Button(action: onTagClick) { Image(systemName: item.tagged ? "checkmark.square.fill" : "square") .foregroundStyle(item.tagged ? Color.accentColor : .secondary) } .buttonStyle(.plain) .help("Tag (T)") Text(item.filename).font(.caption).lineLimit(1).truncationMode(.middle) Spacer(minLength: 2) if !item.companions.isEmpty { Text("+\(item.companions.map { $0.pathExtension.uppercased() }.joined(separator: "+"))") .font(.system(size: 9, weight: .semibold)) .padding(.horizontal, 3) .background(RoundedRectangle(cornerRadius: 3).fill(Color.secondary.opacity(0.25))) } } HStack(spacing: 1) { ForEach(1...5, id: \.self) { n in Image(systemName: n <= item.rating ? "star.fill" : "star") .font(.system(size: 9)) .foregroundStyle(n <= item.rating ? Color.yellow : Color.secondary.opacity(0.4)) } Spacer() if !item.iptc.isEmpty(Self.captionField) { Image(systemName: "text.alignleft").font(.system(size: 9)).foregroundStyle(.secondary) .help("Hat Bildunterschrift") } } } .padding(6) .frame(width: size) .background( RoundedRectangle(cornerRadius: 6) .fill(selected ? Color.accentColor.opacity(0.35) : Color.gray.opacity(0.14)) ) .overlay( RoundedRectangle(cornerRadius: 6) .strokeBorder(item.label.map(\.color) ?? .clear, lineWidth: 3) ) .overlay( RoundedRectangle(cornerRadius: 6) .strokeBorder(isCursor ? Color.white.opacity(0.9) : .clear, lineWidth: 1) ) .task(id: imageURL) { image = ImageLoader.shared.cachedThumbnail(imageURL) if image == nil { image = await ImageLoader.shared.thumbnail(for: imageURL) } } } } extension ColorLabel { var color: Color { switch self { case .red: return .red case .yellow: return .yellow case .green: return .green case .blue: return .blue case .purple: return .purple } } }