KMThumbnailTableviewCell.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. lazy var pageView = KMPDFThumbnialPageView()
  17. lazy var pageBox = NSView()
  18. var isSelectCell = false {
  19. didSet {
  20. if (self.isSelectCell) {
  21. self.thumImage.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  22. self.thumImage.layer?.borderWidth = 1.0
  23. self.pageBox.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  24. self.pageBox.layer?.borderWidth = 1.0
  25. self.labelView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  26. self.pageNumLabel.textColor = KMAppearance.Layout.w0Color()
  27. self.sizeLabel.textColor = KMAppearance.Layout.w0Color()
  28. } else {
  29. self.thumImage.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  30. self.thumImage.layer?.borderWidth = 1.0
  31. self.pageBox.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  32. self.pageBox.layer?.borderWidth = 1.0
  33. self.labelView.layer?.backgroundColor = .clear
  34. self.pageNumLabel.textColor = KMAppearance.Layout.h0Color()
  35. self.sizeLabel.textColor = KMAppearance.Layout.h0Color()
  36. }
  37. }
  38. }
  39. override func awakeFromNib() {
  40. super.awakeFromNib()
  41. self.wantsLayer = true
  42. self.layer?.backgroundColor = .clear
  43. self.layer?.cornerRadius = 0.0
  44. self.thumImage.wantsLayer = true
  45. self.labelView.wantsLayer = true
  46. self.addSubview(self.pageBox)
  47. self.pageBox.wantsLayer = true
  48. self.addSubview(self.pageView)
  49. self.thumImage.isHidden = true
  50. }
  51. override func layout() {
  52. super.layout()
  53. let width = NSWidth(self.bounds)
  54. let height = NSHeight(self.bounds)
  55. let border: CGFloat = 10
  56. var bounds = self.pageView.page?.bounds ?? .zero
  57. let rotate = self.pageView.page?.rotation ?? 0 % 360
  58. if (bounds.size.equalTo(.zero)) {
  59. return
  60. }
  61. if rotate == 90 || rotate == 270 {
  62. let tmp = bounds.size.width
  63. bounds.size.width = bounds.size.height
  64. bounds.size.height = tmp
  65. }
  66. var pageSelectionSize = CGSize(width: NSWidth(self.bounds)-30, height: NSHeight(self.bounds)-40)
  67. var size = NSMakeSize(pageSelectionSize.width - 2 * border, pageSelectionSize.height - 2 * border)
  68. // let minScale = min(size.width/bounds.size.width, size.height/bounds.size.height)
  69. let minScale = size.height/bounds.size.height
  70. size.width = bounds.size.width * minScale
  71. size.height = bounds.size.height * minScale
  72. if rotate == 90 || rotate == 270 {
  73. let max = max(size.height, size.width)
  74. // let ws = size.width / max
  75. let hs = size.height / max
  76. let tmp = size.width
  77. // size.width = size.height * hs
  78. // size.height = tmp
  79. }
  80. let pageViewX = (pageSelectionSize.width-size.width) * 0.5 + 15
  81. let pageViewY = (pageSelectionSize.height-size.height) * 0.5 + 20 + 20
  82. self.pageView.frame = NSMakeRect(pageViewX, pageViewY, size.width, size.height)
  83. self.pageBox.frame = NSInsetRect(self.pageView.frame, -5, -5)
  84. }
  85. }