KMDisplayPreferences.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // KMDisplayPreferences.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/6.
  6. //
  7. import Cocoa
  8. @objcMembers class KMDisplayPreferences: NSViewController {
  9. @IBOutlet weak var thumbSizeLabel: NSTextField!
  10. @IBOutlet weak var pagesLabel: NSTextField!
  11. @IBOutlet weak var pagesSlider: NSSlider!
  12. @IBOutlet weak var snapshotLabel: NSTextField!
  13. @IBOutlet weak var snapshotSlider: NSSlider!
  14. @IBOutlet weak var discreteSizeCheckbox: NSButton!
  15. @IBOutlet var tableFontLabelField: NSTextField!
  16. @IBOutlet var tableFontComboBox: NSComboBox!
  17. @IBOutlet weak var displayLabel: NSTextField!
  18. @IBOutlet var greekingLabelField: NSTextField!
  19. @IBOutlet var greekingTextField: NSTextField!
  20. @IBOutlet var antiAliasCheckButton: NSButton!
  21. @IBOutlet weak var backgroundColorLabel: NSTextField!
  22. @IBOutlet weak var normalColorLabel: NSTextField!
  23. @IBOutlet weak var normalColorWell: NSColorWell!
  24. @IBOutlet weak var fullScreenLabel: NSTextField!
  25. @IBOutlet weak var fullScreenColorWell: NSColorWell!
  26. @IBOutlet weak var readBarLabel: NSTextField!
  27. @IBOutlet weak var readingbarColorLabel: NSTextField!
  28. @IBOutlet weak var readBarColorWell: NSColorWell!
  29. @IBOutlet weak var invertBarButton: NSButton!
  30. var thumbnailSizeLabels: [NSTextField] = []
  31. var thumbnailSizeControls: [NSControl] = []
  32. var colorLabels: [NSTextField] = []
  33. var colorControls: [NSControl] = []
  34. let SKDefaultFontSizes: [CGFloat] = [8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 16.0, 18.0, 20.0, 24.0, 28.0, 32.0, 48.0, 64.0]
  35. override var nibName: NSNib.Name? {
  36. return "DisplayPreferences"
  37. }
  38. override func loadView() {
  39. super.loadView()
  40. self.thumbnailSizeLabels = [self.pagesLabel, self.snapshotLabel]
  41. self.thumbnailSizeControls = [self.pagesSlider, self.snapshotSlider, self.discreteSizeCheckbox]
  42. _ = KMAutoSizeLabelFields(self.thumbnailSizeLabels, self.thumbnailSizeControls, false)
  43. self.discreteSizeCheckbox.sizeToFit()
  44. _ = KMAutoSizeLabelFields([self.tableFontLabelField], [self.tableFontComboBox], false)
  45. _ = KMAutoSizeLabelFields([self.greekingLabelField], [self.greekingTextField], false)
  46. self.antiAliasCheckButton.sizeToFit()
  47. self.colorLabels = [self.normalColorLabel, self.readingbarColorLabel]
  48. self.colorControls = [self.normalColorWell, self.fullScreenLabel, self.fullScreenColorWell, self.readBarColorWell, self.invertBarButton]
  49. _ = KMAutoSizeLabelFields(self.colorLabels, self.colorControls, false)
  50. _ = KMAutoSizeLabelFields([self.fullScreenLabel], [self.fullScreenColorWell], false);
  51. self.invertBarButton.sizeToFit()
  52. var w = 0.0
  53. for view in self.view.subviews {
  54. if view.autoresizingMask.contains(.width) == false {
  55. var x = NSMaxX(view.frame)
  56. if view.isKind(of: NSSlider.self) || view.isKind(of: NSButton.self) {
  57. x -= 2.0
  58. } else if view.isKind(of: NSComboBox.self) {
  59. x -= 3.0
  60. }
  61. w = fmax(w, x)
  62. }
  63. }
  64. var size = self.view.frame.size
  65. size.width = w + 20.0
  66. self.view.setFrameSize(size)
  67. }
  68. override func viewDidLoad() {
  69. super.viewDidLoad()
  70. // Do view setup here.
  71. thumbSizeLabel.stringValue = KMLocalizedString("Thumbnail sizes:")
  72. pagesLabel.stringValue = KMLocalizedString("Pages:")
  73. snapshotLabel.stringValue = KMLocalizedString("Snapshots:")
  74. discreteSizeCheckbox.title = KMLocalizedString("Discrete sizes")
  75. tableFontLabelField.stringValue = KMLocalizedString("Outline font size:")
  76. displayLabel.stringValue = KMLocalizedString("PDF display:")
  77. greekingLabelField.stringValue = KMLocalizedString("Greeking threshold:")
  78. antiAliasCheckButton.title = KMLocalizedString("Anti-alias text and line art")
  79. backgroundColorLabel.stringValue = KMLocalizedString("Background color:")
  80. normalColorLabel.stringValue = KMLocalizedString("Normal:")
  81. fullScreenLabel.stringValue = KMLocalizedString("Full Screen:")
  82. readBarLabel.stringValue = KMLocalizedString("Reading bar:")
  83. readingbarColorLabel.stringValue = KMLocalizedString("Color:")
  84. invertBarButton.title = KMLocalizedString("Invert bar")
  85. self.pagesSlider.target = self
  86. self.pagesSlider.action = #selector(pageSliderAction)
  87. self.snapshotSlider.target = self
  88. self.snapshotSlider.action = #selector(snapshotSliderAction)
  89. self.tableFontComboBox.addItems(withObjectValues: SKDefaultFontSizes)
  90. self.tableFontComboBox.selectItem(at: 4)
  91. self.tableFontComboBox.delegate = self
  92. self.greekingTextField.delegate = self
  93. self.antiAliasCheckButton.target = self
  94. self.antiAliasCheckButton.action = #selector(antiAliasCheckAction)
  95. self.normalColorWell.target = self
  96. self.normalColorWell.action = #selector(normalColorAction)
  97. self.fullScreenColorWell.target = self
  98. self.fullScreenColorWell.action = #selector(fullScreenColorAction)
  99. self.readBarColorWell.target = self
  100. self.readBarColorWell.action = #selector(readBarColorAction)
  101. self.invertBarButton.target = self
  102. self.invertBarButton.action = #selector(invertBarAction)
  103. self.greekingTextField.formatter = NumberFormatter()
  104. // 赋值
  105. self.initData()
  106. }
  107. override var title: String? {
  108. set {
  109. super.title = newValue
  110. }
  111. get {
  112. KMLocalizedString("Display", "Preference pane label")
  113. }
  114. }
  115. func initData() {
  116. self.pagesSlider.floatValue = KMPreference.shared.thumbPageSize
  117. self.snapshotSlider.floatValue = KMPreference.shared.thumbSnapshotSize
  118. self.tableFontComboBox.stringValue = String(format: "%.0f", KMPreference.shared.outlineFontSize)
  119. self.greekingTextField.stringValue = String(format: "%.0f", KMPreference.shared.greekThreshold)
  120. self.antiAliasCheckButton.state = KMPreference.shared.antiAliasText ? .on : .off
  121. self.normalColorWell.color = KMPreference.shared.displayBackgroundNormalColor
  122. self.fullScreenColorWell.color = KMPreference.shared.displayBackgroundFullScreenColor
  123. self.readBarColorWell.color = KMPreference.shared.readBarColor
  124. self.invertBarButton.state = KMPreference.shared.invertBar ? .on : .off
  125. }
  126. @objc func pageSliderAction(_ sender: NSSlider) {
  127. KMPreference.shared.thumbPageSize = self.pagesSlider.floatValue
  128. }
  129. @objc func snapshotSliderAction(_ sender: NSSlider) {
  130. KMPreference.shared.thumbSnapshotSize = self.snapshotSlider.floatValue
  131. }
  132. @IBAction func changeDiscreteThumbnailSizes(_ sender: AnyObject?) {
  133. let slider1 = self.pagesSlider
  134. let slider2 = self.snapshotSlider
  135. let button = sender as? NSButton
  136. if let data = button?.state, data == .on {
  137. slider1?.numberOfTickMarks = 8
  138. slider2?.numberOfTickMarks = 8
  139. slider1?.allowsTickMarkValuesOnly = true
  140. slider2?.allowsTickMarkValuesOnly = true
  141. } else {
  142. slider1?.superview?.setNeedsDisplay(slider1?.frame ?? .zero)
  143. slider2?.superview?.setNeedsDisplay(slider2?.frame ?? .zero)
  144. slider1?.numberOfTickMarks = 0
  145. slider2?.numberOfTickMarks = 0
  146. slider1?.allowsTickMarkValuesOnly = false
  147. slider2?.allowsTickMarkValuesOnly = false
  148. }
  149. slider1?.sizeToFit()
  150. slider2?.sizeToFit()
  151. }
  152. @objc func antiAliasCheckAction(_ sender: NSButton) {
  153. KMPreference.shared.antiAliasText = self.antiAliasCheckButton.state == .on
  154. }
  155. @objc func normalColorAction(_ sender: NSColorWell) {
  156. if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  157. KMPreference.shared.displayBackgroundNormalColor = color
  158. }
  159. }
  160. @objc func fullScreenColorAction(_ sender: NSColorWell) {
  161. if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  162. KMPreference.shared.displayBackgroundFullScreenColor = color
  163. }
  164. }
  165. @objc func readBarColorAction(_ sender: NSColorWell) {
  166. if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  167. KMPreference.shared.readBarColor = color
  168. }
  169. }
  170. @objc func invertBarAction(_ sender: NSButton) {
  171. KMPreference.shared.invertBar = sender.state == .on
  172. }
  173. }
  174. extension KMDisplayPreferences: KMPreferencePane {
  175. func defaultsDidRevert() {
  176. // no things.
  177. }
  178. func reloadData() {
  179. self.initData()
  180. }
  181. }
  182. extension KMDisplayPreferences: NSComboBoxDelegate {
  183. func comboBoxSelectionDidChange(_ notification: Notification) {
  184. if self.tableFontComboBox.isEqual(to: notification.object) {
  185. KMPreference.shared.outlineFontSize = self.tableFontComboBox.floatValue
  186. }
  187. }
  188. }
  189. extension KMDisplayPreferences: NSTextFieldDelegate {
  190. func controlTextDidChange(_ obj: Notification) {
  191. if self.greekingTextField.isEqual(to: obj.object) {
  192. KMPreference.shared.greekThreshold = self.greekingTextField.floatValue
  193. }
  194. }
  195. }