Skip to content

Instantly share code, notes, and snippets.

@johnsusek
Created March 7, 2020 04:34
Show Gist options
  • Save johnsusek/f6a8ecfc97303528d407fded9f1e6e34 to your computer and use it in GitHub Desktop.
Save johnsusek/f6a8ecfc97303528d407fded9f1e6e34 to your computer and use it in GitHub Desktop.
import Cocoa
import Foundation
class RecallOutlineView: NSOutlineView {
required init?(coder: NSCoder) {
super.init(coder: coder)
}
var isReceivingDrag = false {
didSet {
needsDisplay = true
}
}
override func awakeFromNib() {
registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
setDraggingSourceOperationMask(.copy, forLocal: false)
let descriptorName = NSSortDescriptor(key: "name", ascending: true)
let descriptorDate = NSSortDescriptor(key: "modificationDate", ascending: true)
let descriptorSize = NSSortDescriptor(key: "size", ascending: true)
self.tableColumns[0].sortDescriptorPrototype = descriptorName
self.tableColumns[1].sortDescriptorPrototype = descriptorDate
self.tableColumns[2].sortDescriptorPrototype = descriptorSize
self.sortDescriptors.append(descriptorName)
self.autosaveTableColumns = true
self.autosaveExpandedItems = true
self.wantsLayer = true
}
override func draw(_ dirtyRect: NSRect) {
if self.isReceivingDrag {
NSColor.selectedControlColor.set()
let path = NSBezierPath(rect: bounds)
path.lineWidth = 5
path.stroke()
}
super.draw(dirtyRect)
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
self.isReceivingDrag = true
return .move
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
return .move
}
override func draggingExited(_ sender: NSDraggingInfo?) {
self.isReceivingDrag = false
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
self.isReceivingDrag = false
guard let pasteboard = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "NSFilenamesPboardType")) as? NSArray,
let path = pasteboard[0] as? String else { return false }
if sender.draggingSource != nil {
return true
}
NotificationCenter.default.post(name: Notification.Name("fileDroppedOnTable"), object: nil, userInfo: ["path": path])
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment