Skip to content

Instantly share code, notes, and snippets.

@pallavtrivedi03
Created March 12, 2018 16:25
Show Gist options
  • Save pallavtrivedi03/5e1fc4adc22e9e4e0d278825c0ab4425 to your computer and use it in GitHub Desktop.
Save pallavtrivedi03/5e1fc4adc22e9e4e0d278825c0ab4425 to your computer and use it in GitHub Desktop.
class DragDropAreView: NSView {
private var fileTypeIsOk = false
private var acceptedFileExtensions = ["jpg","png","doc","docx","pdf","mov","mp4","mp3"]
var delegate: DragDropViewDelegate?
var isReceivingDrag = false {
didSet {
needsDisplay = true
}
}
required init?(coder: NSCoder) {
super.init(coder: coder)
register(forDraggedTypes: [NSFilenamesPboardType])
}
override func draw(_ dirtyRect: NSRect) {
if isReceivingDrag {
NSColor.selectedControlColor.set()
let path = NSBezierPath(rect:bounds)
path.lineWidth = 10
path.stroke()
}
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
fileTypeIsOk = checkExtension(drag: sender)
isReceivingDrag = fileTypeIsOk
return []
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
return fileTypeIsOk ? .move : []
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
isReceivingDrag = false
guard let _ = sender.draggedFileURL else {
return false
}
return true
}
override func draggingExited(_ sender: NSDraggingInfo?) {
isReceivingDrag = false
}
fileprivate func checkExtension(drag: NSDraggingInfo) -> Bool {
guard let fileExtension = drag.draggedFileURL?.pathExtension?.lowercased() else {
return false
}
if acceptedFileExtensions.contains(fileExtension)
{
if fileExtension == "jpg" || fileExtension == "png"
{
delegate?.draggedMediaType(type: .image, url: drag.draggedFileURL!)
}
else if fileExtension == "mp4" || fileExtension == "mov"
{
delegate?.draggedMediaType(type: .video, url: drag.draggedFileURL!)
}
else if fileExtension == "mp3"
{
delegate?.draggedMediaType(type: .audio, url: drag.draggedFileURL!)
}
else if fileExtension == "doc" || fileExtension == "docx" || fileExtension == "pdf" || fileExtension == "epub"
{
delegate?.draggedMediaType(type: .document, url: drag.draggedFileURL!)
}
}
return acceptedFileExtensions.contains(fileExtension)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment