Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 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 view: NSView!
  8.  
  9. lazy var outline: NSOutlineView = {
  10. let v = NSOutlineView()
  11.  
  12. v.dataSource = self
  13. v.delegate = self
  14.  
  15. let col = NSTableColumn()
  16. v.outlineTableColumn = col
  17. v.addTableColumn(col)
  18.  
  19. v.headerView = nil
  20.  
  21. // v.autosaveExpandedItems = true
  22. // v.autosaveName = "outline"
  23.  
  24. v.selectionHighlightStyle = .sourceList
  25. v.backgroundColor = .clear
  26. v.rowSizeStyle = NSTableView.RowSizeStyle.default
  27. v.allowsMultipleSelection = true
  28. v.allowsEmptySelection = true
  29.  
  30. return v
  31.  
  32. }()
  33.  
  34.  
  35. let items: [Item] = [
  36. Item(name: "a", children: [
  37. Item(name: "aa", children: [
  38. Item(name: "aaa", children: []),
  39. Item(name: "aab", children: [
  40. Item(name: "aaba", children: []),
  41. ]),
  42. ]),
  43. Item(name: "ab", children: []),
  44. Item(name: "ac", children: []),
  45. ]),
  46. Item(name: "b", children: [
  47. Item(name: "ba", children: []),
  48. ]),
  49. ]
  50.  
  51.  
  52. func applicationDidFinishLaunching(_ aNotification: Notification) {
  53.  
  54. self.view.addSubview(self.outline)
  55. self.outline.translatesAutoresizingMaskIntoConstraints = false
  56. self.outline.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  57. self.outline.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  58. self.outline.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
  59. self.outline.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
  60.  
  61. self.outline.heightAnchor.constraint(equalToConstant: 400).isActive = true
  62. self.outline.widthAnchor.constraint(equalToConstant: 400).isActive = true
  63.  
  64. self.outline.dataSource = self
  65. self.outline.delegate = self
  66. self.outline.reloadData()
  67. self.outline.sizeLastColumnToFit()
  68.  
  69. }
  70.  
  71.  
  72. }
  73.  
  74. class Item: NSObject {
  75. let name: String
  76. let children: [Item]
  77.  
  78. init(name: String, children: [Item]) {
  79. self.name = name
  80. self.children = children
  81. }
  82.  
  83. }
  84.  
  85. extension AppDelegate: NSOutlineViewDataSource {
  86.  
  87.  
  88. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  89. guard let item = item as? Item else { return items.count }
  90.  
  91. return item.children.count
  92. }
  93.  
  94. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  95. guard let item = item as? Item else { return items[index] }
  96.  
  97. return item.children[index]
  98. }
  99.  
  100. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  101. guard let item = item as? Item else { return false }
  102.  
  103. return !item.children.isEmpty
  104. }
  105.  
  106. func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
  107. return item
  108. }
  109.  
  110.  
  111. }
  112.  
  113. extension AppDelegate: NSOutlineViewDelegate {
  114.  
  115. func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
  116. return false
  117. }
  118.  
  119. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  120. guard let item = item as? Item else { return false }
  121. print("should collapse", item.name)
  122. return !item.children.isEmpty
  123. }
  124.  
  125. func outlineView(_ outlineView: NSOutlineView, willDisplayCell cell: Any, for tableColumn: NSTableColumn?, item: Any) {
  126.  
  127. guard let item = item as? Item else { return }
  128. guard let cell = cell as? NSCell else { return }
  129.  
  130. cell.title = item.name
  131.  
  132. }
  133.  
  134. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  135. guard let i = item as? Item else { return nil }
  136. let v = NSView(frame: NSZeroRect)
  137. v.addSubview(NSTextField(labelWithString: i.name))
  138. return v
  139. }
  140.  
  141. func outlineView(_ outlineView: NSOutlineView, persistentObjectForItem item: Any?) -> Any? {
  142. return item
  143. }
  144.  
  145. func outlineView(_ outlineView: NSOutlineView, itemForPersistentObject object: Any) -> Any? {
  146. return object
  147. }
  148.  
  149. func outlineViewItemDidCollapse(_ notification: Notification) {
  150. guard let item = notification.userInfo?["NSObject"] as? Item else {
  151. return
  152. }
  153.  
  154. let isExpanded = self.outline.isItemExpanded(item)
  155. let isParentExpanded = self.outline.parent(forItem: item).map(self.outline.isItemExpanded)
  156.  
  157. print("DELEGATE DID COLLAPSE", item.name, isExpanded, isParentExpanded)
  158. }
  159.  
  160. func outlineViewItemDidExpand(_ notification: Notification) {
  161. guard let item = notification.userInfo?["NSObject"] as? Item else {
  162. return
  163. }
  164.  
  165. let isExpanded = self.outline.isItemExpanded(item)
  166. let isParentExpanded = self.outline.parent(forItem: item).map(self.outline.isItemExpanded)
  167.  
  168. print("DELEGATE DID EXPAND", item.name, isExpanded, isParentExpanded)
  169. }
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement