KMMergeCollectionPageViewItem.swift 5.2 KB

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