KMPageEditThumbnailItem.swift 11 KB

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