Files
lichtkasten/Sources/Lichtkasten/Views/LoupeInspector.swift
T

201 lines
8.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
import LichtkastenCore
/// Right panel of the loupe (layout B): rating, color class, player suggestion and the IPTC fields
/// you change per image – saving with ⌘N / ⌘B like in the IPTC window. All other fields: I.
struct LoupeInspector: View {
@EnvironmentObject var state: AppState
@State private var texts: FieldTexts = [:]
@State private var dirty: Set<String> = []
@State private var loadedID: String?
@State private var saving = false
private var item: PhotoItem? { state.cursorItem }
private static let quickFields = ["caption", "headline", "keywords", "personInImage", "event", "city", "sublocation", "dateCreated"]
var body: some View {
VStack(spacing: 0) {
header
Rectangle().fill(LK.border).frame(height: 1)
ScrollView {
VStack(alignment: .leading, spacing: 14) {
suggestions
LKSectionLabel(title: "Bildinhalt")
field("caption", "Beschreibung", height: 150)
field("headline", "Überschrift", height: 50)
field("keywords", "Stichwörter", height: 70)
field("personInImage", "Personen")
LKSectionLabel(title: "Ort").padding(.top, 4)
HStack(alignment: .top, spacing: 10) {
field("city", "Stadt")
field("sublocation", "Ort")
}
field("event", "Ereignis")
VStack(alignment: .leading, spacing: 5) {
Text("Datum").font(.system(size: 12)).foregroundStyle(LK.text2)
DateFieldEditor(text: binding("dateCreated"))
}
Button("Alle IPTC-Felder … (I)") { state.openWindow("iptc") }
.buttonStyle(LKButtonStyle(compact: true))
}
.padding(18)
}
Rectangle().fill(LK.border).frame(height: 1)
HStack(spacing: 8) {
Button("← Speichern ⌘N") { saveAndMove(-1) }
.keyboardShortcut("n", modifiers: .command)
.buttonStyle(LKButtonStyle())
.frame(maxWidth: .infinity)
Button("Speichern → ⌘B") { saveAndMove(1) }
.keyboardShortcut("b", modifiers: .command)
.buttonStyle(LKButtonStyle(primary: true))
.frame(maxWidth: .infinity)
}
.padding(14)
.disabled(item == nil || saving)
}
.background(LK.panel)
.onAppear { load(force: true) }
.onChange(of: state.cursorID) { _, _ in load(force: true) }
.onReceive(state.$items) { _ in if dirty.isEmpty { DispatchQueue.main.async { load(force: false) } } }
}
// MARK: Header: name, stars, color class
private var header: some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text(item?.baseName ?? "–").font(LK.mono(13, .medium)).foregroundStyle(LK.text).lineLimit(1).truncationMode(.middle)
Spacer()
if let item {
HStack(spacing: 2) {
ForEach(1...5, id: \.self) { n in
Button { state.setRating(item.rating == n ? 0 : n, advance: false, only: item) } label: {
Image(systemName: n <= item.rating ? "star.fill" : "star")
.font(.system(size: 13))
.foregroundStyle(n <= item.rating ? LK.amber : LK.faint)
}
.buttonStyle(.plain)
.help("\(n) Stern(e) – ⌥\(n)")
}
}
}
}
if let item {
HStack(spacing: 6) {
ForEach(ColorLabel.allCases, id: \.self) { l in
let on = item.label == l
Button { state.setLabel(l, advance: false, only: item) } label: {
Capsule().fill(on ? l.lkColor : l.lkColor.opacity(0.25))
.frame(width: 26, height: on ? 8 : 6)
}
.buttonStyle(.plain)
.help("\(l.pmColorClass) – \(l.germanName)")
}
Spacer()
Button { state.toggleTag() } label: {
Label(item.tagged ? "Getaggt" : "Tag", systemImage: item.tagged ? "checkmark.square.fill" : "square")
.font(.system(size: 12, weight: .semibold))
.foregroundStyle(item.tagged ? LK.amber : LK.text2)
}
.buttonStyle(.plain)
.help("Tag (T)")
}
}
}
.padding(18)
}
// MARK: Player suggestion (jersey numbers)
@ViewBuilder
private var suggestions: some View {
let open = item.map { state.openSuggestions(for: $0.id) } ?? []
if let item, !open.isEmpty {
VStack(alignment: .leading, spacing: 8) {
ForEach(open) { s in
HStack(spacing: 10) {
if let c = s.measuredColor {
RoundedRectangle(cornerRadius: 3).fill(c.swiftUI).frame(width: 12, height: 12)
}
Text(s.code ?? "#\(s.number)").font(LK.mono(12, .semibold)).foregroundStyle(LK.amber)
Text(s.replacement.map { JerseyLogic.personName(replacement: $0, columns: []) } ?? "")
.font(.system(size: 13)).foregroundStyle(LK.text).lineLimit(1)
Text("\(Int(s.confidence * 100)) %").font(LK.mono(11)).foregroundStyle(LK.muted)
Spacer()
Button("Annehmen") { accept([s], item: item) }
.buttonStyle(LKButtonStyle(primary: true, compact: true))
.keyboardShortcut(s.id == open.first?.id ? KeyboardShortcut("j", modifiers: .command) : nil)
Button { state.dismissSuggestion(s, for: item.id) } label: { Image(systemName: "xmark") }
.buttonStyle(LKButtonStyle(compact: true))
.help("Falsch – verwerfen")
}
}
}
.padding(12)
.background(RoundedRectangle(cornerRadius: 10).fill(LK.amberSoft))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(LK.amber.opacity(0.35)))
}
}
// MARK: Fields
private func binding(_ id: String) -> Binding<String> {
Binding(get: { texts[id] ?? "" }, set: { texts[id] = $0; dirty.insert(id) })
}
private func field(_ id: String, _ label: String, height: CGFloat? = nil) -> some View {
VStack(alignment: .leading, spacing: 5) {
HStack {
Text(label).font(.system(size: 12)).foregroundStyle(dirty.contains(id) ? LK.amber : LK.text2)
if let f = IPTCField.byID[id], f.kind == .list {
Text("Komma-getrennt").font(.system(size: 10)).foregroundStyle(LK.faint)
}
}
if let height {
CodeTextView(text: binding(id), codes: state.codes)
.frame(height: height)
.clipShape(RoundedRectangle(cornerRadius: 8))
.overlay(RoundedRectangle(cornerRadius: 8).stroke(LK.borderStrong))
} else {
CodeTextField(text: binding(id), codes: state.codes)
.frame(height: 22)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
// MARK: Actions
private func load(force: Bool) {
guard let item else { texts = [:]; dirty = []; loadedID = nil; return }
if !force && loadedID == item.id && !dirty.isEmpty { return }
texts = item.iptc.texts()
dirty = []
loadedID = item.id
}
private func accept(_ s: [JerseySuggestion], item: PhotoItem) {
let updated = state.applying(s, to: IPTCValues(texts: texts)).texts()
for id in ["caption", "personInImage"] where (texts[id] ?? "") != (updated[id] ?? "") {
texts[id] = updated[id] ?? ""
dirty.insert(id)
}
state.markSuggestionsDone(s, for: item.id)
}
private func saveAndMove(_ delta: Int) {
Task {
if let item, !dirty.isEmpty {
saving = true
let fields = IPTCField.all.filter { dirty.contains($0.id) }
let ok = await state.applyIPTCAndWait(IPTCValues(texts: texts), fields: fields, to: [item])
saving = false
guard ok else { return }
dirty = []
}
state.moveCursor(by: delta)
}
}
}