askrobots / dbbasic-floating-shelf
Public
Floating Shelf: a macOS shelf at the edge of the screen for holding files, images and text while you move between apps.
ShelfViewController.swift · 7.9 KB · 185 lines
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | class ShelfViewController: ObservableObject { |
| 6 | @Published var items: [ShelfItem] = [] |
| 7 | private var isDraggingInternal = false |
| 8 | |
| 9 | func addItem(_ item: ShelfItem) { |
| 10 | items.append(item) |
| 11 | } |
| 12 | |
| 13 | func setInternalDrag(_ dragging: Bool) { |
| 14 | isDraggingInternal = dragging |
| 15 | } |
| 16 | |
| 17 | func removeItem(_ item: ShelfItem) { |
| 18 | items.removeAll { $0.id == item.id } |
| 19 | } |
| 20 | |
| 21 | func clearAll() { |
| 22 | items.removeAll() |
| 23 | } |
| 24 | |
| 25 | func handleDrop(providers: [NSItemProvider]) -> Bool { |
| 26 | // Don't accept drops from internal drags |
| 27 | guard !isDraggingInternal, !providers.isEmpty else { |
| 28 | isDraggingInternal = false |
| 29 | return false |
| 30 | } |
| 31 | |
| 32 | // First, check for file promises (Safari, Photos, Mail) |
| 33 | let pasteboard = NSPasteboard(name: .drag) |
| 34 | if let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil) as? [NSFilePromiseReceiver], !filePromises.isEmpty { |
| 35 | print("Found \(filePromises.count) file promise(s) - Safari/Photos/Mail") |
| 36 | |
| 37 | let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString, isDirectory: true) |
| 38 | |
| 39 | do { |
| 40 | try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true, attributes: nil) |
| 41 | } catch { |
| 42 | print("Failed to create temp directory: \(error)") |
| 43 | return false |
| 44 | } |
| 45 | |
| 46 | let operationQueue = OperationQueue() |
| 47 | let group = DispatchGroup() |
| 48 | |
| 49 | for receiver in filePromises { |
| 50 | group.enter() |
| 51 | receiver.receivePromisedFiles(atDestination: tempDirectory, options: [:], operationQueue: operationQueue) { [weak self] (url, error) in |
| 52 | defer { group.leave() } |
| 53 | |
| 54 | if let error = error { |
| 55 | print("Error receiving promised file: \(error)") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | print("Received promised file: \(url)") |
| 60 | |
| 61 | // Check if it's an image |
| 62 | if let imageData = try? Data(contentsOf: url), |
| 63 | let image = NSImage(data: imageData) { |
| 64 | DispatchQueue.main.async { |
| 65 | let item = ShelfItem(image: image) |
| 66 | self?.addItem(item) |
| 67 | } |
| 68 | } else { |
| 69 | // Add as file if not an image |
| 70 | DispatchQueue.main.async { |
| 71 | let item = ShelfItem(url: url) |
| 72 | self?.addItem(item) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return true |
| 79 | } |
| 80 | |
| 81 | for provider in providers { |
| 82 | print("Provider registered types: \(provider.registeredTypeIdentifiers)") |
| 83 | |
| 84 | // Try file URL (for local files) |
| 85 | if provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) { |
| 86 | provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { [weak self] (urlData, error) in |
| 87 | DispatchQueue.main.async { |
| 88 | if let data = urlData as? Data, |
| 89 | let url = URL(dataRepresentation: data, relativeTo: nil) { |
| 90 | |
| 91 | // Check if it's a local image file |
| 92 | let imageExtensions = ["jpg", "jpeg", "png", "gif", "tiff", "bmp", "heic"] |
| 93 | if url.isFileURL && imageExtensions.contains(url.pathExtension.lowercased()) { |
| 94 | if let imageData = try? Data(contentsOf: url), |
| 95 | let image = NSImage(data: imageData) { |
| 96 | let item = ShelfItem(image: image) |
| 97 | self?.addItem(item) |
| 98 | return |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Add as file (local files only, not web URLs) |
| 103 | if url.isFileURL { |
| 104 | let item = ShelfItem(url: url) |
| 105 | self?.addItem(item) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | continue |
| 111 | } |
| 112 | |
| 113 | // Try loading image data directly (works for Chrome, Firefox) |
| 114 | let imageTypes: [UTType] = [.png, .jpeg, .tiff, .gif, .bmp, .ico, .icns, .svg, .rawImage, .image] |
| 115 | var imageHandled = false |
| 116 | for imageType in imageTypes { |
| 117 | if provider.registeredTypeIdentifiers.contains(imageType.identifier) { |
| 118 | print("Found image type: \(imageType.identifier)") |
| 119 | provider.loadDataRepresentation(forTypeIdentifier: imageType.identifier) { [weak self] data, error in |
| 120 | if let error = error { |
| 121 | print("Error loading image data for \(imageType.identifier): \(error)") |
| 122 | return |
| 123 | } |
| 124 | if let data = data, let image = NSImage(data: data) { |
| 125 | print("Successfully loaded image from \(imageType.identifier): \(data.count) bytes") |
| 126 | DispatchQueue.main.async { |
| 127 | let item = ShelfItem(image: image) |
| 128 | self?.addItem(item) |
| 129 | } |
| 130 | } else { |
| 131 | print("Failed to create NSImage from data for \(imageType.identifier)") |
| 132 | } |
| 133 | } |
| 134 | imageHandled = true |
| 135 | break |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if imageHandled { |
| 140 | continue |
| 141 | } |
| 142 | // Try URL (for web links) |
| 143 | if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) { |
| 144 | provider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { [weak self] (urlData, error) in |
| 145 | DispatchQueue.main.async { |
| 146 | if let url = urlData as? URL { |
| 147 | let item = ShelfItem(text: url.absoluteString) |
| 148 | self?.addItem(item) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | continue |
| 153 | } |
| 154 | // Try HTML |
| 155 | if provider.hasItemConformingToTypeIdentifier(UTType.html.identifier) { |
| 156 | provider.loadDataRepresentation(forTypeIdentifier: UTType.html.identifier) { [weak self] data, error in |
| 157 | DispatchQueue.main.async { |
| 158 | if let data = data, let htmlString = String(data: data, encoding: .utf8) { |
| 159 | // Try to extract plain text from HTML for display |
| 160 | let displayText = htmlString.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression) |
| 161 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 162 | let item = ShelfItem(text: displayText.isEmpty ? "HTML Content" : displayText, isHTML: false) |
| 163 | self?.addItem(item) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | continue |
| 168 | } |
| 169 | // Try plain text |
| 170 | if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) { |
| 171 | provider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { [weak self] (textData, error) in |
| 172 | DispatchQueue.main.async { |
| 173 | if let text = textData as? String { |
| 174 | let item = ShelfItem(text: text) |
| 175 | self?.addItem(item) |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | continue |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return true |
| 184 | } |
| 185 | } |