123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // 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: KMBaseXibView {
- @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.
- }
-
- override 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: "")
- }
-
- override 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
- }
- }
|