Skip to content

Commit

Permalink
Quicklook Previews in Open Quickly overlay (#1266)
Browse files Browse the repository at this point in the history
* add support for quicklook previews in open quickly overlay (using OtherFileView)

* fix logic

* respond to review by adding new supported file formats

* respond to @austincondiff's review

* respond to second review
  • Loading branch information
luah5 authored May 16, 2023
1 parent 6c54323 commit 1454828
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CodeEdit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
6CFF967C29BEBD5200182D6F /* WindowCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967B29BEBD5200182D6F /* WindowCommands.swift */; };
850C631029D6B01D00E1444C /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 850C630F29D6B01D00E1444C /* SettingsView.swift */; };
850C631229D6B03400E1444C /* SettingsPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 850C631129D6B03400E1444C /* SettingsPage.swift */; };
85CD0C5F2A10CC3200E531FD /* URL+isImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85CD0C5E2A10CC3200E531FD /* URL+isImage.swift */; };
B6041F4D29D7A4E9000F3454 /* SettingsPageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6041F4C29D7A4E9000F3454 /* SettingsPageView.swift */; };
B6041F5229D7D6D6000F3454 /* SettingsWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6041F5129D7D6D6000F3454 /* SettingsWindow.swift */; };
B60BE8BD297A167600841125 /* AcknowledgementRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B60BE8BC297A167600841125 /* AcknowledgementRowView.swift */; };
Expand Down Expand Up @@ -712,6 +713,7 @@
6CFF967B29BEBD5200182D6F /* WindowCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowCommands.swift; sourceTree = "<group>"; };
850C630F29D6B01D00E1444C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
850C631129D6B03400E1444C /* SettingsPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsPage.swift; sourceTree = "<group>"; };
85CD0C5E2A10CC3200E531FD /* URL+isImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+isImage.swift"; sourceTree = "<group>"; };
B6041F4C29D7A4E9000F3454 /* SettingsPageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsPageView.swift; sourceTree = "<group>"; };
B6041F5129D7D6D6000F3454 /* SettingsWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsWindow.swift; sourceTree = "<group>"; };
B60BE8BC297A167600841125 /* AcknowledgementRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AcknowledgementRowView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1869,6 +1871,7 @@
58D01C87293167DC00C5B6B4 /* Extensions */ = {
isa = PBXGroup;
children = (
85CD0C5D2A10CC2500E531FD /* URL */,
6C82D6C429C0129E00495C54 /* NSApplication */,
588847672992AAB800996D95 /* Array */,
6CBD1BC42978DE3E006639D5 /* Text */,
Expand Down Expand Up @@ -2047,6 +2050,14 @@
path = Text;
sourceTree = "<group>";
};
85CD0C5D2A10CC2500E531FD /* URL */ = {
isa = PBXGroup;
children = (
85CD0C5E2A10CC3200E531FD /* URL+isImage.swift */,
);
path = URL;
sourceTree = "<group>";
};
B61DA9DD29D929BF00BF4A43 /* Pages */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2617,6 +2628,7 @@
587B9E6929301D8F00AC7927 /* GitLabEvent.swift in Sources */,
587B9E5E29301D8F00AC7927 /* GitLabCommitRouter.swift in Sources */,
58F2EB0D292FB2B0004A9BDE /* ThemeSettings.swift in Sources */,
85CD0C5F2A10CC3200E531FD /* URL+isImage.swift in Sources */,
587B9D9F29300ABD00AC7927 /* SegmentedControl.swift in Sources */,
6C7256D729A3D7D000C2D3E0 /* SplitViewControllerView.swift in Sources */,
B6EA1FE529DA33DB001BF195 /* ThemeModel.swift in Sources */,
Expand Down
28 changes: 27 additions & 1 deletion CodeEdit/Features/QuickOpen/Views/QuickOpenPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ struct QuickOpenPreviewView: View {
}

var body: some View {
CodeFileView(codeFile: document, isEditable: false)
if let url = document.fileURL {
if url.isImage() {
if let image = NSImage(contentsOf: url) {
GeometryReader { proxy in
if image.size.width > proxy.size.width || image.size.height > proxy.size.height {
OtherFileView(document)
} else {
// FIXME: The following code causes a bug where the image size doesn't change when zooming.
// The proper version found in WorkspaceCodeFile.swift line 59 to 60.
// Cannot use that code as the image obscures the open quickly overlay.
// There might be a solution for this in QuickOpenView.swift or OverlayView.swift

OtherFileView(document)
.frame(
width: image.size.width,
height: image.size.height
)
.position(x: proxy.frame(in: .local).midX, y: proxy.frame(in: .local).midY)
}
}
} else {
OtherFileView(document)
}
} else {
CodeFileView(codeFile: document, isEditable: false)
}
}
}
}
38 changes: 38 additions & 0 deletions CodeEdit/Utils/Extensions/URL/URL+isImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// URL+isImage.swift
// CodeEdit
//
// Created by Raymond Vleeshouwer on 14/05/23.
//

import Foundation

extension URL {
func isImage() -> Bool {
let ext: String = self.pathExtension.lowercased()

// A list of supported file types by QLPreviewItem
// Some of the image file types (in UTType) are not supported by QLPreviewItem
let quickLookImageFileTypes: [String] = [
"png",
"jpg",
"jpeg",
"bmp",
"pdf",
"heic",
"webp",
"tiff",
"gif",
"tga",
"avif",
"psd",
"svg"
]

if quickLookImageFileTypes.contains(ext) {
return true
} else {
return false
}
}
}

0 comments on commit 1454828

Please sign in to comment.