Created
March 7, 2020 04:34
-
-
Save johnsusek/f6a8ecfc97303528d407fded9f1e6e34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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