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.
EdgeDetectorWindow.swift · 3.2 KB · 99 lines
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | class EdgeDetectorWindow: NSWindow { |
| 6 | var onDragDetected: ((CGPoint) -> Void)? |
| 7 | private var dragView: DragDetectorView? |
| 8 | |
| 9 | init(frame: NSRect) { |
| 10 | super.init( |
| 11 | contentRect: frame, |
| 12 | styleMask: [.borderless], |
| 13 | backing: .buffered, |
| 14 | defer: false |
| 15 | ) |
| 16 | |
| 17 | self.isOpaque = false |
| 18 | self.backgroundColor = .clear |
| 19 | self.level = .floating |
| 20 | self.ignoresMouseEvents = false |
| 21 | self.collectionBehavior = [.canJoinAllSpaces, .stationary, .fullScreenAuxiliary] |
| 22 | |
| 23 | // Create the drag detector view |
| 24 | dragView = DragDetectorView(frame: frame.size) |
| 25 | dragView?.onDragEntered = { [weak self] location in |
| 26 | self?.onDragDetected?(location) |
| 27 | } |
| 28 | |
| 29 | self.contentView = dragView |
| 30 | self.orderFrontRegardless() |
| 31 | } |
| 32 | |
| 33 | override var canBecomeKey: Bool { |
| 34 | return false |
| 35 | } |
| 36 | |
| 37 | override var canBecomeMain: Bool { |
| 38 | return false |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | class DragDetectorView: NSView { |
| 43 | var onDragEntered: ((CGPoint) -> Void)? |
| 44 | private var hasTriggered = false |
| 45 | |
| 46 | init(frame: CGSize) { |
| 47 | super.init(frame: NSRect(origin: .zero, size: frame)) |
| 48 | registerForDraggedTypes([ |
| 49 | NSPasteboard.PasteboardType.fileURL, |
| 50 | NSPasteboard.PasteboardType(rawValue: "public.png"), |
| 51 | NSPasteboard.PasteboardType(rawValue: "public.jpeg"), |
| 52 | NSPasteboard.PasteboardType(rawValue: "public.tiff"), |
| 53 | NSPasteboard.PasteboardType(rawValue: "public.image"), |
| 54 | NSPasteboard.PasteboardType(rawValue: "public.gif"), |
| 55 | NSPasteboard.PasteboardType(rawValue: "public.bmp"), |
| 56 | NSPasteboard.PasteboardType(rawValue: "com.microsoft.ico"), |
| 57 | NSPasteboard.PasteboardType(rawValue: "com.apple.icns"), |
| 58 | NSPasteboard.PasteboardType(rawValue: "public.svg-image"), |
| 59 | NSPasteboard.PasteboardType(rawValue: "public.camera-raw-image"), |
| 60 | NSPasteboard.PasteboardType.string, |
| 61 | NSPasteboard.PasteboardType(rawValue: "public.url"), |
| 62 | NSPasteboard.PasteboardType.html |
| 63 | ]) |
| 64 | } |
| 65 | |
| 66 | required init?(coder: NSCoder) { |
| 67 | fatalError("init(coder:) has not been implemented") |
| 68 | } |
| 69 | |
| 70 | override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { |
| 71 | // Check if the pasteboard has actual droppable content |
| 72 | let pasteboard = sender.draggingPasteboard |
| 73 | |
| 74 | // Accept file URLs, images, or text |
| 75 | if pasteboard.canReadObject(forClasses: [NSURL.self], options: nil) || |
| 76 | pasteboard.canReadObject(forClasses: [NSImage.self], options: nil) || |
| 77 | pasteboard.canReadObject(forClasses: [NSString.self], options: nil) { |
| 78 | |
| 79 | if !hasTriggered { |
| 80 | hasTriggered = true |
| 81 | let location = NSEvent.mouseLocation |
| 82 | onDragEntered?(location) |
| 83 | |
| 84 | // Reset after a short delay |
| 85 | DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in |
| 86 | self?.hasTriggered = false |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return .copy |
| 91 | } |
| 92 | |
| 93 | return [] |
| 94 | } |
| 95 | |
| 96 | override func draggingExited(_ sender: NSDraggingInfo?) { |
| 97 | // Optional: handle drag exit |
| 98 | } |
| 99 | } |