KMPreferenceDisplayController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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("Zoom To Width", comment: ""),
  57. NSLocalizedString("Zoom To Fit", comment: ""),
  58. NSLocalizedString("Zoom To 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.removeAllItems()
  70. self.pageIndicatorComboBox.addItems(withObjectValues: self.pageDisplayTypes)
  71. self.zoomSacleLabel.stringValue = NSLocalizedString("Zoom scale:", comment: "")
  72. self.zoomScaleComboBox.removeAllItems()
  73. self.zoomScaleComboBox.addItems(withObjectValues: self.viewZoomScaleTypes)
  74. self.leftSidePanelLabel.stringValue = NSLocalizedString("Left Side Panel", comment: "")
  75. self.defaultDisplayLabel.stringValue = NSLocalizedString("Default display:", comment: "")
  76. self.putAwayRadio.title = NSLocalizedString("Hide when opening a file", comment: "")
  77. self.rememberLastSelectionRadio.title = NSLocalizedString("Remember the last selection when launching.", comment: "")
  78. self.showOutlineCheck.title = NSLocalizedString("Show outline list by default when outline is available.", comment: "")
  79. self.leftSidePanel2Label.stringValue = NSLocalizedString("Right Side Panel", comment: "")
  80. self.displayLabel.stringValue = NSLocalizedString("Default display:", comment: "")
  81. self.autoExpandRadio.title = NSLocalizedString("Auto expand", comment: "")
  82. self.manualExpandRadio.title = NSLocalizedString("Manually expand", comment: "")
  83. self.colorLabel.stringValue = NSLocalizedString("Color", comment: "")
  84. self.backgroundColorLabel.stringValue = NSLocalizedString("Background color:", comment: "")
  85. self.colorNormalLabel.stringValue = NSLocalizedString("Normal", comment: "")
  86. self.colorFullScreenLabel.stringValue = NSLocalizedString("Full Screen", comment: "")
  87. self.hightlightedFormsLabel.title = NSLocalizedString("Highlight forms:", comment: "")
  88. self.fieldHighlightLabel.stringValue = NSLocalizedString("Highlight field", comment: "")
  89. self.requiredFieldHighlightLabel.stringValue = NSLocalizedString("Highlight required field", comment: "")
  90. self.pageIndicator2Label.stringValue = NSLocalizedString("Page indicator:", comment: "")
  91. self.autoDisplayRadio.title = NSLocalizedString("Auto display", comment: "")
  92. self.alwaysShowRadio.title = NSLocalizedString("Always show", comment: "")
  93. self.neverShowRadio.title = NSLocalizedString("Never show", comment: "")
  94. self.highlightedLinksCheck.title = NSLocalizedString("Highlight links", comment: "")
  95. for comboBox in [self.pageIndicatorComboBox, self.zoomScaleComboBox] {
  96. comboBox?.delegate = self
  97. }
  98. for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck,
  99. self.autoExpandRadio, self.manualExpandRadio,
  100. self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] {
  101. radio?.target = self
  102. radio?.action = #selector(radioAction)
  103. }
  104. for check in [self.hightlightedFormsLabel,self.highlightedLinksCheck] {
  105. check?.target = self
  106. check?.action = #selector(checkAction)
  107. }
  108. for color in [self.colorNormalButton, self.colorFullScreenButton,
  109. self.fieldHighlightButton, self.requiredFieldHighlightButton] {
  110. color?.title = ""
  111. color?.wantsLayer = true
  112. color?.layer?.cornerRadius = 10
  113. color?.layer?.borderWidth = 1
  114. color?.layer?.borderColor = NSColor(hex: "#0000001A").cgColor
  115. color?.target = self
  116. color?.action = #selector(colorButtonAction)
  117. }
  118. self.initUIProperty()
  119. self.initData()
  120. }
  121. private func initUIProperty() {
  122. self.defaultViewAndZoomBox.borderColor = NSColor(white: 0, alpha: 0.05)
  123. self.defaultViewAndZoomBox.fillColor = NSColor(white: 0, alpha: 0.05)
  124. self.leftSidePanelBox.borderColor = NSColor(white: 0, alpha: 0.05)
  125. self.leftSidePanelBox.fillColor = NSColor(white: 0, alpha: 0.05)
  126. self.leftSidePanel2Box.borderColor = NSColor(white: 0, alpha: 0.05)
  127. self.leftSidePanel2Box.fillColor = NSColor(white: 0, alpha: 0.05)
  128. self.colorBox.borderColor = NSColor(white: 0, alpha: 0.05)
  129. self.colorBox.fillColor = NSColor(white: 0, alpha: 0.05)
  130. self.defaultViewAndZoomLabel.textColor = NSColor(hex: "#252629")
  131. self.pageIndicatorLabel.textColor = NSColor(hex: "#000000D9")
  132. self.zoomSacleLabel.textColor = NSColor(hex: "#000000D9")
  133. self.leftSidePanelLabel.textColor = NSColor(hex: "#252629")
  134. self.defaultDisplayLabel.textColor = NSColor(hex: "#000000D9")
  135. self.putAwayRadio.setTitleColor(NSColor(hex: "#000000D9"))
  136. self.rememberLastSelectionRadio.setTitleColor(NSColor(hex: "#000000D9"))
  137. self.showOutlineCheck.setTitleColor(NSColor(hex: "#000000D9"))
  138. self.leftSidePanel2Label.textColor = NSColor(hex: "#252629")
  139. self.displayLabel.textColor = NSColor(hex: "#000000D9")
  140. self.autoExpandRadio.setTitleColor(NSColor(hex: "#000000D9"))
  141. self.manualExpandRadio.setTitleColor(NSColor(hex: "#000000D9"))
  142. self.colorLabel.textColor = NSColor(hex: "#252629")
  143. self.backgroundColorLabel.textColor = NSColor(hex: "#000000D9")
  144. self.colorNormalLabel.textColor = NSColor(hex: "#000000D9")
  145. self.colorFullScreenLabel.textColor = NSColor(hex: "#000000D9")
  146. self.hightlightedFormsLabel.setTitleColor(NSColor(hex: "#000000D9"))
  147. self.fieldHighlightLabel.textColor = NSColor(hex: "#000000D9")
  148. self.requiredFieldHighlightLabel.textColor = NSColor(hex: "#000000D9")
  149. self.pageIndicator2Label.textColor = NSColor(hex: "#000000D9")
  150. self.autoDisplayRadio.setTitleColor(NSColor(hex: "#000000D9"))
  151. self.alwaysShowRadio.setTitleColor(NSColor(hex: "#000000D9"))
  152. self.neverShowRadio.setTitleColor(NSColor(hex: "#000000D9"))
  153. self.highlightedLinksCheck.setTitleColor(NSColor(hex: "#000000D9"))
  154. }
  155. private func initData() {
  156. let value = KMPreferenceManager.shared.viewPageDisplayType
  157. if (value.rawValue < self.pageDisplayTypes.count) {
  158. self.pageIndicatorComboBox.stringValue = self.pageDisplayTypes[value.rawValue]
  159. }
  160. let value2 = KMPreferenceManager.shared.viewZoomScaleType
  161. if (value2.rawValue < self.viewZoomScaleTypes.count) {
  162. self.zoomScaleComboBox.stringValue = self.viewZoomScaleTypes[value2.rawValue]
  163. }
  164. let leftSideDisplayType = KMPreferenceManager.shared.leftSideDisplayType
  165. if (leftSideDisplayType == .closeWhenOpenFile) {
  166. self.selectLeftSideRadio(self.putAwayRadio)
  167. } else if (leftSideDisplayType == .openAppSaveLastSelect) {
  168. self.selectLeftSideRadio(self.rememberLastSelectionRadio)
  169. } else if (leftSideDisplayType == .showOutlineIfHas) {
  170. self.selectLeftSideRadio(self.showOutlineCheck)
  171. }
  172. if (KMPreferenceManager.shared.propertyPanelExpandType == .auto) {
  173. self.selectDisplayRadio(self.autoExpandRadio)
  174. } else {
  175. self.selectDisplayRadio(self.manualExpandRadio)
  176. }
  177. if (KMPreferenceManager.shared.highlightForms) {
  178. self.hightlightedFormsLabel.state = .on
  179. } else {
  180. self.hightlightedFormsLabel.state = .off
  181. }
  182. let value3 = KMPreferenceManager.shared.pageIndicatorType
  183. if (value3 == .automatic) {
  184. self.selectPageIndicatorRadio(self.autoDisplayRadio)
  185. } else if (value3 == .always) {
  186. self.selectPageIndicatorRadio(self.alwaysShowRadio)
  187. } else if (value3 == .never) {
  188. self.selectPageIndicatorRadio(self.neverShowRadio)
  189. }
  190. if (KMPreferenceManager.shared.highlightLinks) {
  191. self.highlightedLinksCheck.state = .on
  192. } else {
  193. self.highlightedLinksCheck.state = .off
  194. }
  195. self.colorNormalButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor.cgColor
  196. self.colorFullScreenButton.layer?.backgroundColor = KMPreferenceManager.shared.displayBackgroundFullScreenColor.cgColor
  197. self.fieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayFieldHighlightingColor.cgColor
  198. self.requiredFieldHighlightButton.layer?.backgroundColor = KMPreferenceManager.shared.displayRequiredFieldHighlightingColor.cgColor
  199. }
  200. @objc private func radioAction(sender: NSButton) {
  201. if (self.putAwayRadio.isEqual(sender)) {
  202. self.selectLeftSideRadio(sender)
  203. KMPreferenceManager.shared.leftSideDisplayType = .closeWhenOpenFile
  204. } else if (self.rememberLastSelectionRadio.isEqual(sender)) {
  205. self.selectLeftSideRadio(sender)
  206. KMPreferenceManager.shared.leftSideDisplayType = .openAppSaveLastSelect
  207. } else if (self.showOutlineCheck.isEqual(sender)) {
  208. self.selectLeftSideRadio(sender)
  209. KMPreferenceManager.shared.leftSideDisplayType = .showOutlineIfHas
  210. } else if (self.autoExpandRadio.isEqual(sender)) {
  211. self.selectDisplayRadio(sender)
  212. KMPreferenceManager.shared.propertyPanelExpandType = .auto
  213. } else if (self.manualExpandRadio.isEqual(sender)) {
  214. self.selectDisplayRadio(sender)
  215. KMPreferenceManager.shared.propertyPanelExpandType = .manual
  216. } else if (self.autoDisplayRadio.isEqual(sender)) {
  217. self.selectPageIndicatorRadio(sender)
  218. KMPreferenceManager.shared.pageIndicatorType = .automatic
  219. } else if (self.alwaysShowRadio.isEqual(sender)) {
  220. self.selectPageIndicatorRadio(sender)
  221. KMPreferenceManager.shared.pageIndicatorType = .always
  222. } else if (self.neverShowRadio.isEqual(sender)) {
  223. self.selectPageIndicatorRadio(sender)
  224. KMPreferenceManager.shared.pageIndicatorType = .never
  225. }
  226. }
  227. @objc private func checkAction(sender: NSButton) {
  228. if (self.hightlightedFormsLabel.isEqual(sender)) {
  229. KMPreferenceManager.shared.highlightForms = sender.state == .on
  230. } else if (self.highlightedLinksCheck.isEqual(sender)) {
  231. KMPreferenceManager.shared.highlightLinks = sender.state == .on
  232. }
  233. }
  234. @objc private func colorButtonAction(sender: NSButton) {
  235. self.currentColorButton = sender
  236. var color: NSColor?
  237. let cgColor = sender.layer?.backgroundColor
  238. if (cgColor != nil) {
  239. color = NSColor(cgColor: cgColor!)
  240. }
  241. let colorPanel = NSColorPanel.shared
  242. if (color != nil) {
  243. colorPanel.color = color!
  244. }
  245. colorPanel.setTarget(self)
  246. colorPanel.setAction(#selector(colorPanelAction))
  247. colorPanel.orderFront(nil)
  248. }
  249. @objc private func colorPanelAction(sender: NSColorPanel) {
  250. if (self.currentColorButton == nil) {
  251. return
  252. }
  253. let color = sender.color
  254. self.currentColorButton?.layer?.backgroundColor = color.cgColor
  255. if (self.colorNormalButton.isEqual(self.currentColorButton)) {
  256. KMPreferenceManager.shared.displayBackgroundNormalColor = color
  257. } else if (self.colorFullScreenButton.isEqual(self.currentColorButton)) {
  258. KMPreferenceManager.shared.displayBackgroundFullScreenColor = color
  259. } else if (self.fieldHighlightButton.isEqual(self.currentColorButton)) {
  260. KMPreferenceManager.shared.displayFieldHighlightingColor = color
  261. } else if (self.requiredFieldHighlightButton.isEqual(self.currentColorButton)) {
  262. KMPreferenceManager.shared.displayRequiredFieldHighlightingColor = color
  263. }
  264. }
  265. private func selectLeftSideRadio(_ sender: NSButton) {
  266. for radio in [self.putAwayRadio, self.rememberLastSelectionRadio, self.showOutlineCheck] {
  267. if (sender.isEqual(to: radio)) {
  268. radio?.state = .on
  269. } else {
  270. radio?.state = .off
  271. }
  272. }
  273. }
  274. private func selectDisplayRadio(_ sender: NSButton) {
  275. for radio in [self.autoExpandRadio, self.manualExpandRadio] {
  276. if (sender.isEqual(to: radio)) {
  277. radio?.state = .on
  278. } else {
  279. radio?.state = .off
  280. }
  281. }
  282. }
  283. private func selectPageIndicatorRadio(_ sender: NSButton) {
  284. for radio in [self.autoDisplayRadio, self.alwaysShowRadio, self.neverShowRadio] {
  285. if (sender.isEqual(to: radio)) {
  286. radio?.state = .on
  287. } else {
  288. radio?.state = .off
  289. }
  290. }
  291. }
  292. }
  293. extension KMPreferenceDisplayController: NSComboBoxDelegate {
  294. func comboBoxSelectionDidChange(_ notification: Notification) {
  295. if (self.pageIndicatorComboBox.isEqual(to: notification.object)) {
  296. var index: Int = 0
  297. if (self.pageIndicatorComboBox.indexOfSelectedItem >= 0) {
  298. index = self.pageIndicatorComboBox.indexOfSelectedItem
  299. }
  300. KMPreferenceManager.shared.viewPageDisplayType = KMPDFDisplayType(rawValue: index)!
  301. } else if (self.zoomScaleComboBox.isEqual(notification.object)) {
  302. var index: Int = 0
  303. if (self.zoomScaleComboBox.indexOfSelectedItem >= 0) {
  304. index = self.zoomScaleComboBox.indexOfSelectedItem
  305. }
  306. KMPreferenceManager.shared.viewZoomScaleType = KMPDFZoomType(rawValue: index)!
  307. }
  308. }
  309. }
  310. extension KMPreferenceDisplayController: KMPreferenceDealDataChangeProtocol {
  311. func refreshUI() {
  312. DispatchQueue.main.async {
  313. self.initData()
  314. }
  315. }
  316. }