-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to allow previews without WebKit
- 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
1 parent
977b8ca
commit df49cf2
Showing
97 changed files
with
928 additions
and
825 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.