Skip to content

Commit

Permalink
Refactor to allow previews without WebKit
Browse files Browse the repository at this point in the history
- Add `PreviewVC` protocol which can be implemented by any
`ViewController` to generate previews for a specific file type
- Re-implement CSV previews with `NSTableView`
- Update code style: Capitalize acronyms, abbreviate "ViewController"
with "VC"
- Rename `Asset` protocol to `WebAsset`
  • Loading branch information
samuelmeuli committed Apr 18, 2020
1 parent 977b8ca commit df49cf2
Show file tree
Hide file tree
Showing 97 changed files with 928 additions and 825 deletions.
748 changes: 392 additions & 356 deletions Glance.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

26 changes: 0 additions & 26 deletions QLPlugin/Assets/csv/csv-main.js

This file was deleted.

26 changes: 0 additions & 26 deletions QLPlugin/Assets/csv/csv-papaparse.min.js

This file was deleted.

26 changes: 0 additions & 26 deletions QLPlugin/Assets/csv/test-page.html

This file was deleted.

92 changes: 0 additions & 92 deletions QLPlugin/Assets/markdown/test-page.html

This file was deleted.

2 changes: 1 addition & 1 deletion QLPlugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
<key>NSExtensionPointIdentifier</key>
<string>com.apple.quicklook.preview</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).PreviewViewController</string>
<string>$(PRODUCT_MODULE_NAME).MainVC</string>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 Samuel Meuli. All rights reserved.</string>
Expand Down
96 changes: 96 additions & 0 deletions QLPlugin/MainVC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Cocoa
import os.log
import Quartz

enum PreviewError: Error {
case fileSizeError(path: String)
}

extension PreviewError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .fileSizeError(path):
return NSLocalizedString("File \(path) is too large to preview", comment: "")
}
}
}

class MainVC: NSViewController, QLPreviewingController {
/// Max size of files to render
let maxFileSize = 100_000 // 100000 B = 100 KB

let stats = Stats()

override var nibName: NSNib.Name? {
NSNib.Name("MainVC")
}

override func viewDidLoad() {
super.viewDidLoad()
configureView()
}

private func configureView() {
// Draw border around previews, in similar style to macOS's default previews
view.wantsLayer = true
view.layer?.borderWidth = 1
view.layer?.borderColor = NSColor.tertiaryLabelColor.cgColor
}

/// Function responsible for generating file previews. It's called for previews in Finder,
/// Spotlight, Quick Look and any other UI elements which implement the API
func preparePreviewOfFile(
at fileUrl: URL,
completionHandler handler: @escaping (Error?) -> Void
) {
// Read information about the file to preview
var file: File
do {
file = try File(url: fileUrl)
} catch {
handler(error)
return
}

// Skip preview if the file is too large
if !file.isDirectory, file.size > maxFileSize {
// Log error and fall back to default preview (by calling the completion handler with
// the error)
handler(PreviewError.fileSizeError(path: file.path))
return
}

// Render file preview
os_log("Generating preview for file %s", type: .debug, fileUrl.path)
do {
try previewFile(file: file)
} catch {
// Log error and fall back to default preview (by calling the completion handler with
// the error)
handler(error)
return
}

// Update stats
stats.increaseStatsCounts(fileExtension: file.url.pathExtension)

// Hide preview loading spinner
handler(nil)
}

/// Generates a preview of the selected file and adds the corresponding child view controller
private func previewFile(file: File) throws {
// Initialize `PreviewVC` for the file type
let previewVCType = PreviewVCFactory.getView(fileExtension: file.url.pathExtension)
let previewVC = previewVCType.init(file: file)

// Generate file preview
try previewVC.loadPreview()

// Add `PreviewVC` as a child view controller
addChild(previewVC)
previewVC.view.autoresizingMask = [.height, .width]
previewVC.view.frame = view.frame
view.addSubview(previewVC.view)
}
}
21 changes: 21 additions & 0 deletions QLPlugin/MainVC.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16096" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16096"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="MainVC" customModule="QLPlugin" customModuleProvider="target">
<connections>
<outlet property="view" destination="c22-O7-iKe" id="NRM-P4-wb6"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="c22-O7-iKe" userLabel="Preview View">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<point key="canvasLocation" x="139" y="154"/>
</customView>
</objects>
</document>
94 changes: 0 additions & 94 deletions QLPlugin/PreviewViewController.swift

This file was deleted.

Loading

0 comments on commit df49cf2

Please sign in to comment.