KMRedactPropertiesWindowController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // KMRedactPropertiesWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/19.
  6. //
  7. import Cocoa
  8. class KMRedactPropertiesWindowController: NSWindowController {
  9. var readactAnnotation: CPDFRedactAnnotation?
  10. @IBOutlet weak var outlineColorLabel: NSTextField!
  11. @IBOutlet weak var outlineColorWell: NSColorWell!
  12. @IBOutlet weak var fillColorLabel: NSTextField!
  13. @IBOutlet weak var fillColorWell: NSColorWell!
  14. @IBOutlet weak var overlayTextButton: NSButton!
  15. @IBOutlet weak var overlayTextLabel: NSTextField!
  16. @IBOutlet weak var overlayTextField: NSTextField!
  17. @IBOutlet weak var fontSizeLabel: NSTextField!
  18. @IBOutlet weak var fontSizeComboBox: NSComboBox!
  19. @IBOutlet weak var alignementLabel: NSTextField!
  20. @IBOutlet weak var segmentedControl: NSSegmentedControl!
  21. @IBOutlet weak var fontColorLabel: NSTextField!
  22. @IBOutlet weak var fontColorWell: NSColorWell!
  23. @IBOutlet weak var makePropertiesButton: NSButton!
  24. @IBOutlet weak var cancelButton: NSButton!
  25. @IBOutlet weak var okButton: NSButton!
  26. /*
  27. @property (nonatomic,assign) NSModalSession modalSession;
  28. */
  29. var callback: ((_ anno: CPDFRedactAnnotation?)->Void)?
  30. convenience init() {
  31. self.init(windowNibName: "KMRedactPropertiesWindowController")
  32. }
  33. override func windowDidLoad() {
  34. super.windowDidLoad()
  35. self.outlineColorLabel.stringValue = KMLocalizedString("Redaction Mark Outline Color:", nil)
  36. self.fillColorLabel.stringValue = KMLocalizedString("Redact Area Fill Color:", nil)
  37. self.overlayTextButton.title = KMLocalizedString("Use Overlay Text", nil)
  38. self.overlayTextLabel.stringValue = KMLocalizedString("Overlay Text:", nil)
  39. // self.repeatTextButton.title = NSLocalizedString(@"Repeat Text", nil);
  40. // self.autoSizeTextButton.title = NSLocalizedString(@"Auto-Size text to fit redaction region", nil);
  41. self.alignementLabel.stringValue = KMLocalizedString("Text Alignment", nil)
  42. self.fontSizeLabel.stringValue = KMLocalizedString("Font Size", nil)
  43. self.fontColorLabel.stringValue = String(format: "%@:", KMLocalizedString("Font Color", nil))
  44. self.makePropertiesButton.title = KMLocalizedString("Make Properties Default", nil)
  45. self.cancelButton.title = KMLocalizedString("Cancel", nil)
  46. self.okButton.title = KMLocalizedString("OK", nil)
  47. if let anno = self.readactAnnotation {
  48. self.outlineColorWell.color = anno.borderColor() ?? .red
  49. self.fillColorWell.color = anno.interiorColor() ?? .clear
  50. self.fontColorWell.color = anno.fontColor() ?? .red
  51. self.fontSizeComboBox.stringValue = String(format: "%.lf pt", anno.font().pointSize)
  52. var segment = 0
  53. if(anno.alignment().rawValue == 1) {
  54. segment = 1
  55. } else if (anno.alignment().rawValue == 2) {
  56. segment = 2
  57. }
  58. self.segmentedControl.setSelected(true, forSegment: segment)
  59. self.overlayTextField.stringValue = anno.overlayText() ?? ""
  60. if(anno.overlayText().isEmpty == false) {
  61. self.overlayTextButton.state = .on
  62. } else {
  63. self.overlayTextButton.state = .off
  64. }
  65. self.makePropertiesButton.isHidden = false
  66. } else {
  67. self.outlineColorWell.color = KMPDFAnnotationRedactConfig.shared.redactOutlineColor!
  68. self.fillColorWell.color = KMPDFAnnotationRedactConfig.shared.redactFillColor!
  69. self.fontColorWell.color = KMPDFAnnotationRedactConfig.shared.redactFontColor!
  70. self.overlayTextButton.state = KMPDFAnnotationRedactConfig.shared.overlayText ? .on : .off
  71. // self.repeatTextButton.state = [KMPDFAnnotationRedactConfig sharedInstance].repeatText ? NSControlStateValueOn : NSControlStateValueOff;
  72. // self.autoSizeTextButton.state = [KMPDFAnnotationRedactConfig sharedInstance].autoSizeText ? NSControlStateValueOn : NSControlStateValueOff;
  73. self.overlayTextField.stringValue = KMPDFAnnotationRedactConfig.shared.overlayTextString
  74. self.fontSizeComboBox.stringValue = String(format: "%ld pt", KMPDFAnnotationRedactConfig.shared.fontSize)
  75. var segment = 0
  76. if(KMPDFAnnotationRedactConfig.shared.textAlignment == 1) {
  77. segment = 1
  78. } else if (KMPDFAnnotationRedactConfig.shared.textAlignment == 2) {
  79. segment = 2
  80. }
  81. self.segmentedControl.setSelected(true, forSegment: segment)
  82. self.makePropertiesButton.isHidden = true
  83. }
  84. self.updateUIState()
  85. }
  86. @IBAction func cancel_button(_ sender: AnyObject?) {
  87. if self.callback != nil {
  88. self.callback!(self.readactAnnotation)
  89. }
  90. self.km_quick_endSheet()
  91. }
  92. @IBAction func sure_button(_ sender: AnyObject?) {
  93. let s = (self.fontSizeComboBox.stringValue as? NSString)?.trimmingCharacters(in: CharacterSet(charactersIn: " pt")) ?? ""
  94. let fontsize = Int(s) ?? 12
  95. var alignment: NSTextAlignment = .left
  96. if(self.segmentedControl.indexOfSelectedItem == 1) {
  97. alignment = .center
  98. } else if (self.segmentedControl.indexOfSelectedItem == 2) {
  99. alignment = .right
  100. }
  101. if(self.readactAnnotation == nil || self.makePropertiesButton.state == .on) {
  102. KMPDFAnnotationRedactConfig.shared.redactOutlineColor = self.outlineColorWell.color
  103. KMPDFAnnotationRedactConfig.shared.redactFillColor = self.fillColorWell.color
  104. KMPDFAnnotationRedactConfig.shared.redactFontColor = self.fontColorWell.color
  105. KMPDFAnnotationRedactConfig.shared.overlayText = self.overlayTextButton.state == .on
  106. KMPDFAnnotationRedactConfig.shared.fontSize = fontsize
  107. KMPDFAnnotationRedactConfig.shared.textAlignment = alignment.rawValue
  108. if(self.overlayTextButton.state == .on) {
  109. KMPDFAnnotationRedactConfig.shared.overlayTextString = self.overlayTextField.stringValue
  110. } else {
  111. KMPDFAnnotationRedactConfig.shared.overlayTextString = ""
  112. }
  113. }
  114. if let anno = self.readactAnnotation {
  115. anno.setBorderColor(self.outlineColorWell.color)
  116. anno.setInteriorColor(self.fillColorWell.color)
  117. anno.setFontColor(self.fontColorWell.color)
  118. if(self.overlayTextButton.state == .on) {
  119. anno.setFont(.init(name: "Helvetica", size: fontsize.cgFloat))
  120. anno.setAlignment(alignment)
  121. anno.setOverlayText(self.overlayTextField.stringValue)
  122. } else {
  123. anno.setOverlayText("")
  124. }
  125. }
  126. if self.callback != nil {
  127. self.callback!(self.readactAnnotation)
  128. }
  129. self.km_quick_endSheet()
  130. }
  131. @IBAction func useOverlayText_button(_ sender: AnyObject?) {
  132. self.updateUIState()
  133. }
  134. @IBAction func autoSzieText_button(_ sender: AnyObject?) {
  135. self.updateUIState()
  136. }
  137. @IBAction func fontSize_Combox(_ sender: AnyObject?) {
  138. }
  139. func updateUIState() {
  140. let state = self.overlayTextButton.state
  141. let enabled = state == .on
  142. self.overlayTextLabel.isEnabled = enabled
  143. self.overlayTextField.isEnabled = enabled
  144. // self.autoSizeTextButton.enabled = state;
  145. self.fontSizeLabel.isEnabled = enabled
  146. self.fontSizeComboBox.isEnabled = enabled
  147. self.alignementLabel.isEnabled = enabled
  148. self.segmentedControl.isEnabled = enabled
  149. // self.repeatTextButton.enabled = state;
  150. self.fontColorLabel.isEnabled = enabled
  151. self.fontColorWell.isEnabled = enabled
  152. if(enabled) {
  153. self.overlayTextLabel.textColor = .labelColor
  154. self.fontSizeLabel.textColor = .labelColor
  155. self.alignementLabel.textColor = .labelColor
  156. self.fontColorLabel.textColor = .labelColor
  157. } else {
  158. self.overlayTextLabel.textColor = .tertiaryLabelColor
  159. self.fontSizeLabel.textColor = .tertiaryLabelColor
  160. self.fontColorLabel.textColor = .tertiaryLabelColor
  161. self.alignementLabel.textColor = .tertiaryLabelColor
  162. }
  163. }
  164. }