KMPageEditThumbnailItem.swift 10 KB

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