KMNoteController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // KMNoteController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNoteController: NSViewController {
  10. @IBOutlet var colorBGView: NSView!
  11. @IBOutlet var colorLabel: NSTextField!
  12. @IBOutlet var colorGroup: ComponentCColorGroup!
  13. @IBOutlet var typeBGView: NSView!
  14. @IBOutlet var typeLabel: NSTextField!
  15. @IBOutlet var typeItemA: ComponentCSelector!
  16. @IBOutlet var typeItemB: ComponentCSelector!
  17. @IBOutlet var typeItemC: ComponentCSelector!
  18. @IBOutlet var typeItemD: ComponentCSelector!
  19. @IBOutlet var typeItemE: ComponentCSelector!
  20. @IBOutlet var typeItemF: ComponentCSelector!
  21. @IBOutlet var typeItemG: ComponentCSelector!
  22. private var annotations: [CPDFTextAnnotation] = []
  23. var pdfView: CPDFListView?
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. // Do view setup here.
  27. setupProperty()
  28. }
  29. func setupProperty() {
  30. colorLabel.stringValue = KMLocalizedString("Color")
  31. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  32. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  33. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  34. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  35. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  36. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  37. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  38. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  39. colorGroup.delegate = self
  40. typeLabel.stringValue = KMLocalizedString("Color")
  41. typeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  42. typeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  43. typeItemA.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_1"), identifier: "")
  44. typeItemB.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_2"), identifier: "")
  45. typeItemC.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_3"), identifier: "")
  46. typeItemD.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_4"), identifier: "")
  47. typeItemE.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_5"), identifier: "")
  48. typeItemF.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_6"), identifier: "")
  49. typeItemG.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_7"), identifier: "")
  50. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  51. item!.setTarget(self, action: #selector(selectItemClick(_:)))
  52. }
  53. }
  54. func reloadData() {
  55. guard let pdfView = self.pdfView else {
  56. return
  57. }
  58. self.annotations.removeAll()
  59. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  60. for annotation in allAnnotations {
  61. if annotation is CPDFTextAnnotation {
  62. annotations.append((annotation as! CPDFTextAnnotation))
  63. }
  64. }
  65. if annotations.count == 0 {
  66. return
  67. }
  68. guard let firstAnnotation = annotations.first else {
  69. return
  70. }
  71. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  72. item!.properties.state = .normal
  73. }
  74. if annotations.count == 1 {
  75. colorGroup.currentColor = firstAnnotation.color
  76. colorGroup.refreshUI()
  77. let anchoredIconType: CPDFTextAnnotationIconType = firstAnnotation.iconType()
  78. if anchoredIconType == .comment {
  79. typeItemA.properties.state = .pressed
  80. } else if anchoredIconType == .key {
  81. typeItemC.properties.state = .pressed
  82. } else if anchoredIconType == .note {
  83. typeItemB.properties.state = .pressed
  84. } else if anchoredIconType == .help {
  85. typeItemD.properties.state = .pressed
  86. } else if anchoredIconType == .newParagraph {
  87. typeItemG.properties.state = .pressed
  88. } else if anchoredIconType == .paragraph {
  89. typeItemE.properties.state = .pressed
  90. } else if anchoredIconType == .insert {
  91. typeItemF.properties.state = .pressed
  92. }
  93. } else {
  94. var multiColor: Bool = false
  95. for annotationA in annotations {
  96. for annotationB in annotations {
  97. if annotationA != annotationB {
  98. if annotationA.color != annotationB.color {
  99. multiColor = true
  100. break
  101. }
  102. }
  103. }
  104. if multiColor == true {
  105. break
  106. }
  107. }
  108. if multiColor == true {
  109. colorGroup.currentColor = NSColor.clear
  110. } else {
  111. colorGroup.currentColor = firstAnnotation.color
  112. }
  113. colorGroup.refreshUI()
  114. //
  115. // var multiOpacity: Bool = false
  116. // for annotationA in annotations {
  117. // for annotationB in annotations {
  118. // if annotationA != annotationB {
  119. // if annotationA.opacity != annotationB.opacity {
  120. // multiOpacity = true
  121. // break
  122. // }
  123. // }
  124. // }
  125. // if multiOpacity == true {
  126. // break
  127. // }
  128. // }
  129. //
  130. // if multiOpacity {
  131. // colorSlider.properties.percent = 0
  132. // colorSlider.reloadData()
  133. //
  134. // colorOpacitySelect.resetText("-")
  135. // } else {
  136. // let opacity = firstAnnotation?.opacity ?? 0
  137. //
  138. // colorSlider.properties.percent = opacity
  139. // colorSlider.reloadData()
  140. //
  141. // colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  142. // colorOpacitySelect.reloadData()
  143. // }
  144. }
  145. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  146. item!.reloadData()
  147. }
  148. }
  149. @objc func selectItemClick(_ sender: ComponentCSelector) {
  150. var anchoredIconType: CPDFTextAnnotationIconType = .comment
  151. if sender == typeItemA {
  152. anchoredIconType = .comment
  153. } else if sender == typeItemB {
  154. anchoredIconType = .note
  155. } else if sender == typeItemC {
  156. anchoredIconType = .key
  157. } else if sender == typeItemD {
  158. anchoredIconType = .help
  159. } else if sender == typeItemE {
  160. anchoredIconType = .paragraph
  161. } else if sender == typeItemF {
  162. anchoredIconType = .insert
  163. } else if sender == typeItemG {
  164. anchoredIconType = .newParagraph
  165. }
  166. for annotation in annotations {
  167. annotation.updateIconType(anchoredIconType, withPDFView: pdfView)
  168. }
  169. reloadData()
  170. }
  171. }
  172. extension KMNoteController: ComponentCColorDelegate {
  173. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  174. for annotation in self.annotations {
  175. annotation.updateColor(color, withPDFView: pdfView)
  176. }
  177. reloadData()
  178. }
  179. }