krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Sources/Orgstar/FilePreview.swift

322a87d47526d505d903e05d14e65a5464e1468f
orgstar/Sources/Orgstar/FilePreview.swift history · blame · raw

35 lines · 1100 bytes

 1import AppKit
 2import Quartz
 3import SwiftUI
 4
 5/// A file that isn't text: Quick Look's preview, and buttons to open it elsewhere.
 6struct FilePreview: View {
 7    let path: String
 8
 9    var body: some View {
10        QuickLookView(url: URL(fileURLWithPath: path))
11            .safeAreaInset(edge: .bottom, spacing: 0) {
12                HStack {
13                    Button("Open with Default App") { NSWorkspace.shared.open(URL(fileURLWithPath: path)) }
14                    Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)]) }
15                    Spacer()
16                }
17                .padding(8)
18                .background(.bar)
19            }
20    }
21}
22
23struct QuickLookView: NSViewRepresentable {
24    let url: URL
25
26    func makeNSView(context: Context) -> QLPreviewView {
27        let view = QLPreviewView(frame: .zero, style: .normal)!
28        view.autostarts = true
29        return view
30    }
31
32    func updateNSView(_ view: QLPreviewView, context: Context) {
33        if (view.previewItem as? URL) != url { view.previewItem = url as NSURL }
34    }
35}