diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-01-08 14:45:58 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-01-08 14:45:58 -0800 |
| commit | e9bae0328801d5a056f33beb0f764c6d35e518a2 (patch) | |
| tree | fcf853330f8de68948b089a5b15e2ef4c6dbb47c /Hotline/Shared/FileIconView.swift | |
| parent | 4349f50167f50b30d7cd71854882ab33ac6ea5ea (diff) | |
Move to a standard SwiftUI window for preview windows so I can add toolbar items from SwiftUI. Not ideal. Added support for previewing text files too.
Diffstat (limited to 'Hotline/Shared/FileIconView.swift')
| -rw-r--r-- | Hotline/Shared/FileIconView.swift | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Hotline/Shared/FileIconView.swift b/Hotline/Shared/FileIconView.swift new file mode 100644 index 0000000..7336cf3 --- /dev/null +++ b/Hotline/Shared/FileIconView.swift @@ -0,0 +1,42 @@ +import SwiftUI +import UniformTypeIdentifiers + +struct FileIconView: View { + let filename: String + + #if os(iOS) + private func fileIcon(filename: String) -> Image { + let fileExtension = (filename as NSString).pathExtension + if let fileType = UTType(filenameExtension: fileExtension) { + if fileType.isSubtype(of: .movie) { + return Image(systemName: "play.rectangle") + } + else if fileType.isSubtype(of: .image) { + return Image(systemName: "photo") + } + else if fileType.isSubtype(of: .archive) { + return Image(systemName: "doc.zipper") + } + else if fileType.isSubtype(of: .text) { + return Image(systemName: "doc.text") + } + else { + return Image(systemName: "doc") + } + } + + return Image(systemName: "doc") + } + #elseif os(macOS) + private func fileIcon(filename: String) -> Image { + Image(nsImage: NSWorkspace.shared.icon(for: UTType(filenameExtension: (filename as NSString).pathExtension) ?? UTType.content)) + } + #endif + + + var body: some View { + fileIcon(filename: filename) + .resizable() + .scaledToFit() + } +} |