// // KMTextFieldStepperView.swift // PDF Reader Pro // // Created by lizhe on 2022/12/13. // import Cocoa typealias KMTextFieldStepperViewValueDidChange = (_ view: KMTextFieldStepperView, _ value: String) -> Void class KMTextFieldStepperView: KMBaseXibView, NSTextFieldDelegate { @IBOutlet weak var inputTextField: KMBaseTextField! @IBOutlet weak var downButton: NSButton! @IBOutlet weak var upButton: NSButton! @IBOutlet weak var line1: NSView! @IBOutlet weak var line2: NSView! var presenter: KMBaseTextFieldPresenter = KMBaseTextFieldPresenter() var model: KMBaseTextFieldModel = KMBaseTextFieldModel() { didSet { self.presenter.model = model self.inputTextField.model = model } } var valueDidChange: KMTextFieldStepperViewValueDidChange? var stringValue: String { set { self.inputTextField.stringValue = newValue } get { return self.inputTextField.stringValue } } var isEnabled: Bool = true { didSet { self.inputTextField.isEnabled = isEnabled self.inputTextField.alphaValue = isEnabled ? 1 : 0.5 self.downButton.isEnabled = isEnabled self.downButton.alphaValue = isEnabled ? 1 : 0.5 self.upButton.isEnabled = isEnabled self.upButton.alphaValue = isEnabled ? 1 : 0.5 } } var minValue: Int = 1{ didSet { self.inputTextField.minValue = minValue } } var maxValue: Int = 100 { didSet { self.inputTextField.maxValue = maxValue } } var textColor: NSColor = NSColor.km_init(hex: "#252629") { didSet { self.inputTextField.textField.textColor = textColor } } var font: NSFont = NSFont.SFProTextRegularFont(14.0) { didSet { self.inputTextField.textField.font = font } } deinit { // self.delegate = nil } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func setup() { super.setup() self.presenter.initPresenter(delegate: self, model: self.model) self.inputTextField.textField.textColor = self.textColor self.inputTextField.textField.font = self.font self.inputTextField.textDidEndEditing = {[unowned self] (string) in self.presenter.changeValue(value: (string)) } self.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4) self.line1.backgroundColor(NSColor.km_init(hex: "#DFE1E5")) self.line2.backgroundColor(NSColor.km_init(hex: "#DFE1E5")) } //刷新界面UI 和 数据 override func reloadData() { super.reloadData() self.inputTextField.stringValue = self.model.stringValue } } protocol KMTextFieldStepperViewPrivate {} extension KMTextFieldStepperView: KMTextFieldStepperViewPrivate { } protocol KMTextFieldStepperViewAction {} extension KMTextFieldStepperView: KMTextFieldStepperViewAction { @IBAction func downButtonAction(_ sender: NSButton) { let string = String(Int(self.model.value)! - 1) self.presenter.changeValue(value: string) } @IBAction func upButtonAction(_ sender: NSButton) { let string = String(Int(self.model.value)! + 1) self.presenter.changeValue(value: string) } } extension KMTextFieldStepperView: KMBaseTextFieldPresenterDelegate { func valueDidChange(presenter: KMBaseTextFieldPresenter, model: KMBaseTextFieldModel) { self.model = model self.reloadData() if self.valueDidChange != nil { self.valueDidChange!(self, self.model.stringValue) } } }