KMPreferenceMarkupController.swift 11 KB

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