KMThumbnailTableviewCell.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // KMThumbnailTableviewCell.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. class KMThumbnailTableviewCell: NSTableCellView {
  9. @IBOutlet var thumBox: KMBox!
  10. @IBOutlet var thumImage: NSImageView!
  11. @IBOutlet var pageNumLabel: NSTextField!
  12. @IBOutlet var sizeLabel: NSTextField!
  13. @IBOutlet var labelView: NSView!
  14. @IBOutlet var sizeTopConstant: NSLayoutConstraint!
  15. @IBOutlet var imageAspectRatioLayout: NSLayoutConstraint!
  16. var bkIcon: NSImageView = NSImageView()
  17. lazy var pageView = KMPDFThumbnialPageView()
  18. lazy var pageBox = NSView()
  19. var isSelectCell = false {
  20. didSet {
  21. if (self.isSelectCell) {
  22. self.thumImage.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  23. self.thumImage.layer?.borderWidth = 1.0
  24. self.pageBox.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  25. self.pageBox.layer?.borderWidth = 1.0
  26. self.labelView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  27. self.pageNumLabel.textColor = KMAppearance.Layout.w0Color()
  28. self.sizeLabel.textColor = KMAppearance.Layout.w0Color()
  29. } else {
  30. self.thumImage.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  31. self.thumImage.layer?.borderWidth = 1.0
  32. self.pageBox.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  33. self.pageBox.layer?.borderWidth = 1.0
  34. self.labelView.layer?.backgroundColor = .clear
  35. self.pageNumLabel.textColor = KMAppearance.Layout.h0Color()
  36. self.sizeLabel.textColor = KMAppearance.Layout.h0Color()
  37. }
  38. }
  39. }
  40. override func awakeFromNib() {
  41. super.awakeFromNib()
  42. self.wantsLayer = true
  43. self.layer?.backgroundColor = .clear
  44. self.layer?.cornerRadius = 0.0
  45. self.thumImage.wantsLayer = true
  46. self.labelView.wantsLayer = true
  47. self.addSubview(self.pageBox)
  48. self.pageBox.wantsLayer = true
  49. self.addSubview(self.pageView)
  50. self.thumImage.isHidden = true
  51. self.addSubview(self.bkIcon)
  52. self.bkIcon.wantsLayer = true
  53. // self.bkIcon.layer?.backgroundColor = .black
  54. self.bkIcon.image = NSImage(named: "KMImageNameBookmarkIcon")
  55. }
  56. override func layout() {
  57. super.layout()
  58. let width = NSWidth(self.bounds)
  59. let height = NSHeight(self.bounds)
  60. let border: CGFloat = 10
  61. var bounds = self.pageView.page?.bounds ?? .zero
  62. let rotate = self.pageView.page?.rotation ?? 0 % 360
  63. if (bounds.size.equalTo(.zero)) {
  64. return
  65. }
  66. if rotate == 90 || rotate == 270 {
  67. let tmp = bounds.size.width
  68. bounds.size.width = bounds.size.height
  69. bounds.size.height = tmp
  70. }
  71. var pageSelectionSize = CGSize(width: NSWidth(self.bounds)-30, height: NSHeight(self.bounds)-40)
  72. var size = NSMakeSize(pageSelectionSize.width - 2 * border, pageSelectionSize.height - 2 * border)
  73. // let minScale = min(size.width/bounds.size.width, size.height/bounds.size.height)
  74. let minScale = size.height/bounds.size.height
  75. size.width = bounds.size.width * minScale
  76. size.height = bounds.size.height * minScale
  77. if rotate == 90 || rotate == 270 {
  78. let max = max(size.height, size.width)
  79. // let ws = size.width / max
  80. let hs = size.height / max
  81. let tmp = size.width
  82. // size.width = size.height * hs
  83. // size.height = tmp
  84. }
  85. let pageViewX = (pageSelectionSize.width-size.width) * 0.5 + 15
  86. let pageViewY = (pageSelectionSize.height-size.height) * 0.5 + 20 + 20
  87. self.pageView.frame = NSMakeRect(pageViewX, pageViewY, size.width, size.height)
  88. self.pageBox.frame = NSInsetRect(self.pageView.frame, -5, -5)
  89. let bkWH: CGFloat = 20
  90. let bkX = NSMaxX(self.pageView.frame)-bkWH
  91. let bkY = NSMaxY(self.pageView.frame)-bkWH
  92. self.bkIcon.frame = NSMakeRect(bkX, bkY, bkWH, bkWH)
  93. }
  94. }