KMHighlightController.swift 12 KB

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