123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // KMStepperView.swift
- // PDF Reader Pro
- //
- // 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") ?? NSImage()
- var downImage: NSImage = NSImage(named: "btn_arrow_gray_down_s_norm_on") ?? NSImage()
- 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)
- }
- }
|