KMNThumbnailCollectionView.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // KMNCollectionView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/27.
  6. //
  7. import Cocoa
  8. class KMNThumbnailCollectionView: NSCollectionView {
  9. var menuClickedAction: ((_ clickPoint: NSPoint) -> NSMenu)?
  10. var collectionSelectChanges: (() -> Void)?
  11. override func draw(_ dirtyRect: NSRect) {
  12. super.draw(dirtyRect)
  13. // Drawing code here.
  14. }
  15. override func menu(for event: NSEvent) -> NSMenu? {
  16. let point = event.locationInWindow
  17. let rightIndex = indexPathForItem(at: convert(point, from: nil))
  18. if rightIndex != nil {
  19. let cellView = item(at: rightIndex!) as? KMNThumbnailCollectionViewItem
  20. if(cellView != nil) {
  21. if(cellView?.isSelected == true) {
  22. } else {
  23. self.selectionIndexPaths = NSMutableSet() as! Set<IndexPath>
  24. var indexPaths: Set<IndexPath> = []
  25. indexPaths.insert(rightIndex!)
  26. self.selectionIndexPaths = indexPaths
  27. }
  28. } else {
  29. self.selectionIndexPaths = NSMutableSet() as! Set<IndexPath>
  30. }
  31. } else {
  32. self.selectionIndexPaths = NSMutableSet() as! Set<IndexPath>
  33. }
  34. if menuClickedAction != nil {
  35. let menu = menuClickedAction!(point)
  36. return menu
  37. }
  38. return super.menu(for: event)
  39. }
  40. override var selectionIndexPaths: Set<IndexPath> {
  41. didSet {
  42. collectionSelectChanges?()
  43. }
  44. }
  45. }