// // MainController.swift // AutoCat2Mac // // Created by Selim Mustafaev on 15.06.2022. // import AppKit enum SidebarSection: Int, CaseIterable { case source var name: String { switch self { case .source: return "Vehicles DB" } } } enum VehicleSource: Int, CaseIterable { case local case remote var name: String { switch self { case .local: return "Local history" case .remote: return "Remote DB" } } var iconName: String { switch self { case .local: return "internaldrive" case .remote: return "externaldrive.connected.to.line.below" } } } class SidebarController: NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate { override func viewDidLoad() { super.viewDidLoad() } // MARK: - NSOutlineViewDataSource func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { guard item != nil else { return SidebarSection.allCases.count } if let section = item as? SidebarSection { return VehicleSource.allCases.count } else { return 0 } } func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { guard item != nil else { return SidebarSection.source } if let section = item as? SidebarSection { return VehicleSource.allCases[index] } else { return SidebarSection.source } } func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { return item is SidebarSection } // MARK: - NSOutlineViewDelegate func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { if let category = item as? SidebarSection { let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "SidebarHeaderCell"), owner: nil) as? NSTableCellView cell?.textField?.stringValue = category.name return cell } return nil } }