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

76 lines
3.3 KiB
Swift

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 {
ZStack(alignment: .bottom) {
Color.black
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) }
}
.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: 14) {
Image(systemName: item.tagged ? "checkmark.square.fill" : "square")
Text(item.filename).bold()
HStack(spacing: 1) {
ForEach(1...5, id: \.self) { n in Image(systemName: n <= item.rating ? "star.fill" : "star") }
}
.foregroundStyle(.yellow)
if let l = item.label { Circle().fill(l.color).frame(width: 10, height: 10) }
Spacer()
Text([item.exif["Model"], item.exif["LensModel"], item.exif["FocalLength"],
item.exif["ExposureTime"].map { "\($0) s" }, item.exif["FNumber"].map { "f/\($0)" },
item.exif["ISO"].map { "ISO \($0)" }].compactMap { $0 }.joined(separator: " · "))
.foregroundStyle(.secondary)
HStack(spacing: 4) {
Button { state.zoom(.zoomOut) } label: { Image(systemName: "minus.magnifyingglass") }
Button { state.zoom(.fit) } label: { Text(state.zoom100 ? "\(state.zoomPercent) %" : "Einpassen").monospacedDigit().frame(minWidth: 70) }
.help("Einpassen (Z)")
Button { state.zoom(.actual) } label: { Text("100 %") }
Button { state.zoom(.zoomIn) } label: { Image(systemName: "plus.magnifyingglass") }
}
.buttonStyle(.borderless)
}
.font(.callout)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.background(.ultraThinMaterial)
}
}