123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- //
- // KMHighlightController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/26.
- //
- import Cocoa
- import KMComponentLibrary
- class KMHighlightController: NSViewController {
-
- @IBOutlet var colorBGView: NSView!
- @IBOutlet var colorLabel: NSTextField!
- @IBOutlet var colorGroup: ComponentCColorGroup!
- @IBOutlet var colorSlider: ComponentSlider!
- @IBOutlet var colorOpacitySelect: ComponentSelect!
-
- private var colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
- private var colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
- private var colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
- private var colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: NSColor.clear)
- private var colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.clear)
-
- private var annotations: [CPDFMarkupAnnotation] = []
-
- private var colorsSavedKey: String?
-
- var pdfView: CPDFListView?
- var viewManager: KMPDFViewManager?
-
- //MARK: - func
- override func viewDidAppear() {
- super.viewDidAppear()
-
- colorSlider.reloadData()
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- setupProperty()
- }
-
- func getValidAnnotations() -> [CPDFMarkupAnnotation] {
- return annotations
- }
-
- func setupProperty() {
-
- colorLabel.stringValue = KMLocalizedString("Color")
- colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- colorGroup.delegate = self
-
- colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
- colorSlider.delegate = self
-
- colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- creatable: true,
- text: "100%",
- textUnit: "%",
- regexString: "0123456789%")
- if true {
- var opacityItems: [ComponentMenuitemProperty] = []
- for string in ["25%", "50%", "75%", "100%"] {
- let item = ComponentMenuitemProperty(type: .normal, text: string)
- opacityItems.append(item)
- }
- colorOpacitySelect.updateMenuItemsArr(opacityItems)
- }
- colorOpacitySelect.delegate = self
-
- }
-
- func reloadData() {
- //未选中,单选,多选
- guard let pdfView = self.pdfView else {
- return
- }
- let manager = KMAnnotationPropertiesColorManager.manager
-
- self.annotations.removeAll()
- let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
- for annotation in allAnnotations {
- if annotation is CPDFMarkupAnnotation {
- annotations.append((annotation as! CPDFMarkupAnnotation))
- }
- }
-
- var firstAnnotation: CPDFMarkupAnnotation? = nil
- if annotations.count > 0 {
- firstAnnotation = annotations.first
- }
- if true {
- var colors: [NSColor] = []
- if let annotation = firstAnnotation {
- if annotation.markupType() == .highlight {
- colors = manager.markHighlightColors
- colorsSavedKey = KMMarkupTypeHighlightColorsKey
- } else if annotation.markupType() == .underline {
- colors = manager.markHighlight_Underline_Colors
- colorsSavedKey = KMMarkupTypeUnderlineColorsKey
- } else if annotation.markupType() == .squiggly {
- colors = manager.markHighlight_Wavyline_Colors
- colorsSavedKey = KMMarkupTypeWavylineColorsKey
- } else if annotation.markupType() == .strikeOut {
- colors = manager.markHighlight_StrikeThrough_Colors
- colorsSavedKey = KMMarkupTypeStrikeThroughColorsKey
- } else {
- colors = manager.markOtherColors
- colorsSavedKey = KMMarkupTypeOtherColorsKey
- }
- } else {
- if viewManager?.subToolMode == .Highlight {
- colors = manager.markHighlightColors
- colorsSavedKey = KMMarkupTypeHighlightColorsKey
- } else if viewManager?.subToolMode == .Underline {
- colors = manager.markHighlight_Underline_Colors
- colorsSavedKey = KMMarkupTypeUnderlineColorsKey
- } else if viewManager?.subToolMode == .Waveline {
- colors = manager.markHighlight_Wavyline_Colors
- colorsSavedKey = KMMarkupTypeWavylineColorsKey
- } else if viewManager?.subToolMode == .Strikethrough {
- colors = manager.markHighlight_StrikeThrough_Colors
- colorsSavedKey = KMMarkupTypeStrikeThroughColorsKey
- } else {
- colors = manager.markOtherColors
- colorsSavedKey = KMMarkupTypeOtherColorsKey
- }
- }
- if colors.count > 4 {
- colorAProperty.color = colors[0]
- colorBProperty.color = colors[1]
- colorCProperty.color = colors[2]
- colorDProperty.color = colors[3]
- colorEProperty.color = colors[4]
- }
- colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
- }
-
- if annotations.count == 0 {
-
- var curColor: NSColor = NSColor.clear
- var opacity: CGFloat = 0
-
- if viewManager?.subToolMode == .Highlight {
- curColor = CPDFMarkupAnnotation.defaultColor(.highlight)
- opacity = CPDFMarkupAnnotation.defaultOpacity(.highlight)
- } else if viewManager?.subToolMode == .Underline {
- curColor = CPDFMarkupAnnotation.defaultColor(.underline)
- opacity = CPDFMarkupAnnotation.defaultOpacity(.underline)
- } else if viewManager?.subToolMode == .Waveline {
- curColor = CPDFMarkupAnnotation.defaultColor(.squiggly)
- opacity = CPDFMarkupAnnotation.defaultOpacity(.squiggly)
- } else if viewManager?.subToolMode == .Strikethrough {
- curColor = CPDFMarkupAnnotation.defaultColor(.strikeOut)
- opacity = CPDFMarkupAnnotation.defaultOpacity(.strikeOut)
- }
-
- colorGroup.currentColor = curColor
- colorGroup.refreshUI()
-
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
-
- } else if annotations.count == 1 {
- colorGroup.currentColor = firstAnnotation?.color
- colorGroup.refreshUI()
-
- let opacity = firstAnnotation?.opacity ?? 0
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
- } else {
- let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
- if multiColor == true {
- colorGroup.currentColor = nil
- } else {
- colorGroup.currentColor = firstAnnotation?.color
- }
- colorGroup.refreshUI()
-
- let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
- if multiOpacity {
- colorSlider.properties.percent = 0
- colorSlider.reloadData()
-
- colorOpacitySelect.resetText("-")
- } else {
- let opacity = firstAnnotation?.opacity ?? 0
-
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
- }
- }
- }
-
- //MARK: - Mouse
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- view.window?.makeFirstResponder(nil)
- }
-
-
- }
- //MARK: - ComponentCColorDelegate
- extension KMHighlightController: ComponentCColorDelegate {
- func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
- CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
-
- if annotations.count == 0 {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), color)
- } else {
- for annotation in annotations {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultColor(annotation.exchangeToAnnotationType(annotation.markupType()), color)
- }
- }
-
- NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
-
- reloadData()
-
- }
-
- func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
- return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")]
- }
-
- func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
- if menuItemProperty?.text == KMLocalizedString("Change Color") {
- if NSColorPanel.sharedColorPanelExists {
- NSColorPanel.shared.setTarget(nil)
- NSColorPanel.shared.setAction(nil)
- }
- NSColorPanel.shared.setTarget(self)
- NSColorPanel.shared.setAction(#selector(colorChoose(_:)))
- if let colorItem = view as? ComponentCColorItem, let color = colorItem.properties?.color {
- NSColorPanel.shared.color = color
- }
-
- let viewRect = view.superview?.convert(view.frame, to: nil) ?? CGRectZero
- var rect = NSColorPanel.shared.frame
- rect.origin.x = viewRect.minX
- rect.origin.y = viewRect.minY - 30
- NSColorPanel.shared.setFrame(rect, display: true)
-
- NSColorPanel.shared.orderFront(nil)
- } else if menuItemProperty?.text == KMLocalizedString("Restore default color") {
- if let colorItem = colorGroup.rightClickedItem, let key = colorsSavedKey {
- let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.defaultColors(key: key)
- if colors.count > 4, colorItem.itemIndex >= 0, colorItem.itemIndex < colors.count {
- colorItem.properties?.color = colors[colorItem.itemIndex]
- colorItem.reloadData()
- colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
- }
- }
- }
- }
-
- @objc func colorChoose(_ sender: Any) {
- if let colorItem = colorGroup.rightClickedItem {
- let color = NSColorPanel.shared.color
- colorItem.properties?.color = color
-
- colorItem.reloadData()
-
- colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
- }
- }
-
- func componentCColorGroupColorsUpdates(_ view: NSView, _ colors: [NSColor]) {
- if annotations.count == 0 {
- if viewManager?.subToolMode == .Highlight {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeHighlightColorsKey)
- } else if viewManager?.subToolMode == .Underline {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeUnderlineColorsKey)
- } else if viewManager?.subToolMode == .Waveline {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeWavylineColorsKey)
- } else if viewManager?.subToolMode == .Strikethrough {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeStrikeThroughColorsKey)
- }
- } else {
- for annotation in annotations {
- if annotation.markupType() == .highlight {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeHighlightColorsKey)
- } else if annotation.markupType() == .underline {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeUnderlineColorsKey)
- } else if annotation.markupType() == .squiggly {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeWavylineColorsKey)
- } else if annotation.markupType() == .strikeOut {
- KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMMarkupTypeStrikeThroughColorsKey)
- }
- }
- }
- }
- }
- //MARK: - ComponentSliderDelegate
- extension KMHighlightController: ComponentSliderDelegate {
- func componentSliderDidUpdate(_ view: ComponentSlider) {
- let opacity = view.properties.percent
-
- CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
-
- if annotations.count == 0 {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
- } else {
- for annotation in annotations {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
- }
- }
-
- reloadData()
-
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMHighlightController: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if var result = menuItemProperty?.text {
- if let textUnit = view?.properties.textUnit {
- result = result.stringByDeleteCharString(textUnit)
- }
- let opacity = max(0, min(1, result.stringToCGFloat()/100))
- CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
-
- if annotations.count == 0 {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
- } else {
- for annotation in annotations {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
- }
- }
-
- reloadData()
- }
- }
-
- func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
- if let result = text {
- let opacity = max(0, min(1, result.stringToCGFloat()/100))
- CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
-
- if annotations.count == 0 {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(KMPDFViewManager.getValidPDFAnnotationType(viewManager?.subToolMode), opacity)
- } else {
- for annotation in annotations {
- CPDFMarkupAnnotation.updateMarkupAnnotationDefaultOpacity(annotation.exchangeToAnnotationType(annotation.markupType()), opacity)
- }
- }
-
- reloadData()
-
- }
- }
- }
|