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
ShelfView.swift · 7.8 KB · 238 lines
1import SwiftUI
2
3struct ShelfView: View {
4 @ObservedObject var controller: ShelfViewController
5 @State private var isDragging = false
6
7 var body: some View {
8 ZStack {
9 // Background with blur effect
10 VisualEffectView(material: .hudWindow, blendingMode: .behindWindow)
11 .cornerRadius(16)
12 .shadow(radius: 20)
13
14 VStack(spacing: 0) {
15 // Header with drag handle
16 WindowDragView {
17 HStack {
18 Image(systemName: "tray.2.fill")
19 .foregroundColor(.accentColor)
20 Text("Floating Shelf")
21 .font(.headline)
22 Spacer()
23
24 if !controller.items.isEmpty {
25 Button(action: {
26 controller.clearAll()
27 }) {
28 Image(systemName: "trash")
29 .foregroundColor(.secondary)
30 }
31 .buttonStyle(.plain)
32 .help("Clear all items")
33 }
34
35 // Minimize button (yellow)
36 Button(action: {
37 if let window = NSApplication.shared.windows.first(where: { $0.contentView is NSHostingView<ShelfView> }) {
38 window.orderOut(nil)
39 }
40 }) {
41 Image(systemName: "minus.circle.fill")
42 .foregroundColor(.yellow)
43 }
44 .buttonStyle(.plain)
45 .help("Hide Shelf")
46
47 // Close/Quit button (red)
48 Button(action: {
49 NSApplication.shared.terminate(nil)
50 }) {
51 Image(systemName: "xmark.circle.fill")
52 .foregroundColor(.red)
53 }
54 .buttonStyle(.plain)
55 .help("Quit App")
56 }
57 .padding()
58 }
59
60 Divider()
61
62 // Drop zone and items
63 if controller.items.isEmpty {
64 VStack(spacing: 16) {
65 Image(systemName: "arrow.down.doc.fill")
66 .font(.system(size: 48))
67 .foregroundColor(isDragging ? .accentColor : .secondary)
68 Text("Drop files, images, or text here")
69 .font(.title3)
70 .foregroundColor(.secondary)
71 Text("Drag to screen edge and shake to show")
72 .font(.caption)
73 .foregroundColor(.secondary)
74 }
75 .frame(maxWidth: .infinity, maxHeight: .infinity)
76 .padding()
77 } else {
78 ScrollView {
79 LazyVGrid(columns: [
80 GridItem(.adaptive(minimum: 100), spacing: 12)
81 ], spacing: 12) {
82 ForEach(controller.items) { item in
83 ShelfItemView(item: item, controller: controller)
84 }
85 }
86 .padding()
87 }
88 }
89 }
90 }
91 .frame(width: 400, height: 500)
92 .onDrop(of: [.fileURL, .png, .jpeg, .tiff, .gif, .bmp, .ico, .icns, .svg, .rawImage, .image, .text, .url, .html], isTargeted: $isDragging) { providers in
93 // Only handle drops from external sources
94 return controller.handleDrop(providers: providers)
95 }
96 }
97}
98
99struct ShelfItemView: View {
100 let item: ShelfItem
101 let controller: ShelfViewController
102 @State private var isHovering = false
103
104 var body: some View {
105 VStack(spacing: 4) {
106 // Thumbnail
107 Group {
108 if let image = item.image {
109 Image(nsImage: image)
110 .resizable()
111 .aspectRatio(contentMode: .fit)
112 } else {
113 Image(systemName: "doc.fill")
114 .font(.system(size: 40))
115 .foregroundColor(.accentColor)
116 }
117 }
118 .frame(width: 80, height: 80)
119 .background(Color.secondary.opacity(0.1))
120 .cornerRadius(8)
121
122 // Label
123 Text(item.displayName)
124 .font(.caption)
125 .lineLimit(2)
126 .multilineTextAlignment(.center)
127 .frame(width: 100)
128 }
129 .padding(8)
130 .background(isHovering ? Color.accentColor.opacity(0.1) : Color.clear)
131 .cornerRadius(8)
132 .onHover { hovering in
133 isHovering = hovering
134 }
135 .onDrag {
136 controller.setInternalDrag(true)
137 return item.itemProvider()
138 }
139 .contextMenu {
140 Button("Copy") {
141 item.copyToClipboard()
142 }
143
144 if item.type == .file, let url = item.url {
145 Button("Open") {
146 NSWorkspace.shared.open(url)
147 }
148
149 Button("Show in Finder") {
150 NSWorkspace.shared.activateFileViewerSelecting([url])
151 }
152 }
153
154 Divider()
155
156 Button("Remove") {
157 controller.removeItem(item)
158 }
159 }
160 .onTapGesture(count: 2) {
161 // Double-click to copy
162 item.copyToClipboard()
163
164 // Visual feedback
165 NSSound.beep()
166 }
167 .help("Drag to other apps, double-click to copy, right-click for options")
168 }
169}
170
171// Window drag view - allows dragging the window by this area
172struct WindowDragView<Content: View>: View {
173 let content: Content
174
175 init(@ViewBuilder content: () -> Content) {
176 self.content = content()
177 }
178
179 var body: some View {
180 WindowDragViewRepresentable(content: content)
181 }
182}
183
184struct WindowDragViewRepresentable<Content: View>: NSViewRepresentable {
185 let content: Content
186
187 func makeNSView(context: Context) -> NSHostingView<Content> {
188 let view = NSHostingView(rootView: content)
189
190 // Add mouse tracking for window dragging
191 let mouseDownHandler = NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { event in
192 if let window = view.window, view.hitTest(view.convert(event.locationInWindow, from: nil)) != nil {
193 window.performDrag(with: event)
194 }
195 return event
196 }
197
198 context.coordinator.mouseDownHandler = mouseDownHandler
199 return view
200 }
201
202 func updateNSView(_ nsView: NSHostingView<Content>, context: Context) {
203 nsView.rootView = content
204 }
205
206 func makeCoordinator() -> Coordinator {
207 Coordinator()
208 }
209
210 class Coordinator {
211 var mouseDownHandler: Any?
212
213 deinit {
214 if let handler = mouseDownHandler {
215 NSEvent.removeMonitor(handler)
216 }
217 }
218 }
219}
220
221// Visual effect view for background blur
222struct VisualEffectView: NSViewRepresentable {
223 let material: NSVisualEffectView.Material
224 let blendingMode: NSVisualEffectView.BlendingMode
225
226 func makeNSView(context: Context) -> NSVisualEffectView {
227 let view = NSVisualEffectView()
228 view.material = material
229 view.blendingMode = blendingMode
230 view.state = .active
231 return view
232 }
233
234 func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
235 nsView.material = material
236 nsView.blendingMode = blendingMode
237 }
238}