KMSplitToolbar.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // KMSplitToolbar.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/29.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMSplitToolbar: BaseXibView {
  10. @IBOutlet var contendView: NSView!
  11. @IBOutlet var paginationView: ComponentPagination!
  12. @IBOutlet var dividerView: ComponentDivider!
  13. @IBOutlet var selectView: ComponentSelect!
  14. var pdfView: CPDFListView?
  15. //MARK: - func
  16. public override func draw(_ dirtyRect: NSRect) {
  17. super.draw(dirtyRect)
  18. // Drawing code here.
  19. }
  20. public required init?(coder decoder: NSCoder) {
  21. super.init(coder: decoder)
  22. }
  23. override init(frame frameRect: NSRect) {
  24. super.init(frame: frameRect)
  25. }
  26. public override func awakeFromNib() {
  27. super.awakeFromNib()
  28. setUpUI()
  29. setupProperty()
  30. }
  31. func setUpUI() {
  32. contendView.wantsLayer = true
  33. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  34. contendView.layer?.borderWidth = 1
  35. contendView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/3-default").cgColor
  36. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/s") {
  37. let currentValue = value as! CGFloat
  38. contendView.layer?.cornerRadius = currentValue
  39. }
  40. }
  41. func setupProperty() {
  42. paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
  43. paginationView.delegate = self
  44. dividerView.properties = ComponentDividerProperty(type: .vertical, dash: false)
  45. selectView.properties = ComponentSelectProperties(size: .xxs, text: "100%")
  46. selectView.delegate = self
  47. }
  48. func reloadData() {
  49. selectView.updateMenuItemsArr(KMPDFToolbarConfig.scaleZoomItems())
  50. if let pdfview = self.pdfView, let document = pdfview.document {
  51. let curIndex = Int(pdfview.currentPageIndex) + 1
  52. paginationView.properties.currentIndex = curIndex
  53. paginationView.properties.totalCount = Int(document.pageCount)
  54. paginationView.reloadData()
  55. selectView.properties.text = (NSString(format: "%.0f", pdfview.scaleFactor * 100) as String) + "%"
  56. selectView.reloadData()
  57. }
  58. }
  59. }
  60. //MARK: - ComponentPaginationDelegate
  61. extension KMSplitToolbar: ComponentPaginationDelegate {
  62. public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
  63. if let pdfview = self.pdfView, let document = pdfview.document {
  64. pdfview.go(toPageIndex: pagination.properties.currentIndex-1, animated: true)
  65. }
  66. }
  67. }
  68. //MARK: - ComponentSelectDelegate
  69. extension KMSplitToolbar: ComponentSelectDelegate {
  70. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  71. if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapWidth_Identifier {
  72. if pdfView?.scaleFactor != 1.0 {
  73. pdfView?.scaleFactor = 1.0
  74. pdfView?.autoScales = false
  75. }
  76. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_AdapPage_Identifier {
  77. let pageHeight = pdfView?.currentPage()!.size.height
  78. let pdfviewHeight = pdfView?.bounds.size.height
  79. pdfView?.scaleFactor = pdfviewHeight!/pageHeight!
  80. pdfView?.autoScales = false
  81. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_ActualSize_Identifier {
  82. pdfView?.autoScales = true
  83. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_10_Identifier {
  84. pdfView?.scaleFactor = 0.1
  85. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_25_Identifier {
  86. pdfView?.scaleFactor = 0.25
  87. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_50_Identifier {
  88. pdfView?.scaleFactor = 0.5
  89. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_75_Identifier {
  90. pdfView?.scaleFactor = 0.75
  91. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_100_Identifier {
  92. pdfView?.scaleFactor = 1.0
  93. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_150_Identifier {
  94. pdfView?.scaleFactor = 1.5
  95. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_200_Identifier {
  96. pdfView?.scaleFactor = 2.0
  97. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_400_Identifier {
  98. pdfView?.scaleFactor = 4.0
  99. } else if menuItemProperty?.identifier == KMPDFToolbar_Zoom_800_Identifier {
  100. pdfView?.scaleFactor = 8.0
  101. }
  102. }
  103. }