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

137 lines
5.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
struct PreviewView: View {
@EnvironmentObject var state: AppState
@State private var image: NSImage?
@State private var fullResLoadedFor: String?
var body: some View {
// Layout B: loupe · filmstrip · IPTC panel – in the Leuchttisch look
HStack(spacing: 0) {
VStack(spacing: 0) {
ZStack(alignment: .bottom) {
LK.deep
if let image {
ZoomableImageView(image: image, maxZoom: CGFloat(state.settings.maxZoom), request: state.zoomRequest) { mag, fit in
state.zoom100 = !fit
state.zoomPercent = Int((mag * 100).rounded())
if !fit { Task { await loadFullRes() } }
}
} else {
ProgressView()
}
if let item = state.cursorItem { infoBar(item) }
}
Filmstrip()
}
if state.settings.loupePanel {
Divider().overlay(LK.border)
LoupeInspector().frame(width: 380)
}
}
.task(id: state.cursorID) { await load() }
}
private func load() async {
guard let item = state.cursorItem else { image = nil; return }
let url = state.previewURL(for: item)
image = ImageLoader.shared.cachedPreview(url) ?? ImageLoader.shared.cachedThumbnail(url)
if let big = await ImageLoader.shared.preview(for: url, maxPixel: state.zoom100 ? 9000 : 3200), state.cursorID == item.id {
image = big
fullResLoadedFor = state.zoom100 ? item.id : nil
}
}
/// Zooming in needs the full-resolution image (fit view uses a 3200 px preview).
private func loadFullRes() async {
guard let item = state.cursorItem, fullResLoadedFor != item.id else { return }
fullResLoadedFor = item.id
let url = state.previewURL(for: item)
if let full = await Task.detached(priority: .userInitiated, operation: { ImageLoader.makeImage(url, maxPixel: 9000, minAcceptable: 4000) }).value,
state.cursorID == item.id {
image = full
}
}
private func infoBar(_ item: PhotoItem) -> some View {
HStack(spacing: 12) {
Text([item.exif["FocalLength"], item.exif["FNumber"].map { "f/\($0)" },
item.exif["ExposureTime"].map { "\($0) s" }, item.exif["ISO"].map { "ISO \($0)" }]
.compactMap { $0 }.joined(separator: " "))
.font(LK.mono(12))
.foregroundStyle(LK.text2)
Spacer()
HStack(spacing: 6) {
Button { state.zoom(.zoomOut) } label: { Image(systemName: "minus.magnifyingglass") }
.buttonStyle(LKButtonStyle(compact: true))
Button { state.zoom(.fit) } label: { Text(state.zoom100 ? "\(state.zoomPercent) %" : "Einpassen").font(LK.mono(12)) }
.buttonStyle(LKButtonStyle(compact: true))
.help("Einpassen (Z)")
Button { state.zoom(.actual) } label: { Text("100 %").font(LK.mono(12)) }
.buttonStyle(LKButtonStyle(compact: true))
Button { state.zoom(.zoomIn) } label: { Image(systemName: "plus.magnifyingglass") }
.buttonStyle(LKButtonStyle(compact: true))
Button { state.settings.loupePanel.toggle() } label: { Image(systemName: "sidebar.right") }
.buttonStyle(LKButtonStyle(compact: true))
.help("IPTC-Panel ein/aus")
}
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(LK.ground.opacity(0.85))
}
}
/// Filmstrip under the loupe (layout B): neighbours of the current image, click to jump.
struct Filmstrip: View {
@EnvironmentObject var state: AppState
var body: some View {
ScrollViewReader { proxy in
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 8) {
ForEach(state.visible) { item in
FilmstripCell(item: item, url: state.previewURL(for: item), current: item.id == state.cursorID)
.equatable()
.id(item.id)
.onTapGesture { state.cursorID = item.id; state.selection = [item.id] }
}
}
.padding(.horizontal, 14)
.padding(.vertical, 12)
}
.frame(height: 104)
.background(LK.ground)
.overlay(alignment: .top) { Rectangle().fill(LK.border).frame(height: 1) }
.onChange(of: state.cursorID) { _, id in if let id { withAnimation(.easeOut(duration: 0.15)) { proxy.scrollTo(id, anchor: .center) } } }
.onAppear { if let id = state.cursorID { proxy.scrollTo(id, anchor: .center) } }
}
}
}
struct FilmstripCell: View, Equatable {
let item: PhotoItem
let url: URL
let current: Bool
@State private var image: NSImage?
static func == (a: FilmstripCell, b: FilmstripCell) -> Bool { a.item == b.item && a.url == b.url && a.current == b.current }
var body: some View {
ZStack(alignment: .leading) {
Rectangle().fill(LK.deep)
if let image { Image(nsImage: image).resizable().scaledToFill() }
Rectangle().fill(item.label?.lkColor ?? .clear).frame(width: 4)
}
.frame(width: 118, height: 78)
.clipShape(RoundedRectangle(cornerRadius: 6))
.overlay(RoundedRectangle(cornerRadius: 6).stroke(current ? LK.amber : Color.clear, lineWidth: 2))
.opacity(current ? 1 : 0.72)
.task(id: url) {
image = ImageLoader.shared.cachedThumbnail(url)
if image == nil { image = await ImageLoader.shared.thumbnail(for: url) }
}
}
}