Guest User

Untitled

a guest
May 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import AppKit
  2.  
  3. // This class will manufacture a delegate object for use with NSOutlineView,
  4. // given the following:
  5. // - A setup function, that will take an NSTableViewCell or subclass of the
  6. // given type, and fill in its views.
  7. // - A cell identifier from the storyboard, associated to the outline view.
  8. // - A function that is called when a selection is made...
  9. // Since the selection handler doesn't actually provide the selected node,
  10. // you need to seal a reference to your outline view inside your Selection
  11. // Action closure. Yuck. Make sure to use a capture list to prevent memory cycles.
  12. final class OutlineDelegate<CellType, ModelType> : NSObject, NSOutlineViewDelegate {
  13.  
  14. typealias CellSetup = (CellType, ModelType) -> NSView?
  15. typealias SelectionAction = () -> Void
  16. typealias MoveAction = () -> Void
  17.  
  18. private let cell_identifier: String
  19. private let configure_cell: CellSetup
  20. private let selectionAction: SelectionAction
  21.  
  22. init(cell_identifier: String,
  23. configure_cell: @escaping CellSetup,
  24. selection_action: @escaping SelectionAction)
  25. {
  26. self.cell_identifier = cell_identifier
  27. self.configure_cell = configure_cell
  28. self.selectionAction = selection_action
  29. }
  30.  
  31. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  32. let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cell_identifier), owner: self) as! CellType
  33. return configure_cell(view, item as! ModelType)
  34. }
  35.  
  36. func outlineViewSelectionDidChange(_ notification: Notification) {
  37. selectionAction()
  38. }
  39. }
Add Comment
Please, Sign In to add comment