123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // KMTextFieldStepperView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2022/12/13.
- //
- import Cocoa
- typealias KMTextFieldStepperViewValueDidChange = (_ view: KMTextFieldStepperView, _ value: String) -> Void
- class KMTextFieldStepperView: BaseXibView, 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.
-
- }
-
- func setup() {
-
- self.presenter.initPresenter(delegate: self, model: self.model)
-
- self.inputTextField.textField.textColor = self.textColor
- self.inputTextField.textField.font = self.font
- self.inputTextField.textDidEndEditing = {[weak 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 和 数据
- func 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)
- }
- }
- }
|