KMPreferenceDisplayController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //
  2. // KMPreferenceDisplayController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/3.
  6. //
  7. import Cocoa
  8. /// 偏好设置 Display控制器
  9. class KMPreferenceDisplayController: NSViewController {
  10. @IBOutlet weak var defaultViewAndZoomLabel: NSTextField!
  11. @IBOutlet weak var defaultViewAndZoomBox: NSBox!
  12. @IBOutlet weak var pageIndicatorLabel: NSTextField!
  13. @IBOutlet weak var zoomSacleLabel: NSTextField!
  14. @IBOutlet weak var pageIndicatorComboBox: NSComboBox!
  15. @IBOutlet weak var zoomScaleComboBox: NSComboBox!
  16. @IBOutlet weak var leftSidePanelLabel: NSTextField!
  17. @IBOutlet weak var leftSidePanelBox: NSBox!
  18. @IBOutlet weak var defaultDisplayLabel: NSTextField!
  19. @IBOutlet weak var putAwayRadio: NSButton!
  20. @IBOutlet weak var rememberLastSelectionRadio: NSButton!
  21. @IBOutlet weak var showOutlineCheck: NSButton!
  22. @IBOutlet weak var leftSidePanel2Label: NSTextField!
  23. @IBOutlet weak var leftSidePanel2Box: NSBox!
  24. @IBOutlet weak var displayLabel: NSTextField!
  25. @IBOutlet weak var autoExpandRadio: NSButton!
  26. @IBOutlet weak var manualExpandRadio: NSButton!
  27. @IBOutlet weak var colorLabel: NSTextField!
  28. @IBOutlet weak var colorBox: NSBox!
  29. @IBOutlet weak var backgroundColorLabel: NSTextField!
  30. @IBOutlet weak var colorNormalLabel: NSTextField!
  31. @IBOutlet weak var colorNormalButton: NSButton!
  32. @IBOutlet weak var colorFullScreenLabel: NSTextField!
  33. @IBOutlet weak var colorFullScreenButton: NSButton!
  34. @IBOutlet weak var hightlightedFormsLabel: NSButton!
  35. @IBOutlet weak var fieldHighlightLabel: NSTextField!
  36. @IBOutlet weak var fieldHighlightButton: NSButton!
  37. @IBOutlet weak var requiredFieldHighlightLabel: NSTextField!
  38. @IBOutlet weak var requiredFieldHighlightButton: NSButton!
  39. @IBOutlet weak var pageIndicator2Label: NSTextField!
  40. @IBOutlet weak var highlightedLinksCheck: NSButton!
  41. @IBOutlet weak var autoDisplayRadio: NSButton!
  42. @IBOutlet weak var alwaysShowRadio: NSButton!
  43. @IBOutlet weak var neverShowRadio: NSButton!
  44. var pageDisplayTypes: [String] {
  45. get {
  46. return [NSLocalizedString("Single Page", comment: ""),
  47. NSLocalizedString("Single Page Continuous", comment: ""),
  48. NSLocalizedString("Two Pages", comment: ""),
  49. NSLocalizedString("Two Pages Continuous", comment: ""),
  50. NSLocalizedString("Book Mode", comment: ""),
  51. NSLocalizedString("Book Continuous", comment: "")]
  52. }
  53. }
  54. var viewZoomScaleTypes: [String] {
  55. get {
  56. return [NSLocalizedString("Fit Width", comment: ""),
  57. NSLocalizedString("Fit Page", comment: ""),
  58. NSLocalizedString("Actual Size", comment: "")]
  59. }
  60. }
  61. var currentColorButton: NSButton?
  62. convenience init() {
  63. self.init(nibName: "KMPreferenceDisplayController", bundle: nil)
  64. }
  65. override func viewDidLoad() {
  66. super.viewDidLoad()
  67. self.defaultViewAndZoomLabel.stringValue = NSLocalizedString("Default View and Zoom", comment: "")
  68. self.pageIndicatorLabel.stringValue = NSLocalizedString("Page display:", comment: "")
  69. self.pageIndicatorComboBox.isEditable = false
  70. self.pageIndicatorComboBox.removeAllItems()
  71. self.pageIndicatorComboBox.addItems(withObjectValues: self.pageDisplayTypes)
  72. self.zoomSacleLabel.stringValue = NSLocalizedString("Zoom scale:", comment: "")
  73. self.zoomScaleComboBox.isEditable = false
  74. self.zoomScaleComboBox.removeAllItems()
  75. self.zoomScaleComboBox.addItems(withObjectValues: self.viewZoomScaleTypes)
  76. self.leftSidePanelLabel.stringValue = NSLocalizedString("Left Side Panel", comment: "")
  77. self.defaultDisplayLabel.stringValue = NSLocalizedString("Default display:", comment: "")
  78. self.putAwayRadio.title = NSLocalizedString("Hide when opening a file", comment: "")
  79. self.rememberLastSelectionRadio.title = NSLocalizedString("Remember the last selection when launching.", comment: "")
  80. self.showOutlineCheck.title = NSLocalizedString("Show outline list by default when outline is available.", comment: "")
  81. self.leftSidePanel2Label.stringValue = NSLocalizedString("Right Side Panel", comment: "")
  82. self.displayLabel.stringValue = NSLocalizedString("Default display:", comment: "")
  83. self.autoExpandRadio.title = NSLocalizedString("Auto expand", comment: "")
  84. self.manualExpandRadio.title = NSLocalizedString("Manually expand", comment: "")
  85. self.colorLabel.stringValue = NSLocalizedString("Color", comment: "")
  86. self.backgroundColorLabel.stringValue = NSLocalizedString("Background color:", comment: "")
  87. self.colorNormalLabel.stringValue = NSLocalizedString("Normal", comment: "")
  88. self.colorFullScreenLabel.stringValue = NSLocalizedString("Full Screen", comment: "")
  89. self.hightlightedFormsLabel.title = NSLocalizedString("Highlight forms:", comment: "")
  90. self.fieldHighlightLabel.stringValue = NSLocalizedString("Highlight field", comment: "")
  91. self.requiredFieldHighlightLabel.stringValue = NSLocalizedString("Highlight required field", comment: "")
  92. self.pageIndicator2Label.stringValue = NSLocalizedString("Page indicator:", comment: "")
  93. self.autoDisplayRadio.title = NSLocalizedString("Auto display", comment: "")
  94. self.alwaysShowRadio.title = NSLocalizedString("Always show", comment: "")
  95. self.neverShowRadio.title = NSLocalizedString("Never show", comment: "")
  96. self.highlightedLinksCheck.title = NSLocalizedString("Highlight links", comment: "")
  97. for comboBox in [self.pageIndicatorComboBox, self.zoomScaleComboBox] {
  98. comboBox?.delegate = self
  99. }
  100. for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck,
  101. self.autoExpandRadio, self.manualExpandRadio,
  102. self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] {
  103. radio?.target = self
  104. radio?.action = #selector(radioAction)
  105. }
  106. for check in [self.hightlightedFormsLabel,self.highlightedLinksCheck] {
  107. check?.target = self
  108. check?.action = #selector(checkAction)
  109. }
  110. for color in [self.colorNormalButton, self.colorFullScreenButton,
  111. self.fieldHighlightButton, self.requiredFieldHighlightButton] {
  112. color?.title = ""
  113. color?.wantsLayer = true
  114. color?.layer?.cornerRadius = 10
  115. color?.layer?.borderWidth = 1
  116. color?.layer?.borderColor = NSColor.km_init(hex: "#0000001A").cgColor
  117. color?.target = self
  118. color?.action = #selector(colorButtonAction)
  119. }
  120. self.initUIProperty()
  121. self.initData()
  122. }
  123. private func initUIProperty() {
  124. self.defaultViewAndZoomBox.borderColor = NSColor(white: 0, alpha: 0.05)
  125. self.defaultViewAndZoomBox.fillColor = NSColor(white: 0, alpha: 0.05)
  126. self.leftSidePanelBox.borderColor = NSColor(white: 0, alpha: 0.05)
  127. self.leftSidePanelBox.fillColor = NSColor(white: 0, alpha: 0.05)
  128. self.leftSidePanel2Box.borderColor = NSColor(white: 0, alpha: 0.05)
  129. self.leftSidePanel2Box.fillColor = NSColor(white: 0, alpha: 0.05)
  130. self.colorBox.borderColor = NSColor(white: 0, alpha: 0.05)
  131. self.colorBox.fillColor = NSColor(white: 0, alpha: 0.05)
  132. self.defaultViewAndZoomLabel.textColor = NSColor.km_init(hex: "#252629")
  133. self.pageIndicatorLabel.textColor = NSColor.km_init(hex: "#000000D9")
  134. self.zoomSacleLabel.textColor = NSColor.km_init(hex: "#000000D9")
  135. self.leftSidePanelLabel.textColor = NSColor.km_init(hex: "#252629")
  136. self.defaultDisplayLabel.textColor = NSColor.km_init(hex: "#000000D9")
  137. self.putAwayRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  138. self.rememberLastSelectionRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  139. self.showOutlineCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  140. self.leftSidePanel2Label.textColor = NSColor.km_init(hex: "#252629")
  141. self.displayLabel.textColor = NSColor.km_init(hex: "#000000D9")
  142. self.autoExpandRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  143. self.manualExpandRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  144. self.colorLabel.textColor = NSColor.km_init(hex: "#252629")
  145. self.backgroundColorLabel.textColor = NSColor.km_init(hex: "#000000D9")
  146. self.colorNormalLabel.textColor = NSColor.km_init(hex: "#000000D9")
  147. self.colorFullScreenLabel.textColor = NSColor.km_init(hex: "#000000D9")
  148. self.hightlightedFormsLabel.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  149. self.fieldHighlightLabel.textColor = NSColor.km_init(hex: "#000000D9")
  150. self.requiredFieldHighlightLabel.textColor = NSColor.km_init(hex: "#000000D9")
  151. self.pageIndicator2Label.textColor = NSColor.km_init(hex: "#000000D9")
  152. self.autoDisplayRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  153. self.alwaysShowRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  154. self.neverShowRadio.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  155. self.highlightedLinksCheck.setTitleColor(NSColor.km_init(hex: "#000000D9"))
  156. }
  157. private func initData() {
  158. let value = KMPreferenceManager.shared.viewPageDisplayType
  159. if (value.rawValue < self.pageDisplayTypes.count) {
  160. self.pageIndicatorComboBox.stringValue = self.pageDisplayTypes[value.rawValue]
  161. }
  162. let value2 = KMPreferenceManager.shared.viewZoomScaleType
  163. if (value2.rawValue < self.viewZoomScaleTypes.count) {
  164. self.zoomScaleComboBox.stringValue = self.viewZoomScaleTypes[value2.rawValue]
  165. }
  166. let leftSideDisplayType = KMPreferenceManager.shared.leftSideDisplayType
  167. if (leftSideDisplayType == .closeWhenOpenFile) {
  168. self.selectLeftSideRadio(self.putAwayRadio)
  169. } else if (leftSideDisplayType == .openAppSaveLastSelect) {
  170. self.selectLeftSideRadio(self.rememberLastSelectionRadio)
  171. } else if (leftSideDisplayType == .showOutlineIfHas) {
  172. self.selectLeftSideRadio(self.showOutlineCheck)
  173. }
  174. if (KMPreferenceManager.shared.propertyPanelExpandType == .auto) {
  175. self.selectDisplayRadio(self.autoExpandRadio)
  176. } else {
  177. self.selectDisplayRadio(self.manualExpandRadio)
  178. }
  179. if (KMPreferenceManager.shared.highlightForms) {
  180. self.hightlightedFormsLabel.state = .on
  181. } else {
  182. self.hightlightedFormsLabel.state = .off
  183. }
  184. let value3 = KMPreferenceManager.shared.pageIndicatorType
  185. if (value3 == .automatic) {
  186. self.selectPageIndicatorRadio(self.autoDisplayRadio)
  187. } else if (value3 == .always) {
  188. self.selectPageIndicatorRadio(self.alwaysShowRadio)
  189. } else if (value3 == .never) {
  190. self.selectPageIndicatorRadio(self.neverShowRadio)
  191. }
  192. if (KMPreferenceManager.shared.highlightLinks) {
  193. self.highlightedLinksCheck.state = .on
  194. } else {
  195. self.highlightedLinksCheck.state = .off
  196. }
  197. self.colorNormalButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor.cgColor
  198. self.colorFullScreenButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor.cgColor
  199. self.fieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayFieldHighlightingColor.cgColor
  200. self.requiredFieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayRequiredFieldHighlightingColor.cgColor
  201. }
  202. @objc private func radioAction(sender: NSButton) {
  203. if (self.putAwayRadio.isEqual(sender)) {
  204. self.selectLeftSideRadio(sender)
  205. KMPreferenceManager.shared.leftSideDisplayType = .closeWhenOpenFile
  206. } else if (self.rememberLastSelectionRadio.isEqual(sender)) {
  207. self.selectLeftSideRadio(sender)
  208. KMPreferenceManager.shared.leftSideDisplayType = .openAppSaveLastSelect
  209. } else if (self.showOutlineCheck.isEqual(sender)) {
  210. self.selectLeftSideRadio(sender)
  211. KMPreferenceManager.shared.leftSideDisplayType = .showOutlineIfHas
  212. } else if (self.autoExpandRadio.isEqual(sender)) {
  213. self.selectDisplayRadio(sender)
  214. KMPreferenceManager.shared.propertyPanelExpandType = .auto
  215. } else if (self.manualExpandRadio.isEqual(sender)) {
  216. self.selectDisplayRadio(sender)
  217. KMPreferenceManager.shared.propertyPanelExpandType = .manual
  218. } else if (self.autoDisplayRadio.isEqual(sender)) {
  219. self.selectPageIndicatorRadio(sender)
  220. KMPreferenceManager.shared.pageIndicatorType = .automatic
  221. } else if (self.alwaysShowRadio.isEqual(sender)) {
  222. self.selectPageIndicatorRadio(sender)
  223. KMPreferenceManager.shared.pageIndicatorType = .always
  224. } else if (self.neverShowRadio.isEqual(sender)) {
  225. self.selectPageIndicatorRadio(sender)
  226. KMPreferenceManager.shared.pageIndicatorType = .never
  227. }
  228. }
  229. @objc private func checkAction(sender: NSButton) {
  230. if (self.hightlightedFormsLabel.isEqual(sender)) {
  231. KMPreferenceManager.shared.highlightForms = sender.state == .on
  232. } else if (self.highlightedLinksCheck.isEqual(sender)) {
  233. KMPreferenceManager.shared.highlightLinks = sender.state == .on
  234. }
  235. }
  236. @objc private func colorButtonAction(sender: NSButton) {
  237. self.currentColorButton = sender
  238. var color: NSColor?
  239. let cgColor = sender.layer?.backgroundColor
  240. if (cgColor != nil) {
  241. color = NSColor(cgColor: cgColor!)
  242. }
  243. let colorPanel = NSColorPanel.shared
  244. if (color != nil) {
  245. colorPanel.color = color!
  246. }
  247. colorPanel.setTarget(self)
  248. colorPanel.setAction(#selector(colorPanelAction))
  249. colorPanel.orderFront(nil)
  250. }
  251. @objc private func colorPanelAction(sender: NSColorPanel) {
  252. if (self.currentColorButton == nil) {
  253. return
  254. }
  255. let color = sender.color
  256. self.currentColorButton?.layer?.backgroundColor = color.cgColor
  257. if (self.colorNormalButton.isEqual(self.currentColorButton)) {
  258. KMPreferenceManager.shared.displayBackgroundNormalColor = color
  259. } else if (self.colorFullScreenButton.isEqual(self.currentColorButton)) {
  260. KMPreferenceManager.shared.displayBackgroundFullScreenColor = color
  261. } else if (self.fieldHighlightButton.isEqual(self.currentColorButton)) {
  262. KMPreferenceManager.shared.displayFieldHighlightingColor = color
  263. } else if (self.requiredFieldHighlightButton.isEqual(self.currentColorButton)) {
  264. KMPreferenceManager.shared.displayRequiredFieldHighlightingColor = color
  265. }
  266. }
  267. private func selectLeftSideRadio(_ sender: NSButton) {
  268. for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck] {
  269. if (sender.isEqual(to: radio)) {
  270. radio?.state = .on
  271. } else {
  272. radio?.state = .off
  273. }
  274. }
  275. }
  276. private func selectDisplayRadio(_ sender: NSButton) {
  277. for radio in [self.autoExpandRadio, self.manualExpandRadio] {
  278. if (sender.isEqual(to: radio)) {
  279. radio?.state = .on
  280. } else {
  281. radio?.state = .off
  282. }
  283. }
  284. }
  285. private func selectPageIndicatorRadio(_ sender: NSButton) {
  286. for radio in [self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] {
  287. if (sender.isEqual(to: radio)) {
  288. radio?.state = .on
  289. } else {
  290. radio?.state = .off
  291. }
  292. }
  293. }
  294. }
  295. extension KMPreferenceDisplayController: NSComboBoxDelegate {
  296. func comboBoxSelectionDidChange(_ notification: Notification) {
  297. if (self.pageIndicatorComboBox.isEqual(to: notification.object)) {
  298. var index: Int = 0
  299. if (self.pageIndicatorComboBox.indexOfSelectedItem >= 0) {
  300. index = self.pageIndicatorComboBox.indexOfSelectedItem
  301. }
  302. KMPreferenceManager.shared.viewPageDisplayType = KMPDFDisplayType(rawValue: index)!
  303. } else if (self.zoomScaleComboBox.isEqual(notification.object)) {
  304. var index: Int = 0
  305. if (self.zoomScaleComboBox.indexOfSelectedItem >= 0) {
  306. index = self.zoomScaleComboBox.indexOfSelectedItem
  307. }
  308. KMPreferenceManager.shared.viewZoomScaleType = KMPDFZoomType(rawValue: index)!
  309. }
  310. }
  311. }
  312. extension KMPreferenceDisplayController: KMPreferenceDealDataChangeProtocol {
  313. func refreshUI() {
  314. DispatchQueue.main.async {
  315. self.initData()
  316. }
  317. }
  318. }