KMHighlightController.swift 17 KB

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