KMNPreView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // KMNPreView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/25.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNPreView: NSView, NibLoadable {
  10. @IBOutlet weak var pdfPreView: KMNPDFPreView!
  11. @IBOutlet weak var paginationBox: NSBox!
  12. @IBOutlet weak var pdfPreViewLeftConst: NSLayoutConstraint!
  13. @IBOutlet weak var pdfPreViewTopConst: NSLayoutConstraint!
  14. @IBOutlet weak var pdfPreViewRightConst: NSLayoutConstraint!
  15. @IBOutlet weak var paginationTopSpaceConst: NSLayoutConstraint!
  16. @IBOutlet weak var paginationLeftConst: NSLayoutConstraint!
  17. @IBOutlet weak var paginationRightConst: NSLayoutConstraint!
  18. @IBOutlet weak var paginationBottomConst: NSLayoutConstraint!
  19. var contentInset: NSEdgeInsets = .init(top: 16, left: 16, bottom: 16, right: 16) {
  20. didSet {
  21. pdfPreViewLeftConst.constant = contentInset.left
  22. pdfPreViewTopConst.constant = contentInset.top
  23. pdfPreViewRightConst.constant = contentInset.right
  24. paginationBottomConst.constant = contentInset.bottom
  25. }
  26. }
  27. private var pagination_ = ComponentPagination()
  28. var pdfView: CPDFView? {
  29. get {
  30. return self.pdfPreView.pdfView
  31. }
  32. }
  33. var pageCount: Int {
  34. get {
  35. return Int(self.pdfPreView.pdfView.document?.pageCount ?? 0)
  36. }
  37. }
  38. deinit {
  39. KMPrint("KMNPreView deinit.")
  40. }
  41. override func draw(_ dirtyRect: NSRect) {
  42. super.draw(dirtyRect)
  43. // Drawing code here.
  44. }
  45. convenience init(fileUrl: URL, password: String?) {
  46. self.init()
  47. initSubviews()
  48. initDefaultValue()
  49. setFileUrl(fileUrl, password: password)
  50. }
  51. override func awakeFromNib() {
  52. super.awakeFromNib()
  53. initSubviews()
  54. initDefaultValue()
  55. }
  56. func initSubviews() {
  57. self.paginationBox.contentView = self.pagination_
  58. pagination_.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
  59. pagination_.delegate = self
  60. }
  61. func initDefaultValue() {
  62. self.paginationBox.borderWidth = 0
  63. }
  64. func setFileUrl(_ fileUrl: URL, password: String?) {
  65. pdfPreView.setFileUrl(fileUrl, password: password)
  66. }
  67. func reloadUI() {
  68. pagination_.properties.totalCount = Int(self.pdfPreView.pdfView.document.pageCount)
  69. pagination_.properties.currentIndex = self.pdfPreView.pdfView.currentPageIndex + 1
  70. pagination_.reloadData()
  71. }
  72. }
  73. //MARK: - ComponentPaginationDelegate
  74. extension KMNPreView: ComponentPaginationDelegate {
  75. public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
  76. // if choosedIndex != pagination.properties.currentIndex {
  77. // choosedIndex = pagination.properties.currentIndex
  78. //
  79. // isGo = false
  80. //
  81. // refreshGoButton()
  82. // }
  83. let idx = pagination.properties.currentIndex
  84. self.pdfPreView.pdfView.go(toPageIndex: idx-1, animated: true)
  85. }
  86. }