KMHighlightController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
  16. private var colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
  17. private var colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
  18. private var colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
  19. private var colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.clear)
  20. private var annotations: [CPDFMarkupAnnotation] = []
  21. var pdfView: CPDFListView?
  22. var viewManager: KMPDFViewManager?
  23. //MARK: - func
  24. override func viewDidAppear() {
  25. super.viewDidAppear()
  26. colorSlider.reloadData()
  27. }
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. // Do view setup here.
  31. setupProperty()
  32. }
  33. func getValidAnnotations() -> [CPDFMarkupAnnotation] {
  34. return annotations
  35. }
  36. func getValidAnnotationType() -> CAnnotationType {
  37. return .highlight
  38. }
  39. func setupProperty() {
  40. colorLabel.stringValue = KMLocalizedString("Color")
  41. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  42. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  43. colorGroup.delegate = self
  44. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  45. colorSlider.delegate = self
  46. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  47. state: .normal,
  48. creatable: true,
  49. text: "100%",
  50. textUnit: "%",
  51. regexString: "0123456789%")
  52. if true {
  53. var opacityItems: [ComponentMenuitemProperty] = []
  54. for string in ["25%", "50%", "75%", "100%"] {
  55. let item = ComponentMenuitemProperty(type: .normal, text: string)
  56. opacityItems.append(item)
  57. }
  58. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  59. }
  60. colorOpacitySelect.delegate = self
  61. }
  62. func reloadData() {
  63. //未选中,单选,多选
  64. guard let pdfView = self.pdfView else {
  65. return
  66. }
  67. let manager = KMAnnotationPropertiesColorManager.manager
  68. self.annotations.removeAll()
  69. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  70. for annotation in allAnnotations {
  71. if annotation is CPDFMarkupAnnotation {
  72. annotations.append((annotation as! CPDFMarkupAnnotation))
  73. }
  74. }
  75. var firstAnnotation: CPDFMarkupAnnotation? = nil
  76. if annotations.count > 0 {
  77. firstAnnotation = annotations.first
  78. }
  79. if true {
  80. var colors: [NSColor] = []
  81. if let annotation = firstAnnotation {
  82. if annotation.markupType() == .highlight {
  83. colors = manager.markHighlightColors
  84. } else if annotation.markupType() == .underline {
  85. colors = manager.markHighlight_Underline_Colors
  86. } else if annotation.markupType() == .squiggly {
  87. colors = manager.markHighlight_Wavyline_Colors
  88. } else if annotation.markupType() == .strikeOut {
  89. colors = manager.markHighlight_StrikeThrough_Colors
  90. } else {
  91. colors = manager.markOtherColors
  92. }
  93. } else {
  94. if viewManager?.subToolMode == .Highlight {
  95. colors = manager.markHighlightColors
  96. } else if viewManager?.subToolMode == .Underline {
  97. colors = manager.markHighlight_Underline_Colors
  98. } else if viewManager?.subToolMode == .Waveline {
  99. colors = manager.markHighlight_Wavyline_Colors
  100. } else if viewManager?.subToolMode == .Strikethrough {
  101. colors = manager.markHighlight_StrikeThrough_Colors
  102. } else {
  103. colors = manager.markOtherColors
  104. }
  105. }
  106. if colors.count > 4 {
  107. colorAProperty.color = colors[0]
  108. colorBProperty.color = colors[1]
  109. colorCProperty.color = colors[2]
  110. colorDProperty.color = colors[3]
  111. colorEProperty.color = colors[4]
  112. }
  113. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  114. }
  115. if annotations.count == 0 {
  116. var curColor: NSColor = NSColor.clear
  117. var opacity: CGFloat = 0
  118. if viewManager?.subToolMode == .Highlight {
  119. curColor = CPDFMarkupAnnotation.defaultColor(.highlight)
  120. opacity = CPDFMarkupAnnotation.defaultOpacity(.highlight)
  121. } else if viewManager?.subToolMode == .Underline {
  122. curColor = CPDFMarkupAnnotation.defaultColor(.underline)
  123. opacity = CPDFMarkupAnnotation.defaultOpacity(.underline)
  124. } else if viewManager?.subToolMode == .Waveline {
  125. curColor = CPDFMarkupAnnotation.defaultColor(.squiggly)
  126. opacity = CPDFMarkupAnnotation.defaultOpacity(.squiggly)
  127. } else if viewManager?.subToolMode == .Strikethrough {
  128. curColor = CPDFMarkupAnnotation.defaultColor(.strikeOut)
  129. opacity = CPDFMarkupAnnotation.defaultOpacity(.strikeOut)
  130. }
  131. colorGroup.currentColor = curColor
  132. colorGroup.refreshUI()
  133. colorSlider.properties.percent = opacity
  134. colorSlider.reloadData()
  135. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  136. colorOpacitySelect.reloadData()
  137. } else if annotations.count == 1 {
  138. colorGroup.currentColor = firstAnnotation?.color
  139. colorGroup.refreshUI()
  140. let opacity = firstAnnotation?.opacity ?? 0
  141. colorSlider.properties.percent = opacity
  142. colorSlider.reloadData()
  143. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  144. colorOpacitySelect.reloadData()
  145. } else {
  146. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  147. if multiColor == true {
  148. colorGroup.currentColor = nil
  149. } else {
  150. colorGroup.currentColor = firstAnnotation?.color
  151. }
  152. colorGroup.refreshUI()
  153. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  154. if multiOpacity {
  155. colorSlider.properties.percent = 0
  156. colorSlider.reloadData()
  157. colorOpacitySelect.resetText("-")
  158. } else {
  159. let opacity = firstAnnotation?.opacity ?? 0
  160. colorSlider.properties.percent = opacity
  161. colorSlider.reloadData()
  162. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  163. colorOpacitySelect.reloadData()
  164. }
  165. }
  166. }
  167. //MARK: - Mouse
  168. override func mouseDown(with event: NSEvent) {
  169. super.mouseDown(with: event)
  170. view.window?.makeFirstResponder(nil)
  171. }
  172. }
  173. //MARK: - ComponentCColorDelegate
  174. extension KMHighlightController: ComponentCColorDelegate {
  175. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  176. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  177. if annotations.count == 0 {
  178. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), color)
  179. } else {
  180. for annotation in annotations {
  181. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(annotation.exchangeToAnnotationType(annotation.markupType()), color)
  182. }
  183. }
  184. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  185. reloadData()
  186. }
  187. // func componentCColorDidRightMouseUpWithItems(_ view: NSView) -> [ComponentMenuitemProperty] {
  188. // let itemA = ComponentMenuitemProperty(text: "12312312312312")
  189. // return [itemA, itemA]
  190. // }
  191. // func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
  192. // return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")]
  193. // }
  194. func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
  195. print(menuItemProperty?.text)
  196. }
  197. }
  198. //MARK: - ComponentSliderDelegate
  199. extension KMHighlightController: ComponentSliderDelegate {
  200. func componentSliderDidUpdate(_ view: ComponentSlider) {
  201. let opacity = view.properties.percent
  202. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  203. if annotations.count == 0 {
  204. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  205. } else {
  206. for annotation in annotations {
  207. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  208. }
  209. }
  210. reloadData()
  211. }
  212. }
  213. //MARK: - ComponentSelectDelegate
  214. extension KMHighlightController: ComponentSelectDelegate {
  215. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  216. if var result = menuItemProperty?.text {
  217. if let textUnit = view?.properties.textUnit {
  218. result = result.stringByDeleteCharString(textUnit)
  219. }
  220. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  221. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  222. if annotations.count == 0 {
  223. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  224. } else {
  225. for annotation in annotations {
  226. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  227. }
  228. }
  229. reloadData()
  230. }
  231. }
  232. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  233. if let result = text {
  234. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  235. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  236. if annotations.count == 0 {
  237. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  238. } else {
  239. for annotation in annotations {
  240. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  241. }
  242. }
  243. reloadData()
  244. }
  245. }
  246. }