KMPageEditThumbnailView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. private var defaultThumbnailSzie: NSSize?
  13. override var selectionIndexPaths: Set<IndexPath> {
  14. get {
  15. return super.selectionIndexPaths
  16. }
  17. set {
  18. super.selectionIndexPaths = newValue
  19. // 通知外面
  20. guard let callback = self.selectionDidChange else {
  21. return
  22. }
  23. callback(self.collectionView.selectionIndexPaths)
  24. }
  25. }
  26. override func initDefaultValue() {
  27. super.initDefaultValue()
  28. self.itemSize = NSMakeSize(208, 294)
  29. self.defaultThumbnailSzie = self.thumbnailSzie
  30. self.isShowPageSize = true
  31. self.register(KMPageEditThumbnailItem.self)
  32. self.collectionView.allowsMultipleSelection = true
  33. }
  34. func selectPages(at indexpaths: Set<IndexPath>) {
  35. self.selectionIndexPaths = indexpaths
  36. }
  37. public func canZoomOut() -> Bool {
  38. return self.itemSize.width <= self.defaultItemSize.width
  39. }
  40. public func zoomOut() {
  41. if (self.canZoomOut() == false) {
  42. return
  43. }
  44. let indexpaths = self.collectionView.selectionIndexPaths
  45. if (self.itemSize.width < self.defaultItemSize.width) {
  46. self.itemSize = self.defaultItemSize
  47. self.thumbnailSzie = self.defaultThumbnailSzie!
  48. } else if (self.itemSize.width == self.defaultItemSize.width) {
  49. self.itemSize = NSSize(width: self.defaultItemSize.width*1.5, height: self.defaultItemSize.height*1.5)
  50. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie!.width*1.5, height: self.defaultThumbnailSzie!.height*1.5)
  51. }
  52. self.reloadData()
  53. self.collectionView.selectionIndexPaths = indexpaths
  54. }
  55. public func canZoomIn() -> Bool {
  56. return self.itemSize.width >= self.defaultItemSize.width
  57. }
  58. public func zoomIn() {
  59. if (self.canZoomIn() == false) {
  60. return
  61. }
  62. let indexpaths = self.collectionView.selectionIndexPaths
  63. if (self.itemSize.width > self.defaultItemSize.width) {
  64. self.itemSize = self.defaultItemSize
  65. self.thumbnailSzie = self.defaultThumbnailSzie!
  66. } else if (self.itemSize.width == self.defaultItemSize.width) {
  67. self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
  68. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie!.width*0.5, height: self.defaultThumbnailSzie!.height*0.5)
  69. }
  70. self.reloadData()
  71. self.collectionView.selectionIndexPaths = indexpaths
  72. }
  73. }
  74. extension KMPageEditThumbnailView {
  75. override func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  76. super.collectionView(collectionView, didSelectItemsAt: indexPaths)
  77. DispatchQueue.main.async {
  78. guard let callback = self.selectionDidChange else {
  79. return
  80. }
  81. callback(collectionView.selectionIndexPaths)
  82. }
  83. }
  84. override func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  85. super.collectionView(collectionView, didDeselectItemsAt: indexPaths)
  86. DispatchQueue.main.async {
  87. guard let callback = self.selectionDidChange else {
  88. return
  89. }
  90. callback(collectionView.selectionIndexPaths)
  91. }
  92. }
  93. }