Last active
June 6, 2018 14:02
-
-
Save 128keaton/e6030ddbcedabe10f1a834cd269f13dc to your computer and use it in GitHub Desktop.
Dragging view in Swift 3
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 Foundation | |
import Cocoa | |
class DestinationView: NSView { | |
var isHighlighed: Bool! = false | |
override func awakeFromNib() { | |
self.register(forDraggedTypes: [NSFilenamesPboardType]) | |
} | |
func isHighlighted() -> Bool! { | |
return self.isHighlighed | |
} | |
func setHighlighted(value: Bool) { | |
self.isHighlighed = value | |
needsDisplay = true | |
} | |
override func draw(_ dirtyRect: NSRect) { | |
super.draw(dirtyRect) | |
if self.isHighlighed == true { | |
NSBezierPath.setDefaultLineWidth(6.0) | |
NSColor.keyboardFocusIndicatorColor.set() | |
NSBezierPath.stroke(dirtyRect) | |
} | |
} | |
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { | |
let pasteBoard = sender.draggingPasteboard() | |
if (pasteBoard.types?.contains(NSFilenamesPboardType))! { | |
let paths = pasteBoard.propertyList(forType: NSFilenamesPboardType) as! [String] | |
for path in paths { | |
let utiType = try! NSWorkspace.shared().type(ofFile: path) | |
if !NSWorkspace.shared().type(utiType, conformsToType: String(kUTTypeFolder)) { | |
self.setHighlighted(value: false) | |
return [] | |
} | |
} | |
} | |
NSCursor.dragCopy().set() | |
self.setHighlighted(value: true) | |
return NSDragOperation.every | |
} | |
override func draggingExited(_ sender: NSDraggingInfo?) { | |
NSCursor.arrow().set() | |
self.setHighlighted(value: false) | |
} | |
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool { | |
return true | |
} | |
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { | |
self.setHighlighted(value: false) | |
return true | |
} | |
override func concludeDragOperation(_ sender: NSDraggingInfo?) { | |
let files = sender?.draggingPasteboard().propertyList(forType: NSFilenamesPboardType) | |
Swift.print(files ?? "No files") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment