KMNoteController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. colorGroup.delegate = self
  34. typeLabel.stringValue = KMLocalizedString("Color")
  35. typeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  36. typeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  37. typeItemA.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_1"), identifier: "")
  38. typeItemB.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_2"), identifier: "")
  39. typeItemC.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_3"), identifier: "")
  40. typeItemD.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_4"), identifier: "")
  41. typeItemE.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_5"), identifier: "")
  42. typeItemF.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_6"), identifier: "")
  43. typeItemG.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_7"), identifier: "")
  44. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  45. item!.setTarget(self, action: #selector(selectItemClick(_:)))
  46. }
  47. }
  48. func reloadData() {
  49. guard let pdfView = self.pdfView else {
  50. return
  51. }
  52. self.annotations.removeAll()
  53. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  54. for annotation in allAnnotations {
  55. if annotation is CPDFTextAnnotation {
  56. annotations.append((annotation as! CPDFTextAnnotation))
  57. }
  58. }
  59. var firstAnnotation: CPDFTextAnnotation?
  60. if annotations.count > 0 {
  61. firstAnnotation = annotations.first
  62. }
  63. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  64. item!.properties.state = .normal
  65. }
  66. if true {
  67. var colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.noteColors
  68. if colors.count > 4 {
  69. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  70. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  71. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  72. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  73. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  74. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  75. }
  76. }
  77. if annotations.count == 0 {
  78. colorGroup.currentColor = CPDFTextAnnotation.defaultColor()
  79. colorGroup.refreshUI()
  80. let anchoredIconType: CPDFTextAnnotationIconType = CPDFTextAnnotation.defaultType()
  81. if anchoredIconType == .comment {
  82. typeItemA.properties.state = .pressed
  83. } else if anchoredIconType == .key {
  84. typeItemC.properties.state = .pressed
  85. } else if anchoredIconType == .note {
  86. typeItemB.properties.state = .pressed
  87. } else if anchoredIconType == .help {
  88. typeItemD.properties.state = .pressed
  89. } else if anchoredIconType == .newParagraph {
  90. typeItemG.properties.state = .pressed
  91. } else if anchoredIconType == .paragraph {
  92. typeItemE.properties.state = .pressed
  93. } else if anchoredIconType == .insert {
  94. typeItemF.properties.state = .pressed
  95. }
  96. } else if annotations.count == 1, let annotation = firstAnnotation {
  97. colorGroup.currentColor = annotation.color
  98. colorGroup.refreshUI()
  99. let anchoredIconType: CPDFTextAnnotationIconType = annotation.iconType()
  100. if anchoredIconType == .comment {
  101. typeItemA.properties.state = .pressed
  102. } else if anchoredIconType == .key {
  103. typeItemC.properties.state = .pressed
  104. } else if anchoredIconType == .note {
  105. typeItemB.properties.state = .pressed
  106. } else if anchoredIconType == .help {
  107. typeItemD.properties.state = .pressed
  108. } else if anchoredIconType == .newParagraph {
  109. typeItemG.properties.state = .pressed
  110. } else if anchoredIconType == .paragraph {
  111. typeItemE.properties.state = .pressed
  112. } else if anchoredIconType == .insert {
  113. typeItemF.properties.state = .pressed
  114. }
  115. } else {
  116. var multiColor: Bool = pdfView.isAnnotationsContainMultiType(annotations, withType: .color)
  117. if multiColor == true {
  118. colorGroup.currentColor = NSColor.clear
  119. } else {
  120. if let annotation = firstAnnotation {
  121. colorGroup.currentColor = annotation.color
  122. }
  123. }
  124. colorGroup.refreshUI()
  125. var multiType = pdfView.isAnnotationsContainMultiType(annotations, withType: .note_Type)
  126. if multiType == true {
  127. } else if let annotation = firstAnnotation {
  128. let anchoredIconType: CPDFTextAnnotationIconType = annotation.iconType()
  129. if anchoredIconType == .comment {
  130. typeItemA.properties.state = .pressed
  131. } else if anchoredIconType == .key {
  132. typeItemC.properties.state = .pressed
  133. } else if anchoredIconType == .note {
  134. typeItemB.properties.state = .pressed
  135. } else if anchoredIconType == .help {
  136. typeItemD.properties.state = .pressed
  137. } else if anchoredIconType == .newParagraph {
  138. typeItemG.properties.state = .pressed
  139. } else if anchoredIconType == .paragraph {
  140. typeItemE.properties.state = .pressed
  141. } else if anchoredIconType == .insert {
  142. typeItemF.properties.state = .pressed
  143. }
  144. }
  145. }
  146. for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] {
  147. item!.reloadData()
  148. }
  149. }
  150. @objc func selectItemClick(_ sender: ComponentCSelector) {
  151. var anchoredIconType: CPDFTextAnnotationIconType = .comment
  152. if sender == typeItemA {
  153. anchoredIconType = .comment
  154. } else if sender == typeItemB {
  155. anchoredIconType = .note
  156. } else if sender == typeItemC {
  157. anchoredIconType = .key
  158. } else if sender == typeItemD {
  159. anchoredIconType = .help
  160. } else if sender == typeItemE {
  161. anchoredIconType = .paragraph
  162. } else if sender == typeItemF {
  163. anchoredIconType = .insert
  164. } else if sender == typeItemG {
  165. anchoredIconType = .newParagraph
  166. }
  167. CPDFTextAnnotation.updateIconType(annotations, anchoredIconType, withPDFView: pdfView)
  168. CPDFAnnotationConfig.setDefaultIntValue(anchoredIconType.rawValue, toKey: CAnchoredNoteIconTypeKey)
  169. reloadData()
  170. }
  171. }
  172. extension KMNoteController: ComponentCColorDelegate {
  173. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  174. CPDFTextAnnotation.updateColor(annotations, color, withPDFView: pdfView)
  175. if let resultColor = color {
  176. CPDFAnnotationConfig.standard.setColor(resultColor, toType: .anchored)
  177. }
  178. reloadData()
  179. }
  180. }