KMPageEditThumbnailItem.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // KMPageEditThumbnailItem.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/6.
  6. //
  7. import Cocoa
  8. class KMPageEditThumbnailItem: NSCollectionViewItem {
  9. private let _textBoxSpace: CGFloat = 2
  10. private let _textLabelHeight: CGFloat = 15
  11. internal var backgroundView = NSView()
  12. internal lazy var pageBox : NSView = {
  13. let pageBox = NSView()
  14. pageBox.wantsLayer = true
  15. return pageBox
  16. }()
  17. internal lazy var textBox : NSView = {
  18. let textBox = NSView()
  19. return textBox
  20. }()
  21. internal lazy var pageTextLabel : NSTextField = {
  22. let label = NSTextField()
  23. label.isEditable = false
  24. label.isBordered = false
  25. label.isSelectable = false
  26. label.drawsBackground = false
  27. return label
  28. }()
  29. internal lazy var pageSizeTextLabel : NSTextField = {
  30. let label = NSTextField()
  31. label.isEditable = false
  32. label.isBordered = false
  33. label.drawsBackground = false
  34. return label
  35. }()
  36. internal lazy var pageView = KMPDFThumbnialPageView()
  37. var page: CPDFPage?
  38. var isShowPageSize: Bool = false
  39. var doubleClickAction: KMCommonBlock?
  40. var mouseDownAction: KMCommonBlock?
  41. var rightMouseDownAction: KMCommonBlock?
  42. override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
  43. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  44. }
  45. required init?(coder: NSCoder) {
  46. super.init(coder: coder)
  47. }
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. self.view.addSubview(self.backgroundView)
  51. self.view.addSubview(self.pageBox)
  52. self.pageBox.addSubview(self.pageView)
  53. self.view.addSubview(self.textBox)
  54. self.textBox.addSubview(self.pageTextLabel)
  55. self.textBox.addSubview(self.pageSizeTextLabel)
  56. self.backgroundView.wantsLayer = true
  57. self.backgroundView.layer?.cornerRadius = 8.0
  58. self.backgroundView.layer?.masksToBounds = true
  59. self.backgroundView.layer?.borderWidth = 1.0
  60. self.isSelected = false
  61. self.pageView.wantsLayer = true
  62. self.pageView.layer?.borderWidth = 1
  63. self.pageView.layer?.borderColor = NSColor.km_init(hex: "#0000001A").cgColor
  64. self.pageView.layer?.cornerRadius = 4
  65. self.pageView.layer?.masksToBounds = true
  66. self.pageTextLabel.font = NSFont.SFProTextRegularFont(14)
  67. self.pageTextLabel.textColor = NSColor.titleColor()
  68. self.pageSizeTextLabel.font = NSFont.SFProTextRegularFont(14)
  69. self.pageSizeTextLabel.textColor = NSColor.km_init(hex: "#616469")
  70. }
  71. override func mouseDown(with event: NSEvent) {
  72. super.mouseDown(with: event)
  73. if (event.clickCount > 1) {
  74. guard let callback = self.doubleClickAction else {
  75. return
  76. }
  77. callback()
  78. } else {
  79. if (event.modifierFlags.contains(.command) || event.modifierFlags.contains(.shift)) {
  80. return
  81. }
  82. if (self.isSelected == false) {
  83. return
  84. }
  85. guard let callback = self.mouseDownAction else {
  86. return
  87. }
  88. callback()
  89. }
  90. }
  91. override func rightMouseDown(with event: NSEvent) {
  92. if (self.isSelected == true) {
  93. super.rightMouseDown(with: event)
  94. return
  95. }
  96. guard let callback = self.rightMouseDownAction else {
  97. super.rightMouseDown(with: event)
  98. return
  99. }
  100. callback()
  101. super.rightMouseDown(with: event)
  102. }
  103. override var isSelected: Bool {
  104. get {
  105. return super.isSelected
  106. }
  107. set {
  108. super.isSelected = newValue
  109. if (newValue) {
  110. self.backgroundView.layer?.backgroundColor = NSColor.km_init(hex: "#1770F41A").cgColor
  111. self.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  112. } else {
  113. self.backgroundView.layer?.backgroundColor = NSColor.clear.cgColor
  114. self.backgroundView.layer?.borderColor = NSColor.clear.cgColor
  115. }
  116. }
  117. }
  118. func setPage(page:CPDFPage!) {
  119. self.page = page
  120. self.pageView.page = page
  121. let index = page.document.index(for: page) + 1
  122. self.pageTextLabel.stringValue = "\(index)"
  123. let rect = page.bounds(for: .cropBox)
  124. let width = KMPageSizeTool.conversion(withUnit: "mm", value: rect.width/595*210)
  125. let height = KMPageSizeTool.conversion(withUnit: "mm", value: rect.height/842*297)
  126. if (page.rotation == 90 || page.rotation == 270 || page.rotation == -90 || page.rotation == -270) {
  127. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", height.stringToCGFloat(), width.stringToCGFloat())
  128. } else {
  129. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", width.stringToCGFloat(), height.stringToCGFloat())
  130. }
  131. self.updateFrame(page: page)
  132. }
  133. func updateFrame(page: CPDFPage) {
  134. let viewWidth: CGFloat = NSWidth(self.view.bounds)
  135. let viewHeight: CGFloat = NSHeight(self.view.bounds)
  136. let backgroundViewX: CGFloat = 12;
  137. let backgroundViewY: CGFloat = 24;
  138. let backgroundViewW: CGFloat = viewWidth - backgroundViewX * 2;
  139. let backgroundViewH: CGFloat = viewHeight - 24;
  140. self.backgroundView.frame = NSMakeRect(backgroundViewX, backgroundViewY, backgroundViewW, backgroundViewH)
  141. var bounds = page.bounds
  142. let transform = page.transform()
  143. bounds = bounds.applying(transform)
  144. var size = NSMakeSize(backgroundViewW-32, backgroundViewH-46)
  145. if (self.isShowPageSize == false) {
  146. size = NSMakeSize(backgroundViewW-32, backgroundViewH-72)
  147. }
  148. if (page.rotation == -90 || page.rotation == -270) {
  149. let height = bounds.size.height
  150. bounds.size.height = bounds.size.width
  151. bounds.size.width = height
  152. }
  153. if (page.rotation == -90 || page.rotation == -270) {
  154. let height = size.height
  155. size.height = size.width
  156. size.width = height
  157. }
  158. if bounds.size.width > bounds.size.height {
  159. if (bounds.size.width > 0) {
  160. size.height = size.width * bounds.size.height / bounds.size.width
  161. }
  162. } else {
  163. if (bounds.size.height > 0) {
  164. size.width = size.height * bounds.size.width / bounds.size.height
  165. }
  166. }
  167. let height = _textLabelHeight
  168. let labelBoxY: CGFloat = _textBoxSpace + NSMinY(self.backgroundView.frame)+6
  169. let labelSize = NSMakeSize(size.width, CGFloat(MAXFLOAT))
  170. let textSize = self.pageTextLabel.sizeThatFits(labelSize)
  171. if self.isShowPageSize == false {
  172. self.pageSizeTextLabel.sizeToFit()
  173. let pageSizeLabelSize = self.pageSizeTextLabel.frame.size
  174. let width = max(textSize.width, pageSizeLabelSize.width) + 5
  175. self.pageSizeTextLabel.isHidden = false
  176. self.pageSizeTextLabel.frame = NSMakeRect((width-pageSizeLabelSize.width)*0.5, 4, pageSizeLabelSize.width, 22);
  177. self.pageTextLabel.frame = NSMakeRect((width-textSize.width)*0.5, self.pageSizeTextLabel.frame.maxY+8, textSize.width, height);
  178. let boxH: CGFloat = 48
  179. self.textBox.frame = NSMakeRect((viewWidth - width)*0.5, labelBoxY, width, boxH)
  180. } else {
  181. self.pageSizeTextLabel.isHidden = true
  182. let textWidth = textSize.width + 5
  183. let boxH: CGFloat = 22
  184. self.textBox.frame = NSMakeRect((viewWidth-textWidth)*0.5, labelBoxY, textWidth, boxH)
  185. self.pageTextLabel.frame = NSMakeRect((textWidth-textSize.width)*0.5, 4, textSize.width, height);
  186. }
  187. let margin: CGFloat = 16 + NSMinX(self.backgroundView.frame)
  188. let pageBoxY: CGFloat = self.textBox.frame.maxY+8
  189. let pageBoxW: CGFloat = viewWidth-margin*2
  190. let pageBoxH: CGFloat = viewHeight-pageBoxY-8
  191. self.pageBox.frame = NSMakeRect(margin, pageBoxY, pageBoxW, pageBoxH)
  192. var pageX: CGFloat = (NSWidth(self.pageBox.frame)-size.width)*0.5
  193. if (pageX < 0) {
  194. let tempWidth = size.width + pageX * 2
  195. let scale = tempWidth / size.width
  196. size.width = size.width * scale
  197. size.height = size.height * scale
  198. pageX = 0
  199. }
  200. let pageY: CGFloat = (NSHeight(self.pageBox.frame)-size.height)*0.5
  201. self.pageView.frame = NSMakeRect(pageX, pageY, size.width, size.height)
  202. }
  203. }