123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- //
- // 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)
- }
- }
|