InputNumberVC.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // InputNumberVC.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/8/27.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class InputNumberVC: NSViewController, NSTextFieldDelegate {
  10. @IBOutlet weak var inputNumberView: ComponentInputNumber!
  11. @IBOutlet weak var sizeBox: NSComboBox!
  12. @IBOutlet weak var errorBtn: NSButton!
  13. @IBOutlet weak var disableButton: NSButton!
  14. @IBOutlet weak var prefixBtn: NSButton!
  15. @IBOutlet weak var suffixButton: NSButton!
  16. @IBOutlet weak var showErrorBtn: NSButton!
  17. @IBOutlet weak var isCenterBtn: NSButton!
  18. @IBOutlet weak var minSizeField: NSTextField!
  19. @IBOutlet weak var maxSizeField: NSTextField!
  20. @IBOutlet weak var viewheightConst: NSLayoutConstraint!
  21. let properties: ComponentInputNumberProperty = ComponentInputNumberProperty.init(alignment: .left, size: .m, state: .normal, isError: false)
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do view setup here.
  25. self.minSizeField.intValue = 0
  26. self.maxSizeField.intValue = 10
  27. inputNumberView.delegate = self
  28. self.minSizeField.delegate = self
  29. self.maxSizeField.delegate = self
  30. self.configSizeValue()
  31. self.reloadData()
  32. }
  33. func reloadData() {
  34. if self.sizeBox.indexOfSelectedItem == 0 {
  35. properties.size = .m
  36. } else {
  37. properties.size = .s
  38. }
  39. properties.isDisabled = self.disableButton.state == .on
  40. properties.showPrefix = self.prefixBtn.state == .on
  41. properties.showSuffix = self.suffixButton.state == .on
  42. if self.isCenterBtn.state == .on {
  43. properties.alignment = .center
  44. } else {
  45. properties.alignment = .left
  46. }
  47. var minSize = self.minSizeField.intValue
  48. var maxSize = self.maxSizeField.intValue
  49. if maxSize < 0 {
  50. maxSize = 50
  51. }
  52. if minSize > maxSize {
  53. minSize = 0
  54. }
  55. self.minSizeField.stringValue = String(minSize)
  56. self.maxSizeField.stringValue = String(maxSize)
  57. properties.minSize = Int(minSize)
  58. properties.maxSize = Int(maxSize)
  59. inputNumberView.properties = properties
  60. self.viewheightConst.constant = properties.propertyInfo.viewHeight
  61. }
  62. @IBAction func sizeAction(_ sender: Any) {
  63. self.reloadData()
  64. }
  65. @IBAction func disableAction(_ sender: Any) {
  66. self.reloadData()
  67. }
  68. @IBAction func prefixAction(_ sender: Any) {
  69. self.reloadData()
  70. }
  71. @IBAction func suffixAction(_ sender: Any) {
  72. self.reloadData()
  73. }
  74. @IBAction func isCenterAction(_ sender: Any) {
  75. self.reloadData()
  76. }
  77. func configSizeValue() {
  78. self.reloadData()
  79. }
  80. override func mouseDown(with event: NSEvent) {
  81. super.mouseDown(with: event)
  82. self.view.window?.makeFirstResponder(nil)
  83. }
  84. func controlTextDidBeginEditing(_ obj: Notification) {
  85. self.configSizeValue()
  86. }
  87. func controlTextDidEndEditing(_ obj: Notification) {
  88. self.configSizeValue()
  89. }
  90. }
  91. extension InputNumberVC: ComponentInputNumberDelegate {
  92. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  93. self.viewheightConst.constant = properties.propertyInfo.viewHeight
  94. }
  95. }