// // 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") 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)) } } var firstAnnotation: CPDFTextAnnotation? if annotations.count > 0 { firstAnnotation = annotations.first } for item in [typeItemA, typeItemB, typeItemC, typeItemD, typeItemE, typeItemF, typeItemG] { item!.properties.state = .normal } if true { let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.noteColors if colors.count > 4 { 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 { colorGroup.currentColor = CPDFTextAnnotation.defaultColor() colorGroup.refreshUI() let anchoredIconType: CPDFTextAnnotationIconType = CPDFTextAnnotation.defaultType() 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 if annotations.count == 1, let annotation = firstAnnotation { colorGroup.currentColor = annotation.color colorGroup.refreshUI() let anchoredIconType: CPDFTextAnnotationIconType = annotation.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 { let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color) if multiColor == true { colorGroup.currentColor = nil } else { if let annotation = firstAnnotation { colorGroup.currentColor = annotation.color } } colorGroup.refreshUI() let multiType = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .note_Type) if multiType == true { } else if let annotation = firstAnnotation { let anchoredIconType: CPDFTextAnnotationIconType = annotation.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 } } } 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 } CPDFAnnotation.updateAnnotations(annotations, newIconType: anchoredIconType, withPDFView: pdfView) CPDFTextAnnotation.updateDefaultIconType(anchoredIconType) reloadData() } } extension KMNoteController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView) CPDFTextAnnotation.updateDefaultColor(color) NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil) reloadData() } }