KMPageEditThumbnailView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // KMPageEditThumbnailView.swift
  3. // PDF Reader Pro
  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. var area: NSTrackingArea?
  14. override var selectionIndexPaths: Set<IndexPath> {
  15. get {
  16. return super.selectionIndexPaths
  17. }
  18. set {
  19. super.selectionIndexPaths = newValue
  20. // 通知外面
  21. guard let callback = self.selectionDidChange else {
  22. return
  23. }
  24. callback(self.collectionView.selectionIndexPaths)
  25. }
  26. }
  27. override func initDefaultValue() {
  28. super.initDefaultValue()
  29. self.itemSize = NSMakeSize(208, 294)
  30. self.defaultThumbnailSzie = self.thumbnailSzie
  31. self.isShowPageSize = true
  32. self.collectionView.km.register(cell: KMPageEditThumbnailItem.self)
  33. self.collectionView.allowsMultipleSelection = true
  34. }
  35. func selectPages(at indexpaths: Set<IndexPath>) {
  36. self.selectionIndexPaths = indexpaths
  37. }
  38. public func canZoomOut() -> Bool {
  39. return self.itemSize.width <= self.defaultItemSize.width
  40. }
  41. public func zoomOut() {
  42. if (self.canZoomOut() == false) {
  43. return
  44. }
  45. let indexpaths = self.collectionView.selectionIndexPaths
  46. if (self.itemSize.width < self.defaultItemSize.width) {
  47. self.itemSize = self.defaultItemSize
  48. self.thumbnailSzie = self.defaultThumbnailSzie!
  49. } else if (self.itemSize.width == self.defaultItemSize.width) {
  50. self.itemSize = NSSize(width: self.defaultItemSize.width*1.5, height: self.defaultItemSize.height*1.5)
  51. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie!.width*1.5, height: self.defaultThumbnailSzie!.height*1.5)
  52. }
  53. self.reloadData()
  54. self.collectionView.selectionIndexPaths = indexpaths
  55. }
  56. public func canZoomIn() -> Bool {
  57. return self.itemSize.width >= self.defaultItemSize.width
  58. }
  59. public func zoomIn() {
  60. if (self.canZoomIn() == false) {
  61. return
  62. }
  63. let indexpaths = self.collectionView.selectionIndexPaths
  64. if (self.itemSize.width > self.defaultItemSize.width) {
  65. self.itemSize = self.defaultItemSize
  66. self.thumbnailSzie = self.defaultThumbnailSzie!
  67. } else if (self.itemSize.width == self.defaultItemSize.width) {
  68. self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
  69. self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie!.width*0.5, height: self.defaultThumbnailSzie!.height*0.5)
  70. }
  71. self.reloadData()
  72. self.collectionView.selectionIndexPaths = indexpaths
  73. }
  74. override func updateTrackingAreas() {
  75. super.updateTrackingAreas()
  76. if let _area = self.area, _area.rect.isEmpty == false {
  77. if (_area.rect.equalTo(self.bounds)) {
  78. return
  79. }
  80. }
  81. if (self.area != nil) {
  82. self.removeTrackingArea(self.area!)
  83. self.area = nil
  84. }
  85. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  86. self.addTrackingArea(self.area!)
  87. // self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
  88. }
  89. override func mouseMoved(with event: NSEvent) {
  90. super.mouseMoved(with: event)
  91. self.window?.mouseMoved(with: event)
  92. }
  93. }
  94. extension KMPageEditThumbnailView {
  95. override func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  96. super.collectionView(collectionView, didSelectItemsAt: indexPaths)
  97. DispatchQueue.main.async {
  98. guard let callback = self.selectionDidChange else {
  99. return
  100. }
  101. callback(collectionView.selectionIndexPaths)
  102. }
  103. }
  104. override func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  105. super.collectionView(collectionView, didDeselectItemsAt: indexPaths)
  106. DispatchQueue.main.async {
  107. guard let callback = self.selectionDidChange else {
  108. return
  109. }
  110. callback(collectionView.selectionIndexPaths)
  111. }
  112. }
  113. }