KMPageEditThumbnailItem.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 bkIcon = NSImageView()
  59. var page: CPDFPage?
  60. var isShowPageSize: Bool = false
  61. var doubleClickAction: KMCommonBlock?
  62. var mouseDownAction: KMCommonBlock?
  63. var rightMouseDownAction: KMCommonBlock?
  64. override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
  65. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  66. }
  67. required init?(coder: NSCoder) {
  68. super.init(coder: coder)
  69. }
  70. override func viewDidLoad() {
  71. super.viewDidLoad()
  72. (self.view as? KMPageEditThumbnailItem_View)?.pageView = self.pageView
  73. self.view.addSubview(self.backgroundView)
  74. self.view.addSubview(self.pageBox)
  75. self.pageBox.addSubview(self.pageView)
  76. self.view.addSubview(self.textBox)
  77. self.textBox.addSubview(self.pageTextLabel)
  78. self.textBox.addSubview(self.pageSizeTextLabel)
  79. self.pageTextLabel.wantsLayer = true
  80. self.pageTextLabel.layer?.cornerRadius = 2
  81. self.pageTextLabel.layer?.masksToBounds = true
  82. self.pageBox.wantsLayer = true
  83. self.pageBox.layer?.backgroundColor = NSColor(white: 192.0/255.0, alpha: 1.0).cgColor
  84. self.pageBox.layer?.cornerRadius = 6
  85. self.pageBox.layer?.masksToBounds = true
  86. // self.backgroundView.wantsLayer = true
  87. // self.backgroundView.layer?.cornerRadius = 8.0
  88. // self.backgroundView.layer?.masksToBounds = true
  89. // self.backgroundView.layer?.borderWidth = 1.0
  90. self.isSelected = false
  91. self.pageView.wantsLayer = true
  92. self.pageView.layer?.borderWidth = 1
  93. self.pageView.layer?.borderColor = NSColor.km_init(hex: "#0000001A").cgColor
  94. self.pageView.layer?.cornerRadius = 4
  95. self.pageView.layer?.masksToBounds = true
  96. self.pageTextLabel.font = NSFont.SFProTextRegularFont(14)
  97. self.pageTextLabel.textColor = NSColor.km_init(hex: "#252629")
  98. self.pageSizeTextLabel.font = NSFont.SFProTextRegularFont(14)
  99. self.pageSizeTextLabel.textColor = NSColor.km_init(hex: "#616469")
  100. self.pageBox.addSubview(self.bkIcon)
  101. self.bkIcon.wantsLayer = true
  102. // self.bkIcon.layer?.backgroundColor = .black
  103. self.bkIcon.image = NSImage(named: "KMImageNameBookmarkIcon")
  104. }
  105. override func mouseDown(with event: NSEvent) {
  106. super.mouseDown(with: event)
  107. if (event.clickCount > 1) {
  108. guard let callback = self.doubleClickAction else {
  109. return
  110. }
  111. callback()
  112. } else {
  113. if (event.modifierFlags.contains(.command) || event.modifierFlags.contains(.shift)) {
  114. return
  115. }
  116. if (self.isSelected == false) {
  117. return
  118. }
  119. guard let callback = self.mouseDownAction else {
  120. return
  121. }
  122. callback()
  123. }
  124. }
  125. override func rightMouseDown(with event: NSEvent) {
  126. if (self.isSelected == true) {
  127. super.rightMouseDown(with: event)
  128. return
  129. }
  130. guard let callback = self.rightMouseDownAction else {
  131. super.rightMouseDown(with: event)
  132. return
  133. }
  134. callback()
  135. super.rightMouseDown(with: event)
  136. }
  137. override var isSelected: Bool {
  138. get {
  139. return super.isSelected
  140. }
  141. set {
  142. super.isSelected = newValue
  143. if (newValue) {
  144. // self.backgroundView.layer?.backgroundColor = NSColor.km_init(hex: "#1770F41A").cgColor
  145. // self.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  146. self.pageTextLabel.textColor = .white
  147. self.pageTextLabel.layer?.backgroundColor = NSColor.systemBlue.cgColor
  148. if KMAppearance.isDarkMode() {
  149. self.pageBox.layer?.backgroundColor = NSColor(white: 84.0/255.0, alpha: 1.0).cgColor
  150. } else {
  151. self.pageBox.layer?.backgroundColor = NSColor(white: 192.0/255.0, alpha: 1.0).cgColor
  152. }
  153. } else {
  154. // self.backgroundView.layer?.backgroundColor = NSColor.clear.cgColor
  155. // self.backgroundView.layer?.borderColor = NSColor.clear.cgColor
  156. self.pageBox.layer?.backgroundColor = .clear
  157. self.pageTextLabel.textColor = .labelColor
  158. self.pageTextLabel.layer?.backgroundColor = .clear
  159. }
  160. }
  161. }
  162. func setPage(page:CPDFPage!) {
  163. self.page = page
  164. self.pageView.page = page
  165. let index = page.document.index(for: page) + 1
  166. self.pageTextLabel.stringValue = "\(index)"
  167. let rect = page.bounds(for: .cropBox)
  168. let width = KMPageSizeTool.conversion(withUnit: "mm", value: rect.width/595*210)
  169. let height = KMPageSizeTool.conversion(withUnit: "mm", value: rect.height/842*297)
  170. if (page.rotation == 90 || page.rotation == 270 || page.rotation == -90 || page.rotation == -270) {
  171. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", height.stringToCGFloat(), width.stringToCGFloat())
  172. } else {
  173. self.pageSizeTextLabel.stringValue = String(format: "%.f * %.f \(NSLocalizedString("mm", comment: ""))", width.stringToCGFloat(), height.stringToCGFloat())
  174. }
  175. self.updateFrame(page: page)
  176. }
  177. func updateFrame(page: CPDFPage) {
  178. let viewWidth: CGFloat = NSWidth(self.view.bounds)
  179. let viewHeight: CGFloat = NSHeight(self.view.bounds)
  180. let backgroundViewX: CGFloat = 0;
  181. let backgroundViewY: CGFloat = 24;
  182. let backgroundViewW: CGFloat = viewWidth - backgroundViewX * 2;
  183. let backgroundViewH: CGFloat = viewHeight - 24;
  184. // self.backgroundView.frame = NSMakeRect(backgroundViewX, backgroundViewY, backgroundViewW, backgroundViewH)
  185. var bounds = page.bounds
  186. let transform = page.transform()
  187. bounds = bounds.applying(transform)
  188. var size = NSMakeSize(backgroundViewW, backgroundViewH-26)
  189. if (self.isShowPageSize == false) {
  190. size = NSMakeSize(backgroundViewW, backgroundViewH-52)
  191. }
  192. if (page.rotation == -90 || page.rotation == -270) {
  193. let height = bounds.size.height
  194. bounds.size.height = bounds.size.width
  195. bounds.size.width = height
  196. }
  197. if (page.rotation == -90 || page.rotation == -270) {
  198. let height = size.height
  199. size.height = size.width
  200. size.width = height
  201. }
  202. if bounds.size.width > bounds.size.height {
  203. if (bounds.size.width > 0) {
  204. size.height = size.width * bounds.size.height / bounds.size.width
  205. }
  206. } else {
  207. if (bounds.size.height > 0) {
  208. size.width = size.height * bounds.size.width / bounds.size.height
  209. }
  210. }
  211. let height = _textLabelHeight
  212. let labelBoxY: CGFloat = _textBoxSpace + NSMinY(self.backgroundView.frame)+6
  213. let labelSize = NSMakeSize(size.width, CGFloat(MAXFLOAT))
  214. self.pageTextLabel.sizeToFit()
  215. let textSize = self.pageTextLabel.frame.size
  216. if self.isShowPageSize == false {
  217. self.pageSizeTextLabel.sizeToFit()
  218. let pageSizeLabelSize = self.pageSizeTextLabel.frame.size
  219. let width = max(textSize.width, pageSizeLabelSize.width) + 5
  220. self.pageSizeTextLabel.isHidden = false
  221. self.pageSizeTextLabel.frame = NSMakeRect((width-pageSizeLabelSize.width)*0.5, 4, pageSizeLabelSize.width, 22);
  222. self.pageTextLabel.frame = NSMakeRect((width-textSize.width)*0.5-5, self.pageSizeTextLabel.frame.maxY+8, textSize.width+10, height);
  223. let boxH: CGFloat = 48
  224. self.textBox.frame = NSMakeRect((viewWidth - width)*0.5, labelBoxY, width, boxH)
  225. } else {
  226. self.pageSizeTextLabel.isHidden = true
  227. let textWidth = textSize.width + 5
  228. let boxH: CGFloat = 22
  229. self.textBox.frame = NSMakeRect((viewWidth-textWidth)*0.5, labelBoxY, textWidth, boxH)
  230. self.pageTextLabel.frame = NSMakeRect((textWidth-textSize.width)*0.5-5, 4, textSize.width+10, height);
  231. }
  232. let margin: CGFloat = NSMinX(self.backgroundView.frame)
  233. let pageBoxY: CGFloat = self.textBox.frame.maxY+8
  234. let pageBoxW: CGFloat = viewWidth-margin*2
  235. let pageBoxH: CGFloat = viewHeight-pageBoxY
  236. self.pageBox.frame = NSMakeRect(margin, pageBoxY, pageBoxW, pageBoxH)
  237. var pageX: CGFloat = (NSWidth(self.pageBox.frame)-size.width)*0.5
  238. if (pageX < 0) {
  239. let tempWidth = size.width + pageX * 2
  240. let scale = tempWidth / size.width
  241. size.width = size.width * scale
  242. size.height = size.height * scale
  243. pageX = 0
  244. }
  245. let pageY: CGFloat = (NSHeight(self.pageBox.frame)-size.height)*0.5
  246. self.pageView.frame = NSMakeRect(pageX, pageY, size.width, size.height)
  247. let bkWH: CGFloat = 20
  248. let bkX = NSMaxX(self.pageView.frame)-bkWH
  249. let bkY = NSMaxY(self.pageView.frame)-bkWH
  250. self.bkIcon.frame = NSMakeRect(bkX, bkY, bkWH, bkWH)
  251. self.bkIcon.autoresizingMask = [.minXMargin, .minYMargin]
  252. }
  253. }
  254. extension KMPageEditThumbnailItem: KMReusable {}