// // KMDesignTextField.swift // PDF Reader Pro // // Created by tangchao on 2023/6/7. // // 输入框 组件 class KMDesignTextField: KMDesignBaseView { private var textField: KMCustomTextField? private var _designSize: KMDesignSize = .medium var designSize: KMDesignSize { get { return self._designSize } } var stringValue: String? { get { return self.textField?.stringValue } set { self.textField?.stringValue = newValue ?? "" } } override var kmEnabled: Bool { get { return super.kmEnabled } set { super.kmEnabled = newValue self.textField?.kmEnabled = newValue if (newValue) { // self.window?.makeFirstResponder(self.textField) } else { self.window?.makeFirstResponder(nil) } } } weak var delegate: KMTextFieldDelegate? convenience init(designSize: KMDesignSize = .medium) { self.init() self._designSize = designSize } override func initSubViews() { super.initSubViews() let textFiled = KMCustomTextField() self.addSubview(textFiled) self.textField = textFiled self.textField?.delegate = self } override func initDefaultValue() { super.initDefaultValue() self.textField?.backgroundView.wantsLayer = true self.textField?.hoverAction = { [unowned self] sender, action in if (!self.canHover()) { return } if (action == .enter) { self.state = .Hov } else if (action == .exit) { self.state = .Norm } } let model = KMDesignToken.shared.tokenUsesModel(withToken: self.find_bgToken(state: .Norm)) if let data = model.height as? String, let _height = Float(data) { self.height = CGFloat(_height) } if let data = model.horizontalPadding as? String, let _vSpace = Float(data) { self.horizontalPadding = CGFloat(_vSpace) self.textField?.offset = self.horizontalPadding } self.state = .Norm } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let heigth: CGFloat = NSHeight(self.bounds) let y: CGFloat = (heigth - self.height) * 0.5 self.textField?.frame = NSMakeRect(0, y, width, self.height) } override func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey { if (state == .Norm) { return self.designSize == .medium ? "input.m.bg.norm" : "input.s.bg.norm" } else if (state == .Hov) { return self.designSize == .medium ? "input.m.bg.hov" : "input.s.bg.hov" } else if (state == .Focus) { return self.designSize == .medium ? "input.m.bg.focus" : "input.s.bg.focus" } else if (state == .Disabled) { return self.designSize == .medium ? "input.m.bg.dis" : "input.s.bg.dis" } else if (state == .Error_def) { return self.designSize == .medium ? "input.m.bg.error-def" : "input.s.bg.error-def" } else if (state == .Error_focus) { return self.designSize == .medium ? "input.m.bg.error-focus" : "input.s.bg.error-focus" } return KMDesignTokenNone } override func updateUI(state: KMDesignTokenState) { super.updateUI(state: state) self.textField?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor self.textField?.backgroundView.layer?.borderWidth = self.borderWidth self.textField?.backgroundView.layer?.borderColor = self.borderColor.cgColor self.textField?.backgroundView.layer?.cornerRadius = self.borderRadius } } extension KMDesignTextField: KMTextFieldDelegate { func km_didBecomeFirstResponder(textField: AnyObject) { if (textField.isEqual(to: self.textField)) { self.state = .Focus } self.delegate?.km_didBecomeFirstResponder(textField: self) } func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool { if let should = self.delegate?.km_controlTextShouldBeginEditing(textField: self) { return should } return true } func km_controlTextDidBeginEditing(textField: AnyObject) { self.delegate?.km_controlTextDidBeginEditing(textField: self) } func km_controlTextDidChange(textField: AnyObject) { self.delegate?.km_controlTextDidChange(textField: self) } func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool { if let should = self.delegate?.km_controlTextShouldEndEditing(textField: self) { return should } return true } func km_controlTextDidEndEditing(textField: AnyObject) { if (textField.isEqual(to: self.textField)) { self.state = .Norm } self.delegate?.km_controlTextDidEndEditing(textField: self) } }