KMRedactPropertyContentView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. self.outsideColorView.color = (newValue?.borderColor())!
  32. self.fillColorView.color = newValue!.interiorColor()
  33. self.textColorView.color = newValue!.fontColor()
  34. if ((newValue?.overlayText().isEmpty)!) {
  35. self.isOverText(false)
  36. } else {
  37. self.isOverText(true)
  38. }
  39. }
  40. }
  41. override func awakeFromNib() {
  42. super.awakeFromNib()
  43. self.outsideLabel.stringValue = NSLocalizedString("Redaction Mark Outline Color:", comment: "")
  44. self.fillLabel.stringValue = NSLocalizedString("Redact Area Fill Color:", comment: "")
  45. self.overTextCheck.title = NSLocalizedString("Use Overlay Text", comment: "")
  46. self.overTextCheck.target = self
  47. self.overTextCheck.action = #selector(overTextCheckAction)
  48. self.overTextView.textContainerInset = NSMakeSize(6, 8)
  49. self.overTextView.enclosingScrollView?.borderType = .noBorder
  50. self.overTextView.enclosingScrollView?.contentView.wantsLayer = true
  51. self.overTextView.enclosingScrollView?.contentView.layer?.borderWidth = 1
  52. self.overTextView.enclosingScrollView?.contentView.layer?.borderColor = NSColor.black.cgColor
  53. self.overTextView.string = NSLocalizedString("Redact", comment: "")
  54. self.overTextView.delegate = self
  55. self.textLabel.stringValue = NSLocalizedString("Font", comment: "")
  56. var fontNames: Array<String> = []
  57. for font in CPDFAnnotationModel.supportFonts() {
  58. let fontDict: NSDictionary = font as! NSDictionary
  59. let fontName: String = fontDict.allKeys.first as! String
  60. fontNames.append(fontName)
  61. }
  62. self.fontStyleComboBox.delegate = self
  63. self.fontStyleComboBox.removeAllItems()
  64. self.fontStyleComboBox.addItems(withObjectValues: fontNames)
  65. self.updateFontName(fontNames[1])
  66. self.fontTypeComboBox.delegate = self
  67. self.fontSizeComboBox.delegate = self
  68. self.fontSizeComboBox.removeAllItems()
  69. self.fontSizeComboBox.addItems(withObjectValues: self.fontSizes)
  70. self.fontSizeComboBox.stringValue = NSLocalizedString("Auto", comment: "")
  71. /*
  72. self.interiorColor = annotationModel.interiorColor;
  73. self.borderColor = annotationModel.color;
  74. if (annotationModel.isOverlayText) {
  75. self.overlayText = annotationModel.overlayText?:@"";
  76. self.fontColor = annotationModel.fontColor;
  77. self.alignment = annotationModel.alignment;
  78. self.font = [NSFont fontWithName:@"Helvetica" size:annotationModel.fontSize];
  79. }
  80. */
  81. self.outsideColorView.itemClick = { [weak self] idx in
  82. if (idx == 2) {
  83. guard let callback = self!.itemClick else {
  84. return
  85. }
  86. callback(1, nil)
  87. }
  88. }
  89. self.fillColorView.itemClick = { [weak self] idx in
  90. if (idx == 2) {
  91. guard let callback = self!.itemClick else {
  92. return
  93. }
  94. callback(2, nil)
  95. }
  96. }
  97. self.textColorView.itemClick = { [weak self] idx in
  98. if (idx == 2) {
  99. guard let callback = self!.itemClick else {
  100. return
  101. }
  102. callback(8, nil)
  103. }
  104. }
  105. self.textAligementView.itemClick = {
  106. (index: Int) in
  107. guard let callback = self.itemClick else {
  108. return
  109. }
  110. callback(9, index)
  111. }
  112. }
  113. // MARK: Private Methods
  114. @objc private func overTextCheckAction(sender: NSButton) {
  115. if (sender.state == .on) {
  116. self.isOverText(true)
  117. } else {
  118. self.isOverText(false)
  119. }
  120. guard let callback = self.itemClick else {
  121. return
  122. }
  123. callback(3, sender.state == .on)
  124. }
  125. private func isOverText(_ isOver: Bool) {
  126. if (isOver) {
  127. self.overTextCheck.state = .on
  128. self.overTextView.isEditable = true
  129. self.fontStyleComboBox.isEnabled = true
  130. self.fontSizeComboBox.isEnabled = true
  131. self.fontTypeComboBox.isEnabled = true
  132. self.textColorView.isEnabled = true
  133. self.textAligementView.isEnabled = true
  134. } else {
  135. self.overTextCheck.state = .off
  136. self.overTextView.isEditable = false
  137. self.fontStyleComboBox.isEnabled = false
  138. self.fontSizeComboBox.isEnabled = false
  139. self.fontTypeComboBox.isEnabled = false
  140. self.textColorView.isEnabled = false
  141. self.textAligementView.isEnabled = false
  142. }
  143. }
  144. private func updateFontName(_ fontName: String) {
  145. var styleNames: Array<String>?
  146. for font in CPDFAnnotationModel.supportFonts() {
  147. let fontDict = font as! NSDictionary
  148. let name: String = fontDict.allKeys.first as! String
  149. if (name == fontName) {
  150. let datas: Array<String> = fontDict.allValues.first as! Array<String>
  151. styleNames = datas
  152. self.fontStyleComboBox.stringValue = fontName
  153. break
  154. }
  155. }
  156. if (styleNames != nil) {
  157. self.fontTypeComboBox.removeAllItems()
  158. self.fontTypeComboBox.addItems(withObjectValues: styleNames!)
  159. self.fontTypeComboBox.selectItem(at: 0)
  160. }
  161. }
  162. }
  163. extension KMRedactPropertyContentView: NSTextViewDelegate {
  164. func textDidChange(_ notification: Notification) {
  165. if (self.overTextView.isEqual(to: notification.object)) {
  166. guard let callback = self.itemClick else {
  167. return
  168. }
  169. callback(4, self.overTextView.string)
  170. }
  171. }
  172. }
  173. extension KMRedactPropertyContentView: NSComboBoxDelegate {
  174. func comboBoxSelectionDidChange(_ notification: Notification) {
  175. if (self.fontStyleComboBox.isEqual(to: notification.object)) {
  176. var index: Int = self.fontStyleComboBox.indexOfSelectedItem
  177. if (index < 0) {
  178. index = 0
  179. }
  180. guard let callback = self.itemClick else {
  181. return
  182. }
  183. callback(5, self.fontStyleComboBox.itemObjectValue(at: index))
  184. } else if (self.fontSizeComboBox.isEqual(to: notification.object)) {
  185. var index: Int = self.fontSizeComboBox.indexOfSelectedItem
  186. if (index < 0) {
  187. index = 0
  188. }
  189. guard let callback = self.itemClick else {
  190. return
  191. }
  192. callback(6, self.fontSizeComboBox.itemObjectValue(at: index))
  193. } else if (self.fontTypeComboBox.isEqual(to: notification.object)) {
  194. var index: Int = self.fontTypeComboBox.indexOfSelectedItem
  195. if (index < 0) {
  196. index = 0
  197. }
  198. guard let callback = self.itemClick else {
  199. return
  200. }
  201. callback(7, self.fontTypeComboBox.itemObjectValue(at: index))
  202. }
  203. }
  204. }