123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // KMPrintPreviewView.swift
- // PDF Master
- //
- // Created by lizhe on 2022/12/8.
- //
- import Cocoa
- class KMPrintPreviewView: KMBaseXibView {
- @IBOutlet weak var previewContentView: NSView!
- @IBOutlet weak var previewView: CPDFListView!
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var previousButton: NSButton!
-
- @IBOutlet weak var nextButton: NSButton!
-
- @IBOutlet weak var currentPageTextField: KMBaseTextField!
- @IBOutlet weak var pageCountLabel: NSTextField!
- @IBOutlet weak var partitionLabel: NSTextField!
- private lazy var presenter: KMPrintPreviewPresenter = KMPrintPreviewPresenter()
- var pdfDocument: CPDFDocument? {
- didSet {
- self.previewView.document = pdfDocument
- self.previewView.autoScales = true
- self.previewView.setDisplay(.singlePage)
- self.previewView.documentView().documentView?.enclosingScrollView?.hasVerticalScroller = false
- self.previewView.documentView().documentView?.enclosingScrollView?.hasHorizontalScroller = false
- self.presenter.initPresenter(delegate: self, view: self.previewView)
- self.reloadData()
- }
- }
-
- deinit {
- // self.delegate = nil
- }
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
-
- }
-
- override func setup() {
- super.setup()
-
- self.previewContentView.wantsLayer = true
- self.previewContentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
-
- self.currentPageTextField.textField.font = NSFont.SFProTextRegularFont(12.0)
- self.currentPageTextField.textField.textColor = NSColor.km_init(hex: "#252629")
- self.currentPageTextField.textDidEndEditing = {[unowned self] (string) in
- self.presenter.toPage(index: UInt(string) ?? 0)
- }
-
- self.partitionLabel.textColor = NSColor.km_init(hex: "#252629")
-
- self.titleLabel.font = NSFont.SFProTextRegularFont(12.0)
- self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
-
- self.pageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
- self.pageCountLabel.textColor = NSColor.km_init(hex: "#252629")
-
- self.previousButton.wantsLayer = true
- self.previousButton.layer?.borderWidth = 1
- self.previousButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
- self.previousButton.layer?.cornerRadius = 4
- self.previousButton.image = NSImage(named: "icon_btn_pagination_back_norm")
-
- self.nextButton.wantsLayer = true
- self.nextButton.layer?.borderWidth = 1
- self.nextButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
- self.nextButton.layer?.cornerRadius = 4
- self.nextButton.image = NSImage(named: "icon_btn_pagination_back_norm")
- self.nextButton.image = NSImage(named: "icon_btn_pagination_lastpage_dis")
- }
-
- func notification() {
- NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
- }
-
- //刷新PDFPreview
- override func reloadData() {
- super.reloadData()
- //UI 显示
- if self.pdfDocument != nil {
- let page = self.previewView.currentPage()
- let pageIndex = self.previewView.document.index(for: page)
- self.currentPageTextField.stringValue = (pageIndex + 1).description
- self.pageCountLabel.stringValue = self.previewView.document.pageCount.description
-
- if page != nil {
- self.titleLabel.stringValue = NSLocalizedString("size", comment: "") + ":" + String(Int(page!.size.width / 28.3 * 10)) + NSLocalizedString("mm", comment: "") + "*" + String(Int(page!.size.height / 28.3 * 10)) + NSLocalizedString("mm", comment: "")
- }
- }
- }
-
- //MARK: 通知
- @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
- // self.reloadData()
- }
- }
- protocol KMPrintPreviewViewAction { }
- extension KMPrintPreviewView: KMPrintPreviewViewAction {
- @IBAction func nextButton(_ sender: NSButton) {
- self.presenter.nextPage()
- }
-
- @IBAction func previousAction(_ sender: NSButton) {
- self.presenter.previousPage()
- }
-
- func toPageIndex(_ index: UInt = 0) {
- self.presenter.toPage(index: index)
- }
- }
- extension KMPrintPreviewView: KMPrintPreviewPresenterDelegate {
- func dataDidChange(presenter: KMPrintPreviewPresenter, view: CPDFView) {
- self.reloadData()
- }
-
- func documentDidChange(presenter: KMPrintPreviewPresenter, document: CPDFDocument) {
- self.previewView.document = document
- self.reloadData()
- }
- }
|