Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import Cocoa
  2.  
  3. @NSApplicationMain
  4. class AppDelegate: NSObject, NSApplicationDelegate {
  5.  
  6. @IBOutlet weak var window : NSWindow!
  7. @IBOutlet weak var tableView : NSTableView!
  8.  
  9. func applicationDidFinishLaunching(_ aNotification: Notification) {
  10. tableView.delegate = self
  11. tableView.dataSource = self
  12.  
  13. tableView.allowsEmptySelection = true
  14. tableView.allowsColumnResizing = false
  15. tableView.allowsMultipleSelection = false
  16. tableView.allowsColumnSelection = false
  17. tableView.gridStyleMask = []
  18. tableView.gridColor = .clear
  19.  
  20. // This is required to make the labels selectable!
  21. tableView.selectionHighlightStyle = .none
  22.  
  23. tableView.columnAutoresizingStyle = .firstColumnOnlyAutoresizingStyle
  24.  
  25. let tc = tableView.tableColumns[0]
  26. tc.isEditable = false
  27. tc.resizingMask = .autoresizingMask
  28. }
  29. }
  30.  
  31. let viewID = NSUserInterfaceItemIdentifier(rawValue: "run-view")
  32.  
  33. final class MyTextField : NSTextField {
  34. #if false
  35. override func draw(_ dirtyRect: NSRect) {
  36. // Just doing this makes it work
  37. super.draw(dirtyRect)
  38. }
  39. #endif
  40. }
  41.  
  42. extension AppDelegate : NSTableViewDelegate {
  43.  
  44. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  45. return 32
  46. }
  47.  
  48. func tableView(_ tv: NSTableView, viewFor tc: NSTableColumn?, row: Int)
  49. -> NSView?
  50. {
  51. let v = (tv.makeView(withIdentifier: viewID, owner: nil) as? NSTextField)
  52. ?? MyTextField()
  53. v.isSelectable = false
  54. v.isEditable = false
  55. v.stringValue = data[row]
  56. v.identifier = viewID
  57. return v
  58. }
  59.  
  60. func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int)
  61. -> NSPasteboardWriting?
  62. {
  63. return MyPasteboardItem(value: data[row])
  64. }
  65. }
  66.  
  67. final class MyPasteboardItem : NSObject, NSPasteboardWriting {
  68.  
  69. let value : String
  70.  
  71. init(value: String) { self.value = value }
  72.  
  73. func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
  74. return [ .myOwnType, .string ]
  75. }
  76.  
  77. func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
  78. switch type {
  79. case .string: return value
  80. case .myOwnType: return [ "value": value ]
  81. default: return nil
  82. }
  83. }
  84. }
  85.  
  86. extension NSPasteboard.PasteboardType {
  87. static let myOwnType =
  88. NSPasteboard.PasteboardType(rawValue: "de.zeezide.own-type")
  89. }
  90.  
  91. extension AppDelegate : NSTableViewDataSource {
  92. func numberOfRows(in tableView: NSTableView) -> Int { return data.count }
  93. }
  94.  
  95. fileprivate let data = [
  96. "Hello",
  97. "World",
  98. "Blub"
  99. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement