KMBookmarkOutlineView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // KMBookemarkOutlineView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/3/4.
  6. //
  7. import Cocoa
  8. private let kLabelIdentifier = NSUserInterfaceItemIdentifier("Label")
  9. private let kFileIdentifier = NSUserInterfaceItemIdentifier("File")
  10. private let kPageIdentifier = NSUserInterfaceItemIdentifier("Page")
  11. private let kSeparatorIdentifier = NSUserInterfaceItemIdentifier("Separator")
  12. typealias KMBookmarkOutlineViewDoubleClickAction = (_ view: KMBookmarkOutlineView) -> Void
  13. class KMBookmarkOutlineView: KMBaseXibView {
  14. @IBOutlet weak var labelColumn: NSTableColumn!
  15. @IBOutlet weak var fileColumn: NSTableColumn!
  16. @IBOutlet weak var pageColumn: NSTableColumn!
  17. @IBOutlet weak var outlineView: NSOutlineView!
  18. var doubleClickAction: KMBookmarkOutlineViewDoubleClickAction?
  19. var data: KMRootBookmark? {
  20. didSet {
  21. self.reloadData()
  22. }
  23. }
  24. override func draw(_ dirtyRect: NSRect) {
  25. super.draw(dirtyRect)
  26. // Drawing code here.
  27. }
  28. override func setup() {
  29. outlineView.delegate = self
  30. outlineView.dataSource = self
  31. outlineView.registerForDraggedTypes([kPasteboardTypeBookmarkRows, .fileURL, .string])
  32. outlineView.doubleAction = #selector(doubleClickBookmark)
  33. labelColumn.title = NSLocalizedString("Label", comment: "")
  34. fileColumn.title = NSLocalizedString("File", comment: "")
  35. pageColumn.title = NSLocalizedString("Page", comment: "")
  36. }
  37. override func reloadData() {
  38. guard let data = data else { return }
  39. self.outlineView.reloadData()
  40. }
  41. }
  42. //MARK: Action
  43. extension KMBookmarkOutlineView {
  44. @objc func doubleClickBookmark() {
  45. print("doubleClickBookmark")
  46. self.doubleClickAction?(self)
  47. }
  48. }
  49. // MARK: - NSOutlineViewDataSource
  50. extension KMBookmarkOutlineView: NSOutlineViewDataSource {
  51. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  52. let bookmark = item as? KMBookmark ?? data
  53. guard let bookmark = bookmark else { return 0}
  54. return bookmark.bookmarkType == .folder ? bookmark.children.count : 0
  55. }
  56. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  57. let bookmark = item as! KMBookmark
  58. return bookmark.bookmarkType == .folder
  59. }
  60. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  61. let bookmark = (item as? KMBookmark) ?? data
  62. return bookmark?.objectOfChidren(index: index) as Any
  63. }
  64. }
  65. extension KMBookmarkOutlineView: NSOutlineViewDelegate {
  66. // MARK: - NSOutlineViewDelegate
  67. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  68. guard let column = tableColumn else { return nil }
  69. guard let item = item as? KMBookmark else { return nil }
  70. let identifier = column.identifier
  71. var cell: NSTableCellView = NSTableCellView()
  72. if item.bookmarkType == .separator {
  73. cell = KMBookmarkOutlineSeparatorCellView()
  74. } else {
  75. switch identifier {
  76. case kLabelIdentifier: do {
  77. cell = KMBookmarkOutlineTitleCellView()
  78. let cell = cell as! KMBookmarkOutlineTitleCellView
  79. cell.titleLabel.stringValue = item.label
  80. cell.iconImageView.image = item.icon
  81. }
  82. case kPageIdentifier: do {
  83. cell = KMBookmarkOutlinePageCellView()
  84. let cell = cell as! KMBookmarkOutlinePageCellView
  85. cell.titleLabel.stringValue = item.pageIndex.description
  86. }
  87. case kFileIdentifier: do {
  88. cell = KMBookmarkOutlineFileCellView()
  89. let cell = cell as! KMBookmarkOutlineFileCellView
  90. var stringValue = ""
  91. if item.bookmarkType == .folder ||
  92. item.bookmarkType == .session {
  93. let count = item.children.count
  94. stringValue = count == 1 ? NSLocalizedString("1 item", comment: "Bookmark folder description") : String.localizedStringWithFormat(NSLocalizedString("%ld items", comment: "Bookmark folder description"), count)
  95. } else {
  96. stringValue = item.fileURL?.path ?? ""
  97. }
  98. cell.titleLabel.stringValue = stringValue
  99. }
  100. default:
  101. break
  102. }
  103. }
  104. return cell
  105. }
  106. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  107. // 创建一个自定义的行视图
  108. let rowView = KMBookmarkOutlineRowView()
  109. guard let item = item as? KMBookmark else { return rowView }
  110. // 根据需要设置行视图的背景颜色
  111. if item.bookmarkType == .separator{
  112. rowView.hiddenLineView(isHidden: false)
  113. } else {
  114. rowView.hiddenLineView()
  115. }
  116. return rowView
  117. }
  118. }