Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
460 lines
19 KiB
Swift
460 lines
19 KiB
Swift
import SwiftUI
|
||
import LichtkastenCore
|
||
|
||
/// Main browser window in Photo Mechanic's layout:
|
||
/// sidebar (Favorites / Navigator / Tasks) · tabs · filter bar · contact sheet · status bar.
|
||
struct ContentView: View {
|
||
@EnvironmentObject var state: AppState
|
||
@EnvironmentObject var uploads: UploadQueue
|
||
@State private var keyboard = KeyboardMonitor()
|
||
@Environment(\.openWindow) private var openWindow
|
||
|
||
var body: some View {
|
||
NavigationSplitView {
|
||
PMSidebar()
|
||
.navigationSplitViewColumnWidth(min: 200, ideal: 260, max: 420)
|
||
} detail: {
|
||
VStack(spacing: 0) {
|
||
TabBar()
|
||
if state.folder != nil { ContactSheetHeader() }
|
||
Divider()
|
||
if state.folder == nil {
|
||
EmptyStateView()
|
||
} else if state.previewMode {
|
||
PreviewView()
|
||
} else {
|
||
ContactSheetView()
|
||
}
|
||
PMStatusBar()
|
||
}
|
||
}
|
||
.navigationTitle(state.folder?.lastPathComponent ?? "Lichtkasten")
|
||
.toolbar { MainToolbar() }
|
||
.searchable(text: $state.searchText, placement: .toolbar, prompt: "Dateiname, IPTC …")
|
||
.background(WindowAccessor { state.browserWindow = $0 })
|
||
.onAppear { keyboard.install(state: state) }
|
||
.onChange(of: state.windowRequest) { _, req in
|
||
if let req { openWindow(id: req.id) }
|
||
}
|
||
.sheet(isPresented: $state.showTemplatePicker) { TemplatePickerSheet() }
|
||
.sheet(isPresented: $state.showTemplateManager) { TemplateManagerView().frame(minWidth: 1280, minHeight: 760) }
|
||
.sheet(isPresented: $state.showIngest) { IngestView() }
|
||
.alert(item: $state.alert) { a in
|
||
Alert(title: Text(a.title), message: Text(a.message), dismissButton: .default(Text("OK")))
|
||
}
|
||
}
|
||
}
|
||
|
||
struct EmptyStateView: View {
|
||
@EnvironmentObject var state: AppState
|
||
var body: some View {
|
||
VStack(spacing: 16) {
|
||
Image(systemName: "photo.on.rectangle.angled").font(.system(size: 56)).foregroundStyle(.secondary)
|
||
Text("Ordner in Favoriten oder Navigator wählen").font(.title2)
|
||
HStack {
|
||
Button("Ordner öffnen … (⌘O)") { state.chooseFolder() }
|
||
Button("Ingest … (⌘G)") { state.showIngest = true }
|
||
}
|
||
.controlSize(.large)
|
||
}
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
}
|
||
}
|
||
|
||
// MARK: - Toolbar (actions only – filters live in the contact sheet header like PM)
|
||
|
||
struct MainToolbar: ToolbarContent {
|
||
@EnvironmentObject var state: AppState
|
||
@EnvironmentObject var uploads: UploadQueue
|
||
@EnvironmentObject var tasks: TaskCenter
|
||
|
||
var body: some ToolbarContent {
|
||
ToolbarItemGroup(placement: .navigation) {
|
||
Button { state.toggleRawSent() } label: {
|
||
Label(state.isInSentFolder ? "RAW" : state.sentFolderName,
|
||
systemImage: state.isInSentFolder ? "camera" : "paperplane")
|
||
}
|
||
.help(state.isInSentFolder ? "Zum RAW/INPUT-Ordner (⌘⌥S)" : "Zum \(state.sentFolderName)-Ordner (⌘⌥S)")
|
||
.disabled(state.folder == nil)
|
||
}
|
||
ToolbarItemGroup(placement: .primaryAction) {
|
||
Button { state.showIngest = true } label: { Label("Ingest", systemImage: "sdcard") }
|
||
.help("Ingest (⌘G)")
|
||
Button { state.openWindow("iptc") } label: { Label("Info", systemImage: "info.circle") }
|
||
.help("IPTC-Info für ein Bild (I)")
|
||
Button { state.openWindow("iptc-batch") } label: { Label("Auswahl", systemImage: "square.stack.3d.up") }
|
||
.help("IPTC für alle ausgewählten Bilder (⌘I)")
|
||
Menu {
|
||
Button("Bearbeiten (⌘E)") { state.sendToLightroom(export: false, develop: true) }
|
||
Button("Nur importieren (⌘⇧L)") { state.sendToLightroom(export: false) }
|
||
Button("Vorgabe + JPEG nach SENT (⌘⌥L)") { state.sendToLightroom(export: true) }
|
||
} label: {
|
||
Label("Lightroom", systemImage: "camera.aperture")
|
||
} primaryAction: {
|
||
state.sendToLightroom(export: false, develop: true)
|
||
}
|
||
.help("In Lightroom Classic bearbeiten (⌘E)")
|
||
Button { state.openWindow("upload") } label: {
|
||
Label(uploads.activeCount > 0 ? "Upload (\(uploads.activeCount))" : "Upload", systemImage: "icloud.and.arrow.up")
|
||
}
|
||
.help("Upload-Fenster (⌘U)")
|
||
Button { state.openWindow("tasks") } label: {
|
||
Label(tasks.runningCount > 0 ? "Aufgaben (\(tasks.runningCount))" : "Aufgaben", systemImage: "list.bullet.rectangle")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Tabs
|
||
|
||
struct TabBar: View {
|
||
@EnvironmentObject var state: AppState
|
||
|
||
var body: some View {
|
||
HStack(spacing: 4) {
|
||
ForEach(state.tabs, id: \.self) { url in
|
||
let active = state.folder == url
|
||
HStack(spacing: 6) {
|
||
Button { state.closeTab(url) } label: { Image(systemName: "xmark.circle.fill") }
|
||
.buttonStyle(.plain).foregroundStyle(.secondary)
|
||
Image(systemName: "folder.fill").foregroundStyle(.secondary)
|
||
Text(url.lastPathComponent).lineLimit(1)
|
||
if active {
|
||
Text("\(state.visible.count)")
|
||
.font(.caption.monospacedDigit())
|
||
.padding(.horizontal, 5)
|
||
.background(Capsule().fill(Color.secondary.opacity(0.3)))
|
||
}
|
||
}
|
||
.padding(.horizontal, 8)
|
||
.padding(.vertical, 3)
|
||
.background(RoundedRectangle(cornerRadius: 5).fill(active ? Color.secondary.opacity(0.28) : Color.clear))
|
||
.contentShape(Rectangle())
|
||
.onTapGesture { state.open(folder: url) }
|
||
.help(url.path)
|
||
}
|
||
Button { state.newTab() } label: { Image(systemName: "plus.circle") }
|
||
.buttonStyle(.plain)
|
||
.help("Neuer Tab")
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 8)
|
||
.padding(.vertical, 4)
|
||
.background(.bar)
|
||
}
|
||
}
|
||
|
||
// MARK: - PM filter bar
|
||
|
||
struct ContactSheetHeader: View {
|
||
@EnvironmentObject var state: AppState
|
||
|
||
var body: some View {
|
||
HStack(spacing: 12) {
|
||
Slider(value: $state.settings.thumbnailSize, in: 120...max(200, state.settings.maxThumbnailSize))
|
||
.frame(width: 140)
|
||
.help("Vorschaugröße")
|
||
|
||
Picker("", selection: $state.sortOrder) {
|
||
ForEach(SortOrder.allCases, id: \.self) { Text($0.rawValue).tag($0) }
|
||
}
|
||
.labelsHidden()
|
||
.frame(width: 140)
|
||
Toggle("Umkehren", isOn: $state.reverseSort).toggleStyle(.checkbox)
|
||
|
||
Picker("", selection: $state.filter.tag) {
|
||
ForEach(TagFilter.allCases, id: \.self) { Text($0.rawValue).tag($0) }
|
||
}
|
||
.labelsHidden()
|
||
.frame(width: 130)
|
||
|
||
Spacer(minLength: 8)
|
||
|
||
// Stars filter (PM: checkbox + 0★…5★ toggles)
|
||
Toggle("", isOn: $state.filter.ratingOn).toggleStyle(.checkbox).labelsHidden()
|
||
.help("Nach Sternen filtern")
|
||
HStack(spacing: 0) {
|
||
ForEach(0...5, id: \.self) { n in
|
||
let on = state.filter.ratings.contains(n)
|
||
Button { toggle(rating: n) } label: {
|
||
Text("\(n)★").font(.system(size: 11, weight: on ? .bold : .regular))
|
||
.frame(width: 30, height: 20)
|
||
.background(on ? Color.accentColor.opacity(0.45) : Color.secondary.opacity(0.15))
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
}
|
||
.clipShape(RoundedRectangle(cornerRadius: 4))
|
||
.opacity(state.filter.ratingOn ? 1 : 0.5)
|
||
|
||
// Color class filter (PM: checkbox + color swatches, last = no color)
|
||
Toggle("", isOn: $state.filter.colorOn).toggleStyle(.checkbox).labelsHidden()
|
||
.help("Nach Farbklasse filtern")
|
||
HStack(spacing: 0) {
|
||
ForEach(ColorLabel.allCases, id: \.self) { l in
|
||
swatch(l.color, n: l.pmColorClass, help: "\(l.pmColorClass) – \(l.germanName)")
|
||
}
|
||
swatch(.clear, n: 0, help: "Ohne Farbklasse")
|
||
}
|
||
.clipShape(RoundedRectangle(cornerRadius: 4))
|
||
.opacity(state.filter.colorOn ? 1 : 0.5)
|
||
}
|
||
.padding(.horizontal, 10)
|
||
.padding(.vertical, 6)
|
||
}
|
||
|
||
private func swatch(_ c: Color, n: Int, help: String) -> some View {
|
||
let on = state.filter.colors.contains(n)
|
||
return Button { toggle(color: n) } label: {
|
||
ZStack {
|
||
if n == 0 {
|
||
Image(systemName: "checkerboard.rectangle").font(.system(size: 12)).foregroundStyle(.secondary)
|
||
} else {
|
||
c
|
||
}
|
||
if on { Image(systemName: "checkmark").font(.system(size: 10, weight: .bold)).foregroundStyle(.white).shadow(radius: 1) }
|
||
}
|
||
.frame(width: 20, height: 20)
|
||
.overlay(Rectangle().stroke(on ? Color.white : Color.clear, lineWidth: 1.5))
|
||
}
|
||
.buttonStyle(.plain)
|
||
.help(help)
|
||
}
|
||
|
||
private func toggle(rating n: Int) {
|
||
if state.filter.ratings.contains(n) { state.filter.ratings.remove(n) } else { state.filter.ratings.insert(n) }
|
||
state.filter.ratingOn = !state.filter.ratings.isEmpty
|
||
}
|
||
|
||
private func toggle(color n: Int) {
|
||
if state.filter.colors.contains(n) { state.filter.colors.remove(n) } else { state.filter.colors.insert(n) }
|
||
state.filter.colorOn = !state.filter.colors.isEmpty
|
||
}
|
||
}
|
||
|
||
// MARK: - Status bar
|
||
|
||
struct PMStatusBar: View {
|
||
@EnvironmentObject var state: AppState
|
||
@EnvironmentObject var uploads: UploadQueue
|
||
|
||
var body: some View {
|
||
HStack(spacing: 12) {
|
||
if state.loadingMetadata { ProgressView().controlSize(.small) }
|
||
Text(summary).lineLimit(1).truncationMode(.middle)
|
||
Spacer()
|
||
if state.lrPendingCount > 0 { Label("LR: \(state.lrPendingCount)", systemImage: "hourglass") }
|
||
if uploads.failedCount > 0 {
|
||
Label("\(uploads.failedCount) Upload(s) fehlgeschlagen", systemImage: "exclamationmark.triangle.fill")
|
||
.foregroundStyle(.red)
|
||
.onTapGesture { state.openWindow("tasks") }
|
||
}
|
||
if !state.statusText.isEmpty { Text(state.statusText).foregroundStyle(.secondary).lineLimit(1) }
|
||
}
|
||
.font(.system(size: 11))
|
||
.padding(.horizontal, 10)
|
||
.padding(.vertical, 4)
|
||
.background(.bar)
|
||
}
|
||
|
||
/// "Ausgewählt 1 von 2189 (3,22 MB) | 2189 gesamt | /Users/…/datei.jpg" like PM.
|
||
private var summary: String {
|
||
let sel = state.selectedItems.count
|
||
let size = ByteCountFormatter.string(fromByteCount: state.selectedBytes, countStyle: .file)
|
||
var parts = ["Ausgewählt \(sel) von \(state.visible.count) (\(size))", "\(state.items.count) gesamt"]
|
||
if state.filter.isActive { parts.append("Filter aktiv") }
|
||
if let c = state.cursorItem { parts.append(c.primary.path) }
|
||
return parts.joined(separator: " | ")
|
||
}
|
||
}
|
||
|
||
// MARK: - Sidebar: Favorites / Navigator / Tasks
|
||
|
||
struct PMSidebar: View {
|
||
@EnvironmentObject var state: AppState
|
||
@EnvironmentObject var tasks: TaskCenter
|
||
|
||
var body: some View {
|
||
VSplitView {
|
||
panel("Favoriten") {
|
||
List {
|
||
ForEach(state.settings.favoriteFolders, id: \.self) { path in
|
||
folderRow(URL(fileURLWithPath: path))
|
||
.contextMenu {
|
||
Button("In neuem Tab öffnen") { state.open(folder: URL(fileURLWithPath: path), newTab: true) }
|
||
Button("Aus Favoriten entfernen") { state.settings.favoriteFolders.removeAll { $0 == path } }
|
||
}
|
||
}
|
||
.onMove { state.settings.favoriteFolders.move(fromOffsets: $0, toOffset: $1) }
|
||
}
|
||
.listStyle(.sidebar)
|
||
} accessory: {
|
||
if let f = state.folder, !state.settings.favoriteFolders.contains(f.path) {
|
||
Button { state.settings.favoriteFolders.append(f.path) } label: { Image(systemName: "plus") }
|
||
.buttonStyle(.borderless).help("Aktuellen Ordner zu Favoriten")
|
||
}
|
||
}
|
||
.frame(minHeight: 90, idealHeight: 160)
|
||
|
||
panel("Navigator") {
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 0) {
|
||
ForEach(navigatorRoots(), id: \.self) { FolderTreeRow(url: $0, depth: 0) }
|
||
}
|
||
.padding(.vertical, 4)
|
||
}
|
||
} accessory: { EmptyView() }
|
||
.frame(minHeight: 150, idealHeight: 320)
|
||
|
||
panel("Aufgaben") {
|
||
if tasks.tasks.isEmpty {
|
||
Text("Keine Aufgaben").font(.caption).foregroundStyle(.secondary)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
} else {
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 8) {
|
||
ForEach(tasks.tasks.prefix(12)) { t in MiniTaskRow(task: t) }
|
||
}
|
||
.padding(8)
|
||
}
|
||
}
|
||
} accessory: {
|
||
Button { state.openWindow("tasks") } label: { Image(systemName: "arrow.up.forward.app") }
|
||
.buttonStyle(.borderless).help("Aufgaben-Fenster (⌘⌥U)")
|
||
}
|
||
.frame(minHeight: 80, idealHeight: 150)
|
||
}
|
||
}
|
||
|
||
private func panel<C: View, A: View>(_ title: String, @ViewBuilder content: () -> C, @ViewBuilder accessory: () -> A) -> some View {
|
||
VStack(spacing: 0) {
|
||
HStack {
|
||
Text(title).font(.system(size: 12, weight: .semibold))
|
||
Spacer()
|
||
accessory()
|
||
}
|
||
.padding(.horizontal, 10)
|
||
.padding(.vertical, 5)
|
||
.background(Color.secondary.opacity(0.12))
|
||
content()
|
||
}
|
||
}
|
||
|
||
private func folderRow(_ url: URL) -> some View {
|
||
Button { state.open(folder: url) } label: {
|
||
Label(url.lastPathComponent, systemImage: state.folder == url ? "folder.fill" : "folder")
|
||
}
|
||
.buttonStyle(.borderless)
|
||
.help(url.path)
|
||
}
|
||
|
||
/// Like PM: volumes, home, and the usual home folders.
|
||
private func navigatorRoots() -> [URL] {
|
||
let fm = FileManager.default
|
||
let home = fm.homeDirectoryForCurrentUser
|
||
var roots: [URL] = []
|
||
let vols = fm.mountedVolumeURLs(includingResourceValuesForKeys: nil, options: [.skipHiddenVolumes]) ?? []
|
||
roots += vols
|
||
roots.append(home)
|
||
for name in ["Desktop", "Pictures", "Documents", "Downloads"] {
|
||
let u = home.appendingPathComponent(name)
|
||
if fm.fileExists(atPath: u.path) { roots.append(u) }
|
||
}
|
||
let nextcloud = home.appendingPathComponent("Nextcloud")
|
||
if fm.fileExists(atPath: nextcloud.path) { roots.append(nextcloud) }
|
||
return roots
|
||
}
|
||
}
|
||
|
||
struct MiniTaskRow: View {
|
||
let task: BackgroundTask
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
HStack(spacing: 4) {
|
||
Image(systemName: task.systemImage).foregroundStyle(.secondary)
|
||
Text(task.title).font(.caption).lineLimit(1)
|
||
}
|
||
if task.isRunning {
|
||
if task.total > 0 { ProgressView(value: task.progress).controlSize(.small) } else { ProgressView().controlSize(.small) }
|
||
} else {
|
||
Text(task.detail).font(.caption2).foregroundStyle(.secondary).lineLimit(1)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Lazily expanding folder tree (children are only read when a row is opened).
|
||
struct FolderTreeRow: View {
|
||
@EnvironmentObject var state: AppState
|
||
let url: URL
|
||
let depth: Int
|
||
@State private var expanded = false
|
||
@State private var children: [URL]?
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 0) {
|
||
HStack(spacing: 4) {
|
||
Button {
|
||
expanded.toggle()
|
||
if expanded && children == nil { children = Self.subfolders(of: url) }
|
||
} label: {
|
||
Image(systemName: expanded ? "chevron.down" : "chevron.right")
|
||
.font(.system(size: 9, weight: .bold))
|
||
.frame(width: 12)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
.buttonStyle(.plain)
|
||
Image(systemName: depth == 0 && url.path.hasPrefix("/Volumes") ? "externaldrive" : "folder")
|
||
.foregroundStyle(.secondary)
|
||
Text(displayName).lineLimit(1)
|
||
.fontWeight(state.folder == url ? .semibold : .regular)
|
||
Spacer()
|
||
}
|
||
.padding(.leading, CGFloat(depth) * 14 + 6)
|
||
.padding(.vertical, 2)
|
||
.background(state.folder == url ? Color.accentColor.opacity(0.25) : Color.clear)
|
||
.contentShape(Rectangle())
|
||
.onTapGesture(count: 1) { state.open(folder: url) }
|
||
.contextMenu {
|
||
Button("In neuem Tab öffnen") { state.open(folder: url, newTab: true) }
|
||
Button("Zu Favoriten") { if !state.settings.favoriteFolders.contains(url.path) { state.settings.favoriteFolders.append(url.path) } }
|
||
Button("Im Finder zeigen") { NSWorkspace.shared.activateFileViewerSelecting([url]) }
|
||
}
|
||
if expanded, let children {
|
||
ForEach(children, id: \.self) { FolderTreeRow(url: $0, depth: depth + 1) }
|
||
}
|
||
}
|
||
}
|
||
|
||
private var displayName: String {
|
||
let fm = FileManager.default
|
||
if url == fm.homeDirectoryForCurrentUser { return "@" + url.lastPathComponent }
|
||
return fm.displayName(atPath: url.path)
|
||
}
|
||
|
||
static func subfolders(of url: URL) -> [URL] {
|
||
let keys: [URLResourceKey] = [.isDirectoryKey, .isPackageKey]
|
||
let list = (try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: keys, options: [.skipsHiddenFiles])) ?? []
|
||
return list.filter {
|
||
let v = try? $0.resourceValues(forKeys: Set(keys))
|
||
return v?.isDirectory == true && v?.isPackage != true
|
||
}
|
||
.sorted { $0.lastPathComponent.localizedStandardCompare($1.lastPathComponent) == .orderedAscending }
|
||
}
|
||
}
|
||
|
||
/// Gives access to the hosting NSWindow (keyboard monitor needs to know the browser window).
|
||
struct WindowAccessor: NSViewRepresentable {
|
||
let onWindow: (NSWindow?) -> Void
|
||
|
||
func makeNSView(context: Context) -> NSView {
|
||
let v = NSView()
|
||
DispatchQueue.main.async { onWindow(v.window) }
|
||
return v
|
||
}
|
||
|
||
func updateNSView(_ nsView: NSView, context: Context) {
|
||
DispatchQueue.main.async { onWindow(nsView.window) }
|
||
}
|
||
}
|