Skip to content

Screenshots, hung out to dry. A tiny native macOS app that hangs every screenshot on a line at the top of your screen.

Swift

Quick Look a photo with a force click or from its menu

Alejandro Buján5fdcd6f314ab
diff --git a/Sources/Tendedero/AppDelegate.swift b/Sources/Tendedero/AppDelegate.swift
index f3cc674..92868af 100644
--- a/Sources/Tendedero/AppDelegate.swift
+++ b/Sources/Tendedero/AppDelegate.swift
@@ -1,6 +1,7 @@
import AppKit
import Carbon
import Combine
+import Quartz
import ServiceManagement
import SwiftUI
@@ -741,4 +742,18 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
alert.runModal()
}
}
+
+ // MARK: Quick Look
+
+ /// With no window of ours in front, Quick Look finds its controller here.
+ override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool { true }
+
+ override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
+ QuickLook.shared.take(panel)
+ }
+
+ override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
+ QuickLook.shared.release(panel)
+ }
+
}
diff --git a/Sources/Tendedero/GrabArea.swift b/Sources/Tendedero/GrabArea.swift
index b5fb3df..0bd4c36 100644
--- a/Sources/Tendedero/GrabArea.swift
+++ b/Sources/Tendedero/GrabArea.swift
@@ -41,11 +41,13 @@ struct GrabArea: NSViewRepresentable {
view.onTrash = { line.trash(id) }
view.onDiscard = { line.discard(id) }
view.onLongPress = { line.markup(id) }
+ view.onForceClick = { line.quickLook(id) }
view.onPressChange = { pressed in line.pressedID = pressed ? id : nil }
view.menuProvider = {
let menu = NSMenu()
menu.addItem(ClosureMenuItem(L("Copy")) { line.copy(id) })
menu.addItem(ClosureMenuItem(L("Open")) { line.open(id) })
+ menu.addItem(ClosureMenuItem(L("Quick Look")) { line.quickLook(id) })
if !isRecording {
menu.addItem(ClosureMenuItem(L("Markup")) { line.markup(id) })
}
@@ -78,6 +80,7 @@ final class GrabView: NSView, NSDraggingSource {
var onTrash: () -> Void = {}
var onDiscard: () -> Void = {}
var onLongPress: () -> Void = {}
+ var onForceClick: () -> Void = {}
var onPressChange: (Bool) -> Void = { _ in }
var menuProvider: () -> NSMenu = { NSMenu() }
@@ -129,6 +132,15 @@ final class GrabView: NSView, NSDraggingSource {
}
}
+ /// A force click on a trackpad opens Quick Look, as it does in Finder.
+ /// It counts instead of the click and of a long press.
+ override func pressureChange(with event: NSEvent) {
+ guard event.stage == 2, downPoint != nil, !startedDrag, !didLongPress else { return }
+ didLongPress = true
+ endPress()
+ onForceClick()
+ }
+
private func endPress() {
holdTimer?.invalidate()
holdTimer = nil
diff --git a/Sources/Tendedero/Line.swift b/Sources/Tendedero/Line.swift
index 2cdd301..3610d47 100644
--- a/Sources/Tendedero/Line.swift
+++ b/Sources/Tendedero/Line.swift
@@ -226,6 +226,13 @@ final class Line: ObservableObject {
items[i].thumb = thumb
}
+ /// Quick Look on this photo, with the rest of the line a key press away.
+ func quickLook(_ id: UUID) {
+ let live = items.filter { !$0.falling }
+ guard let index = live.firstIndex(where: { $0.id == id }) else { return }
+ QuickLook.shared.show(live.map(\.url), at: index)
+ }
+
/// After a change of size, every photo is redrawn sharp at the new one.
func reloadThumbnails() {
for i in items.indices where !items[i].falling {
diff --git a/Sources/Tendedero/QuickLook.swift b/Sources/Tendedero/QuickLook.swift
new file mode 100644
index 0000000..f776a5f
--- /dev/null
+++ b/Sources/Tendedero/QuickLook.swift
@@ -0,0 +1,68 @@
+import AppKit
+import Quartz
+
+/// A quick look at a photo without opening it: the same Quick Look window
+/// Finder shows. Force click a photo, or choose Quick Look from its menu.
+/// The arrow keys go through the rest of the line, and Space or Escape
+/// closes it.
+///
+/// The line never takes the keyboard from the app you are in, so Space over
+/// a photo cannot open it; the window takes the keyboard once it is open.
+@MainActor
+final class QuickLook: NSObject, QLPreviewPanelDataSource, QLPreviewPanelDelegate {
+ static let shared = QuickLook()
+
+ private var urls: [URL] = []
+
+ func show(_ urls: [URL], at index: Int) {
+ guard !urls.isEmpty, let panel = QLPreviewPanel.shared() else { return }
+ self.urls = urls
+ NSApp.activate(ignoringOtherApps: true)
+ // Quick Look asks the responder chain who controls it; with no
+ // window of ours that is the app delegate, which hands it to us.
+ panel.updateController()
+ panel.reloadData()
+ panel.currentPreviewItemIndex = index
+ panel.makeKeyAndOrderFront(nil)
+ }
+
+ func take(_ panel: QLPreviewPanel) {
+ panel.dataSource = self
+ panel.delegate = self
+ }
+
+ func release(_ panel: QLPreviewPanel) {
+ panel.dataSource = nil
+ panel.delegate = nil
+ urls = []
+ }
+
+ // MARK: QLPreviewPanelDataSource
+
+ nonisolated func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
+ MainActor.assumeIsolated { urls.count }
+ }
+
+ nonisolated func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
+ MainActor.assumeIsolated { urls.indices.contains(index) ? urls[index] as NSURL : nil }
+ }
+
+ // MARK: QLPreviewPanelDelegate
+
+ /// Left and right go to the photo before or after it on the line.
+ nonisolated func previewPanel(_ panel: QLPreviewPanel!, handle event: NSEvent!) -> Bool {
+ guard event.type == .keyDown, let panel else { return false }
+ let step: Int
+ switch Int(event.keyCode) {
+ case 123: step = -1 // left arrow
+ case 124: step = 1 // right arrow
+ default: return false
+ }
+ return MainActor.assumeIsolated {
+ let next = panel.currentPreviewItemIndex + step
+ guard urls.indices.contains(next) else { return true }
+ panel.currentPreviewItemIndex = next
+ return true
+ }
+ }
+}
diff --git a/Sources/Tendedero/Resources/az.lproj/Localizable.strings b/Sources/Tendedero/Resources/az.lproj/Localizable.strings
index 43fcf0c..fffdeea 100644
--- a/Sources/Tendedero/Resources/az.lproj/Localizable.strings
+++ b/Sources/Tendedero/Resources/az.lproj/Localizable.strings
@@ -45,6 +45,7 @@
/* GrabArea */
"Copy" = "Kopyala";
"Open" = "Aç";
+"Quick Look" = "Tez baxış";
"Markup" = "İşarələmə";
"Show in Finder" = "Finder-də göstər";
"Save to Desktop" = "İş masasında saxla";
diff --git a/Sources/Tendedero/Resources/en.lproj/Localizable.strings b/Sources/Tendedero/Resources/en.lproj/Localizable.strings
index 5626eb6..c335896 100644
--- a/Sources/Tendedero/Resources/en.lproj/Localizable.strings
+++ b/Sources/Tendedero/Resources/en.lproj/Localizable.strings
@@ -45,6 +45,7 @@
/* GrabArea */
"Copy" = "Copy";
"Open" = "Open";
+"Quick Look" = "Quick Look";
"Markup" = "Markup";
"Show in Finder" = "Show in Finder";
"Save to Desktop" = "Save to Desktop";
diff --git a/Sources/Tendedero/Resources/es.lproj/Localizable.strings b/Sources/Tendedero/Resources/es.lproj/Localizable.strings
index 2432b3c..c634204 100644
--- a/Sources/Tendedero/Resources/es.lproj/Localizable.strings
+++ b/Sources/Tendedero/Resources/es.lproj/Localizable.strings
@@ -43,6 +43,7 @@
/* GrabArea */
"Copy" = "Copiar";
"Open" = "Abrir";
+"Quick Look" = "Vista Rápida";
"Markup" = "Marcación";
"Show in Finder" = "Mostrar en Finder";
"Save to Desktop" = "Guardar en el Escritorio";
diff --git a/Sources/Tendedero/Resources/tr.lproj/Localizable.strings b/Sources/Tendedero/Resources/tr.lproj/Localizable.strings
index cac5f9e..1041159 100644
--- a/Sources/Tendedero/Resources/tr.lproj/Localizable.strings
+++ b/Sources/Tendedero/Resources/tr.lproj/Localizable.strings
@@ -43,6 +43,7 @@
/* GrabArea */
"Copy" = "Kopyala";
"Open" = "Aç";
+"Quick Look" = "Hızlı Bakış";
"Markup" = "İşaretle";
"Show in Finder" = "Finder'da göster";
"Save to Desktop" = "Masaüstüne kaydet";
diff --git a/Sources/Tendedero/Resources/zh-Hans.lproj/Localizable.strings b/Sources/Tendedero/Resources/zh-Hans.lproj/Localizable.strings
index 5b22d2d..8403c38 100644
--- a/Sources/Tendedero/Resources/zh-Hans.lproj/Localizable.strings
+++ b/Sources/Tendedero/Resources/zh-Hans.lproj/Localizable.strings
@@ -43,6 +43,7 @@
/* GrabArea */
"Copy" = "复制";
"Open" = "打开";
+"Quick Look" = "快速查看";
"Markup" = "标记";
"Show in Finder" = "在访达中显示";
"Save to Desktop" = "存储到桌面";