KMMergeCollectionViewItem.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // KMMergeCollectionViewItem.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/11/23.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. typealias KMMergeCollectionViewItemDoubleClick = (_ viewItem: AnyObject)->()
  10. typealias KMMergeCollectionViewItemPageRangeClick = (_ viewItem: AnyObject)->()
  11. class KMMergeCollectionViewItem: NSCollectionViewItem {
  12. lazy var iconImageView = NSImageView()
  13. lazy var backgroundView = NSView()
  14. lazy var selectedView = NSView()
  15. lazy var pageView = KMPDFThumbnialPageView()
  16. private var expandButton = NSButton()
  17. private var pageRangeButton = NSButton()
  18. lazy var nameLabel = NSTextField(labelWithString: "")
  19. var doubleClick: KMMergeCollectionViewItemDoubleClick!
  20. var pageRangeClick: KMMergeCollectionViewItemPageRangeClick!
  21. private var myModel: KMMergeFileModel!
  22. var model: KMMergeFileModel {
  23. get {
  24. myModel
  25. }
  26. set {
  27. myModel = newValue
  28. pageView.page = newValue.page;
  29. nameLabel.stringValue = model.documentURL.lastPathComponent.components(separatedBy: ".").first!
  30. if newValue.pageRange == .custom {
  31. pageRangeButton.title = model.pageRangeString
  32. } else {
  33. pageRangeButton.title = (newValue.pageRange).rawValue
  34. }
  35. }
  36. }
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. view.addSubview(backgroundView)
  40. view.addSubview(selectedView)
  41. view.addSubview(pageView)
  42. view.addSubview(expandButton)
  43. view.addSubview(pageRangeButton)
  44. view.addSubview(nameLabel)
  45. selectedView.wantsLayer = true
  46. selectedView.layer?.backgroundColor = NSColor.white.cgColor
  47. selectedView.layer?.borderWidth = 1
  48. selectedView.layer?.borderColor = NSColor.white.cgColor
  49. selectedView.layer?.cornerRadius = 8
  50. nameLabel.alignment = .center
  51. nameLabel.lineBreakMode = .byTruncatingTail
  52. expandButton.wantsLayer = true
  53. expandButton.isBordered = false
  54. expandButton.title = NSLocalizedString("Expand", comment: "")
  55. expandButton.layer?.backgroundColor = NSColor.white.cgColor;
  56. expandButton.target = self
  57. expandButton.action = #selector(expandButtonAction)
  58. pageRangeButton.wantsLayer = true
  59. pageRangeButton.isBordered = false
  60. pageRangeButton.layer?.backgroundColor = NSColor.lightGray.cgColor
  61. pageRangeButton.target = self
  62. pageRangeButton.action = #selector(pageRangeButtonAction)
  63. let clickGestureRecongnizer = NSClickGestureRecognizer()
  64. clickGestureRecongnizer.numberOfClicksRequired = 2
  65. // view.addGestureRecognizer(clickGestureRecongnizer)
  66. clickGestureRecongnizer.target = self
  67. clickGestureRecongnizer.action = #selector(clickGestureRecongnizerAction)
  68. }
  69. override func viewDidLayout() {
  70. super.viewDidLayout()
  71. let width = NSWidth(view.bounds)
  72. let height = NSHeight(view.bounds)
  73. iconImageView.frame = view.bounds
  74. backgroundView.frame = view.bounds
  75. let flodSize: CGFloat = 28
  76. expandButton.frame = NSMakeRect(width-flodSize-10, height-flodSize-10, flodSize, flodSize)
  77. pageRangeButton.frame = NSMakeRect(15, 10+20+10, width-30, 28)
  78. selectedView.frame = NSMakeRect(15, 20, width-30, height-20)
  79. nameLabel.frame = NSMakeRect(0, 0, width, 20)
  80. let border: CGFloat = 10
  81. var bounds: NSRect = NSZeroRect
  82. if (myModel != nil) {
  83. bounds = myModel.page.bounds
  84. }
  85. if (bounds.size.width == 0) {
  86. return
  87. }
  88. if (bounds.size.height == 0) {
  89. return
  90. }
  91. var pageSelectionSize = CGSize(width: self.view.frame.size.width-30, height: self.view.frame.size.height-40)
  92. // pageSelectionSize.height -= 40
  93. var size = NSMakeSize(pageSelectionSize.width - 2 * border, pageSelectionSize.height - 2 * border)
  94. let minScale = min(size.width/bounds.size.width, size.height/bounds.size.height)
  95. size.width = bounds.size.width * minScale
  96. size.height = bounds.size.height * minScale
  97. let pageViewX = (pageSelectionSize.width-size.width) * 0.5 + 15
  98. let pageViewY = (pageSelectionSize.height-size.height) * 0.5 + 20
  99. pageView.frame = NSMakeRect(pageViewX, pageViewY, size.width, size.height)
  100. }
  101. override var isSelected: Bool {
  102. get {
  103. super.isSelected
  104. }
  105. set {
  106. super.isSelected = newValue
  107. backgroundView.wantsLayer = true
  108. if newValue {
  109. // backgroundView.layer?.backgroundColor = NSColor.blue.cgColor
  110. selectedView.layer?.borderColor = NSColor.blue.cgColor
  111. } else {
  112. // backgroundView.layer?.backgroundColor = NSColor.clear.cgColor
  113. selectedView.layer?.borderColor = NSColor.white.cgColor
  114. }
  115. }
  116. }
  117. override func mouseDown(with event: NSEvent) {
  118. if event.clickCount == 2 {
  119. guard let callback = doubleClick else {
  120. return
  121. }
  122. callback(self)
  123. } else {
  124. super.mouseDown(with: event)
  125. }
  126. }
  127. @objc func clickGestureRecongnizerAction() {
  128. }
  129. @objc func expandButtonAction() {
  130. guard let callback = doubleClick else {
  131. return
  132. }
  133. callback(self)
  134. }
  135. @objc func pageRangeButtonAction() {
  136. guard let callback = pageRangeClick else {
  137. return
  138. }
  139. callback(self)
  140. }
  141. }