Skip to content

Commit

Permalink
Add Quick Look shortcut to the conversion completed view (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinejr authored and sindresorhus committed May 21, 2019
1 parent 86e4223 commit ece132d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Gifski/Base.lproj/MainMenu.xib
Expand Up @@ -66,6 +66,13 @@
<action selector="open:" target="-1" id="E6Y-20-4aZ"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="TB9-2E-bka"/>
<menuItem title="Quick Look" keyEquivalent=" " id="Wfh-bM-KvX">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="quickLook:" target="-1" id="IQy-U1-llq"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
<menuItem title="Close" keyEquivalent="w" id="3u8-9s-Y0B">
<connections>
Expand Down
69 changes: 69 additions & 0 deletions Gifski/ConversionCompletedView.swift
@@ -1,4 +1,5 @@
import Cocoa
import Quartz

final class ConversionCompletedView: SSView {
private let draggableFile = DraggableFile()
Expand Down Expand Up @@ -28,6 +29,10 @@ final class ConversionCompletedView: SSView {
$0.spacing = 20
}

private var isConversionCompleted: Bool {
return isHidden == false && fileUrl != nil
}

private func createButton(title: String) -> CustomButton {
return with(CustomButton()) {
$0.title = title
Expand Down Expand Up @@ -67,6 +72,9 @@ final class ConversionCompletedView: SSView {
}

func show() {
// We need to manually make self as the first responder, but when the view is hidden it is auto-removed
window?.makeFirstResponder(self)

fadeIn()
}

Expand Down Expand Up @@ -128,3 +136,64 @@ final class ConversionCompletedView: SSView {
])
}
}

extension ConversionCompletedView: QLPreviewPanelDataSource {
@IBAction private func quickLook(_ sender: Any) {
quickLookPreviewItems(nil)
}

override func quickLook(with event: NSEvent) {
quickLookPreviewItems(nil)
}

override func quickLookPreviewItems(_ sender: Any?) {
guard let panel = QLPreviewPanel.shared() else {
return
}

panel.toggle()
}

override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool {
return true
}

override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.delegate = self
panel.dataSource = self
}

override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.dataSource = nil
panel.delegate = nil
}

func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return 1
}

func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
return fileUrl as NSURL
}
}

extension ConversionCompletedView: QLPreviewPanelDelegate {
func previewPanel(_ panel: QLPreviewPanel!, sourceFrameOnScreenFor item: QLPreviewItem!) -> CGRect {
return draggableFile.imageView?.boundsInScreenCoordinates ?? .zero
}

func previewPanel(_ panel: QLPreviewPanel!, transitionImageFor item: QLPreviewItem!, contentRect: UnsafeMutablePointer<CGRect>!) -> Any! {
return draggableFile.image
}
}

extension ConversionCompletedView: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
switch menuItem.action {
case #selector(quickLook(_:))?:
return isConversionCompleted
default:
return true
}
}
}
4 changes: 4 additions & 0 deletions Gifski/DraggableFile.swift
Expand Up @@ -27,6 +27,10 @@ final class DraggableFile: NSImageView {
}
}

var imageView: NSView? {
return subviews.first
}

override init(frame: CGRect) {
super.init(frame: frame)

Expand Down
2 changes: 1 addition & 1 deletion Gifski/MainWindowController.swift
Expand Up @@ -309,7 +309,7 @@ extension MainWindowController: NSMenuItemValidation {
case #selector(open)?:
return !isRunning
default:
return validateMenuItem(menuItem)
return true
}
}
}
18 changes: 18 additions & 0 deletions Gifski/util.swift
@@ -1,5 +1,6 @@
import Cocoa
import AVFoundation
import class Quartz.QLPreviewPanel

/**
Convenience function for initializing an object and modifying its properties
Expand Down Expand Up @@ -2118,3 +2119,20 @@ extension BinaryFloatingPoint {
return (self * divisor).rounded(rule) / divisor
}
}

extension QLPreviewPanel {
func toggle() {
if isVisible {
orderOut(nil)
} else {
makeKeyAndOrderFront(nil)
}
}
}

extension NSView {
/// Get the view frame in screen coordinates.
var boundsInScreenCoordinates: CGRect? {
return window?.convertToScreen(convert(bounds, to: nil))
}
}

0 comments on commit ece132d

Please sign in to comment.