Skip to content

Commit 86b2780

Browse files
authored
Correctly preview non-text files (#1707)
* Package.resolved changed * progress * minor doc change in CodeEditUI EffectView * add WorkspacePdfFileView * allow WorkspacePdfFileView to be able to send data to SwiftUI * properly name `WorkspacePdfFileView` to `WorkspacePDFView` * improve WorkspacePDFView docs * add guards to WorkspacePDFView (makeNSView, updateNSView) * wip * wip 2 * correct padding * progress? not * add attachPDFDocumentToView helper in WorkspacePDFView * update WorkspacePDFView docs * remove WorkspaceNonTextFileView * fix SwiftLint violations in WorkspacePDFView, WorkspaceCodeFileView * add magic numbers comment to WorkspaceCodeFileView * update WorkspacePDFView docs * before i forget * progressss * too slow * wip: the best work yet * it really works!! * add WorkspaceImageView * add WorkspaceCannotPreviewFileView * its working very predictably but showing/hiding navigator pane isn't smooth * remove the use of canPreview as it can cause unexpected behaviour * remove computeFrame helper in WorkspaceCodeFileView * update PDF preview's background color * add note for when PDF preview cannot create PDF document * improve docs for WorkspaceImageView & WorkspacePDFView * use unnamed parameter for imageUrl in WorkspaceImageView * implement `WorkspaceCannotPreviewFileView` * change url variables in `WorkspaceImageView`, `WorkspacePDFView` & `WorkspaceCannotPreviewFileView` to private * add `WorkspaceLoadingView` * spacing violation * change onAppear modifier in WorkspaceCannotPreviewFileView * remove `WorkspaceCannotPreviewFileView` as it creates a bug that makes thumbnailing slightly unpredictable Will use QLPreview instead * improve docs in `WorkspacePDFView` & `WorkspaceImageView` * use `WorkspaceAnyFileView` to preview GIF images * add a note to the documentation for `CodeFileDocument.utType` * add `WorkspaceAnyFileView` to properly preview files of different file types * rearrange files in `CodeEdit/Features/Documents/Views` * add `WorkspaceAnyFileView`, `WorkspaceLoadingView`, `WorkspaceImageView`, & `WorkspacePDFView` to DocC Documentation * update docs in `WorkspaceAnyFileView`, `WorkspaceLoadingView`, `WorkspaceImageView`, & `WorkspacePDFView` * rename `CodeFileDocument.typeOfFile` to `CodeFileDocument.utType` * fix: allow GIF previews to play, and with their proper dimensions * do not return data UTType from CodeFileDocument.utType * update docs for CodeFileDocument.utType * change conformance from movie to audiovisualContent in CodeFileDocument.utType * properly display CodeFileView contents by undo-ing the padding in its parent view * remove text, image, PDF, video restrictions on `CodeFileDocument.utType` * update docs in WorkspaceImageView * WorkspaceCodeFileView: group non-text files, so they inherit modifiers from the parent Group view * WorkspaceCodeFileView: add important note on document.utType * remove SwiftUI VideoPlayer view for now, it creates a bug that I can't work around - the first play click doesn't start the video, but the audio starts playing in the background, even after closing the editor tab - and everything in-between * add `NonTextFileView` to clean up code - NonTextFileView, OtherFileView, URL+isImage, are not needed anymore * remove QLPreviewItem subclass from CodeFileDocument * rename `WorkspaceCodeFileView` to `EditorAreaFileView` * rename `EditorView` to `EditorAreaView` - also added the view to the App Window docs * rename 4 Workspace view files - `WorkspaceAnyFileView` -> `AnyFileView` - `WorkspaceLoadingView` -> `LoadingFileView` - `WorkspaceImageView` -> `ImageFileView` - `WorkspacePDFView` -> `PDFFileView` * move views for opening files to `CodeEdit/Features/Editor` * move `CodeFileDocument` to `CodeEdit/Features/Documents` * NSTableViewWrapper: minor change in comment * move `WindowCodeFileView` to `CodeEdit/Features/Editor/Views` * use `AnyFileView` to view all images - this removes the need to check for GIF images - this also has a nice add-on of using gestures for zoom
1 parent 54e0726 commit 86b2780

File tree

22 files changed

+371
-352
lines changed

22 files changed

+371
-352
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 33 additions & 65 deletions
Large diffs are not rendered by default.

CodeEdit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeEdit/Features/CodeEditUI/Views/EffectView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct EffectView: NSViewRepresentable {
2626
///
2727
/// By setting the
2828
/// [`emphasized`](https://developer.apple.com/documentation/appkit/nsvisualeffectview/1644721-isemphasized)
29-
/// flag the emphasized state of the material will be used if available.
29+
/// flag, the emphasized state of the material will be used if available.
3030
///
3131
/// - Parameters:
3232
/// - material: The material to use. Defaults to `.headerView`.

CodeEdit/Features/CodeFile/Image/ImageFileView.swift

Lines changed: 0 additions & 34 deletions
This file was deleted.

CodeEdit/Features/CodeFile/Other/OtherFileView.swift

Lines changed: 0 additions & 46 deletions
This file was deleted.

CodeEdit/Features/CodeFile/CodeFileDocument.swift renamed to CodeEdit/Features/Documents/CodeFileDocument.swift

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CodeFile.swift
2+
// CodeFileDocument.swift
33
// CodeEditModules/CodeFile
44
//
55
// Created by Rehatbir Singh on 12/03/2022.
@@ -9,7 +9,6 @@ import AppKit
99
import Foundation
1010
import SwiftUI
1111
import UniformTypeIdentifiers
12-
import QuickLookUI
1312
import CodeEditSourceEditor
1413
import CodeEditTextView
1514
import CodeEditLanguages
@@ -22,7 +21,7 @@ enum CodeFileError: Error {
2221
}
2322

2423
@objc(CodeFileDocument)
25-
final class CodeFileDocument: NSDocument, ObservableObject, QLPreviewItem {
24+
final class CodeFileDocument: NSDocument, ObservableObject {
2625
struct OpenOptions {
2726
let cursorPositions: [CursorPosition]
2827
}
@@ -41,37 +40,33 @@ final class CodeFileDocument: NSDocument, ObservableObject, QLPreviewItem {
4140
/// Document-specific overridden line wrap preference.
4241
@Published var wrapLines: Bool?
4342

44-
/*
45-
This is the main type of the document.
46-
For example, if the file is end with '.png', it will be an image,
47-
if the file is end with '.py', it will be a text file.
48-
If text content is not empty, return text
49-
If its neither image or text, this could be nil.
50-
*/
51-
var typeOfFile: UTType? {
43+
/// The type of data this document contains.
44+
///
45+
/// If for example, the file ends with `.py`, its type is a text file.
46+
/// Or if it ends with `.png`, then it is an image.
47+
/// Same goes for PDF and video formats.
48+
///
49+
/// Also, if the text content is not empty, it is a text file.
50+
///
51+
/// - Note: The UTType doesn't necessarily mean the file extension, it can be the MIME
52+
/// type or any other form of data representation.
53+
var utType: UTType? {
5254
if !self.content.isEmpty {
53-
return UTType.text
55+
return .text
5456
}
5557
guard let fileType, let type = UTType(fileType) else {
5658
return nil
5759
}
58-
if type.conforms(to: UTType.image) {
59-
return UTType.image
60+
if type.conforms(to: .text) {
61+
return .text
6062
}
61-
if type.conforms(to: UTType.text) {
62-
return UTType.text
63+
if type.conforms(to: .image) {
64+
return .image
6365
}
64-
if type.conforms(to: .data) {
65-
return .data
66+
if type.conforms(to: .pdf) {
67+
return .pdf
6668
}
67-
return nil
68-
}
69-
70-
/*
71-
This is the QLPreviewItemURL
72-
*/
73-
var previewItemURL: URL? {
74-
fileURL
69+
return type
7570
}
7671

7772
/// Specify options for opening the file such as the initial cursor positions.

CodeEdit/Features/Documents/Views/WorkspaceCodeFileView.swift

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// AnyFileView.swift
3+
// CodeEdit
4+
//
5+
// Created by Paul Ebose on 2024/5/9.
6+
//
7+
8+
import SwiftUI
9+
import QuickLookUI
10+
11+
/// A view for previewing any kind of file.
12+
///
13+
/// ```swift
14+
/// AnyFileView(fileURL)
15+
/// ```
16+
/// If the file cannot be previewed, a file icon thumbnail is shown instead.
17+
struct AnyFileView: NSViewRepresentable {
18+
19+
/// URL of the file to preview. You can pass in any file type.
20+
private let fileURL: URL
21+
22+
init(_ fileURL: URL) {
23+
self.fileURL = fileURL
24+
}
25+
26+
func makeNSView(context: Context) -> QLPreviewView {
27+
let qlPreviewView = QLPreviewView()
28+
qlPreviewView.previewItem = fileURL as any QLPreviewItem
29+
return qlPreviewView
30+
}
31+
32+
func updateNSView(_ qlPreviewView: QLPreviewView, context: Context) {
33+
qlPreviewView.previewItem = fileURL as any QLPreviewItem
34+
}
35+
36+
}

0 commit comments

Comments
 (0)