KMNThumbnailCollectionViewItem.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. DispatchQueue.main.async { [weak self] in
  51. self?.cardFileView.properties.state = self?.isSelected ?? false ? .pressed : .normal
  52. self?.cardFileView.reloadData()
  53. }
  54. }
  55. }
  56. public var isShowFileSize :Bool = false
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. }
  60. override func mouseDown(with event: NSEvent) {
  61. super.mouseDown(with: event)
  62. if (event.clickCount > 1) {
  63. doubleClickBack?()
  64. }
  65. }
  66. }