-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextView.swift
62 lines (55 loc) · 1.8 KB
/
TextView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Foundation
public protocol ITextViewDelegate {
func textView(_ textView: TextView, loadedFile fileName: String)
func textView(_ textView: TextView, gotNonFileText text: String)
}
public class TextView : NSTextView {
var fileName: String?
private func draggingEntered(_ sender: INSDraggingInfo) -> NSDragOperation {
var pb = sender.draggingPasteboard
var dragOperation: NSDragOperation! = sender.draggingSourceOperationMask()
if pb.types?.containsObject(NSFilenamesPboardType) {
if dragOperation & NSDragOperation.Copy != 0 {
return .Copy
}
}
if pb.types?.containsObject(NSPasteboardTypeString) {
if dragOperation & NSDragOperation.Copy != 0 {
return .Copy
}
}
return .None
}
private func performDragOperation(_ sender: INSDraggingInfo) -> Bool {
var pb = sender.draggingPasteboard()
if pb.types?.containsObject(NSFilenamesPboardType) {
var filenames: NSArray! = pb.propertyListForType(NSFilenamesPboardType)
if filenames.count == 1 {
tryLoadFile(filenames[0])
}
} else {
if pb.types?.containsObject(NSPasteboardTypeString) {
var draggedString: NSString! = pb.stringForType(NSPasteboardTypeString)
setString(draggedString)
fileName = nil
if let delegate = delegate as? ITextViewDelegate {
delegate.textView(self, gotNonFileText: draggedString)
}
}
}
return true
}
func tryLoadFile(_ fileName: String) {
var error: NSError? = nil
var fileContents = NSString.stringWithContentsOfFile(fileName, encoding: .UTF8StringEncoding, error: &error)
if let error = error {
// handle error
} else {
setString(fileContents)
self.fileName = fileName
if let delegate = delegate as? ITextViewDelegate {
delegate.textView(self, loadedFile: fileName!)
}
}
}
}