KMNThumbnailCollectionViewItem.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // KMNThumbnailCollectionViewItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/21.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNThumbnailCollectionViewItem: NSCollectionViewItem {
  10. @IBOutlet var cardFileView: ComponentCardFile!
  11. var doubleClickBack: (() -> Void)?
  12. public var thumbnailMode: KMNThumbnail = KMNThumbnail.init() {
  13. didSet {
  14. var orgPageHeight = thumbnailMode.pageSize.height
  15. var orgPagewidth = thumbnailMode.pageSize.width
  16. if (thumbnailMode.thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  17. orgPageHeight = thumbnailMode.pageSize.width
  18. orgPagewidth = thumbnailMode.pageSize.height
  19. }
  20. cardFileView.properties = ComponentCardFileProperty(folder: false,
  21. state: .normal,
  22. icon: NSImage.init(named: "KMNImageNameThumbnailPlaceImage"),
  23. text: String(format: "%d", (thumbnailMode.pageIndex+1)),
  24. subText: String(format: "%.0f x %.0fmm", (thumbnailMode.pageSize.width / 595 * 210), (thumbnailMode.pageSize.height / 842 * 297)),
  25. markIcon:NSImage.init(named: "KMNImageNameThumbnailBookMark"))
  26. cardFileView.properties.isShowSubText = isShowFileSize
  27. cardFileView.properties.propertyInfo.infoLabelHeight = infoThumTitleHeight
  28. cardFileView.properties.isShowMarkIcon = thumbnailMode.isHoveBookMark
  29. cardFileView.reloadData()
  30. let maxThumCellWidth = self.view.frame.size.width - infoThumTitleBottom * 2
  31. var maxThumCellHeight = self.view.frame.size.height
  32. maxThumCellHeight -= cardFileView.properties.propertyInfo.infoLabelHeight
  33. maxThumCellHeight -= cardFileView.properties.propertyInfo.subLabelHeight
  34. maxThumCellHeight -= (cardFileView.properties.propertyInfo.topIconOffset + cardFileView.properties.propertyInfo.bottomOffset + cardFileView.properties.propertyInfo.topLabelOffset) //间隔
  35. let minimumValue = min(maxThumCellHeight/orgPageHeight, maxThumCellWidth/orgPagewidth)
  36. let relCellHeight = orgPageHeight * minimumValue
  37. let relCellWidth = orgPagewidth * minimumValue
  38. thumbnailMode.generateThumImage { image in
  39. self.cardFileView.properties.propertyInfo.iconImageWidth = relCellWidth
  40. self.cardFileView.properties.propertyInfo.iconImageHeight = relCellHeight
  41. if(image != nil) {
  42. self.cardFileView.properties.icon = image
  43. }
  44. self.cardFileView.reloadData()
  45. }
  46. }
  47. }
  48. override var isSelected: Bool{
  49. didSet {
  50. cardFileView.properties.state = isSelected ? .pressed : .normal
  51. cardFileView.reloadData()
  52. }
  53. }
  54. public var isShowFileSize :Bool = false
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. }
  58. override func mouseDown(with event: NSEvent) {
  59. super.mouseDown(with: event)
  60. if (event.clickCount > 1) {
  61. doubleClickBack?()
  62. }
  63. }
  64. }