AutoCat2/AutoCat2Mac/Controllers/Sidebar/SidebarController.swift
2022-07-04 00:37:27 +03:00

109 lines
3.3 KiB
Swift

//
// MainController.swift
// AutoCat2Mac
//
// Created by Selim Mustafaev on 15.06.2022.
//
import AppKit
class SidebarController: NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate {
@IBOutlet weak var outlineView: NSOutlineView!
var sections: [SidebarSection] = [
.database(items: [
SidebarFilterItem.local,
SidebarFilterItem.remote
])
]
var addItem: NSToolbarItem?
override func viewDidLoad() {
super.viewDidLoad()
outlineView.usesAutomaticRowHeights = true
}
// MARK: - NSOutlineViewDataSource
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
guard item != nil else {
return sections.count
}
if let section = item as? SidebarSection {
return section.items.count
} else {
return 0
}
}
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
guard item != nil else {
return sections[index]
}
if let section = item as? SidebarSection {
return section.items[index]
} else {
return sections[index]
}
}
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
} else if let item = item as? SidebarItem {
let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "SidebarItemCell"), owner: nil) as? NSTableCellView
cell?.textField?.stringValue = item.title
cell?.imageView?.image = NSImage(systemSymbolName: item.iconName, accessibilityDescription: item.title)
return cell
}
return nil
}
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
item is SidebarSection
}
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
outlineView.parent(forItem: item) != nil
}
func outlineView(_ outlineView: NSOutlineView, persistentObjectForItem item: Any?) -> Any? {
guard let section = item as? SidebarSection else {
return nil
}
return section.name
}
func outlineView(_ outlineView: NSOutlineView, itemForPersistentObject object: Any) -> Any? {
guard let name = object as? String else {
return nil
}
return sections.first { $0.name == name }
}
func outlineViewSelectionDidChange(_ notification: Notification) {
let item = self.outlineView.item(atRow: self.outlineView.selectedRow)
if let filter = item as? SidebarFilterItem {
print("Selected filter: ", filter.title)
}
}
}