KMTextFieldStepperView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // KMTextFieldStepperView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/13.
  6. //
  7. import Cocoa
  8. typealias KMTextFieldStepperViewValueDidChange = (_ view: KMTextFieldStepperView, _ value: String) -> Void
  9. class KMTextFieldStepperView: BaseXibView, NSTextFieldDelegate {
  10. @IBOutlet weak var inputTextField: KMBaseTextField!
  11. @IBOutlet weak var downButton: NSButton!
  12. @IBOutlet weak var upButton: NSButton!
  13. @IBOutlet weak var line1: NSView!
  14. @IBOutlet weak var line2: NSView!
  15. var presenter: KMBaseTextFieldPresenter = KMBaseTextFieldPresenter()
  16. var model: KMBaseTextFieldModel = KMBaseTextFieldModel() {
  17. didSet {
  18. self.presenter.model = model
  19. self.inputTextField.model = model
  20. }
  21. }
  22. var valueDidChange: KMTextFieldStepperViewValueDidChange?
  23. var stringValue: String {
  24. set {
  25. self.inputTextField.stringValue = newValue
  26. }
  27. get {
  28. return self.inputTextField.stringValue
  29. }
  30. }
  31. var isEnabled: Bool = true {
  32. didSet {
  33. self.inputTextField.isEnabled = isEnabled
  34. self.inputTextField.alphaValue = isEnabled ? 1 : 0.5
  35. self.downButton.isEnabled = isEnabled
  36. self.downButton.alphaValue = isEnabled ? 1 : 0.5
  37. self.upButton.isEnabled = isEnabled
  38. self.upButton.alphaValue = isEnabled ? 1 : 0.5
  39. }
  40. }
  41. var minValue: Int = 1{
  42. didSet {
  43. self.inputTextField.minValue = minValue
  44. }
  45. }
  46. var maxValue: Int = 100 {
  47. didSet {
  48. self.inputTextField.maxValue = maxValue
  49. }
  50. }
  51. var textColor: NSColor = NSColor.km_init(hex: "#252629") {
  52. didSet {
  53. self.inputTextField.textField.textColor = textColor
  54. }
  55. }
  56. var font: NSFont = NSFont.SFProTextRegularFont(14.0) {
  57. didSet {
  58. self.inputTextField.textField.font = font
  59. }
  60. }
  61. deinit {
  62. // self.delegate = nil
  63. }
  64. override func draw(_ dirtyRect: NSRect) {
  65. super.draw(dirtyRect)
  66. // Drawing code here.
  67. }
  68. func setup() {
  69. self.presenter.initPresenter(delegate: self, model: self.model)
  70. self.inputTextField.textField.textColor = self.textColor
  71. self.inputTextField.textField.font = self.font
  72. self.inputTextField.textDidEndEditing = {[weak self] (string) in
  73. self?.presenter.changeValue(value: (string))
  74. }
  75. self.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  76. self.line1.backgroundColor(NSColor.km_init(hex: "#DFE1E5"))
  77. self.line2.backgroundColor(NSColor.km_init(hex: "#DFE1E5"))
  78. }
  79. //刷新界面UI 和 数据
  80. func reloadData() {
  81. self.inputTextField.stringValue = self.model.stringValue
  82. }
  83. }
  84. protocol KMTextFieldStepperViewPrivate {}
  85. extension KMTextFieldStepperView: KMTextFieldStepperViewPrivate {
  86. }
  87. protocol KMTextFieldStepperViewAction {}
  88. extension KMTextFieldStepperView: KMTextFieldStepperViewAction {
  89. @IBAction func downButtonAction(_ sender: NSButton) {
  90. let string = String(Int(self.model.value)! - 1)
  91. self.presenter.changeValue(value: string)
  92. }
  93. @IBAction func upButtonAction(_ sender: NSButton) {
  94. let string = String(Int(self.model.value)! + 1)
  95. self.presenter.changeValue(value: string)
  96. }
  97. }
  98. extension KMTextFieldStepperView: KMBaseTextFieldPresenterDelegate {
  99. func valueDidChange(presenter: KMBaseTextFieldPresenter, model: KMBaseTextFieldModel) {
  100. self.model = model
  101. self.reloadData()
  102. if self.valueDidChange != nil {
  103. self.valueDidChange!(self, self.model.stringValue)
  104. }
  105. }
  106. }