KMPageEditThumbnailView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // KMPageEditThumbnailView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/6.
  6. //
  7. import Cocoa
  8. typealias KMPageEditThumbnailViewSelectionDidChange = (_ selectedIndexs: Set<IndexPath>) -> ()
  9. class KMPageEditThumbnailView: KMPDFThumbnailView {
  10. var selectionDidChange: KMPageEditThumbnailViewSelectionDidChange!
  11. private let defaultItemSize = NSMakeSize(208, 294)
  12. var itemSize: NSSize = NSMakeSize(208, 294)
  13. private var defaultThumbnailSzie: NSSize!
  14. var selectionIndexPaths: Set<IndexPath> {
  15. get {
  16. return self.collectionView.selectionIndexPaths
  17. }
  18. set {
  19. var indexpaths: Set<IndexPath> = []
  20. for indexpath in newValue {
  21. if (indexpath.section >= self.collectionView.numberOfSections) {
  22. continue
  23. }
  24. if (indexpath.item >= self.collectionView.numberOfItems(inSection: indexpath.section)) {
  25. continue
  26. }
  27. indexpaths.insert(indexpath)
  28. }
  29. self.collectionView.selectionIndexPaths = indexpaths
  30. // 通知外面
  31. guard let callback = self.selectionDidChange else {
  32. return
  33. }
  34. callback(self.collectionView.selectionIndexPaths)
  35. }
  36. }
  37. override init(frame frameRect: NSRect) {
  38. super.init(frame: frameRect)
  39. self.initDefalutValue()
  40. }
  41. required init?(coder: NSCoder) {
  42. super.init(coder: coder)
  43. self.initDefalutValue()
  44. }
  45. private func initDefalutValue() {
  46. self.collectionView.allowsMultipleSelection = true
  47. self.collectionView.register(KMPageEditThumbnailItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMPageEditThumbnailItem"))
  48. self.collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType(rawValue: "localForDraggedTypes"), .fileURL,.string,.pdf])
  49. self.collectionView.setDraggingSourceOperationMask([.copy, .delete], forLocal: false)
  50. self.defaultThumbnailSzie = self.thumbnailSzie
  51. self.isShowPageSize = true
  52. }
  53. func selectPages(at indexpaths: Set<IndexPath>) {
  54. self.selectionIndexPaths = indexpaths
  55. }
  56. public func refreshUI(indexs: Set<IndexPath> = []) {
  57. if (Thread.isMainThread) {
  58. if (indexs.isEmpty) {
  59. self.reloadData()
  60. } else {
  61. self.reloadData(indexs: indexs)
  62. }
  63. } else {
  64. DispatchQueue.main.async {
  65. if (indexs.isEmpty) {
  66. self.reloadData()
  67. } else {
  68. self.reloadData(indexs: indexs)
  69. }
  70. }
  71. }
  72. }
  73. public func zoomOut() {
  74. if (self.itemSize.width > self.defaultItemSize.width) {
  75. return
  76. }
  77. if (self.itemSize.width < self.defaultItemSize.width) {
  78. self.itemSize = self.defaultItemSize
  79. self.thumbnailSzie = self.defaultThumbnailSzie
  80. } else if (self.itemSize.width == self.defaultItemSize.width) {
  81. self.itemSize = NSSize(width: self.defaultItemSize.width*1.5, height: self.defaultItemSize.height*1.5)
  82. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie.width*1.5, height: self.defaultThumbnailSzie.height*1.5)
  83. }
  84. let indexpaths = self.collectionView.selectionIndexPaths
  85. self.refreshUI()
  86. self.collectionView.selectionIndexPaths = indexpaths
  87. }
  88. public func zoomIn() {
  89. if (self.itemSize.width < self.defaultItemSize.width) {
  90. return
  91. }
  92. if (self.itemSize.width > self.defaultItemSize.width) {
  93. self.itemSize = self.defaultItemSize
  94. self.thumbnailSzie = self.defaultThumbnailSzie
  95. } else if (self.itemSize.width == self.defaultItemSize.width) {
  96. self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
  97. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie.width*0.5, height: self.defaultThumbnailSzie.height*0.5)
  98. }
  99. let indexpaths = self.collectionView.selectionIndexPaths
  100. self.refreshUI()
  101. self.collectionView.selectionIndexPaths = indexpaths
  102. }
  103. // MARK: NSCollectionView 代理
  104. override func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  105. super.collectionView(collectionView, itemForRepresentedObjectAt: indexPath)
  106. }
  107. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  108. super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
  109. }
  110. override func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  111. super.collectionView(collectionView, didSelectItemsAt: indexPaths)
  112. DispatchQueue.main.async {
  113. guard let callback = self.selectionDidChange else {
  114. return
  115. }
  116. callback(self.collectionView.selectionIndexPaths)
  117. }
  118. }
  119. override func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  120. return super.collectionView(collectionView, shouldSelectItemsAt: indexPaths)
  121. }
  122. override func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  123. super.collectionView(collectionView, didDeselectItemsAt: indexPaths)
  124. DispatchQueue.main.async {
  125. guard let callback = self.selectionDidChange else {
  126. return
  127. }
  128. callback(self.collectionView.selectionIndexPaths)
  129. }
  130. }
  131. override func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
  132. super.collectionView(collectionView, writeItemsAt: indexPaths, to: pasteboard)
  133. }
  134. override func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  135. return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
  136. }
  137. override func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  138. super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
  139. }
  140. }