// // KMStepperView.swift // PDF Master // // Created by wanjun on 2023/1/30. // import Cocoa protocol KMStepperViewDelegate: NSObjectProtocol { func km_controlTextDidEndEditing(_ obj: KMStepperView) func km_controlTextDidChange(_ obj: KMStepperView) func km_controlClick(_ obj: KMStepperView) } class KMStepperView: NSViewController { @IBOutlet weak var mainBox : NSBox! @IBOutlet weak var upButton : NSButton! @IBOutlet weak var downButton : NSButton! @IBOutlet weak var textfield : NSTextField! @IBOutlet weak var horizontalLine : NSView! @IBOutlet weak var verticalLine : NSView! @IBOutlet weak var mainBoxHeight : NSLayoutConstraint! // mainBox 高度约束 @IBOutlet weak var mainBoxWidth : NSLayoutConstraint! // mainBox 宽度约束 @IBOutlet weak var upButtonWidth : NSLayoutConstraint! // upButton 宽度约束 @IBOutlet weak var downButtonWidth : NSLayoutConstraint! // downButton 宽度约束 @IBOutlet weak var horizontalLine_mainBox_spacing : NSLayoutConstraint! // horizontalLine、mainBox 水平间距约束 @IBOutlet weak var horizontalLineHeight : NSLayoutConstraint! // horizontalLine 高度约束 @IBOutlet weak var horizontalLineWidth : NSLayoutConstraint! // horizontalLine 宽度约束 @IBOutlet weak var mainBox_textfield_spacing : NSLayoutConstraint! // mainBox、textfield 水平间距约束 @IBOutlet weak var mainBox_verticalLine_spacing : NSLayoutConstraint! // mainBox、verticalLine 垂直间距约束 @IBOutlet weak var textfield_button_spacing : NSLayoutConstraint! // textfield、button 水平间距约束 @IBOutlet weak var verticalLine_mainBox_spacing : NSLayoutConstraint! // verticalLine、mainBox 垂直间距约束 @IBOutlet weak var verticalLineWidth : NSLayoutConstraint! // verticalLine 宽度约束 var buttonWidth = 20.0 // upButtonWidth、downButtonWidth 宽度 var horizontalLine_mainBox = 0.0 // horizontalLine、mainBox 水平间距 var hLineHeight = 1.0 // horizontalLine 高度 var hLineWidth = 20.0 // horizontalLine 宽度 var mainBox_textfield = 8.0 // mainBox、textfield 水平间距 var mainBox_verticalLine = 0.0 // mainBox、verticalLine 垂直间距 var textfield_button = 8.0 // textfield、button 水平间距 var verticalLine_mainBox = 0.0 // verticalLine、mainBox 垂直间距 var vLineWidth = 1.0 // verticalLine 宽度 var height: Float = 32.0 var width: Float = 72.0 var text: String = "0" var textColor: NSColor = .black var font: NSFont = NSFont.systemFont(ofSize: 14.0) var background: NSColor = .clear var borderWidth: Float = 0.0 var borderColor: NSColor = .clear var cornerRadius: Float = 0.0 var upImage: NSImage = NSImage(named: "btn_arrow_gray_up_s_norm_on")! var downImage: NSImage = NSImage(named: "btn_arrow_gray_down_s_norm_on")! open weak var delete: KMStepperViewDelegate? var enabled: Bool = true // 是否可点击 override func viewDidLoad() { super.viewDidLoad() // Do view setup here. horizontalLine.wantsLayer = true horizontalLine.layer?.backgroundColor = NSColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor verticalLine.wantsLayer = true verticalLine.layer?.backgroundColor = NSColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor updateUI() } // MARK: Private Methods func updateUI() -> Void { mainBox.fillColor = background mainBox.borderWidth = CGFloat(borderWidth) mainBox.cornerRadius = CGFloat(cornerRadius) mainBox.borderColor = borderColor textfield.stringValue = text textfield.textColor = textColor textfield.font = font upButton.image = upImage downButton.image = downImage upButton.isEnabled = enabled downButton.isEnabled = enabled textfield.isEnabled = enabled mainBoxHeight.constant = CGFloat(height) mainBoxWidth.constant = CGFloat(width) upButtonWidth.constant = buttonWidth downButtonWidth.constant = buttonWidth horizontalLine_mainBox_spacing.constant = horizontalLine_mainBox horizontalLineHeight.constant = hLineHeight horizontalLineWidth.constant = hLineWidth mainBox_textfield_spacing.constant = mainBox_textfield mainBox_verticalLine_spacing.constant = mainBox_verticalLine textfield_button_spacing.constant = textfield_button verticalLine_mainBox_spacing.constant = verticalLine_mainBox verticalLineWidth.constant = vLineWidth } // MARK: Action @IBAction func upButtonAction(_ sender: NSButton) { var float = text.stringToCGFloat() float += 0.1 text = String(format: "%.2f", float) self.delete?.km_controlClick(self) } @IBAction func downButtonAction(_ sender: NSButton) { var float = text.stringToCGFloat() float -= 0.1 text = String(format: "%.2f", float) self.delete?.km_controlClick(self) } } extension KMStepperView: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { let object = obj.object as! NSTextField text = object.stringValue self.delete?.km_controlTextDidEndEditing(self) } func controlTextDidChange(_ obj: Notification) { let object = obj.object as! NSTextField text = object.stringValue self.delete?.km_controlTextDidChange(self) } }