// // KMBookemarkOutlineView.swift // PDF Reader Pro // // Created by lizhe on 2024/3/4. // import Cocoa private let kLabelIdentifier = NSUserInterfaceItemIdentifier("Label") private let kFileIdentifier = NSUserInterfaceItemIdentifier("File") private let kPageIdentifier = NSUserInterfaceItemIdentifier("Page") private let kSeparatorIdentifier = NSUserInterfaceItemIdentifier("Separator") typealias KMBookmarkOutlineViewDoubleClickAction = (_ view: KMBookmarkOutlineView) -> Void class KMBookmarkOutlineView: BaseXibView { @IBOutlet weak var labelColumn: NSTableColumn! @IBOutlet weak var fileColumn: NSTableColumn! @IBOutlet weak var pageColumn: NSTableColumn! @IBOutlet weak var outlineView: NSOutlineView! var doubleClickAction: KMBookmarkOutlineViewDoubleClickAction? var data: KMRootBookmark? { didSet { self.reloadData() } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } func setup() { outlineView.delegate = self outlineView.dataSource = self outlineView.registerForDraggedTypes([kPasteboardTypeBookmarkRows, .fileURL, .string]) outlineView.doubleAction = #selector(doubleClickBookmark) labelColumn.title = NSLocalizedString("Label", comment: "") fileColumn.title = NSLocalizedString("File", comment: "") pageColumn.title = NSLocalizedString("Page", comment: "") } func reloadData() { guard let data = data else { return } self.outlineView.reloadData() } } //MARK: Action extension KMBookmarkOutlineView { @objc func doubleClickBookmark() { print("doubleClickBookmark") self.doubleClickAction?(self) } } // MARK: - NSOutlineViewDataSource extension KMBookmarkOutlineView: NSOutlineViewDataSource { func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { let bookmark = item as? KMBookmark ?? data guard let bookmark = bookmark else { return 0} return bookmark.bookmarkType == .folder ? bookmark.children.count : 0 } func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { let bookmark = item as! KMBookmark return bookmark.bookmarkType == .folder } func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { let bookmark = (item as? KMBookmark) ?? data return bookmark?.objectOfChidren(index: index) as Any } } extension KMBookmarkOutlineView: NSOutlineViewDelegate { // MARK: - NSOutlineViewDelegate func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { guard let column = tableColumn else { return nil } guard let item = item as? KMBookmark else { return nil } let identifier = column.identifier var cell: NSTableCellView = NSTableCellView() if item.bookmarkType == .separator { cell = KMBookmarkOutlineSeparatorCellView() } else { switch identifier { case kLabelIdentifier: do { cell = KMBookmarkOutlineTitleCellView() let cell = cell as! KMBookmarkOutlineTitleCellView cell.titleLabel.stringValue = item.label cell.iconImageView.image = item.icon } case kPageIdentifier: do { cell = KMBookmarkOutlinePageCellView() let cell = cell as! KMBookmarkOutlinePageCellView cell.titleLabel.stringValue = item.pageIndex.description } case kFileIdentifier: do { cell = KMBookmarkOutlineFileCellView() let cell = cell as! KMBookmarkOutlineFileCellView var stringValue = "" if item.bookmarkType == .folder || item.bookmarkType == .session { let count = item.children.count stringValue = count == 1 ? NSLocalizedString("1 item", comment: "Bookmark folder description") : String.localizedStringWithFormat(NSLocalizedString("%ld items", comment: "Bookmark folder description"), count) } else { stringValue = item.fileURL?.path ?? "" } cell.titleLabel.stringValue = stringValue } default: break } } return cell } func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? { // 创建一个自定义的行视图 let rowView = KMBookmarkOutlineRowView() guard let item = item as? KMBookmark else { return rowView } // 根据需要设置行视图的背景颜色 if item.bookmarkType == .separator{ rowView.hiddenLineView(isHidden: false) } else { rowView.hiddenLineView() } return rowView } }