// // KMNoteController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/26. // import Cocoa import KMComponentLibrary class KMNoteController: NSViewController { @IBOutlet var colorBGView: NSView! @IBOutlet var colorLabel: NSTextField! @IBOutlet var colorGroup: ComponentCColorGroup! @IBOutlet var typeBGView: NSView! @IBOutlet var typeLabel: NSTextField! @IBOutlet var typeItemA: ComponentCSelector! @IBOutlet var typeItemB: ComponentCSelector! @IBOutlet var typeItemC: ComponentCSelector! @IBOutlet var typeItemD: ComponentCSelector! @IBOutlet var typeItemE: ComponentCSelector! @IBOutlet var typeItemF: ComponentCSelector! @IBOutlet var typeItemG: ComponentCSelector! private var annotations: [CPDFTextAnnotation] = [] var pdfView: CPDFListView? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. setupProperty() } func setupProperty() { colorLabel.stringValue = KMLocalizedString("Color") 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 typeLabel.stringValue = KMLocalizedString("Color") typeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") typeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") typeItemA.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_1"), identifier: "") typeItemB.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_2"), identifier: "") typeItemC.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_3"), identifier: "") typeItemD.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_4"), identifier: "") typeItemE.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_5"), identifier: "") typeItemF.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_6"), identifier: "") typeItemG.properties = ComponentCSelectorProperty(size: .m, state: .normal, iconImage: NSImage(named: "note_7"), identifier: "") for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] { item!.setTarget(self, action: #selector(selectItemClick(_:))) } } func reloadData() { guard let pdfView = self.pdfView else { return } self.annotations.removeAll() let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? [] for annotation in allAnnotations { if annotation is CPDFTextAnnotation { annotations.append((annotation as! CPDFTextAnnotation)) } } if annotations.count == 0 { return } guard let firstAnnotation = annotations.first else { return } for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] { item!.properties.state = .normal } if annotations.count == 1 { colorGroup.currentColor = firstAnnotation.color colorGroup.refreshUI() let anchoredIconType: CPDFTextAnnotationIconType = firstAnnotation.iconType() if anchoredIconType == .comment { typeItemA.properties.state = .pressed } else if anchoredIconType == .key { typeItemC.properties.state = .pressed } else if anchoredIconType == .note { typeItemB.properties.state = .pressed } else if anchoredIconType == .help { typeItemD.properties.state = .pressed } else if anchoredIconType == .newParagraph { typeItemG.properties.state = .pressed } else if anchoredIconType == .paragraph { typeItemE.properties.state = .pressed } else if anchoredIconType == .insert { typeItemF.properties.state = .pressed } } else { var multiColor: Bool = false for annotationA in annotations { for annotationB in annotations { if annotationA != annotationB { if annotationA.color != annotationB.color { multiColor = true break } } } if multiColor == true { break } } if multiColor == true { colorGroup.currentColor = NSColor.clear } else { colorGroup.currentColor = firstAnnotation.color } colorGroup.refreshUI() // // var multiOpacity: Bool = false // for annotationA in annotations { // for annotationB in annotations { // if annotationA != annotationB { // if annotationA.opacity != annotationB.opacity { // multiOpacity = true // break // } // } // } // if multiOpacity == true { // break // } // } // // 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() // } } for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] { item!.reloadData() } } @objc func selectItemClick(_ sender: ComponentCSelector) { var anchoredIconType: CPDFTextAnnotationIconType = .comment if sender == typeItemA { anchoredIconType = .comment } else if sender == typeItemB { anchoredIconType = .note } else if sender == typeItemC { anchoredIconType = .key } else if sender == typeItemD { anchoredIconType = .help } else if sender == typeItemE { anchoredIconType = .paragraph } else if sender == typeItemF { anchoredIconType = .insert } else if sender == typeItemG { anchoredIconType = .newParagraph } for annotation in annotations { annotation.updateIconType(anchoredIconType, withPDFView: pdfView) } reloadData() } } extension KMNoteController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { for annotation in self.annotations { annotation.updateColor(color, withPDFView: pdfView) } reloadData() } }