Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Quick Look shortcut to the conversion completed view #79

Merged
merged 12 commits into from May 21, 2019
Merged
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"/>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what changed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unpinned & pinned the action again and so the id regenerated 🤕

</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()
}
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

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
}
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -321,7 +321,7 @@ extension MainWindowController: NSMenuItemValidation {
case #selector(open)?:
return !isRunning
default:
return validateMenuItem(menuItem)
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2112,3 +2113,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))
}
}