KMPreferenceMarkupController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // KMPreferenceMarkupController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/3.
  6. //
  7. import Cocoa
  8. /// 偏好设置 Markup控制器
  9. class KMPreferenceMarkupController: NSViewController {
  10. @IBOutlet weak var markupColorLabel: NSTextField!
  11. @IBOutlet weak var markupColorBox: NSBox!
  12. var markupColorView = KMPreferenceMarkupColorView()
  13. @IBOutlet weak var markupFontLabel: NSTextField!
  14. @IBOutlet weak var markupFontBox: NSBox!
  15. @IBOutlet weak var textLabel: NSTextField!
  16. @IBOutlet weak var textComboBox: NSComboBox!
  17. @IBOutlet weak var noteLabel: NSTextField!
  18. @IBOutlet weak var noteComboBox: NSComboBox!
  19. @IBOutlet weak var aligementView: KMPreferenceAligementView!
  20. var currentColorItemView: KMPreferenceMarkupColorItemView?
  21. lazy var fontNames = KMPreferenceManager.supportFonts
  22. deinit {
  23. KMPrint("KMPreferenceMarkupController 已释放.")
  24. if (NSColorPanel.sharedColorPanelExists) {
  25. let panel = NSColorPanel.shared
  26. panel.setTarget(nil)
  27. panel.setAction(nil)
  28. panel.close()
  29. }
  30. }
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. self.markupColorLabel.stringValue = NSLocalizedString("Markup Default Color", comment: "")
  34. self.markupFontLabel.stringValue = NSLocalizedString("Markup Default Font", comment: "")
  35. self.textLabel.stringValue = NSLocalizedString("Text:", comment: "")
  36. self.noteLabel.stringValue = NSLocalizedString("Note", comment: "")
  37. self.markupColorBox.contentView?.addSubview(self.markupColorView)
  38. self.markupColorView.frame = self.markupColorBox.contentView!.bounds
  39. self.markupColorView.autoresizingMask = [.width, .height]
  40. let keys: [KMPreferenceMarkupColorKey] = [.highlight, .rectangleFill,
  41. .strikthrough, .rectangleBorder,
  42. .underline, .circleFill,
  43. .pen, .circleBorder,
  44. .text, .line,
  45. .note, .arrow]
  46. self.markupColorView.initKeys(keys)
  47. self.markupColorView.itemClick = { [weak self] itemView in
  48. self!.currentColorItemView = itemView
  49. var color: NSColor?
  50. let cgColor = itemView?.button.layer?.backgroundColor
  51. if (cgColor != nil) {
  52. color = NSColor(cgColor: cgColor!)
  53. }
  54. let panel = NSColorPanel.shared
  55. if (color != nil) {
  56. panel.color = color!
  57. }
  58. panel.setTarget(self)
  59. panel.setAction(#selector(self!.colorPanelAction))
  60. panel.orderFront(nil)
  61. }
  62. let labels = [NSLocalizedString("Highlight:", comment: ""),
  63. NSLocalizedString("Rectangle fill:", comment: ""),
  64. "\(NSLocalizedString("Strikethrough", comment: "")):",
  65. NSLocalizedString("Rectangle border:", comment: ""),
  66. NSLocalizedString("Underline:", comment: ""),
  67. NSLocalizedString("Circle fill:", comment: ""),
  68. NSLocalizedString("Pen:", comment: ""),
  69. NSLocalizedString("Circle border:", comment: ""),
  70. NSLocalizedString("Text:", comment: ""),
  71. NSLocalizedString("Line:", comment: ""),
  72. NSLocalizedString("Anchored:", comment: ""),
  73. NSLocalizedString("Arrow:", comment: "")]
  74. var i: Int = 0
  75. for itemView in self.markupColorView.itemViews {
  76. itemView.label.stringValue = labels[i]
  77. itemView.button.wantsLayer = true
  78. itemView.button.layer?.cornerRadius = 10
  79. itemView.button.layer?.borderWidth = 1
  80. itemView.button.layer?.borderColor = NSColor.km_init(hex: "#0000001A").cgColor
  81. i += 1
  82. }
  83. self.textComboBox.isEditable = false
  84. self.textComboBox.removeAllItems()
  85. self.textComboBox.addItems(withObjectValues: self.fontNames)
  86. self.noteComboBox.isEditable = false
  87. self.noteComboBox.removeAllItems()
  88. self.noteComboBox.addItems(withObjectValues: self.fontNames)
  89. for comboBox in [self.textComboBox, self.noteComboBox] {
  90. comboBox?.delegate = self
  91. }
  92. self.aligementView.itemClick = { index in
  93. if (index == 0) {
  94. KMPreferenceManager.shared.markupFontTextAligment = .left
  95. } else if (index == 1) {
  96. KMPreferenceManager.shared.markupFontTextAligment = .center
  97. } else if (index == 2) {
  98. KMPreferenceManager.shared.markupFontTextAligment = .right
  99. }
  100. }
  101. self.initUIProperty()
  102. self.initData()
  103. }
  104. private func initUIProperty() {
  105. self.markupColorBox.borderColor = NSColor(white: 0, alpha: 0.05)
  106. self.markupColorBox.fillColor = NSColor.white
  107. self.markupColorView.vLine.layer?.backgroundColor = NSColor(white: 0, alpha: 0.1).cgColor
  108. self.markupFontBox.borderColor = NSColor(white: 0, alpha: 0.05)
  109. self.markupFontBox.fillColor = NSColor(white: 0, alpha: 0.05)
  110. self.markupColorLabel.textColor = NSColor.km_init(hex: "#252629")
  111. self.markupFontLabel.textColor = NSColor.km_init(hex: "#252629")
  112. self.textLabel.textColor = NSColor.km_init(hex: "#000000D9")
  113. self.noteLabel.textColor = NSColor.km_init(hex: "#000000D9")
  114. for itemView in self.markupColorView.itemViews {
  115. itemView.label.textColor = NSColor.km_init(hex: "#000000D9")
  116. }
  117. }
  118. private func initData() {
  119. for itemView in self.markupColorView.itemViews {
  120. var color: NSColor?
  121. if (itemView.key == .highlight) {
  122. color = KMPreferenceManager.shared.markupHighlightColor
  123. } else if (itemView.key == .rectangleFill) {
  124. color = KMPreferenceManager.shared.markupRectangleFillColor
  125. } else if (itemView.key == .strikthrough) {
  126. color = KMPreferenceManager.shared.markupStrikthroughColor
  127. } else if (itemView.key == .rectangleBorder) {
  128. color = KMPreferenceManager.shared.markupRectangleBorderColor
  129. } else if (itemView.key == .underline) {
  130. color = KMPreferenceManager.shared.markupUnderlineColor
  131. } else if (itemView.key == .circleFill) {
  132. color = KMPreferenceManager.shared.markupCircleFillColor
  133. } else if (itemView.key == .pen) {
  134. color = KMPreferenceManager.shared.markupPenColor
  135. } else if (itemView.key == .circleBorder) {
  136. color = KMPreferenceManager.shared.markupCircleBorderColor
  137. } else if (itemView.key == .text) {
  138. color = KMPreferenceManager.shared.markupTextColor
  139. } else if (itemView.key == .line) {
  140. color = KMPreferenceManager.shared.markupLineColor
  141. } else if (itemView.key == .note) {
  142. color = KMPreferenceManager.shared.markupNoteColor
  143. } else if (itemView.key == .arrow) {
  144. color = KMPreferenceManager.shared.markupArrowColor
  145. }
  146. if (color != nil) {
  147. var red: CGFloat = 0.0
  148. var green: CGFloat = 0.0
  149. var blue: CGFloat = 0.0
  150. var alpha: CGFloat = 0.0
  151. color!.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  152. if (red == 0 && green == 0 && blue == 0 && alpha == 0) {
  153. itemView.button.image = NSImage(named: "KMImageNamePreferenceClearColor")
  154. } else {
  155. itemView.button.image = nil
  156. itemView.button.layer?.backgroundColor = color!.cgColor
  157. }
  158. }
  159. }
  160. self.textComboBox.stringValue = KMPreferenceManager.shared.markupFontTextString
  161. let aliigment = KMPreferenceManager.shared.markupFontTextAligment
  162. if (aliigment == .left) {
  163. self.aligementView.selectIndex(index: 0)
  164. } else if (aliigment == .center) {
  165. self.aligementView.selectIndex(index: 1)
  166. } else if (aliigment == .right) {
  167. self.aligementView.selectIndex(index: 2)
  168. }
  169. self.noteComboBox.stringValue = KMPreferenceManager.shared.markupFontNoteString
  170. }
  171. @objc private func colorPanelAction(sender: NSColorPanel) {
  172. if (self.currentColorItemView == nil) {
  173. return
  174. }
  175. let itemView = self.currentColorItemView!
  176. itemView.button.image = nil
  177. itemView.button.layer?.backgroundColor = sender.color.cgColor
  178. if (itemView.key == .highlight) {
  179. KMPreferenceManager.shared.markupHighlightColor = sender.color
  180. } else if (itemView.key == .rectangleFill) {
  181. KMPreferenceManager.shared.markupRectangleFillColor = sender.color
  182. } else if (itemView.key == .strikthrough) {
  183. KMPreferenceManager.shared.markupStrikthroughColor = sender.color
  184. } else if (itemView.key == .rectangleBorder) {
  185. KMPreferenceManager.shared.markupRectangleBorderColor = sender.color
  186. } else if (itemView.key == .underline) {
  187. KMPreferenceManager.shared.markupUnderlineColor = sender.color
  188. } else if (itemView.key == .circleFill) {
  189. KMPreferenceManager.shared.markupCircleFillColor = sender.color
  190. } else if (itemView.key == .pen) {
  191. KMPreferenceManager.shared.markupPenColor = sender.color
  192. } else if (itemView.key == .circleBorder) {
  193. KMPreferenceManager.shared.markupCircleBorderColor = sender.color
  194. } else if (itemView.key == .text) {
  195. KMPreferenceManager.shared.markupTextColor = sender.color
  196. } else if (itemView.key == .line) {
  197. KMPreferenceManager.shared.markupLineColor = sender.color
  198. } else if (itemView.key == .note) {
  199. KMPreferenceManager.shared.markupNoteColor = sender.color
  200. } else if (itemView.key == .arrow) {
  201. KMPreferenceManager.shared.markupArrowColor = sender.color
  202. }
  203. }
  204. }
  205. extension KMPreferenceMarkupController: NSComboBoxDelegate {
  206. func comboBoxSelectionDidChange(_ notification: Notification) {
  207. if (self.textComboBox.isEqual(to: notification.object)) {
  208. var index = self.textComboBox.indexOfSelectedItem
  209. if (index < 0) {
  210. index = 0
  211. }
  212. KMPreferenceManager.shared.markupFontTextString = self.fontNames[index]
  213. } else if (self.noteComboBox.isEqual(to: notification.object)) {
  214. var index = self.noteComboBox.indexOfSelectedItem
  215. if (index < 0) {
  216. index = 0
  217. }
  218. KMPreferenceManager.shared.markupFontNoteString = self.fontNames[index]
  219. }
  220. }
  221. }
  222. extension KMPreferenceMarkupController: KMPreferenceDealDataChangeProtocol {
  223. func refreshUI() {
  224. DispatchQueue.main.async {
  225. self.initData()
  226. }
  227. }
  228. }