|
@@ -0,0 +1,228 @@
|
|
|
+//
|
|
|
+// KMAnnotationButtonWidgetOptionsViewController.swift
|
|
|
+// PDF Reader Pro
|
|
|
+//
|
|
|
+// Created by wanjun on 2024/1/16.
|
|
|
+//
|
|
|
+
|
|
|
+import Cocoa
|
|
|
+
|
|
|
+class KMAnnotationButtonWidgetOptionsViewController: NSViewController {
|
|
|
+
|
|
|
+ private var _annotations: [CPDFButtonWidgetAnnotation] = []
|
|
|
+ private var _formMode: CAnnotationType = .radioButton
|
|
|
+ var pdfView: CPDFListView?
|
|
|
+ var annotationModel: CPDFAnnotationModel?
|
|
|
+
|
|
|
+ @IBOutlet private var selectedByDefaultButton: NSButton!
|
|
|
+ @IBOutlet private var warningView: NSView!
|
|
|
+ @IBOutlet private var warningLabel: NSTextField!
|
|
|
+ @IBOutlet private var singleButtonOptionLabel: NSTextField!
|
|
|
+ @IBOutlet private var singleButtonOptionTextField: NSTextField!
|
|
|
+ @IBOutlet private var radioTopView: NSView!
|
|
|
+ @IBOutlet private var radioTopViewConstraint: NSLayoutConstraint!
|
|
|
+ @IBOutlet private var warningViewConstraint: NSLayoutConstraint!
|
|
|
+ private var annotation: CPDFButtonWidgetAnnotation?
|
|
|
+ private var isFromMode: Bool = false
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ annotation = nil
|
|
|
+ NotificationCenter.default.removeObserver(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewDidLoad() {
|
|
|
+ super.viewDidLoad()
|
|
|
+ // Do view setup here.
|
|
|
+
|
|
|
+ selectedByDefaultButton.title = NSLocalizedString("Button is checked by default", comment: "")
|
|
|
+
|
|
|
+ if let annotation = self.annotation {
|
|
|
+ if annotation.controlType() == .radioButtonControl || annotation.controlType() == .checkBoxControl {
|
|
|
+ let buttonWidgetState: Int = annotation.state()
|
|
|
+ if buttonWidgetState == 0 {
|
|
|
+ selectedByDefaultButton.state = NSControl.StateValue.off
|
|
|
+ } else if buttonWidgetState == 1 {
|
|
|
+ selectedByDefaultButton.state = NSControl.StateValue.on
|
|
|
+ }
|
|
|
+
|
|
|
+ if annotation.controlType() == .radioButtonControl {
|
|
|
+ radioTopView.isHidden = false
|
|
|
+
|
|
|
+ warningView.wantsLayer = true
|
|
|
+ warningView.layer?.backgroundColor = KMAppearance.Status.errBG1Color().cgColor
|
|
|
+ warningView.layer?.cornerRadius = 1.0
|
|
|
+ warningLabel.stringValue = " " + NSLocalizedString("1 button in group. At least 2 buttons needed.", comment: "")
|
|
|
+ warningLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+ singleButtonOptionLabel.stringValue = NSLocalizedString("Radio Button Choice", comment: "") + ":"
|
|
|
+ singleButtonOptionLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(singleButtonOptionTextFieldDidChange(_:)), name: NSControl.textDidChangeNotification, object: singleButtonOptionTextField)
|
|
|
+
|
|
|
+ if annotations.count > 1 {
|
|
|
+ singleButtonOptionTextField.isEnabled = false
|
|
|
+ }
|
|
|
+
|
|
|
+ if let fieldName = annotation.fieldName(), annotations.count == 1 {
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ singleButtonOptionTextField.stringValue = annotation.buttonWidgetStateString().removingPercentEncoding ?? ""
|
|
|
+ } else {
|
|
|
+ singleButtonOptionTextField.stringValue = fieldName
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ var annotationString = ""
|
|
|
+ for (index, note) in annotations.enumerated() {
|
|
|
+ if let annotation = note as? PDFAnnotation, let stateString = annotation.fieldName {
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ annotationString += annotation.buttonWidgetStateString ?? ""
|
|
|
+ } else {
|
|
|
+ annotationString += stateString
|
|
|
+ }
|
|
|
+
|
|
|
+ if index != annotations.count - 1 {
|
|
|
+ annotationString += ", "
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ singleButtonOptionTextField.stringValue = annotationString
|
|
|
+ }
|
|
|
+
|
|
|
+ let currentPage: CPDFPage = pdfView!.currentPage()
|
|
|
+ let noteFieldName: String = annotation.fieldName()
|
|
|
+ let noteStateString: String = singleButtonOptionTextField.stringValue
|
|
|
+ var sameCount = 1
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ for case let note as CPDFButtonWidgetAnnotation in currentPage.annotations {
|
|
|
+ var isSame = false
|
|
|
+ if note.isKind(of: CPDFButtonWidgetAnnotation.self) {
|
|
|
+ if (note.fieldName() == noteFieldName) && note.buttonWidgetStateString() != noteStateString {
|
|
|
+ isSame = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isSame {
|
|
|
+ sameCount += 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if sameCount > 1 {
|
|
|
+ warningView.isHidden = true
|
|
|
+ warningViewConstraint.constant = -warningView.frame.height - 16
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ radioTopView.isHidden = true
|
|
|
+ }
|
|
|
+
|
|
|
+ singleButtonOptionTextField.backgroundColor = KMAppearance.Layout.l_1Color()
|
|
|
+ singleButtonOptionTextField.wantsLayer = true
|
|
|
+ singleButtonOptionTextField.layer?.borderWidth = 1.0
|
|
|
+ singleButtonOptionTextField.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
|
|
|
+ singleButtonOptionTextField.layer?.cornerRadius = 1.0
|
|
|
+
|
|
|
+ radioTopViewConstraint.constant = radioTopView.isHidden ? -radioTopView.frame.size.height : 0.0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Set & Get
|
|
|
+
|
|
|
+ var formMode: CAnnotationType {
|
|
|
+ get {
|
|
|
+ return _formMode
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _formMode = newValue
|
|
|
+ isFromMode = true
|
|
|
+
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ var note: CPDFButtonWidgetAnnotation? = nil
|
|
|
+ let bounds = NSMakeRect(0, 0, 60, 25)
|
|
|
+
|
|
|
+ if formMode == .radioButton {
|
|
|
+ note = CPDFButtonWidgetAnnotation(document: pdfView?.document)
|
|
|
+ note?.setState(userDefaults.integer(forKey: SKAnnotationRadioButtonWidgetSelectedKey))
|
|
|
+ } else if formMode == .checkBox {
|
|
|
+ note = CPDFButtonWidgetAnnotation(document: pdfView?.document)
|
|
|
+ note?.setState(userDefaults.integer(forKey: SKAnnotationCheckBoxWidgetSelectedKey))
|
|
|
+ }
|
|
|
+
|
|
|
+ if let note1 = note {
|
|
|
+ annotations = [note1]
|
|
|
+ }
|
|
|
+ annotation = note
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var annotations: [CPDFButtonWidgetAnnotation] {
|
|
|
+ get {
|
|
|
+ return _annotations
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _annotations = newValue
|
|
|
+ annotation = _annotations.first
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Private Method
|
|
|
+
|
|
|
+ private func updateAnnotationMode() {
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ let annotation = self.annotation
|
|
|
+ let annotationMode = self.formMode
|
|
|
+
|
|
|
+ if annotationMode == .radioButton {
|
|
|
+ userDefaults.set(annotation?.state(), forKey: SKAnnotationRadioButtonWidgetSelectedKey)
|
|
|
+ } else if annotationMode == .checkBox {
|
|
|
+ userDefaults.set(annotation?.state(), forKey: SKAnnotationCheckBoxWidgetSelectedKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private func updateAnnotation() {
|
|
|
+ if annotationModel?.annotation != nil {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: NSButton Action
|
|
|
+
|
|
|
+ @IBAction func selectedByDefaultAction(_ sender: NSButton) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// tAnnotation.removeAllAppearanceStreams()
|
|
|
+
|
|
|
+ if sender.state == .on {
|
|
|
+ tAnnotation.setState(CPDFWidgetCellState.onState.rawValue)
|
|
|
+ } else {
|
|
|
+ tAnnotation.setState(CPDFWidgetCellState.offState.rawValue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: NSNotification
|
|
|
+
|
|
|
+ @objc func generalNameTextFieldDidChange(_ notification: Notification) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// tAnnotation.removeAllAppearanceStreams()
|
|
|
+
|
|
|
+ tAnnotation.setOnStateValue(singleButtonOptionTextField.stringValue)
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func singleButtonOptionTextFieldDidChange(_ notification: Notification) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// tAnnotation.removeAllAppearanceStreams()
|
|
|
+
|
|
|
+ let singleString = singleButtonOptionTextField.stringValue
|
|
|
+ let encodingString = singleString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
|
|
|
+
|
|
|
+ if #available(macOS 10.13, *) {
|
|
|
+ tAnnotation.setButtonWidgetStateString(encodingString)
|
|
|
+ } else {
|
|
|
+ tAnnotation.setFieldName(singleButtonOptionTextField.stringValue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+}
|