Skip to content

Commit

Permalink
use AnyFileView to view all images
Browse files Browse the repository at this point in the history
- this removes the need to check for GIF images
- this also has a nice add-on of using gestures for zoom
  • Loading branch information
plbstl committed May 10, 2024
1 parent 950d7ed commit b373bb6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
4 changes: 0 additions & 4 deletions CodeEdit/Features/Documents/CodeFileDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ final class CodeFileDocument: NSDocument, ObservableObject {
if type.conforms(to: .text) {
return .text
}
// GIF conforms to image, so to differentiate, the GIF check has to come before the image check.
if type.conforms(to: .gif) {
return .gif
}
if type.conforms(to: .image) {
return .image
}
Expand Down
33 changes: 8 additions & 25 deletions CodeEdit/Features/Editor/Views/ImageFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,24 @@

import SwiftUI

/// A view for previewing an image, while respecting its image dimensions.
/// A view for previewing an image, while respecting its dimensions.
///
/// It receives a URL to an image file and attempts to preview it.
///
/// ```swift
/// ImageFileView(imageURL)
/// ```
/// This implementation allows for proper image scaling, especially when the image dimensions is smaller than
/// This implementation allows for proper image scaling, especially when the image dimensions are smaller than
/// the size of the image view area.
///
/// If the preview image cannot be created, it shows a *"Cannot preview image"* text.
///
/// - Note: This view wraps around SwiftUI Image. Since SwiftUI Image view do not play GIFs, you should indicate
/// when passing in a GIF file, so this view can handle the GIF file correctly.
struct ImageFileView: View {

/// URL of the image you want to preview.
private let imageURL: URL

/// Indicates whether the image is a GIF.
private let isGif: Bool

init(_ imageURL: URL, isGif: Bool = false) {
init(_ imageURL: URL) {
self.imageURL = imageURL
self.isGif = isGif
}

var body: some View {
Expand All @@ -43,21 +36,11 @@ struct ImageFileView: View {

GeometryReader { proxy in
ZStack {
if isGif {
AnyFileView(imageURL)
.frame(
maxWidth: min(pixelWidth, proxy.size.width, nsImage.size.width),
maxHeight: min(pixelHeight, proxy.size.height, nsImage.size.height)
)
} else {
Image(nsImage: nsImage)
.resizable()
.scaledToFit()
.frame(
maxWidth: min(pixelWidth, proxy.size.width, nsImage.size.width),
maxHeight: min(pixelHeight, proxy.size.height, nsImage.size.height)
)
}
AnyFileView(imageURL)
.frame(
maxWidth: min(pixelWidth, proxy.size.width, nsImage.size.width),
maxHeight: min(pixelHeight, proxy.size.height, nsImage.size.height)
)
}
.frame(width: proxy.size.width, height: proxy.size.height)
}
Expand Down
4 changes: 0 additions & 4 deletions CodeEdit/Features/Editor/Views/NonTextFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ struct NonTextFileView: View {
if let fileURL = fileDocument.fileURL {

switch fileDocument.utType {
case .some(.gif):
// GIF conforms to image, so to differentiate, the GIF check has to come before the image check.
ImageFileView(fileURL, isGif: true)

case .some(.image):
ImageFileView(fileURL)

Expand Down

0 comments on commit b373bb6

Please sign in to comment.