KMLinkPageView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // KMLinkPageView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMLinkPageViewDelegate: AnyObject {
  10. @objc optional func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int)
  11. @objc optional func kmLinkPageViewDidChangeDestination(_ view: KMLinkPageView, _ pageIndex: Int)
  12. }
  13. public class KMLinkPageView: BaseXibView {
  14. @IBOutlet var infoContendView: NSView!
  15. @IBOutlet var imageBGView: NSView!
  16. @IBOutlet var pageImage: NSImageView!
  17. @IBOutlet var paginationView: ComponentPagination!
  18. @IBOutlet var goButton: ComponentButton!
  19. var pdfView: CPDFView? = nil
  20. var annotation: CPDFLinkAnnotation? = nil
  21. var startPageIndex: Int = 0
  22. var choosedIndex: Int = 0
  23. var isGo: Bool = false
  24. weak open var delegate: KMLinkPageViewDelegate?
  25. //MARK: - func
  26. public override func draw(_ dirtyRect: NSRect) {
  27. super.draw(dirtyRect)
  28. // Drawing code here.
  29. }
  30. public required init?(coder decoder: NSCoder) {
  31. super.init(coder: decoder)
  32. }
  33. override init(frame frameRect: NSRect) {
  34. super.init(frame: frameRect)
  35. }
  36. public override func awakeFromNib() {
  37. super.awakeFromNib()
  38. self.setUpUI()
  39. }
  40. func setUpUI() {
  41. infoContendView.wantsLayer = true
  42. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/m") {
  43. let currentValue = value as! CGFloat
  44. infoContendView.layer?.cornerRadius = currentValue
  45. }
  46. infoContendView.layer?.borderWidth = 1
  47. infoContendView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
  48. infoContendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
  49. infoContendView.layer?.masksToBounds = true
  50. imageBGView.wantsLayer = true
  51. if let value = ComponentLibrary.shared.getComponentValueFromKey("comp-field/radius") {
  52. let currentValue = value as! CGFloat
  53. imageBGView.layer?.cornerRadius = currentValue
  54. }
  55. imageBGView.layer?.borderWidth = 1
  56. imageBGView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
  57. imageBGView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
  58. imageBGView.layer?.masksToBounds = true
  59. paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
  60. paginationView.delegate = self
  61. goButton.properties = ComponentButtonProperty(type: .default_tertiary,
  62. size: .s,
  63. buttonText: KMLocalizedString("Go"))
  64. goButton.setTarget(self, action: #selector(buttonClicked(_:)))
  65. }
  66. func reloadData() {
  67. guard let pdfView = self.pdfView else {
  68. return
  69. }
  70. paginationView.properties.currentIndex = pdfView.currentPageIndex + 1
  71. if let destination = annotation?.destination() {
  72. if let page = destination.page() {
  73. let pageIndex = page.document?.index(for: page) ?? 0
  74. paginationView.properties.currentIndex = Int((pageIndex + 1))
  75. }
  76. }
  77. paginationView.properties.totalCount = Int(pdfView.document.pageCount)
  78. paginationView.reloadData()
  79. isGo = false
  80. goButton.properties.buttonText = KMLocalizedString("Go")
  81. choosedIndex = paginationView.properties.currentIndex
  82. startPageIndex = paginationView.properties.currentIndex
  83. }
  84. func refreshGoButton() {
  85. if isGo {
  86. goButton.properties.buttonText = KMLocalizedString("Back")
  87. } else {
  88. goButton.properties.buttonText = KMLocalizedString("Go")
  89. }
  90. goButton.reloadData()
  91. }
  92. @objc func buttonClicked(_ sender: NSView) {
  93. if isGo == false {
  94. delegate?.kmLinkPageViewDidChangeDestination?(self, choosedIndex)
  95. isGo = true
  96. } else {
  97. delegate?.kmLinkPageViewDidGoToPage?(self, startPageIndex)
  98. isGo = false
  99. }
  100. refreshGoButton()
  101. }
  102. //MARK: - MouseEvent
  103. public override func mouseDown(with event: NSEvent) {
  104. super.mouseDown(with: event)
  105. window?.makeFirstResponder(nil)
  106. }
  107. }
  108. //MARK: - ComponentPaginationDelegate
  109. extension KMLinkPageView: ComponentPaginationDelegate {
  110. public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
  111. if choosedIndex != pagination.properties.currentIndex {
  112. choosedIndex = pagination.properties.currentIndex
  113. isGo = false
  114. refreshGoButton()
  115. }
  116. }
  117. }