KMAutoFlowOptionsSheetController.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // KMAutoFlowOptionsSheetController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/1/16.
  6. //
  7. import Cocoa
  8. class KMAutoFlowOptionsSheetController: NSWindowController {
  9. @IBOutlet weak var timeIntervalLabel: NSTextField!
  10. @IBOutlet weak var timeIntervalSlider: NSSlider!
  11. @IBOutlet weak var timeIntervalTextField: NSTextField!
  12. @IBOutlet weak var secLabel: NSTextField!
  13. @IBOutlet weak var jumpSpaceLabel: NSTextField!
  14. @IBOutlet weak var jumpSpaceSlider: NSSlider!
  15. @IBOutlet weak var jumpSpaceTextField: NSTextField!
  16. @IBOutlet weak var pxLabel: NSTextField!
  17. @IBOutlet weak var closeButton: NSButton!
  18. @IBOutlet weak var okButton: NSButton!
  19. var timeInterval: String?
  20. var jumpSpace: String?
  21. override init(window: NSWindow?) {
  22. super.init(window: window)
  23. }
  24. required init?(coder: NSCoder) {
  25. super.init(coder: coder)
  26. }
  27. override func windowDidLoad() {
  28. super.windowDidLoad()
  29. self.closeButton.title = NSLocalizedString("Cancel", comment: "")
  30. self.okButton.title = NSLocalizedString("OK", comment: "")
  31. self.timeIntervalLabel.stringValue = NSLocalizedString("Time Interval", comment: "")
  32. self.jumpSpaceLabel.stringValue = NSLocalizedString("Jump Space", comment: "")
  33. self.secLabel.stringValue = NSLocalizedString("sec", comment: "")
  34. self.pxLabel.stringValue = NSLocalizedString("px", comment: "")
  35. self.timeIntervalTextField.delegate = self
  36. self.jumpSpaceTextField.delegate = self
  37. let timeInterval = KMAutoFlowOptionsSheetController.timeInterval()
  38. let jumpSpace = KMAutoFlowOptionsSheetController.jumpSpace()
  39. self.timeInterval = String(timeInterval)
  40. self.jumpSpace = String(jumpSpace)
  41. }
  42. @IBAction func closeButtonAction(_ sender: Any) {
  43. self.close()
  44. }
  45. @IBAction func okButtonAction(_ sender: Any) {
  46. let sec = self.timeIntervalTextField.stringValue.isEmpty ? "0" : self.timeIntervalTextField.stringValue
  47. var jumpSpaceStr = self.jumpSpaceTextField.stringValue.isEmpty ? "0" : self.jumpSpaceTextField.stringValue
  48. //替换”,“
  49. jumpSpaceStr = jumpSpaceStr.replacingOccurrences(of: ",", with: "")
  50. KMAutoFlowOptionsSheetController.setTimeInterval(Float(sec) ?? 0.0)
  51. KMAutoFlowOptionsSheetController.setJumpSpace(Float(jumpSpaceStr) ?? 0.0)
  52. self.close()
  53. }
  54. @IBAction func timeIntervalSliderAction(_ sender: NSSlider) {
  55. self.timeIntervalTextField.stringValue = sender.stringValue
  56. }
  57. @IBAction func jumpSpaceSliderAction(_ sender: NSSlider) {
  58. self.jumpSpaceTextField.stringValue = sender.stringValue
  59. }
  60. class func timeInterval() -> Float {
  61. if let storedValue = UserDefaults.standard.object(forKey: "km_pdfview_autoflow_timeinterval") as? Float {
  62. return storedValue
  63. }
  64. return 5.0
  65. }
  66. class func setTimeInterval(_ timeInterval: Float) {
  67. UserDefaults.standard.set(timeInterval, forKey: "km_pdfview_autoflow_timeinterval")
  68. UserDefaults.standard.synchronize()
  69. }
  70. class func jumpSpace() -> Float {
  71. if let storedValue = UserDefaults.standard.object(forKey: "km_pdfview_autoflow_jumpspace") as? Float {
  72. return storedValue
  73. }
  74. return 40.0
  75. }
  76. class func setJumpSpace(_ jumpSpace: Float) {
  77. UserDefaults.standard.set(jumpSpace, forKey: "km_pdfview_autoflow_jumpspace")
  78. UserDefaults.standard.synchronize()
  79. }
  80. }
  81. extension KMAutoFlowOptionsSheetController: NSTextFieldDelegate {
  82. func controlTextDidEndEditing(_ obj: Notification) {
  83. let textField: NSTextField = obj.object as! NSTextField;
  84. if textField == self.timeIntervalTextField {
  85. self.timeIntervalSlider.stringValue = self.timeIntervalTextField.stringValue
  86. print("参数改变了")
  87. } else if textField == self.jumpSpaceTextField {
  88. print("参数改变了2")
  89. self.jumpSpaceSlider.stringValue = self.jumpSpaceTextField.stringValue
  90. }
  91. }
  92. }