KMHighlightController.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. let manager = KMAnnotationPropertiesColorManager.manager
  61. self.annotations.removeAll()
  62. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  63. for annotation in allAnnotations {
  64. if annotation is CPDFMarkupAnnotation {
  65. annotations.append((annotation as! CPDFMarkupAnnotation))
  66. }
  67. }
  68. if annotations.count == 0 {
  69. return
  70. }
  71. let firstAnnotation = annotations.first
  72. if annotations.count == 1 {
  73. colorGroup.currentColor = firstAnnotation?.color
  74. colorGroup.refreshUI()
  75. let opacity = firstAnnotation?.opacity ?? 0
  76. colorSlider.properties.percent = opacity
  77. colorSlider.reloadData()
  78. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  79. colorOpacitySelect.reloadData()
  80. } else {
  81. let multiColor: Bool = pdfView.isAnnotationsContainMultiType(annotations, withType: .color)
  82. if multiColor == true {
  83. colorGroup.currentColor = NSColor.clear
  84. } else {
  85. colorGroup.currentColor = firstAnnotation?.color
  86. }
  87. colorGroup.refreshUI()
  88. let multiOpacity: Bool = pdfView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  89. if multiOpacity {
  90. colorSlider.properties.percent = 0
  91. colorSlider.reloadData()
  92. colorOpacitySelect.resetText("-")
  93. } else {
  94. let opacity = firstAnnotation?.opacity ?? 0
  95. colorSlider.properties.percent = opacity
  96. colorSlider.reloadData()
  97. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  98. colorOpacitySelect.reloadData()
  99. }
  100. }
  101. }
  102. //MARK: - Mouse
  103. override func mouseDown(with event: NSEvent) {
  104. super.mouseDown(with: event)
  105. view.window?.makeFirstResponder(nil)
  106. }
  107. }
  108. //MARK: - ComponentCColorDelegate
  109. extension KMHighlightController: ComponentCColorDelegate {
  110. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  111. for annotation in self.annotations {
  112. annotation.updateColor(color, withPDFView: pdfView)
  113. }
  114. reloadData()
  115. }
  116. // func componentCColorDidRightMouseUpWithItems(_ view: NSView) -> [ComponentMenuitemProperty] {
  117. // let itemA = ComponentMenuitemProperty(text: "12312312312312")
  118. // return [itemA, itemA]
  119. // }
  120. // func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
  121. // return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")]
  122. // }
  123. func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
  124. print(menuItemProperty?.text)
  125. }
  126. }
  127. //MARK: - ComponentSliderDelegate
  128. extension KMHighlightController: ComponentSliderDelegate {
  129. func componentSliderDidUpdate(_ view: ComponentSlider) {
  130. let percent = view.properties.percent
  131. for annotation in self.annotations {
  132. annotation.updateOpacity(percent, withPDFView: pdfView)
  133. }
  134. reloadData()
  135. }
  136. }
  137. //MARK: - ComponentSelectDelegate
  138. extension KMHighlightController: ComponentSelectDelegate {
  139. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  140. if let result = text {
  141. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  142. for annotation in self.annotations {
  143. annotation.updateOpacity(opacity, withPDFView: pdfView)
  144. }
  145. reloadData()
  146. }
  147. }
  148. }