// // 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? //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") let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4]) colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) 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 } self.annotations.removeAll() let pdfAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? [] for annotation in pdfAnnotations { if annotation is CPDFMarkupAnnotation { self.annotations.append((annotation as! CPDFMarkupAnnotation)) } } let manager = KMAnnotationPropertiesColorManager.manager if annotations.count == 1 { let curAnnotation = annotations.first colorGroup.currentColor = curAnnotation?.color let opacity = curAnnotation?.opacity ?? 0 colorSlider.properties.percent = opacity colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") } else { } colorGroup.refreshUI() colorSlider.reloadData() colorOpacitySelect.reloadData() } } //MARK: - ComponentCColorDelegate extension KMHighlightController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { pdfView?.setAnnotationsColor(annotations, color) reloadData() } } //MARK: - ComponentSliderDelegate extension KMHighlightController: ComponentSliderDelegate { func componentSliderDidUpdate(_ view: ComponentSlider) { let percent = view.properties.percent pdfView?.setAnnotationsOpacity(annotations, percent) reloadData() } } //MARK: - ComponentSelectDelegate extension KMHighlightController: ComponentSelectDelegate { func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) { if let result = text { let opacity = max(0, min(1, result.stringToCGFloat()/100)) pdfView?.setAnnotationsOpacity(annotations, opacity) reloadData() } } }