KMTextFieldStepperView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: KMBaseXibView, 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. override func setup() {
  69. super.setup()
  70. self.presenter.initPresenter(delegate: self, model: self.model)
  71. self.inputTextField.textField.textColor = self.textColor
  72. self.inputTextField.textField.font = self.font
  73. self.inputTextField.textDidEndEditing = {[weak self] (string) in
  74. self?.presenter.changeValue(value: (string))
  75. }
  76. self.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  77. self.line1.backgroundColor(NSColor.km_init(hex: "#DFE1E5"))
  78. self.line2.backgroundColor(NSColor.km_init(hex: "#DFE1E5"))
  79. }
  80. //刷新界面UI 和 数据
  81. override func reloadData() {
  82. super.reloadData()
  83. self.inputTextField.stringValue = self.model.stringValue
  84. }
  85. }
  86. protocol KMTextFieldStepperViewPrivate {}
  87. extension KMTextFieldStepperView: KMTextFieldStepperViewPrivate {
  88. }
  89. protocol KMTextFieldStepperViewAction {}
  90. extension KMTextFieldStepperView: KMTextFieldStepperViewAction {
  91. @IBAction func downButtonAction(_ sender: NSButton) {
  92. let string = String(Int(self.model.value)! - 1)
  93. self.presenter.changeValue(value: string)
  94. }
  95. @IBAction func upButtonAction(_ sender: NSButton) {
  96. let string = String(Int(self.model.value)! + 1)
  97. self.presenter.changeValue(value: string)
  98. }
  99. }
  100. extension KMTextFieldStepperView: KMBaseTextFieldPresenterDelegate {
  101. func valueDidChange(presenter: KMBaseTextFieldPresenter, model: KMBaseTextFieldModel) {
  102. self.model = model
  103. self.reloadData()
  104. if self.valueDidChange != nil {
  105. self.valueDidChange!(self, self.model.stringValue)
  106. }
  107. }
  108. }