KMPenController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // KMPenController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMPenController: NSViewController {
  10. //Color
  11. @IBOutlet var colorBGView: NSView!
  12. @IBOutlet var colorLabel: NSTextField!
  13. @IBOutlet var colorGroup: ComponentCColorGroup!
  14. @IBOutlet var colorSlider: ComponentSlider!
  15. @IBOutlet var colorOpacitySelect: ComponentSelect!
  16. //Line
  17. @IBOutlet var lineBGView: NSView!
  18. @IBOutlet var lineLabel: NSTextField!
  19. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  20. @IBOutlet var lineWidthSlider: ComponentSlider!
  21. @IBOutlet var lineWidthSelect: ComponentSelect!
  22. private var annotations: [CPDFAnnotation] = []
  23. var pdfView: CPDFListView?
  24. //MARK: - func
  25. override func viewDidAppear() {
  26. super.viewDidAppear()
  27. colorSlider.reloadData()
  28. }
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do view setup here.
  32. setupProperty()
  33. }
  34. func setupProperty() {
  35. //Color
  36. colorLabel.stringValue = KMLocalizedString("Color")
  37. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  38. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  39. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  40. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  41. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  42. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  43. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  44. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  45. colorGroup.delegate = self
  46. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  47. colorSlider.delegate = self
  48. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  49. state: .normal,
  50. creatable: true,
  51. text: "100%",
  52. textUnit: "%",
  53. regexString: "0123456789%")
  54. if true {
  55. var opacityItems: [ComponentMenuitemProperty] = []
  56. for string in ["25%", "50%", "75%", "100%"] {
  57. let item = ComponentMenuitemProperty(type: .normal, text: string)
  58. opacityItems.append(item)
  59. }
  60. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  61. }
  62. colorOpacitySelect.delegate = self
  63. //Line
  64. lineLabel.stringValue = KMLocalizedString("Line")
  65. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  66. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  67. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  68. lineWidthSlider.delegate = self
  69. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  70. state: .normal,
  71. creatable: true,
  72. text: "2",
  73. textUnit: " pt",
  74. regexString: "0123456789 pt")
  75. if true {
  76. var opacityItems: [ComponentMenuitemProperty] = []
  77. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  78. let item = ComponentMenuitemProperty(type: .normal, text: string)
  79. opacityItems.append(item)
  80. }
  81. lineWidthSelect.updateMenuItemsArr(opacityItems)
  82. }
  83. lineWidthSelect.delegate = self
  84. }
  85. func reloadData() {
  86. guard let pdfView = self.pdfView else {
  87. return
  88. }
  89. let manager = KMAnnotationPropertiesColorManager.manager
  90. self.annotations.removeAll()
  91. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  92. for annotation in allAnnotations {
  93. if annotation is CPDFMarkupAnnotation {
  94. annotations.append((annotation as! CPDFMarkupAnnotation))
  95. }
  96. }
  97. if annotations.count == 0 {
  98. return
  99. }
  100. let firstAnnotation = annotations.first
  101. if annotations.count == 1 {
  102. colorGroup.currentColor = firstAnnotation?.color
  103. colorGroup.refreshUI()
  104. let opacity = firstAnnotation?.opacity ?? 0
  105. colorSlider.properties.percent = opacity
  106. colorSlider.reloadData()
  107. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  108. colorOpacitySelect.reloadData()
  109. } else {
  110. var multiColor: Bool = false
  111. for annotationA in annotations {
  112. for annotationB in annotations {
  113. if annotationA != annotationB {
  114. if annotationA.color != annotationB.color {
  115. multiColor = true
  116. break
  117. }
  118. }
  119. }
  120. if multiColor == true {
  121. break
  122. }
  123. }
  124. if multiColor == true {
  125. colorGroup.currentColor = NSColor.clear
  126. } else {
  127. colorGroup.currentColor = firstAnnotation?.color
  128. }
  129. colorGroup.refreshUI()
  130. var multiOpacity: Bool = false
  131. for annotationA in annotations {
  132. for annotationB in annotations {
  133. if annotationA != annotationB {
  134. if annotationA.opacity != annotationB.opacity {
  135. multiOpacity = true
  136. break
  137. }
  138. }
  139. }
  140. if multiOpacity == true {
  141. break
  142. }
  143. }
  144. if multiOpacity {
  145. colorSlider.properties.percent = 0
  146. colorSlider.reloadData()
  147. colorOpacitySelect.resetText("-")
  148. } else {
  149. let opacity = firstAnnotation?.opacity ?? 0
  150. colorSlider.properties.percent = opacity
  151. colorSlider.reloadData()
  152. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  153. colorOpacitySelect.reloadData()
  154. }
  155. }
  156. }
  157. //MARK: - Mouse
  158. override func mouseDown(with event: NSEvent) {
  159. super.mouseDown(with: event)
  160. view.window?.makeFirstResponder(nil)
  161. }
  162. }
  163. //MARK: - ComponentCColorDelegate
  164. extension KMPenController: ComponentCColorDelegate {
  165. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  166. reloadData()
  167. }
  168. }
  169. //MARK: - ComponentSliderDelegate
  170. extension KMPenController: ComponentSliderDelegate {
  171. func componentSliderDidUpdate(_ view: ComponentSlider) {
  172. let percent = view.properties.percent
  173. reloadData()
  174. }
  175. }
  176. //MARK: - ComponentSelectDelegate
  177. extension KMPenController: ComponentSelectDelegate {
  178. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  179. if let result = text {
  180. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  181. reloadData()
  182. }
  183. }
  184. }