123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // KMNPreView.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/10/25.
- //
- import Cocoa
- import KMComponentLibrary
- class KMNPreView: NSView, NibLoadable {
- @IBOutlet weak var pdfPreView: KMNPDFPreView!
- @IBOutlet weak var paginationBox: NSBox!
-
- @IBOutlet weak var pdfPreViewLeftConst: NSLayoutConstraint!
- @IBOutlet weak var pdfPreViewTopConst: NSLayoutConstraint!
- @IBOutlet weak var pdfPreViewRightConst: NSLayoutConstraint!
-
- @IBOutlet weak var paginationTopSpaceConst: NSLayoutConstraint!
- @IBOutlet weak var paginationLeftConst: NSLayoutConstraint!
- @IBOutlet weak var paginationRightConst: NSLayoutConstraint!
- @IBOutlet weak var paginationBottomConst: NSLayoutConstraint!
-
- var contentInset: NSEdgeInsets = .init(top: 16, left: 16, bottom: 16, right: 16) {
- didSet {
- pdfPreViewLeftConst.constant = contentInset.left
- pdfPreViewTopConst.constant = contentInset.top
- pdfPreViewRightConst.constant = contentInset.right
-
- paginationBottomConst.constant = contentInset.bottom
- }
- }
-
- private var pagination_ = ComponentPagination()
-
- var pdfView: CPDFView? {
- get {
- return self.pdfPreView.pdfView
- }
- }
-
- var pageCount: Int {
- get {
- return Int(self.pdfPreView.pdfView.document?.pageCount ?? 0)
- }
- }
-
- deinit {
- KMPrint("KMNPreView deinit.")
- }
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- convenience init(fileUrl: URL, password: String?) {
- self.init()
-
- initSubviews()
- initDefaultValue()
-
- setFileUrl(fileUrl, password: password)
- }
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- initSubviews()
- initDefaultValue()
- }
-
- func initSubviews() {
- self.paginationBox.contentView = self.pagination_
- pagination_.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
- pagination_.delegate = self
- }
-
- func initDefaultValue() {
- self.paginationBox.borderWidth = 0
- }
-
- func setFileUrl(_ fileUrl: URL, password: String?) {
- pdfPreView.setFileUrl(fileUrl, password: password)
- }
-
- func reloadUI() {
- pagination_.properties.totalCount = Int(self.pdfPreView.pdfView.document.pageCount)
- pagination_.properties.currentIndex = self.pdfPreView.pdfView.currentPageIndex + 1
- pagination_.reloadData()
- }
-
- }
- //MARK: - ComponentPaginationDelegate
- extension KMNPreView: ComponentPaginationDelegate {
- public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
- // if choosedIndex != pagination.properties.currentIndex {
- // choosedIndex = pagination.properties.currentIndex
- //
- // isGo = false
- //
- // refreshGoButton()
-
- // }
- let idx = pagination.properties.currentIndex
- self.pdfPreView.pdfView.go(toPageIndex: idx-1, animated: true)
- }
- }
|