KMTextField.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // KMTextField.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. import Cocoa
  8. protocol KMTextFieldDelegate: AnyObject {
  9. // associatedtype KMTextFieldType
  10. func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool
  11. func km_controlTextDidBeginEditing(textField: AnyObject)
  12. func km_controlTextDidChange(textField: AnyObject)
  13. func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool
  14. func km_controlTextDidEndEditing(textField: AnyObject)
  15. func km_didBecomeFirstResponder(textField: AnyObject)
  16. }
  17. extension KMTextFieldDelegate {
  18. func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool { return true }
  19. func km_controlTextDidBeginEditing(textField: AnyObject) {}
  20. func km_controlTextDidChange(textField: AnyObject) {}
  21. func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool { return true }
  22. func km_controlTextDidEndEditing(textField: AnyObject) {}
  23. func km_didBecomeFirstResponder(textField: AnyObject) {}
  24. }
  25. class KMTextField: NSTextField {
  26. private class _KMTextFieldCell: NSTextFieldCell {
  27. // 边框粗细
  28. var borderThickness: CGFloat = 1
  29. // 左右两边缩进
  30. var offset: CGFloat = 8.0
  31. // override func drawingRect(forBounds theRect: NSRect) -> NSRect {
  32. // var newRect = super.drawingRect(forBounds: theRect)
  33. //
  34. // let textSize = self.cellSize(forBounds: theRect)
  35. // let heightInset = newRect.size.height - textSize.height
  36. // if (heightInset > 0) {
  37. // newRect.size.height = textSize.height
  38. // newRect.origin.y += heightInset * 0.5
  39. // } else {
  40. // newRect.size.height = textSize.height
  41. // newRect.origin.y += heightInset
  42. // }
  43. //
  44. // newRect.origin.x += self.offset
  45. // newRect.size.width = theRect.width - self.offset * 2
  46. //
  47. // return newRect
  48. // }
  49. override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
  50. // Area that covers the NSTextField itself. That is the total height minus our custom border size.
  51. let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - self.borderThickness)
  52. let path = NSBezierPath()
  53. path.lineWidth = self.borderThickness
  54. // Line width is at the center of the line.
  55. path.move(to: NSPoint(x: 0, y: cellFrame.height))
  56. path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height))
  57. path.line(to: NSPoint(x: cellFrame.width, y: 0))
  58. path.line(to: NSPoint(x: 0, y: 0))
  59. NSColor.clear.setStroke()
  60. path.stroke()
  61. // Pass in area minus the border thickness in the height
  62. drawInterior(withFrame: interiorFrame, in: controlView)
  63. }
  64. }
  65. var borderThickness: CGFloat = 1 {
  66. didSet {
  67. if let _cell = self.cell as? _KMTextFieldCell {
  68. _cell.borderThickness = self.borderThickness
  69. }
  70. self.needsDisplay = true
  71. }
  72. }
  73. var offset: CGFloat = 8.0 {
  74. didSet {
  75. if let _cell = self.cell as? _KMTextFieldCell {
  76. _cell.offset = self.offset
  77. }
  78. self.needsDisplay = true
  79. }
  80. }
  81. override init(frame frameRect: NSRect) {
  82. super.init(frame: frameRect)
  83. // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result {
  84. // self.cell = _KMTextFieldCell()
  85. // }
  86. }
  87. var firstResponderHandler: ((Bool)->Void)?
  88. required init?(coder: NSCoder) {
  89. super.init(coder: coder)
  90. // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result {
  91. // self.cell = _KMTextFieldCell()
  92. // }
  93. }
  94. override class var cellClass: AnyClass? {
  95. set {
  96. super.cellClass = newValue
  97. }
  98. get {
  99. return _KMTextFieldCell.self
  100. }
  101. }
  102. override func becomeFirstResponder() -> Bool {
  103. let result = super.becomeFirstResponder()
  104. guard let callback = self.firstResponderHandler else {
  105. return result
  106. }
  107. callback(result)
  108. return result
  109. }
  110. }