KMHighlightController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KMHighlightController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMHighlightController: NSViewController {
  10. @IBOutlet var colorBGView: NSView!
  11. @IBOutlet var colorLabel: NSTextField!
  12. @IBOutlet var colorGroup: ComponentCColorGroup!
  13. @IBOutlet var colorSlider: ComponentSlider!
  14. @IBOutlet var colorOpacitySelect: ComponentSelect!
  15. private var annotations: [CPDFMarkupAnnotation] = []
  16. var pdfView: CPDFListView?
  17. //MARK: - func
  18. override func viewDidAppear() {
  19. super.viewDidAppear()
  20. colorSlider.reloadData()
  21. }
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do view setup here.
  25. setupProperty()
  26. }
  27. func setupProperty() {
  28. colorLabel.stringValue = KMLocalizedString("Font")
  29. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  30. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  31. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  32. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  33. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  34. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  35. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  36. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  37. colorGroup.delegate = self
  38. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  39. colorSlider.delegate = self
  40. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  41. state: .normal,
  42. creatable: true,
  43. text: "100%",
  44. textUnit: "%",
  45. regexString: "0123456789%")
  46. if true {
  47. var opacityItems: [ComponentMenuitemProperty] = []
  48. for string in ["25%", "50%", "75%", "100%"] {
  49. let item = ComponentMenuitemProperty(type: .normal, text: string)
  50. opacityItems.append(item)
  51. }
  52. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  53. }
  54. colorOpacitySelect.delegate = self
  55. }
  56. func reloadData() {
  57. guard let pdfView = self.pdfView else {
  58. return
  59. }
  60. self.annotations.removeAll()
  61. let pdfAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  62. for annotation in pdfAnnotations {
  63. if annotation is CPDFMarkupAnnotation {
  64. self.annotations.append((annotation as! CPDFMarkupAnnotation))
  65. }
  66. }
  67. let manager = KMAnnotationPropertiesColorManager.manager
  68. if annotations.count == 1 {
  69. let curAnnotation = annotations.first
  70. colorGroup.currentColor = curAnnotation?.color
  71. let opacity = curAnnotation?.opacity ?? 0
  72. colorSlider.properties.percent = opacity
  73. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  74. } else {
  75. }
  76. colorGroup.refreshUI()
  77. colorSlider.reloadData()
  78. colorOpacitySelect.reloadData()
  79. }
  80. }
  81. //MARK: - ComponentCColorDelegate
  82. extension KMHighlightController: ComponentCColorDelegate {
  83. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  84. pdfView?.setAnnotationsColor(annotations, color)
  85. reloadData()
  86. }
  87. }
  88. //MARK: - ComponentSliderDelegate
  89. extension KMHighlightController: ComponentSliderDelegate {
  90. func componentSliderDidUpdate(_ view: ComponentSlider) {
  91. let percent = view.properties.percent
  92. pdfView?.setAnnotationsOpacity(annotations, percent)
  93. reloadData()
  94. }
  95. }
  96. //MARK: - ComponentSelectDelegate
  97. extension KMHighlightController: ComponentSelectDelegate {
  98. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  99. if let result = text {
  100. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  101. pdfView?.setAnnotationsOpacity(annotations, opacity)
  102. reloadData()
  103. }
  104. }
  105. }