KMPrintPreviewView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // KMPrintPreviewView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/12/8.
  6. //
  7. import Cocoa
  8. class KMPrintPreviewView: KMBaseXibView {
  9. @IBOutlet weak var previewContentView: NSView!
  10. @IBOutlet weak var previewView: CPDFListView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var previousButton: NSButton!
  13. @IBOutlet weak var nextButton: NSButton!
  14. @IBOutlet weak var currentPageTextField: KMBaseTextField!
  15. @IBOutlet weak var pageCountLabel: NSTextField!
  16. @IBOutlet weak var partitionLabel: NSTextField!
  17. private lazy var presenter: KMPrintPreviewPresenter = KMPrintPreviewPresenter()
  18. var pdfDocument: CPDFDocument? {
  19. didSet {
  20. self.previewView.document = pdfDocument
  21. self.previewView.autoScales = true
  22. self.previewView.setDisplay(.singlePage)
  23. self.previewView.documentView().documentView?.enclosingScrollView?.hasVerticalScroller = false
  24. self.previewView.documentView().documentView?.enclosingScrollView?.hasHorizontalScroller = false
  25. self.presenter.initPresenter(delegate: self, view: self.previewView)
  26. self.reloadData()
  27. }
  28. }
  29. deinit {
  30. // self.delegate = nil
  31. }
  32. override func draw(_ dirtyRect: NSRect) {
  33. super.draw(dirtyRect)
  34. // Drawing code here.
  35. }
  36. override func setup() {
  37. super.setup()
  38. self.previewContentView.wantsLayer = true
  39. self.previewContentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  40. self.currentPageTextField.textField.font = NSFont.SFProTextRegularFont(12.0)
  41. self.currentPageTextField.textField.textColor = NSColor.km_init(hex: "#252629")
  42. self.currentPageTextField.textDidEndEditing = {[unowned self] (string) in
  43. self.presenter.toPage(index: UInt(string) ?? 0)
  44. }
  45. self.partitionLabel.textColor = NSColor.km_init(hex: "#252629")
  46. self.titleLabel.font = NSFont.SFProTextRegularFont(12.0)
  47. self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  48. self.pageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
  49. self.pageCountLabel.textColor = NSColor.km_init(hex: "#252629")
  50. self.previousButton.wantsLayer = true
  51. self.previousButton.layer?.borderWidth = 1
  52. self.previousButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  53. self.previousButton.layer?.cornerRadius = 4
  54. self.previousButton.image = NSImage(named: "icon_btn_pagination_back_norm")
  55. self.nextButton.wantsLayer = true
  56. self.nextButton.layer?.borderWidth = 1
  57. self.nextButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  58. self.nextButton.layer?.cornerRadius = 4
  59. self.nextButton.image = NSImage(named: "icon_btn_pagination_back_norm")
  60. self.nextButton.image = NSImage(named: "icon_btn_pagination_lastpage_dis")
  61. }
  62. func notification() {
  63. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  64. }
  65. //刷新PDFPreview
  66. override func reloadData() {
  67. super.reloadData()
  68. //UI 显示
  69. if self.pdfDocument != nil {
  70. let page = self.previewView.currentPage()
  71. let pageIndex = self.previewView.document.index(for: page)
  72. self.currentPageTextField.stringValue = (pageIndex + 1).description
  73. self.pageCountLabel.stringValue = self.previewView.document.pageCount.description
  74. if page != nil {
  75. 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: "")
  76. }
  77. }
  78. }
  79. //MARK: 通知
  80. @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
  81. // self.reloadData()
  82. }
  83. }
  84. protocol KMPrintPreviewViewAction { }
  85. extension KMPrintPreviewView: KMPrintPreviewViewAction {
  86. @IBAction func nextButton(_ sender: NSButton) {
  87. self.presenter.nextPage()
  88. }
  89. @IBAction func previousAction(_ sender: NSButton) {
  90. self.presenter.previousPage()
  91. }
  92. func toPageIndex(_ index: UInt = 0) {
  93. self.presenter.toPage(index: index)
  94. }
  95. }
  96. extension KMPrintPreviewView: KMPrintPreviewPresenterDelegate {
  97. func dataDidChange(presenter: KMPrintPreviewPresenter, view: CPDFView) {
  98. self.reloadData()
  99. }
  100. func documentDidChange(presenter: KMPrintPreviewPresenter, document: CPDFDocument) {
  101. self.previewView.document = document
  102. self.reloadData()
  103. }
  104. }