Skip to content

Instantly share code, notes, and snippets.

@ospfranco
Created January 16, 2022 12:44
Show Gist options
  • Save ospfranco/f2a74edcc191ac264339032b3a1344da to your computer and use it in GitHub Desktop.
Save ospfranco/f2a74edcc191ac264339032b3a1344da to your computer and use it in GitHub Desktop.
React Native macOS Draggable SDWebImage
import Cocoa
import SDWebImage
class InternalWebImage: NSView, NSDraggingSource, NSPasteboardItemDataProvider {
let image = NSImageView()
@objc var url: NSString = "" {
didSet {
self.setupView()
}
}
@objc func pasteboard(_ pasteboard: NSPasteboard?, item: NSPasteboardItem, provideDataForType type: NSPasteboard.PasteboardType) {
let imageUrl = FileConstants.tempUrl.appendingPathComponent("test_image.png")
item.setString(imageUrl.absoluteString, forType: .fileURL)
}
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return .copy
}
override func mouseDragged(with event: NSEvent) {
let pasteboardItem = NSPasteboardItem()
pasteboardItem.setDataProvider(self, forTypes: [.fileURL])
let draggingItem = NSDraggingItem(pasteboardWriter: pasteboardItem)
draggingItem.setDraggingFrame(self.bounds, contents: self.image)
beginDraggingSession(with: [draggingItem], event: event, source: self)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func setupView() {
let imageUrl = URL(string: self.url as String)
image.sd_setImage(with: imageUrl)
image.autoresizingMask = [.height, .width]
self.addSubview(image)
}
}
@objc (WebImageManager)
class WebImageManager: RCTViewManager {
override static func requiresMainQueueSetup() -> Bool {
return true
}
override func view() -> NSView! {
return InternalWebImage()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment