Last active
June 28, 2020 14:52
-
-
Save erica/e83485ae85e497c145d28cfa140a0258 to your computer and use it in GitHub Desktop.
Revisions
-
erica revised this gist
Apr 14, 2016 . 1 changed file with 5 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,24 +6,17 @@ public extension String { public var ns: NSString {return self as NSString} } /// Custom Labeled Playground-Based Drag-and-Drop window public class DropView: NSTextField { // Default action handler public var handler: ([String]) -> Void = { paths in Swift.print(paths) } // Drag and drop notification public override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func performDragOperation(sender: NSDraggingInfo) -> Bool { let pboard = sender.draggingPasteboard() guard let paths = pboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return false } handler(paths) return true } public required init?(coder: NSCoder) { super.init(coder: coder) } @@ -37,8 +30,5 @@ public class DropView: NSTextField { // Register for file name drags registerForDraggedTypes([NSFilenamesPboardType]) } } -
erica revised this gist
Apr 13, 2016 . No changes.There are no files selected for viewing
-
erica revised this gist
Apr 13, 2016 . No changes.There are no files selected for viewing
-
erica revised this gist
Apr 13, 2016 . No changes.There are no files selected for viewing
-
erica revised this gist
Apr 13, 2016 . No changes.There are no files selected for viewing
-
erica revised this gist
Apr 13, 2016 . 1 changed file with 5 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,25 +7,22 @@ public extension String { public var ns: NSString {return self as NSString} } public class DropView: NSTextField { // Drag and drop notification public override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func performDragOperation(sender: NSDraggingInfo) -> Bool { let pboard = sender.draggingPasteboard() let note = NSNotification(name: String(ObjectIdentifier(self).hashValue), object: pboard) NSNotificationCenter.defaultCenter().postNotification(note) return true } // Default action handler public var handler: ([String]) -> Void = { paths in Swift.print(paths) } // Fetch file names for handler @objc private func handle(note: NSNotification) { guard let pasteboard = note.object as? NSPasteboard else { return } guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return } handler(paths) } @@ -42,9 +39,6 @@ public class DropView: NSTextField { registerForDraggedTypes([NSFilenamesPboardType]) // Convey drags to handler NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handle(_:)), name: String(ObjectIdentifier(self).hashValue), object: nil) } } -
erica revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -36,10 +36,7 @@ public class DropView: NSTextField { // Presentation stringValue = "Drop Here"; editable = false font = NSFont(name: "Palatino", size: 48.0); alignment = .Center wantsLayer = true; layer?.backgroundColor = NSColor.whiteColor().CGColor // Register for file name drags registerForDraggedTypes([NSFilenamesPboardType]) -
erica revised this gist
Apr 13, 2016 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,13 +34,12 @@ public class DropView: NSTextField { super.init(frame: frameRect) // Presentation stringValue = "Drop Here"; editable = false font = NSFont(name: "Palatino", size: 48.0); alignment = .Center // Background wantsLayer = true layer?.backgroundColor = NSColor.whiteColor().CGColor // Register for file name drags registerForDraggedTypes([NSFilenamesPboardType]) @@ -51,4 +50,4 @@ public class DropView: NSTextField { name: String(ObjectIdentifier(self).hashValue), object: nil) } } -
erica revised this gist
Apr 13, 2016 . No changes.There are no files selected for viewing
-
erica created this gist
Apr 13, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ import Cocoa // Support Foundation calls on String public extension String { public var ns: NSString {return self as NSString} } /// Custom Labeled Playground-Based Drag-and-Drop window public class DropView: NSTextField { // Drag and drop notification public override func draggingEntered( sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func draggingUpdated( sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy } public override func performDragOperation(sender: NSDraggingInfo) -> Bool { let pboard = sender.draggingPasteboard() let note = NSNotification(name: String(ObjectIdentifier(self).hashValue), object: pboard) NSNotificationCenter.defaultCenter().postNotification(note) return true } // Action handler public var handler: ([String]) -> Void = { paths in Swift.print(paths) } // Fetch file names for handler @objc private func handle(note: NSNotification) { guard let pasteboard = note.object as? NSPasteboard else { return } guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return } handler(paths) } public required init?(coder: NSCoder) { super.init(coder: coder) } public override init(frame frameRect: NSRect) { super.init(frame: frameRect) // Presentation (stringValue, editable, font) = ("Drop Here", false, NSFont(name: "Palatino", size: 48.0)) // Background wantsLayer = true layer?.backgroundColor = NSColor.whiteColor().CGColor layer?.borderWidth = 2.0 layer?.cornerRadius = 4.0 // Register for file name drags registerForDraggedTypes([NSFilenamesPboardType]) // Convey drags to handler NSNotificationCenter.defaultCenter() .addObserver(self, selector: #selector(handle(_:)), name: String(ObjectIdentifier(self).hashValue), object: nil) } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ import Cocoa import XCPlayground // Establish drop view let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200)) dropView.stringValue = "Drop text files here for word counts" dropView.handler = { print("Word Counts") for path in $0 { guard let string = try? String(contentsOfFile: path) else { continue } print(path.ns.lastPathComponent, ":", string.ns.componentsSeparatedByString(" ").count) } } // Run live XCPlaygroundPage.currentPage.liveView = dropView XCPlaygroundPage.currentPage.needsIndefiniteExecution = true This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ import Cocoa import XCPlayground // Establish drop view let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200)) dropView.stringValue = "Drop Pictures" dropView.handler = { // Print out each file name and image size $0.forEach { if let image = NSImage(contentsOfFile: $0) { Swift.print($0.ns.lastPathComponent, image.size) } else { Swift.print($0.ns.lastPathComponent, "is not an image") } } } // Run live XCPlaygroundPage.currentPage.liveView = dropView XCPlaygroundPage.currentPage.needsIndefiniteExecution = true