KMPreferenceMarkupController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(hex: "#0000001A").cgColor
  81. i += 1
  82. }
  83. self.textComboBox.removeAllItems()
  84. self.textComboBox.addItems(withObjectValues: self.fontNames)
  85. self.noteComboBox.removeAllItems()
  86. self.noteComboBox.addItems(withObjectValues: self.fontNames)
  87. for comboBox in [self.textComboBox, self.noteComboBox] {
  88. comboBox?.delegate = self
  89. }
  90. self.aligementView.itemClick = { index in
  91. if (index == 0) {
  92. KMPreferenceManager.shared.markupFontTextAligment = .left
  93. } else if (index == 1) {
  94. KMPreferenceManager.shared.markupFontTextAligment = .center
  95. } else if (index == 2) {
  96. KMPreferenceManager.shared.markupFontTextAligment = .right
  97. }
  98. }
  99. self.initUIProperty()
  100. self.initData()
  101. }
  102. private func initUIProperty() {
  103. self.markupColorBox.borderColor = NSColor(white: 0, alpha: 0.05)
  104. self.markupColorBox.fillColor = NSColor.white
  105. self.markupColorView.vLine.layer?.backgroundColor = NSColor(white: 0, alpha: 0.1).cgColor
  106. self.markupFontBox.borderColor = NSColor(white: 0, alpha: 0.05)
  107. self.markupFontBox.fillColor = NSColor(white: 0, alpha: 0.05)
  108. self.markupColorLabel.textColor = NSColor(hex: "#252629")
  109. self.markupFontLabel.textColor = NSColor(hex: "#252629")
  110. self.textLabel.textColor = NSColor(hex: "#000000D9")
  111. self.noteLabel.textColor = NSColor(hex: "#000000D9")
  112. for itemView in self.markupColorView.itemViews {
  113. itemView.label.textColor = NSColor(hex: "#000000D9")
  114. }
  115. }
  116. private func initData() {
  117. for itemView in self.markupColorView.itemViews {
  118. var color: NSColor?
  119. if (itemView.key == .highlight) {
  120. color = KMPreferenceManager.shared.markupHighlightColor
  121. } else if (itemView.key == .rectangleFill) {
  122. color = KMPreferenceManager.shared.markupRectangleFillColor
  123. } else if (itemView.key == .strikthrough) {
  124. color = KMPreferenceManager.shared.markupStrikthroughColor
  125. } else if (itemView.key == .rectangleBorder) {
  126. color = KMPreferenceManager.shared.markupRectangleBorderColor
  127. } else if (itemView.key == .underline) {
  128. color = KMPreferenceManager.shared.markupUnderlineColor
  129. } else if (itemView.key == .circleFill) {
  130. color = KMPreferenceManager.shared.markupCircleFillColor
  131. } else if (itemView.key == .pen) {
  132. color = KMPreferenceManager.shared.markupPenColor
  133. } else if (itemView.key == .circleBorder) {
  134. color = KMPreferenceManager.shared.markupCircleBorderColor
  135. } else if (itemView.key == .text) {
  136. color = KMPreferenceManager.shared.markupTextColor
  137. } else if (itemView.key == .line) {
  138. color = KMPreferenceManager.shared.markupLineColor
  139. } else if (itemView.key == .note) {
  140. color = KMPreferenceManager.shared.markupNoteColor
  141. } else if (itemView.key == .arrow) {
  142. color = KMPreferenceManager.shared.markupArrowColor
  143. }
  144. if (color != nil) {
  145. var red: CGFloat = 0.0
  146. var green: CGFloat = 0.0
  147. var blue: CGFloat = 0.0
  148. var alpha: CGFloat = 0.0
  149. color!.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  150. if (red == 0 && green == 0 && blue == 0 && alpha == 0) {
  151. itemView.button.image = NSImage(named: "KMImageNamePreferenceClearColor")
  152. } else {
  153. itemView.button.image = nil
  154. itemView.button.layer?.backgroundColor = color!.cgColor
  155. }
  156. }
  157. }
  158. self.textComboBox.stringValue = KMPreferenceManager.shared.markupFontTextString
  159. let aliigment = KMPreferenceManager.shared.markupFontTextAligment
  160. if (aliigment == .left) {
  161. self.aligementView.selectIndex(index: 0)
  162. } else if (aliigment == .center) {
  163. self.aligementView.selectIndex(index: 1)
  164. } else if (aliigment == .right) {
  165. self.aligementView.selectIndex(index: 2)
  166. }
  167. self.noteComboBox.stringValue = KMPreferenceManager.shared.markupFontNoteString
  168. }
  169. @objc private func colorPanelAction(sender: NSColorPanel) {
  170. if (self.currentColorItemView == nil) {
  171. return
  172. }
  173. let itemView = self.currentColorItemView!
  174. itemView.button.image = nil
  175. itemView.button.layer?.backgroundColor = sender.color.cgColor
  176. if (itemView.key == .highlight) {
  177. KMPreferenceManager.shared.markupHighlightColor = sender.color
  178. } else if (itemView.key == .rectangleFill) {
  179. KMPreferenceManager.shared.markupRectangleFillColor = sender.color
  180. } else if (itemView.key == .strikthrough) {
  181. KMPreferenceManager.shared.markupStrikthroughColor = sender.color
  182. } else if (itemView.key == .rectangleBorder) {
  183. KMPreferenceManager.shared.markupRectangleBorderColor = sender.color
  184. } else if (itemView.key == .underline) {
  185. KMPreferenceManager.shared.markupUnderlineColor = sender.color
  186. } else if (itemView.key == .circleFill) {
  187. KMPreferenceManager.shared.markupCircleFillColor = sender.color
  188. } else if (itemView.key == .pen) {
  189. KMPreferenceManager.shared.markupPenColor = sender.color
  190. } else if (itemView.key == .circleBorder) {
  191. KMPreferenceManager.shared.markupCircleBorderColor = sender.color
  192. } else if (itemView.key == .text) {
  193. KMPreferenceManager.shared.markupTextColor = sender.color
  194. } else if (itemView.key == .line) {
  195. KMPreferenceManager.shared.markupLineColor = sender.color
  196. } else if (itemView.key == .note) {
  197. KMPreferenceManager.shared.markupNoteColor = sender.color
  198. } else if (itemView.key == .arrow) {
  199. KMPreferenceManager.shared.markupArrowColor = sender.color
  200. }
  201. }
  202. }
  203. extension KMPreferenceMarkupController: NSComboBoxDelegate {
  204. func comboBoxSelectionDidChange(_ notification: Notification) {
  205. if (self.textComboBox.isEqual(to: notification.object)) {
  206. var index = self.textComboBox.indexOfSelectedItem
  207. if (index < 0) {
  208. index = 0
  209. }
  210. KMPreferenceManager.shared.markupFontTextString = self.fontNames[index]
  211. } else if (self.noteComboBox.isEqual(to: notification.object)) {
  212. var index = self.noteComboBox.indexOfSelectedItem
  213. if (index < 0) {
  214. index = 0
  215. }
  216. KMPreferenceManager.shared.markupFontNoteString = self.fontNames[index]
  217. }
  218. }
  219. }
  220. extension KMPreferenceMarkupController: KMPreferenceDealDataChangeProtocol {
  221. func refreshUI() {
  222. DispatchQueue.main.async {
  223. self.initData()
  224. }
  225. }
  226. }