KMRedactPropertyContentView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // KMRedactPropertyContentView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/17.
  6. //
  7. import Cocoa
  8. class KMRedactPropertyContentView: KMRedactContentBaseView {
  9. @IBOutlet weak var outsideBox: NSBox!
  10. @IBOutlet weak var outsideLabel: NSTextField!
  11. @IBOutlet weak var outsideColorView: KMRedactColorView!
  12. @IBOutlet weak var fillLabel: NSTextField!
  13. @IBOutlet weak var fillColorView: KMRedactColorView!
  14. @IBOutlet weak var overTextCheck: NSButton!
  15. @IBOutlet var overTextView: NSTextView!
  16. @IBOutlet weak var textLabel: NSTextField!
  17. @IBOutlet weak var fontStyleComboBox: NSComboBox!
  18. @IBOutlet weak var fontSizeComboBox: NSComboBox!
  19. @IBOutlet weak var fontTypeComboBox: NSComboBox!
  20. @IBOutlet weak var textColorView: KMRedactColorView!
  21. @IBOutlet weak var textAligementView: KMRedactAligementView!
  22. let fontSizes: Array<Int> = [8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72]
  23. var annotation : CPDFRedactAnnotation? {
  24. get {
  25. return nil
  26. }
  27. set {
  28. if (newValue == nil) {
  29. return
  30. }
  31. if ((newValue?.overlayText().isEmpty)!) {
  32. self.isOverText(false)
  33. } else {
  34. self.isOverText(true)
  35. }
  36. }
  37. }
  38. override func awakeFromNib() {
  39. super.awakeFromNib()
  40. self.outsideLabel.stringValue = NSLocalizedString("Redaction Mark Outline Color:", comment: "")
  41. self.fillLabel.stringValue = NSLocalizedString("Redact Area Fill Color:", comment: "")
  42. self.overTextCheck.title = NSLocalizedString("Use Overlay Text", comment: "")
  43. self.overTextCheck.target = self
  44. self.overTextCheck.action = #selector(overTextCheckAction)
  45. self.overTextView.textContainerInset = NSMakeSize(6, 8)
  46. self.overTextView.enclosingScrollView?.borderType = .noBorder
  47. self.overTextView.enclosingScrollView?.contentView.wantsLayer = true
  48. self.overTextView.enclosingScrollView?.contentView.layer?.borderWidth = 1
  49. self.overTextView.enclosingScrollView?.contentView.layer?.borderColor = NSColor.black.cgColor
  50. self.overTextView.string = NSLocalizedString("Redact", comment: "")
  51. self.overTextView.delegate = self
  52. self.textLabel.stringValue = NSLocalizedString("Font", comment: "")
  53. var fontNames: Array<String> = []
  54. for font in CPDFAnnotationModel.supportFonts() {
  55. let fontDict: NSDictionary = font as! NSDictionary
  56. let fontName: String = fontDict.allKeys.first as! String
  57. fontNames.append(fontName)
  58. }
  59. self.fontStyleComboBox.delegate = self
  60. self.fontStyleComboBox.removeAllItems()
  61. self.fontStyleComboBox.addItems(withObjectValues: fontNames)
  62. self.updateFontName(fontNames[1])
  63. self.fontTypeComboBox.delegate = self
  64. self.fontSizeComboBox.delegate = self
  65. self.fontSizeComboBox.removeAllItems()
  66. self.fontSizeComboBox.addItems(withObjectValues: self.fontSizes)
  67. self.fontSizeComboBox.stringValue = NSLocalizedString("Auto", comment: "")
  68. /*
  69. self.interiorColor = annotationModel.interiorColor;
  70. self.borderColor = annotationModel.color;
  71. if (annotationModel.isOverlayText) {
  72. self.overlayText = annotationModel.overlayText?:@"";
  73. self.fontColor = annotationModel.fontColor;
  74. self.alignment = annotationModel.alignment;
  75. self.font = [NSFont fontWithName:@"Helvetica" size:annotationModel.fontSize];
  76. }
  77. */
  78. }
  79. // MARK: Private Methods
  80. @objc private func overTextCheckAction(sender: NSButton) {
  81. if (sender.state == .on) {
  82. self.isOverText(true)
  83. } else {
  84. self.isOverText(false)
  85. }
  86. guard let callback = self.itemClick else {
  87. return
  88. }
  89. callback(3, sender.state == .on)
  90. }
  91. private func isOverText(_ isOver: Bool) {
  92. if (isOver) {
  93. self.overTextCheck.state = .on
  94. self.overTextView.isEditable = true
  95. self.fontStyleComboBox.isEnabled = true
  96. self.fontSizeComboBox.isEnabled = true
  97. self.fontTypeComboBox.isEnabled = true
  98. } else {
  99. self.overTextCheck.state = .off
  100. self.overTextView.isEditable = false
  101. self.fontStyleComboBox.isEnabled = false
  102. self.fontSizeComboBox.isEnabled = false
  103. self.fontTypeComboBox.isEnabled = false
  104. }
  105. }
  106. private func updateFontName(_ fontName: String) {
  107. var styleNames: Array<String>?
  108. for font in CPDFAnnotationModel.supportFonts() {
  109. let fontDict = font as! NSDictionary
  110. let name: String = fontDict.allKeys.first as! String
  111. if (name == fontName) {
  112. let datas: Array<String> = fontDict.allValues.first as! Array<String>
  113. styleNames = datas
  114. self.fontStyleComboBox.stringValue = fontName
  115. break
  116. }
  117. }
  118. if (styleNames != nil) {
  119. self.fontTypeComboBox.removeAllItems()
  120. self.fontTypeComboBox.addItems(withObjectValues: styleNames!)
  121. self.fontTypeComboBox.selectItem(at: 0)
  122. }
  123. }
  124. }
  125. extension KMRedactPropertyContentView: NSTextViewDelegate {
  126. func textDidChange(_ notification: Notification) {
  127. if (self.overTextView.isEqual(to: notification.object)) {
  128. guard let callback = self.itemClick else {
  129. return
  130. }
  131. callback(4, self.overTextView.string)
  132. }
  133. }
  134. }
  135. extension KMRedactPropertyContentView: NSComboBoxDelegate {
  136. func comboBoxSelectionDidChange(_ notification: Notification) {
  137. if (self.fontStyleComboBox.isEqual(to: notification.object)) {
  138. var index: Int = self.fontStyleComboBox.indexOfSelectedItem
  139. if (index < 0) {
  140. index = 0
  141. }
  142. guard let callback = self.itemClick else {
  143. return
  144. }
  145. callback(5, self.fontStyleComboBox.itemObjectValue(at: index))
  146. } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
  147. var index: Int = self.fontSizeComboBox.indexOfSelectedItem
  148. if (index < 0) {
  149. index = 0
  150. }
  151. guard let callback = self.itemClick else {
  152. return
  153. }
  154. callback(6, self.fontSizeComboBox.itemObjectValue(at: index))
  155. } else if (self.fontTypeComboBox.isEqual(to: notification.object)) {
  156. var index: Int = self.fontTypeComboBox.indexOfSelectedItem
  157. if (index < 0) {
  158. index = 0
  159. }
  160. guard let callback = self.itemClick else {
  161. return
  162. }
  163. callback(7, self.fontTypeComboBox.itemObjectValue(at: index))
  164. }
  165. }
  166. }