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

176 lines
6.7 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
/// Editor texts for all fields (lists are kept as raw "a, b, c" text while typing).
typealias FieldTexts = [String: String]
extension IPTCValues {
func texts() -> FieldTexts {
var t: FieldTexts = [:]
for f in IPTCField.all { t[f.id] = self[f] }
return t
}
init(texts: FieldTexts) {
self.init()
for f in IPTCField.all { self[f] = (texts[f.id] ?? "").trimmingCharacters(in: .whitespaces) }
}
}
/// PM's blue section header bar.
struct PMSectionHeader: View {
let title: String
var body: some View {
Text(title)
.font(.system(size: 13, weight: .semibold))
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 3)
.background(
Capsule().fill(LinearGradient(colors: [Color(red: 0.30, green: 0.55, blue: 0.90), Color(red: 0.14, green: 0.38, blue: 0.75)],
startPoint: .top, endPoint: .bottom))
)
}
}
/// The metadata form in Photo Mechanic's layout: blue section bars, labels left, big caption box,
/// optional PM checkboxes ("apply this field"), two columns for template / mass edit, one for the info window.
struct PMMetadataForm: View {
@Binding var texts: FieldTexts
var columns = 2
var mixed: Set<String> = []
var dirty: Set<String> = []
/// PM checkboxes; nil = no checkboxes (info window).
var applied: Binding<Set<String>>? = nil
var codes: CodeReplacements? = nil
var onEdit: (String) -> Void = { _ in }
/// Extra rows (e.g. "Image Preferences → Label") inserted after Image Content.
var extraAfterContent: AnyView? = nil
var body: some View {
if columns == 2 {
HStack(alignment: .top, spacing: 18) {
column(0)
column(1)
}
} else {
VStack(alignment: .leading, spacing: 14) {
ForEach(IPTCSection.allCases, id: \.self) { sectionView($0) }
}
}
}
private func column(_ c: Int) -> some View {
VStack(alignment: .leading, spacing: 14) {
ForEach(IPTCSection.allCases.filter { $0.column == c }, id: \.self) { sectionView($0) }
}
.frame(maxWidth: .infinity, alignment: .top)
}
@ViewBuilder
private func sectionView(_ section: IPTCSection) -> some View {
VStack(alignment: .leading, spacing: 6) {
PMSectionHeader(title: section.rawValue)
ForEach(IPTCField.all.filter { $0.section == section }) { field in
PMFieldRow(field: field, text: binding(field),
isMixed: mixed.contains(field.id) && !dirty.contains(field.id),
isDirty: dirty.contains(field.id),
applied: appliedBinding(field))
}
if section == .imageContent, let extra = extraAfterContent { extra }
}
}
private func appliedBinding(_ f: IPTCField) -> Binding<Bool>? {
guard let applied else { return nil }
return Binding(get: { applied.wrappedValue.contains(f.id) },
set: { on in if on { applied.wrappedValue.insert(f.id) } else { applied.wrappedValue.remove(f.id) } })
}
private func binding(_ f: IPTCField) -> Binding<String> {
Binding(get: { texts[f.id] ?? "" },
set: { new in
var value = new
// PM code replacement while typing: "\bvb\" expands as soon as the closing delimiter is typed.
if let codes, !codes.table.isEmpty, new.last == codes.delimiter, new.count > (texts[f.id]?.count ?? 0) {
value = codes.expand(new)
}
texts[f.id] = value
// Editing a field ticks its checkbox (PM behaviour).
applied?.wrappedValue.insert(f.id)
onEdit(f.id)
})
}
}
struct PMFieldRow: View {
let field: IPTCField
@Binding var text: String
let isMixed: Bool
let isDirty: Bool
let applied: Binding<Bool>?
private var height: CGFloat? {
switch field.id {
case "caption": return 210
case "keywords": return 130
case "headline", "altText", "extDescription", "personInImage", "instructions", "usageTerms": return 62
default: return nil
}
}
var body: some View {
HStack(alignment: .top, spacing: 6) {
if let applied {
Toggle("", isOn: applied).toggleStyle(.checkbox).labelsHidden().padding(.top, 3)
}
VStack(alignment: .leading, spacing: 2) {
Text(field.pmLabel + ":")
.font(.system(size: 12))
.foregroundStyle(isDirty ? Color.accentColor : .primary)
.help(field.label)
if let max = field.iimMaxBytes, text.utf8.count > max, !text.contains("{") {
Text("\(text.utf8.count)/\(max)").font(.caption2.monospacedDigit()).foregroundStyle(.red)
.help("IPTC-IIM-Limit in Bytes – Agenturen kürzen längere Werte.")
}
}
.frame(width: 150, alignment: .leading)
.padding(.top, 3)
Group {
if let h = height {
TextEditor(text: $text)
.font(.system(size: 13))
.scrollContentBackground(.hidden)
.padding(4)
.background(Color(nsColor: .textBackgroundColor))
.overlay(alignment: .topLeading) {
if isMixed && text.isEmpty {
Text("‹mehrere Werte›").foregroundStyle(.tertiary).padding(8).allowsHitTesting(false)
}
}
.overlay(RoundedRectangle(cornerRadius: 3).stroke(Color.secondary.opacity(0.4)))
.frame(height: h)
} else {
TextField(isMixed ? "‹mehrere Werte›" : "", text: $text)
.textFieldStyle(.roundedBorder)
}
}
.frame(maxWidth: .infinity)
Menu {
Section("Variablen") {
ForEach(VariableExpander.documented, id: \.0) { v in
Button("\(v.0) – \(v.1)") { text += v.0 }
}
}
Button("Feld leeren") { text = "" }
} label: { Image(systemName: "chevron.down") }
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.frame(width: 22)
.padding(.top, 2)
}
}
}