// // KMLinkPageView.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/10/18. // import Cocoa import KMComponentLibrary @objc public protocol KMLinkPageViewDelegate: AnyObject { @objc optional func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int) @objc optional func kmLinkPageViewDidChangeDestination(_ view: KMLinkPageView, _ pageIndex: Int) } public class KMLinkPageView: BaseXibView { @IBOutlet var infoContendView: NSView! @IBOutlet var imageBGView: NSView! @IBOutlet var pageImage: NSImageView! @IBOutlet var paginationView: ComponentPagination! @IBOutlet var goButton: ComponentButton! var pdfView: CPDFView? = nil var annotation: CPDFLinkAnnotation? = nil var startPageIndex: Int = 0 var choosedIndex: Int = 0 var isGo: Bool = false weak open var delegate: KMLinkPageViewDelegate? //MARK: - func public override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } public required init?(coder decoder: NSCoder) { super.init(coder: decoder) } override init(frame frameRect: NSRect) { super.init(frame: frameRect) } public override func awakeFromNib() { super.awakeFromNib() self.setUpUI() } func setUpUI() { infoContendView.wantsLayer = true if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/m") { let currentValue = value as! CGFloat infoContendView.layer?.cornerRadius = currentValue } infoContendView.layer?.borderWidth = 1 infoContendView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor infoContendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor infoContendView.layer?.masksToBounds = true imageBGView.wantsLayer = true if let value = ComponentLibrary.shared.getComponentValueFromKey("comp-field/radius") { let currentValue = value as! CGFloat imageBGView.layer?.cornerRadius = currentValue } imageBGView.layer?.borderWidth = 1 imageBGView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor imageBGView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor imageBGView.layer?.masksToBounds = true paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1) paginationView.delegate = self goButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Go")) goButton.setTarget(self, action: #selector(buttonClicked(_:))) } func reloadData() { guard let pdfView = self.pdfView else { return } paginationView.properties.currentIndex = pdfView.currentPageIndex + 1 if let destination = annotation?.destination() { if let page = destination.page() { let pageIndex = page.document?.index(for: page) ?? 0 paginationView.properties.currentIndex = Int((pageIndex + 1)) } } paginationView.properties.totalCount = Int(pdfView.document.pageCount) paginationView.reloadData() isGo = false goButton.properties.buttonText = KMLocalizedString("Go") choosedIndex = paginationView.properties.currentIndex startPageIndex = paginationView.properties.currentIndex } func refreshGoButton() { if isGo { goButton.properties.buttonText = KMLocalizedString("Back") } else { goButton.properties.buttonText = KMLocalizedString("Go") } goButton.reloadData() } @objc func buttonClicked(_ sender: NSView) { if isGo == false { delegate?.kmLinkPageViewDidChangeDestination?(self, choosedIndex) isGo = true } else { delegate?.kmLinkPageViewDidGoToPage?(self, startPageIndex) isGo = false } refreshGoButton() } //MARK: - MouseEvent public override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) window?.makeFirstResponder(nil) } } //MARK: - ComponentPaginationDelegate extension KMLinkPageView: ComponentPaginationDelegate { public func componentPaginationDidValueChanged(pagination: ComponentPagination) { if choosedIndex != pagination.properties.currentIndex { choosedIndex = pagination.properties.currentIndex isGo = false refreshGoButton() } } }