// // KMThumbnailTableviewCell.swift // PDF Reader Pro // // Created by tangchao on 2023/11/16. // import Cocoa class KMThumbnailTableviewCell: NSTableCellView { @IBOutlet var thumBox: KMBox! @IBOutlet var thumImage: NSImageView! @IBOutlet var pageNumLabel: NSTextField! @IBOutlet var sizeLabel: NSTextField! @IBOutlet var labelView: NSView! @IBOutlet var sizeTopConstant: NSLayoutConstraint! @IBOutlet var imageAspectRatioLayout: NSLayoutConstraint! lazy var pageView = KMPDFThumbnialPageView() lazy var pageBox = NSView() var isSelectCell = false { didSet { if (self.isSelectCell) { self.thumImage.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor self.thumImage.layer?.borderWidth = 1.0 self.pageBox.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor self.pageBox.layer?.borderWidth = 1.0 self.labelView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor self.pageNumLabel.textColor = KMAppearance.Layout.w0Color() self.sizeLabel.textColor = KMAppearance.Layout.w0Color() } else { self.thumImage.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor self.thumImage.layer?.borderWidth = 1.0 self.pageBox.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor self.pageBox.layer?.borderWidth = 1.0 self.labelView.layer?.backgroundColor = .clear self.pageNumLabel.textColor = KMAppearance.Layout.h0Color() self.sizeLabel.textColor = KMAppearance.Layout.h0Color() } } } override func awakeFromNib() { super.awakeFromNib() self.wantsLayer = true self.layer?.backgroundColor = .clear self.layer?.cornerRadius = 0.0 self.thumImage.wantsLayer = true self.labelView.wantsLayer = true self.addSubview(self.pageBox) self.pageBox.wantsLayer = true self.addSubview(self.pageView) self.thumImage.isHidden = true } override func layout() { super.layout() let width = NSWidth(self.bounds) let height = NSHeight(self.bounds) let border: CGFloat = 10 var bounds = self.pageView.page?.bounds ?? .zero let rotate = self.pageView.page?.rotation ?? 0 % 360 if (bounds.size.equalTo(.zero)) { return } if rotate == 90 || rotate == 270 { let tmp = bounds.size.width bounds.size.width = bounds.size.height bounds.size.height = tmp } var pageSelectionSize = CGSize(width: NSWidth(self.bounds)-30, height: NSHeight(self.bounds)-40) var size = NSMakeSize(pageSelectionSize.width - 2 * border, pageSelectionSize.height - 2 * border) // let minScale = min(size.width/bounds.size.width, size.height/bounds.size.height) let minScale = size.height/bounds.size.height size.width = bounds.size.width * minScale size.height = bounds.size.height * minScale if rotate == 90 || rotate == 270 { let max = max(size.height, size.width) // let ws = size.width / max let hs = size.height / max let tmp = size.width // size.width = size.height * hs // size.height = tmp } let pageViewX = (pageSelectionSize.width-size.width) * 0.5 + 15 let pageViewY = (pageSelectionSize.height-size.height) * 0.5 + 20 + 20 self.pageView.frame = NSMakeRect(pageViewX, pageViewY, size.width, size.height) self.pageBox.frame = NSInsetRect(self.pageView.frame, -5, -5) } }