// // KMRedactPropertiesWindowController.swift // PDF Reader Pro // // Created by tangchao on 2023/12/19. // import Cocoa import KMComponentLibrary class KMRedactPropertiesWindowController: KMNBaseWindowController { var readactAnnotation: CPDFRedactAnnotation? @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var subLabel: NSTextField! @IBOutlet weak var outlineColorLabel: NSTextField! @IBOutlet weak var outlineColorItem: ComponentCColorItem! @IBOutlet weak var fillColorLabel: NSTextField! @IBOutlet weak var fillColorItem: ComponentCColorItem! @IBOutlet weak var overlayTextLabel: NSTextField! @IBOutlet weak var overlayTextarea: ComponentTextarea! @IBOutlet weak var fontSizeLabel: NSTextField! @IBOutlet weak var fontSizeComboSelect: ComponentSelect! @IBOutlet weak var alignementLabel: NSTextField! @IBOutlet var alightLeftButton: ComponentButton! @IBOutlet var alightRightButton: ComponentButton! @IBOutlet var alightCenteButton: ComponentButton! @IBOutlet weak var fontColorLabel: NSTextField! @IBOutlet weak var fontColorItem: ComponentCColorItem! @IBOutlet weak var makePropertiesButton: ComponentButton! @IBOutlet weak var cancelButton: ComponentButton! @IBOutlet weak var okButton: ComponentButton! @IBOutlet var cancelWidthButton:NSLayoutConstraint! @IBOutlet var okWidthButton:NSLayoutConstraint! @IBOutlet var makePropertiesWidthButton:NSLayoutConstraint! var callback: ((_ anno: CPDFRedactAnnotation?)->Void)? convenience init() { self.init(windowNibName: "KMRedactPropertiesWindowController") } override func initContentView() { super.initContentView() fontColorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.red) fontColorItem.delegate = self fontColorItem.reloadData() outlineColorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.red) outlineColorItem.delegate = self outlineColorItem.reloadData() fillColorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: NSColor.red) fillColorItem.delegate = self fillColorItem.reloadData() alightLeftButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, state: .normal, onlyIcon: true, icon:NSImage(named: "KMNImageNameListViewPopAlightLeft")) alightLeftButton.setTarget(self, action: #selector(alightLeftButtonClicked(_ :))) alightRightButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, state: .normal, onlyIcon: true, icon:NSImage(named: "KMNImageNameListViewPopAlightRight")) alightRightButton.setTarget(self, action: #selector(alightRightButtonClicked(_ :))) alightCenteButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, state: .normal, onlyIcon: true, icon:NSImage(named: "KMNImageNameListViewPopAlightCenter")) alightCenteButton.setTarget(self, action: #selector(alightCenterButtonClicked(_ :))) titleLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-m-medium") subLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-medium") outlineColorLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-regular") fillColorLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-regular") overlayTextLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-medium") fontColorLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-regular") fontSizeLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-regular") alignementLabel.font = ComponentLibrary.shared.getFontFromKey ("mac/body-s-regular") okButton.properties = ComponentButtonProperty(type: .primary, size: .s, state: .normal, buttonText: KMLocalizedString("OK")) okButton.setTarget(self, action: #selector(okButtonClicked(_ :))) cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, buttonText: KMLocalizedString("Cancel")) cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :))) makePropertiesButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, buttonText: KMLocalizedString("Make Properties Default")) makePropertiesButton.setTarget(self, action: #selector(makePropertiesButtonClicked(_ :))) overlayTextarea.properties = ComponentTextareaProperty(size: .s, state: .normal, isError: false, placeholderString: KMLocalizedString("Encrypted Content"), totalCount: -1, text: "", isDisabled: false) overlayTextarea.delegate = self fontSizeComboSelect.properties = ComponentSelectProperties(size: .s, state: .normal, placeholder: nil, errorText: nil, creatable: true, text: "12 pt", textUnit: " pt", regexString: "0123456789 pt") let menuItemString:[String] = ["4 pt", "6 pt", "8 pt", "9 pt", "10 pt", "11 pt", "12 pt", "13 pt", "14 pt", "15 pt", "16 pt", "17 pt", "18 pt", "24 pt", "36 pt", "48 pt", "64 pt", "72 pt", "98 pt", "144 pt", "288 pt"] var menuItemArr: [ComponentMenuitemProperty] = [] for language in menuItemString { let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: language) menuItemArr.append(itemProperty) } fontSizeComboSelect.delegate = self fontSizeComboSelect.updateMenuItemsArr(menuItemArr) } override func updateUILanguage() { super.updateUILanguage() outlineColorLabel.stringValue = KMLocalizedString("Redaction Mark Outline Color:") fillColorLabel.stringValue = KMLocalizedString("Redact Area Fill Color:") overlayTextLabel.stringValue = KMLocalizedString("Overlay Text:") alignementLabel.stringValue = KMLocalizedString("Text Alignment") fontSizeLabel.stringValue = KMLocalizedString("Font Size") fontColorLabel.stringValue = String(format: "%@:", KMLocalizedString("Font Color")) titleLabel.stringValue = KMLocalizedString("Redact Properties") subLabel.stringValue = KMLocalizedString("Mark Color") okButton.properties.buttonText = KMLocalizedString("OK") cancelButton.properties.buttonText = KMLocalizedString("Cancel") makePropertiesButton.properties.buttonText = KMLocalizedString("Make Properties Default") okWidthButton.constant = okButton.properties.propertyInfo.viewWidth cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth makePropertiesWidthButton.constant = makePropertiesButton.properties.propertyInfo.viewWidth } override func updateUIThemeColor() { super.updateUIThemeColor() window?.contentView?.wantsLayer = true window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1") subLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") outlineColorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") fillColorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") overlayTextLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") fontColorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") fontSizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") alignementLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") } private func updateLabelUI() { if overlayTextarea.properties.text.isEmpty == true { fontColorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label-dis") fontSizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label-dis") alignementLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label-dis") fontSizeComboSelect.properties.isDisabled = true fontSizeComboSelect.reloadData() alightLeftButton.properties.isDisabled = true alightLeftButton.reloadData() alightCenteButton.properties.isDisabled = true alightCenteButton.reloadData() alightRightButton.properties.isDisabled = true alightRightButton.reloadData() } else { fontColorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") fontSizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") alignementLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label") fontSizeComboSelect.properties.isDisabled = false fontSizeComboSelect.reloadData() alightLeftButton.properties.isDisabled = false alightLeftButton.reloadData() alightCenteButton.properties.isDisabled = false alightCenteButton.reloadData() alightRightButton.properties.isDisabled = false alightRightButton.reloadData() } } override func windowDidLoad() { super.windowDidLoad() if let anno = readactAnnotation { outlineColorItem.properties?.color = anno.borderColor() ?? .red outlineColorItem.reloadData() fillColorItem.properties?.color = anno.interiorColor() ?? .clear fillColorItem.reloadData() fontColorItem.properties?.color = anno.fontColor() ?? .red fontColorItem.reloadData() fontSizeComboSelect.properties.text = String(format: "%.lf pt", anno.fontSize()) fontSizeComboSelect.reloadData() if(anno.alignment().rawValue == 1) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .pressed alightRightButton.properties.state = .normal } else if (anno.alignment().rawValue == 2) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .normal alightRightButton.properties.state = .pressed } else { alightLeftButton.properties.state = .pressed alightCenteButton.properties.state = .normal alightRightButton.properties.state = .normal } alightLeftButton.reloadData() alightCenteButton.reloadData() alightRightButton.reloadData() overlayTextarea.properties.text = anno.overlayText() ?? "" overlayTextarea.reloadData() makePropertiesButton.isHidden = false } else { outlineColorItem.properties?.color = CPDFRedactAnnotation.defaultOutlineColor() outlineColorItem.reloadData() fillColorItem.properties?.color = CPDFRedactAnnotation.defaultFillColor() fillColorItem.reloadData() fontColorItem.properties?.color = CPDFRedactAnnotation.defaultTextColor() fontColorItem.reloadData() overlayTextarea.properties.text = CPDFRedactAnnotation.defaultOverlayText() overlayTextarea.reloadData() fontSizeComboSelect.properties.text = String(format: "%.0f pt", CPDFRedactAnnotation.defaultFontSize()) fontSizeComboSelect.reloadData() if(CPDFRedactAnnotation.defaultFontAlignment() == .center) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .pressed alightRightButton.properties.state = .normal } else if (CPDFRedactAnnotation.defaultFontAlignment() == .right) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .normal alightRightButton.properties.state = .pressed } else { alightLeftButton.properties.state = .pressed alightCenteButton.properties.state = .normal alightRightButton.properties.state = .normal } alightLeftButton.reloadData() alightCenteButton.reloadData() alightRightButton.reloadData() makePropertiesButton.isHidden = true } updateLabelUI() } //MARK: - Action @objc func alightLeftButtonClicked(_ sender: NSView) { alightLeftButton.properties.state = .pressed alightCenteButton.properties.state = .normal alightRightButton.properties.state = .normal alightLeftButton.reloadData() alightCenteButton.reloadData() alightRightButton.reloadData() } @objc func alightRightButtonClicked(_ sender: NSView) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .normal alightRightButton.properties.state = .pressed alightLeftButton.reloadData() alightCenteButton.reloadData() alightRightButton.reloadData() } @objc func alightCenterButtonClicked(_ sender: NSView) { alightLeftButton.properties.state = .normal alightCenteButton.properties.state = .pressed alightRightButton.properties.state = .normal alightLeftButton.reloadData() alightCenteButton.reloadData() alightRightButton.reloadData() } @objc func makePropertiesButtonClicked(_ sender: AnyObject?) { let s = (fontSizeComboSelect.properties.text as? NSString)?.trimmingCharacters(in: CharacterSet(charactersIn: " pt")) ?? "" let fontsize = s.stringToCGFloat() CPDFRedactAnnotation.update_defaultOutlineColor(outlineColorItem.properties?.color) CPDFRedactAnnotation.update_defaultFillColor(fillColorItem.properties?.color) CPDFRedactAnnotation.update_defaultTextColor(fontColorItem.properties?.color) CPDFRedactAnnotation.update_defaultFontSize(fontsize) if alightLeftButton.properties.state == .pressed { CPDFRedactAnnotation.update_defaultFontAlignment(.left) } else if alightCenteButton.properties.state == .pressed { CPDFRedactAnnotation.update_defaultFontAlignment(.center) } else if alightRightButton.properties.state == .pressed { CPDFRedactAnnotation.update_defaultFontAlignment(.right) } CPDFRedactAnnotation.update_defaultOverlayText(overlayTextarea.properties.text) } @objc func cancelButtonClicked(_ sender: AnyObject?) { if self.callback != nil { self.callback!(self.readactAnnotation) } self.km_quick_endSheet() } @objc func okButtonClicked(_ sender: AnyObject?) { let s = (fontSizeComboSelect.properties.text as? NSString)?.trimmingCharacters(in: CharacterSet(charactersIn: " pt")) ?? "" let fontsize = Int(s) ?? 12 var alignment: NSTextAlignment = .left if alightCenteButton.properties.state == .pressed { alignment = .center } else if alightRightButton.properties.state == .pressed { alignment = .right } if let anno = readactAnnotation { anno.setBorderColor(outlineColorItem.properties?.color) anno.setInteriorColor(fillColorItem.properties?.color) anno.setFontColor(fontColorItem.properties?.color) anno.fontSize = fontsize.cgFloat anno.setAlignment(alignment) anno.setOverlayText(overlayTextarea.properties.text) } else { makePropertiesButtonClicked(makePropertiesButton) } callback?(readactAnnotation) self.km_quick_endSheet() } @IBAction func useOverlayText_button(_ sender: AnyObject?) { updateLabelUI() } } //MARK: - ComponentTextareaDelegate extension KMRedactPropertiesWindowController: ComponentTextareaDelegate { func componentTextareaTextDidBeginEditing(_ view: ComponentTextarea) { } func componentTextareaTextDidChange(_ view: ComponentTextarea) { updateLabelUI() } func componentTextareaTextDidEndEditing(_ view: ComponentTextarea) { } } //MARK: - ComponentSelectDelegate extension KMRedactPropertiesWindowController: ComponentSelectDelegate { func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { } func componentSelectTextDidBeginEditing(_ view: ComponentSelect) { } func componentSelectTextDidChange(_ view: ComponentSelect) { } func componentSelectTextDidEndEditing(_ view: ComponentSelect) { } func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) { } @objc func fontColorChange(_ sender: Any) { if let color = (sender as? NSColorPanel)?.color { fontColorItem.properties?.color = color fontColorItem.reloadData() } } @objc func borderColorChange(_ sender: Any) { if let color = (sender as? NSColorPanel)?.color { outlineColorItem.properties?.color = color outlineColorItem.reloadData() } } @objc func fillColorChange(_ sender: Any) { if let color = (sender as? NSColorPanel)?.color { fillColorItem.properties?.color = color fillColorItem.reloadData() } } } //MARK: - ComponentSelectDelegate extension KMRedactPropertiesWindowController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { let colorPanel = NSColorPanel.shared colorPanel.setTarget(self) colorPanel.showsAlpha = false if (view == fontColorItem) { fontColorItem.properties?.state = .normal fontColorItem.reloadData() colorPanel.setAction(#selector(self.fontColorChange(_:))) } else if(view == outlineColorItem) { outlineColorItem.properties?.state = .normal outlineColorItem.reloadData() colorPanel.setAction(#selector(self.borderColorChange(_:))) } else if(view == fillColorItem) { fillColorItem.properties?.state = .normal fillColorItem.reloadData() colorPanel.setAction(#selector(self.fillColorChange(_:))) } colorPanel.orderFront(nil) } }