diff --git a/Sources/Photochk/Views/ThumbnailView.swift b/Sources/Photochk/Views/ThumbnailView.swift new file mode 100644 index 0000000..ff78fac --- /dev/null +++ b/Sources/Photochk/Views/ThumbnailView.swift @@ -0,0 +1,35 @@ +import SwiftUI + +/// QuickLook のサムネイルを非同期で読み込んで表示する。 +struct ThumbnailView: View { + let url: URL + let size: CGFloat + + @State private var image: NSImage? + + /// 読み込み済みならキャッシュから直接描く。プレースホルダの一瞬のちらつきを防ぐ。 + private var displayImage: NSImage? { + image ?? ThumbnailLoader.shared.cached(for: url, size: size) + } + + var body: some View { + ZStack { + RoundedRectangle(cornerRadius: 6, style: .continuous) + .fill(.quaternary) + + if let image = displayImage { + Image(nsImage: image) + .resizable() + .aspectRatio(contentMode: .fit) + } else { + ProgressView() + .controlSize(.small) + } + } + .frame(width: size, height: size) + .clipShape(RoundedRectangle(cornerRadius: 6, style: .continuous)) + .task(id: url) { + image = await ThumbnailLoader.shared.thumbnail(for: url, size: size) + } + } +}