Add thumbnail view
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user