Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AppKit
- // This class will manufacture a delegate object for use with NSOutlineView,
- // given the following:
- // - A setup function, that will take an NSTableViewCell or subclass of the
- // given type, and fill in its views.
- // - A cell identifier from the storyboard, associated to the outline view.
- // - A function that is called when a selection is made...
- // Since the selection handler doesn't actually provide the selected node,
- // you need to seal a reference to your outline view inside your Selection
- // Action closure. Yuck. Make sure to use a capture list to prevent memory cycles.
- final class OutlineDelegate<CellType, ModelType> : NSObject, NSOutlineViewDelegate {
- typealias CellSetup = (CellType, ModelType) -> NSView?
- typealias SelectionAction = () -> Void
- typealias MoveAction = () -> Void
- private let cell_identifier: String
- private let configure_cell: CellSetup
- private let selectionAction: SelectionAction
- init(cell_identifier: String,
- configure_cell: @escaping CellSetup,
- selection_action: @escaping SelectionAction)
- {
- self.cell_identifier = cell_identifier
- self.configure_cell = configure_cell
- self.selectionAction = selection_action
- }
- func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
- let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cell_identifier), owner: self) as! CellType
- return configure_cell(view, item as! ModelType)
- }
- func outlineViewSelectionDidChange(_ notification: Notification) {
- selectionAction()
- }
- }
Add Comment
Please, Sign In to add comment