KMPageEditThumbnailItem.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.pageTextLabel.textColor = NSColor.white
  140. self.pageTextLabel.layer?.backgroundColor = KMAppearance.Layout.m_1Color().cgColor
  141. if KMAppearance.isDarkMode() {
  142. self.pageBox.layer?.backgroundColor = KMAppearance.Layout.m_1Color().withAlphaComponent(0.2).cgColor
  143. } else {
  144. self.pageBox.layer?.backgroundColor = KMAppearance.Layout.m_1Color().withAlphaComponent(0.2).cgColor
  145. }
  146. } else {
  147. self.pageBox.layer?.backgroundColor = .clear
  148. self.pageTextLabel.textColor = .labelColor
  149. self.pageTextLabel.layer?.backgroundColor = .clear
  150. }
  151. }
  152. }
  153. func setPage(page:CPDFPage!) {
  154. self.page = page
  155. self.pageView.page = page
  156. let index = page.document.index(for: page) + 1
  157. self.pageTextLabel.stringValue = "\(index)"
  158. let rect = page.bounds(for: .cropBox)
  159. let width = KMPageSizeTool.conversion(withUnit: "mm", value: rect.width/595*210)
  160. let height = KMPageSizeTool.conversion(withUnit: "mm", value: rect.height/842*297)
  161. if (page.rotation == 90 || page.rotation == 270 || page.rotation == -90 || page.rotation == -270) {
  162. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", height.stringToCGFloat(), width.stringToCGFloat())
  163. } else {
  164. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", width.stringToCGFloat(), height.stringToCGFloat())
  165. }
  166. self.updateFrame(page: page)
  167. }
  168. func updateFrame(page: CPDFPage) {
  169. let viewWidth: CGFloat = NSWidth(self.view.bounds)
  170. let viewHeight: CGFloat = NSHeight(self.view.bounds)
  171. let backgroundViewX: CGFloat = 0;
  172. let backgroundViewY: CGFloat = 24;
  173. let backgroundViewW: CGFloat = viewWidth - backgroundViewX * 2;
  174. let backgroundViewH: CGFloat = viewHeight - 24;
  175. // self.backgroundView.frame = NSMakeRect(backgroundViewX, backgroundViewY, backgroundViewW, backgroundViewH)
  176. var bounds = page.bounds
  177. let transform = page.transform()
  178. bounds = bounds.applying(transform)
  179. var size = NSMakeSize(backgroundViewW, backgroundViewH-26)
  180. if (self.isShowPageSize == false) {
  181. size = NSMakeSize(backgroundViewW, backgroundViewH-52)
  182. }
  183. if (page.rotation == -90 || page.rotation == -270) {
  184. let height = bounds.size.height
  185. bounds.size.height = bounds.size.width
  186. bounds.size.width = height
  187. }
  188. if (page.rotation == -90 || page.rotation == -270) {
  189. let height = size.height
  190. size.height = size.width
  191. size.width = height
  192. }
  193. if bounds.size.width > bounds.size.height {
  194. if (bounds.size.width > 0) {
  195. size.height = size.width * bounds.size.height / bounds.size.width
  196. }
  197. } else {
  198. if (bounds.size.height > 0) {
  199. size.width = size.height * bounds.size.width / bounds.size.height
  200. }
  201. }
  202. let height = _textLabelHeight
  203. let labelBoxY: CGFloat = _textBoxSpace + NSMinY(self.backgroundView.frame)+6
  204. let labelSize = NSMakeSize(size.width, CGFloat(MAXFLOAT))
  205. self.pageTextLabel.sizeToFit()
  206. let textSize = self.pageTextLabel.frame.size
  207. if self.isShowPageSize == false {
  208. self.pageSizeTextLabel.sizeToFit()
  209. let pageSizeLabelSize = self.pageSizeTextLabel.frame.size
  210. let width = max(textSize.width, pageSizeLabelSize.width) + 5
  211. self.pageSizeTextLabel.isHidden = false
  212. self.pageSizeTextLabel.frame = NSMakeRect((width-pageSizeLabelSize.width)*0.5, 4, pageSizeLabelSize.width, 22);
  213. self.pageTextLabel.frame = NSMakeRect((width-textSize.width)*0.5-5, self.pageSizeTextLabel.frame.maxY+8, textSize.width+10, height);
  214. let boxH: CGFloat = 48
  215. self.textBox.frame = NSMakeRect((viewWidth - width)*0.5, labelBoxY, width, boxH)
  216. } else {
  217. self.pageSizeTextLabel.isHidden = true
  218. let textWidth = textSize.width + 5
  219. let boxH: CGFloat = 22
  220. self.textBox.frame = NSMakeRect((viewWidth-textWidth)*0.5, labelBoxY, textWidth, boxH)
  221. self.pageTextLabel.frame = NSMakeRect((textWidth-textSize.width)*0.5-5, 4, textSize.width+10, height);
  222. }
  223. let margin: CGFloat = NSMinX(self.backgroundView.frame)
  224. let pageBoxY: CGFloat = self.textBox.frame.maxY+8
  225. let pageBoxW: CGFloat = viewWidth-margin*2
  226. let pageBoxH: CGFloat = viewHeight-pageBoxY
  227. self.pageBox.frame = NSMakeRect(margin, pageBoxY, pageBoxW, pageBoxH)
  228. var pageX: CGFloat = (NSWidth(self.pageBox.frame)-size.width)*0.5
  229. if (pageX < 0) {
  230. let tempWidth = size.width + pageX * 2
  231. let scale = tempWidth / size.width
  232. size.width = size.width * scale
  233. size.height = size.height * scale
  234. pageX = 0
  235. }
  236. let pageY: CGFloat = (NSHeight(self.pageBox.frame)-size.height)*0.5
  237. self.pageView.frame = NSMakeRect(pageX, pageY, size.width, size.height)
  238. }
  239. }
  240. extension KMPageEditThumbnailItem: KMReusable {}