// // KMPDFThumbnialPageView.swift // PDF Master // // Created by tangchao on 2022/11/24. // import Cocoa import PDFKit class KMPDFThumbnialPageLayer: CATiledLayer { var page: CPDFPage? override init() { super.init() self.initDefaultValue() } required init?(coder: NSCoder) { super.init(coder: coder) self.initDefaultValue() } public func initDefaultValue() { self.levelsOfDetail = 2 self.levelsOfDetailBias = 3 self.tileSize = CGSize(width: 1024, height: 1024) } override func draw(in ctx: CGContext) { super.draw(in: ctx) if (self.page != nil) { ctx.saveGState(); var bounds = self.page!.bounds let transform = self.page!.transform() bounds = bounds.applying(transform); let viewWidth: CGFloat = NSWidth(self.bounds) let viewHeight: CGFloat = NSHeight(self.bounds) if (self.page!.rotation == -90 || self.page!.rotation == -270) { let height = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = height } ctx.scaleBy(x: viewWidth / bounds.size.width, y: viewHeight / bounds.size.height); self.page!.drawEdit(with: .cropBox, to: ctx) ctx.restoreGState(); } } } class KMPDFThumbnialPageView: NSView { internal var _page: CPDFPage? var page: CPDFPage? { get { self._page } set { self._page = newValue if pageLayer != nil { pageLayer.contents = nil pageLayer.delegate = nil pageLayer.removeFromSuperlayer() } pageLayer = KMPDFThumbnialPageLayer() pageLayer.frame = self.bounds pageLayer.page = newValue self.layer?.addSublayer(pageLayer) } } var pageLayer: KMPDFThumbnialPageLayer! override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.wantsLayer = true self.layer?.backgroundColor = NSColor.white.cgColor self.shadow = NSShadow() self.layer?.shadowColor = NSColor.black.cgColor self.layer?.shadowOpacity = 0.2 self.layer?.shadowRadius = 3 self.layer?.shadowOffset = CGSize(width: 0, height: -3) if (pageLayer != nil) { pageLayer.frame = self.bounds; } } required init?(coder: NSCoder) { super.init(coder: coder) } override func layout() { super.layout() if (pageLayer != nil) { pageLayer.frame = self.bounds; pageLayer.setNeedsDisplay() } } }