KMPageEditThumbnailItem.swift 10.0 KB

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