KMPDFThumbnailItem.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // KMPDFThumbnailItem.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/16.
  6. //
  7. import Cocoa
  8. let pageBoxBorder = 5
  9. let textBoxSpace = 2
  10. let textLabelHeight = 15
  11. typealias KMPDFThumbnailItemMouseDown = (_ view: KMPDFThumbnailItem, _ event: NSEvent) -> Void
  12. typealias KMPDFThumbnailItemHover = (_ view: KMPDFThumbnailItem, _ mouseEntered: Bool) -> Void
  13. typealias KMPDFThumbnailItemRightMouseDown = (_ view: KMPDFThumbnailItem, _ event: NSEvent) -> Void
  14. class KMPDFThumbnailItem: NSCollectionViewItem {
  15. lazy var pageBox : NSView = {
  16. let pageBox = NSView()
  17. pageBox.wantsLayer = true
  18. // pageBox.layer?.borderWidth = 1
  19. // pageBox.layer?.borderColor = NSColor(hex: "#CED0D4").cgColor
  20. // pageBox.layer?.cornerRadius = 2.0
  21. // pageBox.layer?.masksToBounds = true
  22. return pageBox
  23. }()
  24. lazy var textBox : NSView = {
  25. let textBox = NSView()
  26. return textBox
  27. }()
  28. lazy var pageTextLabel : NSTextField = {
  29. let pageTextLabel = NSTextField()
  30. pageTextLabel.isEditable = false
  31. pageTextLabel.isBordered = false
  32. pageTextLabel.isSelectable = false
  33. pageTextLabel.drawsBackground = false
  34. pageTextLabel.font = NSFont.systemFont(ofSize: 14)
  35. pageTextLabel.textColor = NSColor(hex: "#252629")
  36. return pageTextLabel
  37. }()
  38. lazy var pageSizeTextLabel : NSTextField = {
  39. let pageSizeTextLabel = NSTextField()
  40. pageSizeTextLabel.isEditable = false
  41. pageSizeTextLabel.isBordered = false
  42. pageSizeTextLabel.drawsBackground = false
  43. pageSizeTextLabel.font = NSFont.systemFont(ofSize: 14)
  44. pageSizeTextLabel.textColor = NSColor(hex: "#252629")
  45. return pageSizeTextLabel
  46. }()
  47. lazy var pageView = KMPDFThumbnialPageView()
  48. var page : CPDFPage = CPDFPage() {
  49. didSet {
  50. self.pageView.page = page
  51. self.updatePageState(page: page)
  52. }
  53. }
  54. var thumbnailView : KMPDFThumbnailView = KMPDFThumbnailView()
  55. var mouseDownAction: KMPDFThumbnailItemMouseDown?
  56. var rightMouseDownAction: KMPDFThumbnailItemRightMouseDown?
  57. var hoverCallBack: KMPDFThumbnailItemHover?
  58. var contentBox: KMBox?
  59. //注释状态
  60. var annotationShowState: KMAnnotationViewShowType = .none {
  61. didSet {
  62. if self.annotationShowState == .hidden {
  63. for annotation in self.page.annotations {
  64. annotation.setHidden(true)
  65. }
  66. } else {
  67. for annotation in self.page.annotations {
  68. annotation.setHidden(false)
  69. }
  70. }
  71. }
  72. }
  73. var hover: Bool = false {
  74. didSet {
  75. self.updateItemState()
  76. }
  77. }
  78. override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
  79. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  80. }
  81. required init?(coder: NSCoder) {
  82. fatalError("init(coder:) has not been implemented")
  83. }
  84. override func viewDidLoad() {
  85. super.viewDidLoad()
  86. self.view.addSubview(self.pageBox)
  87. self.view.wantsLayer = true
  88. self.view.layer?.cornerRadius = 8.0
  89. self.view.layer?.masksToBounds = true
  90. self.view.layer?.borderWidth = 1.0
  91. self.isSelected = false
  92. self.pageBox.addSubview(pageView)
  93. self.view.addSubview(self.textBox)
  94. self.textBox.addSubview(self.pageTextLabel)
  95. self.textBox.addSubview(self.pageSizeTextLabel)
  96. self.addContentBox()
  97. }
  98. //MARK: Accessors
  99. override var isSelected: Bool {
  100. get {
  101. return super.isSelected
  102. }
  103. set {
  104. super.isSelected = newValue
  105. // if (newValue) {
  106. // self.view.layer?.backgroundColor = NSColor(hex: "#CED0D4", alpha: 0.6).cgColor
  107. // self.view.layer?.borderColor = NSColor(hex: "#CED0D4").cgColor
  108. // } else {
  109. // self.view.layer?.backgroundColor = NSColor.clear.cgColor
  110. // self.view.layer?.borderColor = NSColor.clear.cgColor
  111. // }
  112. self.updateItemState()
  113. }
  114. }
  115. func updatePageState(page: CPDFPage!) {
  116. if page != nil {
  117. let index = page.document.index(for: page) + 1
  118. self.pageTextLabel.stringValue = "\(index)"
  119. let bounds = self.page.bounds
  120. if page.rotation == 90 || page.rotation == 270 {
  121. self.pageSizeTextLabel.stringValue = "\(Int(bounds.size.height))*\(Int(bounds.size.width)) mm"
  122. } else {
  123. self.pageSizeTextLabel.stringValue = "\(Int(bounds.size.width))*\(Int(bounds.size.height)) mm"
  124. }
  125. self.updateFrame()
  126. self.updateItemState()
  127. }
  128. }
  129. class func sizeToFit(size:NSSize,page:CPDFPage,isShow:Bool) -> CGFloat {
  130. var height = 0
  131. var bounds = page.bounds
  132. let transform = page.transform()
  133. bounds = bounds.applying(transform)
  134. var newSize = CGSize(width: size.width, height: size.height)
  135. if bounds.size.width > bounds.size.height {
  136. newSize.height = size.width * bounds.size.height / bounds.size.width
  137. } else {
  138. newSize.width = size.height * bounds.size.width / bounds.size.height
  139. }
  140. height = height + Int(size.height) + pageBoxBorder
  141. if isShow {
  142. height = height + 30 + textBoxSpace
  143. } else {
  144. height = height + 15 + textBoxSpace
  145. }
  146. return CGFloat(height)
  147. }
  148. func updateFrame () {
  149. let viewWidth: CGFloat = NSWidth(self.view.bounds)
  150. let viewHeight: CGFloat = NSHeight(self.view.bounds)
  151. let border: CGFloat = CGFloat(pageBoxBorder)
  152. var bounds = self.page.bounds
  153. let transform = self.page.transform()
  154. bounds = bounds.applying(transform)
  155. var size = self.thumbnailView.thumbnailSzie
  156. if bounds.size.width > bounds.size.height {
  157. if (bounds.size.width > 0) {
  158. size.height = size.width * bounds.size.height / bounds.size.width
  159. }
  160. } else {
  161. if (bounds.size.height > 0) {
  162. size.width = size.height * bounds.size.width / bounds.size.height
  163. }
  164. }
  165. size.width = size.width + CGFloat(border)
  166. size.height = size.height + CGFloat(border)
  167. let height = textLabelHeight
  168. let labelSize = NSMakeSize(size.width, CGFloat(MAXFLOAT))
  169. if self.thumbnailView.isShowPageSize == false {
  170. let pageTextLabelSize = self.pageTextLabel.sizeThatFits(labelSize)
  171. self.pageSizeTextLabel.sizeToFit()
  172. let pageSizeTextLabelSize = self.pageSizeTextLabel.frame.size
  173. let width = max(pageTextLabelSize.width, pageSizeTextLabelSize.width) + 5
  174. self.pageSizeTextLabel.isHidden = false
  175. self.pageSizeTextLabel.frame = NSMakeRect((width - pageSizeTextLabelSize.width) / 2.0, 6, pageSizeTextLabelSize.width, CGFloat(height));
  176. self.pageTextLabel.frame = NSMakeRect((width - pageTextLabelSize.width) / 2.0, self.pageSizeTextLabel.frame.maxY+5, pageTextLabelSize.width, CGFloat(height));
  177. let textBoxHeight: CGFloat = 44
  178. self.textBox.frame = NSMakeRect((self.view.frame.size.width - width) / 2.0, CGFloat(textBoxSpace), width, textBoxHeight)
  179. } else {
  180. let pageTextLabelSize = self.pageTextLabel.sizeThatFits(labelSize)
  181. let width = pageTextLabelSize.width + 5
  182. self.pageSizeTextLabel.isHidden = true
  183. self.pageTextLabel.frame = NSMakeRect((width - pageTextLabelSize.width) / 2.0, 6, pageTextLabelSize.width, CGFloat(height));
  184. let textBoxHeight: CGFloat = 22
  185. self.textBox.frame = NSMakeRect((self.view.frame.size.width - width) / 2.0, CGFloat(textBoxSpace), width, textBoxHeight)
  186. }
  187. let margin: CGFloat = 16
  188. let pageBoxY: CGFloat = self.textBox.frame.maxY+5
  189. let pageBoxW: CGFloat = viewWidth-margin*2
  190. let pageBoxH: CGFloat = viewHeight-pageBoxY-margin
  191. self.pageBox.frame = NSMakeRect(margin, pageBoxY, pageBoxW, pageBoxH)
  192. var pageX: CGFloat = (NSWidth(self.pageBox.frame)-size.width)*0.5
  193. if (pageX < 0) {
  194. let tempWidth = size.width + pageX * 2
  195. let scale = tempWidth / size.width
  196. size.width = size.width * scale
  197. size.height = size.height * scale
  198. }
  199. pageX = (NSWidth(self.pageBox.frame)-size.width)*0.5
  200. let pageY: CGFloat = (NSHeight(self.pageBox.frame)-size.height)*0.5
  201. self.pageView.frame = NSMakeRect(pageX, pageY, size.width, size.height)
  202. }
  203. func updateItemState() {
  204. if isSelected {
  205. self.view.layer?.backgroundColor = NSColor(hex: "#CED0D4", alpha: 0.6).cgColor
  206. self.view.layer?.borderColor = NSColor(hex: "#CED0D4").cgColor
  207. } else if hover {
  208. self.view.layer?.backgroundColor = NSColor(hex: "#EDEEF0").cgColor
  209. self.view.layer?.borderColor = NSColor.clear.cgColor
  210. } else {
  211. self.view.layer?.backgroundColor = NSColor.clear.cgColor
  212. self.view.layer?.borderColor = NSColor.clear.cgColor
  213. }
  214. }
  215. func addContentBox() {
  216. if self.contentBox == nil {
  217. let rect = self.view.bounds
  218. self.contentBox?.wantsLayer = true
  219. self.contentBox?.layer?.masksToBounds = true
  220. self.contentBox = KMBox(frame: rect)
  221. self.contentBox?.borderWidth = 0
  222. // self.box?.borderColor = NSColor(hex: "#EDEEF0")
  223. self.contentBox?.layer?.cornerRadius = 8
  224. self.contentBox?.boxType = .custom
  225. self.view.addSubview(self.contentBox!, positioned: NSWindow.OrderingMode.below, relativeTo: self.view)
  226. self.contentBox?.autoresizingMask = [.width, .height]
  227. self.contentBox?.moveCallback = { [unowned self] (mouseEntered, mouseBox) in
  228. self.hoverCallBack?(self, mouseEntered)
  229. }
  230. self.contentBox?.rightDownCallback = { [unowned self] (downEntered, mouseBox, event) in
  231. self.rightMouseDownAction?(self, event)
  232. }
  233. }
  234. }
  235. override func mouseMoved(with event: NSEvent) {
  236. super.mouseMoved(with: event)
  237. }
  238. override func mouseDown(with event: NSEvent) {
  239. super.mouseDown(with: event)
  240. guard let callBack = mouseDownAction else { return }
  241. callBack(self, event)
  242. }
  243. }