// // KMTextField.swift // PDF Reader Pro // // Created by tangchao on 2023/6/7. // import Cocoa protocol KMTextFieldDelegate: AnyObject { // associatedtype KMTextFieldType func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool func km_controlTextDidBeginEditing(textField: AnyObject) func km_controlTextDidChange(textField: AnyObject) func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool func km_controlTextDidEndEditing(textField: AnyObject) func km_didBecomeFirstResponder(textField: AnyObject) } extension KMTextFieldDelegate { func km_controlTextShouldBeginEditing(textField: AnyObject) -> Bool { return true } func km_controlTextDidBeginEditing(textField: AnyObject) {} func km_controlTextDidChange(textField: AnyObject) {} func km_controlTextShouldEndEditing(textField: AnyObject) -> Bool { return true } func km_controlTextDidEndEditing(textField: AnyObject) {} func km_didBecomeFirstResponder(textField: AnyObject) {} } class KMTextField: NSTextField { private class _KMTextFieldCell: NSTextFieldCell { // 边框粗细 var borderThickness: CGFloat = 1 // 左右两边缩进 var offset: CGFloat = 8.0 // override func drawingRect(forBounds theRect: NSRect) -> NSRect { // var newRect = super.drawingRect(forBounds: theRect) // // let textSize = self.cellSize(forBounds: theRect) // let heightInset = newRect.size.height - textSize.height // if (heightInset > 0) { // newRect.size.height = textSize.height // newRect.origin.y += heightInset * 0.5 // } else { // newRect.size.height = textSize.height // newRect.origin.y += heightInset // } // // newRect.origin.x += self.offset // newRect.size.width = theRect.width - self.offset * 2 // // return newRect // } override func draw(withFrame cellFrame: NSRect, in controlView: NSView) { // Area that covers the NSTextField itself. That is the total height minus our custom border size. let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - self.borderThickness) let path = NSBezierPath() path.lineWidth = self.borderThickness // Line width is at the center of the line. path.move(to: NSPoint(x: 0, y: cellFrame.height)) path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height)) path.line(to: NSPoint(x: cellFrame.width, y: 0)) path.line(to: NSPoint(x: 0, y: 0)) NSColor.clear.setStroke() path.stroke() // Pass in area minus the border thickness in the height drawInterior(withFrame: interiorFrame, in: controlView) } } var borderThickness: CGFloat = 1 { didSet { if let _cell = self.cell as? _KMTextFieldCell { _cell.borderThickness = self.borderThickness } self.needsDisplay = true } } var offset: CGFloat = 8.0 { didSet { if let _cell = self.cell as? _KMTextFieldCell { _cell.offset = self.offset } self.needsDisplay = true } } var enterKeyEndEdit = false override init(frame frameRect: NSRect) { super.init(frame: frameRect) // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result { // self.cell = _KMTextFieldCell() // } } var firstResponderHandler: ((Bool)->Void)? required init?(coder: NSCoder) { super.init(coder: coder) // if let result = self.cell?.isKind(of: _KMTextFieldCell.self), !result { // self.cell = _KMTextFieldCell() // } } override class var cellClass: AnyClass? { set { super.cellClass = newValue } get { return _KMTextFieldCell.self } } override func becomeFirstResponder() -> Bool { let result = super.becomeFirstResponder() guard let callback = self.firstResponderHandler else { return result } callback(result) return result } }