KMPDFThumbnailView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // KMPDFThumbnailView.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/15.
  6. //
  7. class KMPDFThumbnailView: KMThumbnailView {
  8. var document: CPDFDocument?
  9. var isShowPageSize = false
  10. var thumbnailSzie = NSZeroSize
  11. //标记线
  12. var isNeedMarkerLine: Bool = false
  13. var markerLineView: NSView = NSView()
  14. var markBeginIndexes: IndexSet = IndexSet()
  15. //注释状态
  16. var annotationShowState: KMAnnotationViewShowType = .none {
  17. didSet {
  18. self.collectionView.reloadData()
  19. }
  20. }
  21. //悬浮
  22. var hoverIndex: Int = -1
  23. override func initDefaultValue() {
  24. super.initDefaultValue()
  25. self.wantsLayer = true
  26. self.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  27. self.thumbnailSzie = CGSize(width: 120, height: 155)
  28. self.minimumLineSpacing = 8
  29. self.register(KMPDFThumbnailItem.self)
  30. }
  31. override func initSubViews() {
  32. super.initSubViews()
  33. self.markerLineView.wantsLayer = true
  34. self.markerLineView.layer?.backgroundColor = NSColor(hex: "#1770F4").cgColor
  35. self.markerLineView.frame = CGRectMake(0, 0, 100, 2)
  36. self.collectionView.addSubview(markerLineView)
  37. self.markerLineView.isHidden = true
  38. }
  39. }
  40. extension KMPDFThumbnailView {
  41. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  42. if let size_ = self.delegate?.thumbnailView?(thumbanView: self, sizeForItemAt: indexPath) {
  43. return size_
  44. }
  45. let page = self.document?.page(at: UInt(indexPath.item))
  46. let height = KMPDFThumbnailItem.sizeToFit(size: self.thumbnailSzie, page: page!, isShow: self.isShowPageSize)
  47. return NSMakeSize(collectionView.frame.size.width - 20, CGFloat(height))
  48. }
  49. override func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  50. if let count = self.document?.pageCount {
  51. return Int(count)
  52. }
  53. return 0
  54. }
  55. override func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  56. if let item = self.delegate?.thumbnailView?(thumbanView: self, itemForRepresentedObjectAt: indexPath) {
  57. return item
  58. }
  59. let cellView: KMPDFThumbnailItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: NSStringFromClass(KMPDFThumbnailItem.self)), for: indexPath) as! KMPDFThumbnailItem
  60. cellView.thumbnailView = self
  61. if let page_ = self.document?.page(at: UInt(indexPath.item)) {
  62. cellView.page = page_
  63. }
  64. cellView.annotationShowState = self.annotationShowState
  65. if indexPath.item == hoverIndex {
  66. cellView.hover = true
  67. } else {
  68. cellView.hover = false
  69. }
  70. cellView.mouseDownAction = { [unowned self] (view, event) in
  71. self.delegate?.thumbnailView?(thumbanView: self, didSelectItemAt: indexPath, object: event)
  72. }
  73. cellView.rightMouseDownAction = { [unowned self] (view, event) in
  74. self.delegate?.thumbnailView?(thumbanView: self, rightMouseDidClick: indexPath, item: view, object: event)
  75. }
  76. cellView.hoverCallBack = { [unowned self] view, mouseEntered in
  77. if let _ = self.collectionView.item(at: hoverIndex)?.view {
  78. let tempCell = self.collectionView.item(at: hoverIndex) as? KMPDFThumbnailItem
  79. tempCell!.hover = false
  80. }
  81. if mouseEntered {
  82. hoverIndex = indexPath.item
  83. if let _ = self.collectionView.item(at: hoverIndex)?.view {
  84. let tempCell = self.collectionView.item(at: hoverIndex) as? KMPDFThumbnailItem
  85. tempCell!.hover = true
  86. }
  87. } else {
  88. hoverIndex = -1
  89. }
  90. }
  91. return cellView
  92. }
  93. override func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {
  94. if self.isNeedMarkerLine {
  95. self.markBeginIndexes = collectionView.selectionIndexes
  96. }
  97. return super.collectionView(collectionView, draggingSession: session, willBeginAt: screenPoint, forItemsAt: indexes)
  98. }
  99. override func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  100. if self.isNeedMarkerLine {
  101. if self.markBeginIndexes.count != 0 {
  102. if !self.markBeginIndexes.contains(proposedDropIndexPath.pointee.item) {
  103. //标记线
  104. var rect = self.collectionView.frameForItem(at: proposedDropIndexPath.pointee.item)
  105. rect.size.height = 2
  106. self.markerLineView.frame = rect
  107. self.markerLineView.isHidden = false
  108. }
  109. }
  110. }
  111. return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
  112. }
  113. override func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  114. self.markerLineView.isHidden = true
  115. self.markBeginIndexes = IndexSet()
  116. let pboard = draggingInfo.draggingPasteboard
  117. if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
  118. let dragIndexPath = self.dragedIndexPaths.first
  119. if (dragIndexPath == nil) {
  120. return false
  121. }
  122. let dragIndex = dragIndexPath!.item
  123. let toIndex = max(0, indexPath.item)
  124. if dragIndex < 0 || dragIndex > self.document!.pageCount {
  125. return false
  126. }
  127. if (toIndex >= self.document!.pageCount) {
  128. return false
  129. }
  130. if dragIndex == toIndex {
  131. return false
  132. }
  133. return super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
  134. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  135. //获取url
  136. var array: [URL] = []
  137. for item: NSPasteboardItem in pboard.pasteboardItems! {
  138. let string: String = item.string(forType: NSPasteboard.PasteboardType.fileURL)!
  139. let url = NSURL(string: string)
  140. /// MARK: 暂时支持PDF格式
  141. if (url?.pathExtension?.lowercased() == "pdf") {
  142. array.append(url! as URL)
  143. }
  144. }
  145. self.delegate?.thumbnailView?(thumbanView: self, didDragAddFiles: array, indexpath: indexPath)
  146. return true
  147. }
  148. return false
  149. }
  150. }