KMHighlightController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. private var colorsSavedKey: String?
  22. var pdfView: CPDFListView?
  23. var viewManager: KMPDFViewManager?
  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 getValidAnnotations() -> [CPDFMarkupAnnotation] {
  35. return annotations
  36. }
  37. func setupProperty() {
  38. colorLabel.stringValue = KMLocalizedString("Color")
  39. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  40. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  41. colorGroup.delegate = self
  42. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  43. colorSlider.delegate = self
  44. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  45. state: .normal,
  46. creatable: true,
  47. text: "100%",
  48. textUnit: "%",
  49. regexString: "0123456789%")
  50. if true {
  51. var opacityItems: [ComponentMenuitemProperty] = []
  52. for string in ["25%", "50%", "75%", "100%"] {
  53. let item = ComponentMenuitemProperty(type: .normal, text: string)
  54. opacityItems.append(item)
  55. }
  56. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  57. }
  58. colorOpacitySelect.delegate = self
  59. }
  60. func reloadData() {
  61. //未选中,单选,多选
  62. guard let pdfView = self.pdfView else {
  63. return
  64. }
  65. let manager = KMAnnotationPropertiesColorManager.manager
  66. self.annotations.removeAll()
  67. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  68. for annotation in allAnnotations {
  69. if annotation is CPDFMarkupAnnotation {
  70. annotations.append((annotation as! CPDFMarkupAnnotation))
  71. }
  72. }
  73. var firstAnnotation: CPDFMarkupAnnotation? = nil
  74. if annotations.count > 0 {
  75. firstAnnotation = annotations.first
  76. }
  77. if true {
  78. var colors: [NSColor] = []
  79. if let annotation = firstAnnotation {
  80. if annotation.markupType() == .highlight {
  81. colors = manager.markHighlightColors
  82. colorsSavedKey = KMMarkupTypeHighlightColorsKey
  83. } else if annotation.markupType() == .underline {
  84. colors = manager.markHighlight_Underline_Colors
  85. colorsSavedKey = KMMarkupTypeUnderlineColorsKey
  86. } else if annotation.markupType() == .squiggly {
  87. colors = manager.markHighlight_Wavyline_Colors
  88. colorsSavedKey = KMMarkupTypeWavylineColorsKey
  89. } else if annotation.markupType() == .strikeOut {
  90. colors = manager.markHighlight_StrikeThrough_Colors
  91. colorsSavedKey = KMMarkupTypeStrikeThroughColorsKey
  92. } else {
  93. colors = manager.markOtherColors
  94. colorsSavedKey = KMMarkupTypeOtherColorsKey
  95. }
  96. } else {
  97. if viewManager?.subToolMode == .Highlight {
  98. colors = manager.markHighlightColors
  99. colorsSavedKey = KMMarkupTypeHighlightColorsKey
  100. } else if viewManager?.subToolMode == .Underline {
  101. colors = manager.markHighlight_Underline_Colors
  102. colorsSavedKey = KMMarkupTypeUnderlineColorsKey
  103. } else if viewManager?.subToolMode == .Waveline {
  104. colors = manager.markHighlight_Wavyline_Colors
  105. colorsSavedKey = KMMarkupTypeWavylineColorsKey
  106. } else if viewManager?.subToolMode == .Strikethrough {
  107. colors = manager.markHighlight_StrikeThrough_Colors
  108. colorsSavedKey = KMMarkupTypeStrikeThroughColorsKey
  109. } else {
  110. colors = manager.markOtherColors
  111. colorsSavedKey = KMMarkupTypeOtherColorsKey
  112. }
  113. }
  114. if colors.count > 4 {
  115. colorAProperty.color = colors[0]
  116. colorBProperty.color = colors[1]
  117. colorCProperty.color = colors[2]
  118. colorDProperty.color = colors[3]
  119. colorEProperty.color = colors[4]
  120. }
  121. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  122. }
  123. if annotations.count == 0 {
  124. var curColor: NSColor = NSColor.clear
  125. var opacity: CGFloat = 0
  126. if viewManager?.subToolMode == .Highlight {
  127. curColor = CPDFMarkupAnnotation.defaultColor(.highlight)
  128. opacity = CPDFMarkupAnnotation.defaultOpacity(.highlight)
  129. } else if viewManager?.subToolMode == .Underline {
  130. curColor = CPDFMarkupAnnotation.defaultColor(.underline)
  131. opacity = CPDFMarkupAnnotation.defaultOpacity(.underline)
  132. } else if viewManager?.subToolMode == .Waveline {
  133. curColor = CPDFMarkupAnnotation.defaultColor(.squiggly)
  134. opacity = CPDFMarkupAnnotation.defaultOpacity(.squiggly)
  135. } else if viewManager?.subToolMode == .Strikethrough {
  136. curColor = CPDFMarkupAnnotation.defaultColor(.strikeOut)
  137. opacity = CPDFMarkupAnnotation.defaultOpacity(.strikeOut)
  138. }
  139. colorGroup.currentColor = curColor
  140. colorGroup.refreshUI()
  141. colorSlider.properties.percent = opacity
  142. colorSlider.reloadData()
  143. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  144. colorOpacitySelect.reloadData()
  145. } else if annotations.count == 1 {
  146. colorGroup.currentColor = firstAnnotation?.color
  147. colorGroup.refreshUI()
  148. let opacity = firstAnnotation?.opacity ?? 0
  149. colorSlider.properties.percent = opacity
  150. colorSlider.reloadData()
  151. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  152. colorOpacitySelect.reloadData()
  153. } else {
  154. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  155. if multiColor == true {
  156. colorGroup.currentColor = nil
  157. } else {
  158. colorGroup.currentColor = firstAnnotation?.color
  159. }
  160. colorGroup.refreshUI()
  161. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  162. if multiOpacity {
  163. colorSlider.properties.percent = 0
  164. colorSlider.reloadData()
  165. colorOpacitySelect.resetText("-")
  166. } else {
  167. let opacity = firstAnnotation?.opacity ?? 0
  168. colorSlider.properties.percent = opacity
  169. colorSlider.reloadData()
  170. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  171. colorOpacitySelect.reloadData()
  172. }
  173. }
  174. }
  175. //MARK: - Mouse
  176. override func mouseDown(with event: NSEvent) {
  177. super.mouseDown(with: event)
  178. view.window?.makeFirstResponder(nil)
  179. }
  180. }
  181. //MARK: - ComponentCColorDelegate
  182. extension KMHighlightController: ComponentCColorDelegate {
  183. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  184. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  185. if annotations.count == 0 {
  186. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), color)
  187. } else {
  188. for annotation in annotations {
  189. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(annotation.exchangeToAnnotationType(annotation.markupType()), color)
  190. }
  191. }
  192. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  193. reloadData()
  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. if menuItemProperty?.text == KMLocalizedString("Change Color") {
  200. if NSColorPanel.sharedColorPanelExists {
  201. NSColorPanel.shared.setTarget(nil)
  202. NSColorPanel.shared.setAction(nil)
  203. }
  204. NSColorPanel.shared.setTarget(self)
  205. NSColorPanel.shared.setAction(#selector(colorChoose(_:)))
  206. if let colorItem = view as? ComponentCColorItem, let color = colorItem.properties?.color {
  207. NSColorPanel.shared.color = color
  208. }
  209. let viewRect = view.superview?.convert(view.frame, to: nil) ?? CGRectZero
  210. var rect = NSColorPanel.shared.frame
  211. rect.origin.x = viewRect.minX
  212. rect.origin.y = viewRect.minY - 30
  213. NSColorPanel.shared.setFrame(rect, display: true)
  214. NSColorPanel.shared.orderFront(nil)
  215. } else if menuItemProperty?.text == KMLocalizedString("Restore default color") {
  216. if let colorItem = colorGroup.rightClickedItem, let key = colorsSavedKey {
  217. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.defaultColors(key: key)
  218. if colors.count > 4, colorItem.itemIndex >= 0, colorItem.itemIndex < colors.count {
  219. colorItem.properties?.color = colors[colorItem.itemIndex]
  220. colorItem.reloadData()
  221. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  222. }
  223. }
  224. }
  225. }
  226. @objc func colorChoose(_ sender: Any) {
  227. if let colorItem = colorGroup.rightClickedItem {
  228. let color = NSColorPanel.shared.color
  229. colorItem.properties?.color = color
  230. colorItem.reloadData()
  231. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  232. }
  233. }
  234. func componentCColorGroupColorsUpdates(_ view: NSView, _ colors: [NSColor]) {
  235. if annotations.count == 0 {
  236. if viewManager?.subToolMode == .Highlight {
  237. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeHighlightColorsKey)
  238. } else if viewManager?.subToolMode == .Underline {
  239. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeUnderlineColorsKey)
  240. } else if viewManager?.subToolMode == .Waveline {
  241. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeWavylineColorsKey)
  242. } else if viewManager?.subToolMode == .Strikethrough {
  243. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeStrikeThroughColorsKey)
  244. }
  245. } else {
  246. for annotation in annotations {
  247. if annotation.markupType() == .highlight {
  248. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeHighlightColorsKey)
  249. } else if annotation.markupType() == .underline {
  250. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeUnderlineColorsKey)
  251. } else if annotation.markupType() == .squiggly {
  252. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeWavylineColorsKey)
  253. } else if annotation.markupType() == .strikeOut {
  254. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeStrikeThroughColorsKey)
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //MARK: - ComponentSliderDelegate
  261. extension KMHighlightController: ComponentSliderDelegate {
  262. func componentSliderDidUpdate(_ view: ComponentSlider) {
  263. let opacity = view.properties.percent
  264. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  265. if annotations.count == 0 {
  266. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  267. } else {
  268. for annotation in annotations {
  269. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  270. }
  271. }
  272. reloadData()
  273. }
  274. }
  275. //MARK: - ComponentSelectDelegate
  276. extension KMHighlightController: ComponentSelectDelegate {
  277. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  278. if var result = menuItemProperty?.text {
  279. if let textUnit = view?.properties.textUnit {
  280. result = result.stringByDeleteCharString(textUnit)
  281. }
  282. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  283. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  284. if annotations.count == 0 {
  285. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  286. } else {
  287. for annotation in annotations {
  288. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  289. }
  290. }
  291. reloadData()
  292. }
  293. }
  294. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  295. if let result = text {
  296. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  297. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  298. if annotations.count == 0 {
  299. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
  300. } else {
  301. for annotation in annotations {
  302. CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
  303. }
  304. }
  305. reloadData()
  306. }
  307. }
  308. }