Floating Shelf: a macOS shelf at the edge of the screen for holding files, images and text while you move between apps.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-floating-shelf.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-floating-shelf.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-floating-shelf.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add MIT License 76c5d29 1 year ago History
ShelfItem.swift · 3.7 KB · 132 lines
1import SwiftUI
2import AppKit
3
4struct ShelfItem: Identifiable, Equatable {
5 let id = UUID()
6 let type: ItemType
7 let url: URL?
8 let image: NSImage?
9 let text: String?
10 let timestamp: Date
11 let isHTML: Bool
12
13 enum ItemType {
14 case file
15 case image
16 case text
17 }
18
19 init(url: URL) {
20 self.type = .file
21 self.url = url
22 self.image = Self.getThumbnail(for: url)
23 self.text = url.lastPathComponent
24 self.timestamp = Date()
25 self.isHTML = false
26 }
27
28 init(image: NSImage) {
29 self.type = .image
30 self.url = nil
31 self.image = image
32 self.text = nil
33 self.timestamp = Date()
34 self.isHTML = false
35 }
36
37 init(text: String, isHTML: Bool = false) {
38 self.type = .text
39 self.url = nil
40 self.image = nil
41 self.text = text
42 self.timestamp = Date()
43 self.isHTML = isHTML
44 }
45
46 static func == (lhs: ShelfItem, rhs: ShelfItem) -> Bool {
47 lhs.id == rhs.id
48 }
49
50 private static func getThumbnail(for url: URL) -> NSImage? {
51 if url.hasDirectoryPath {
52 return NSWorkspace.shared.icon(forFile: url.path)
53 } else {
54 return NSWorkspace.shared.icon(forFile: url.path)
55 }
56 }
57
58 var displayName: String {
59 if let text = text {
60 return text
61 }
62 return "Item"
63 }
64
65 func copyToClipboard() {
66 let pasteboard = NSPasteboard.general
67 pasteboard.clearContents()
68
69 switch type {
70 case .file:
71 if let url = url {
72 pasteboard.writeObjects([url as NSPasteboardWriting])
73 }
74 case .image:
75 if let image = image {
76 pasteboard.writeObjects([image])
77 }
78 case .text:
79 if let text = text {
80 pasteboard.setString(text, forType: .string)
81 }
82 }
83 }
84
85 func itemProvider() -> NSItemProvider {
86 switch type {
87 case .file:
88 if let url = url {
89 return NSItemProvider(contentsOf: url) ?? NSItemProvider()
90 }
91 case .image:
92 if let image = image,
93 let tiffData = image.tiffRepresentation,
94 let bitmapImage = NSBitmapImageRep(data: tiffData),
95 let pngData = bitmapImage.representation(using: .png, properties: [:]) {
96
97 // Create a temporary file that persists during the drag
98 let fileName = "FloatingShelf-\(UUID().uuidString).png"
99 let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
100
101 do {
102 try pngData.write(to: tempURL)
103 // Return provider pointing to the actual file
104 return NSItemProvider(contentsOf: tempURL) ?? NSItemProvider()
105 } catch {
106 print("Error writing temp file: \(error)")
107 }
108 }
109 case .text:
110 if let text = text {
111 let provider = NSItemProvider()
112
113 // Register as string for direct paste support
114 provider.registerObject(NSString(string: text), visibility: .all)
115
116 // Also register as plain text data
117 provider.registerDataRepresentation(forTypeIdentifier: "public.plain-text", visibility: .all) { completion in
118 if let data = text.data(using: .utf8) {
119 completion(data, nil)
120 } else {
121 completion(nil, NSError(domain: "FloatingShelf", code: -1))
122 }
123 return nil
124 }
125
126 return provider
127 }
128 }
129
130 return NSItemProvider()
131 }
132}