// // 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 annotations: [CPDFMarkupAnnotation] = [] 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 setupProperty() { colorLabel.stringValue = KMLocalizedString("Font") 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 } else { colors = manager.markOtherColors } } else { if viewManager?.subToolMode == .Highlight { colors = manager.markHighlightColors } else { colors = manager.markOtherColors } } let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, 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 = CPDFAnnotationConfig.standard.colorWithType(.highlight) ?? .clear opacity = CPDFAnnotationConfig.standard.opacityWithType(.highlight) } else if viewManager?.subToolMode == .Underline { curColor = CPDFAnnotationConfig.standard.colorWithType(.underline) ?? .clear opacity = CPDFAnnotationConfig.standard.opacityWithType(.underline) } else if viewManager?.subToolMode == .Waveline { curColor = CPDFAnnotationConfig.standard.colorWithType(.squiggly) ?? .clear opacity = CPDFAnnotationConfig.standard.opacityWithType(.squiggly) } else if viewManager?.subToolMode == .Strikethrough { curColor = CPDFAnnotationConfig.standard.colorWithType(.strikeOut) ?? .clear opacity = CPDFAnnotationConfig.standard.opacityWithType(.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 = pdfView.isAnnotationsContainMultiType(annotations, withType: .color) if multiColor == true { colorGroup.currentColor = NSColor.clear } else { colorGroup.currentColor = firstAnnotation?.color } colorGroup.refreshUI() let multiOpacity: Bool = pdfView.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() } } } func updateDefaultInfo(_ color: NSColor?, _ opacity: CGFloat?) { if let resultColor = color { if annotations.count == 0 { if viewManager?.subToolMode == .Highlight { CPDFAnnotationConfig.standard.setColor(resultColor, toType: .highlight) } else if viewManager?.subToolMode == .Underline { CPDFAnnotationConfig.standard.setColor(resultColor, toType: .underline) } else if viewManager?.subToolMode == .Strikethrough { CPDFAnnotationConfig.standard.setColor(resultColor, toType: .strikeOut) } else if viewManager?.subToolMode == .Waveline { CPDFAnnotationConfig.standard.setColor(resultColor, toType: .squiggly) } } else { for annotation in annotations { CPDFAnnotationConfig.standard.setColor(resultColor, toType: annotation.exchangeToAnnotationType(annotation.markupType())) } } } if let resultOpacity = opacity { if annotations.count == 0 { if viewManager?.subToolMode == .Highlight { CPDFAnnotationConfig.standard.setOpacity(resultOpacity, toType: .highlight) } else if viewManager?.subToolMode == .Underline { CPDFAnnotationConfig.standard.setOpacity(resultOpacity, toType: .underline) } else if viewManager?.subToolMode == .Strikethrough { CPDFAnnotationConfig.standard.setOpacity(resultOpacity, toType: .strikeOut) } else if viewManager?.subToolMode == .Waveline { CPDFAnnotationConfig.standard.setOpacity(resultOpacity, toType: .squiggly) } } else { for annotation in annotations { CPDFAnnotationConfig.standard.setOpacity(resultOpacity, toType: annotation.exchangeToAnnotationType(annotation.markupType())) } } } } //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?) { CPDFMarkupAnnotation.update(annotations, color, PDFView: pdfView) updateDefaultInfo(color, nil) reloadData() } // func componentCColorDidRightMouseUpWithItems(_ view: NSView) -> [ComponentMenuitemProperty] { // let itemA = ComponentMenuitemProperty(text: "12312312312312") // return [itemA, itemA] // } // func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] { // return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")] // } func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) { print(menuItemProperty?.text) } } //MARK: - ComponentSliderDelegate extension KMHighlightController: ComponentSliderDelegate { func componentSliderDidUpdate(_ view: ComponentSlider) { let opacity = view.properties.percent CPDFMarkupAnnotation.update(annotations, opacity, withPDFView: pdfView) updateDefaultInfo(nil, 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)) CPDFMarkupAnnotation.update(annotations, opacity, withPDFView: pdfView) updateDefaultInfo(nil, opacity) reloadData() } } func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) { if let result = text { let opacity = max(0, min(1, result.stringToCGFloat()/100)) CPDFMarkupAnnotation.update(annotations, opacity, withPDFView: pdfView) updateDefaultInfo(nil, opacity) reloadData() } } }