123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // KMDesignTextField.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/6/7.
- //
- // 输入框 组件
- class KMDesignTextField: KMDesignBaseView {
- private var textField: KMCustomTextField?
- var textF: KMCustomTextField? {
- get {
- return self.textField
- }
- }
-
- 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?
-
- var kFormatter: Formatter? {
- set {
- self.textField?.textFiled.formatter = newValue
- }
- get {
- return self.textField?.textFiled.formatter
- }
- }
-
- var enterKeyEndEdit: Bool {
- set {
- let textFiled = self.textField?.textFiled as? KMTextField
- textFiled?.enterKeyEndEdit = newValue
- }
- get {
- let textFiled = self.textField?.textFiled as? KMTextField
- return textFiled?.enterKeyEndEdit ?? false
- }
- }
-
- var useToken = true
-
- 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) {
- if self.useToken {
- super.updateUI(state: state)
- }
-
- self.textField?.backgroundView.layer?.backgroundColor = self.fillColor.cgColor
- self.textField?.backgroundView.layer?.borderWidth = self.borderWidth
- if self.useToken {
- self.textField?.backgroundView.layer?.borderColor = self.borderColor.cgColor
- } else {
- if state == .Hov {
- let borderColor = NSColor(hex: "68ACF8")
- self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
- } else if state == .Focus {
- let borderColor = NSColor(hex: "68ACF8")
- self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
- } else {
- if KMAppearance.isDarkMode() {
- // let borderColor = KMAppearance.Interactive.s0Color()
- let borderColor = NSColor(hex: "56585A")
- self.textField?.backgroundView.layer?.borderColor = borderColor.cgColor
- } else {
- let borderColor = NSColor(hex: "DFE1E5")
- self.textField?.backgroundView.layer?.borderColor = 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)
- }
- }
|