KMStepperView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KMStepperView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/1/30.
  6. //
  7. import Cocoa
  8. protocol KMStepperViewDelegate: NSObjectProtocol {
  9. func km_controlTextDidEndEditing(_ obj: KMStepperView)
  10. func km_controlTextDidChange(_ obj: KMStepperView)
  11. func km_controlClick(_ obj: KMStepperView)
  12. }
  13. class KMStepperView: NSViewController {
  14. @IBOutlet weak var mainBox : NSBox!
  15. @IBOutlet weak var upButton : NSButton!
  16. @IBOutlet weak var downButton : NSButton!
  17. @IBOutlet weak var textfield : NSTextField!
  18. @IBOutlet weak var horizontalLine : NSView!
  19. @IBOutlet weak var verticalLine : NSView!
  20. @IBOutlet weak var mainBoxHeight : NSLayoutConstraint! // mainBox 高度约束
  21. @IBOutlet weak var mainBoxWidth : NSLayoutConstraint! // mainBox 宽度约束
  22. @IBOutlet weak var upButtonWidth : NSLayoutConstraint! // upButton 宽度约束
  23. @IBOutlet weak var downButtonWidth : NSLayoutConstraint! // downButton 宽度约束
  24. @IBOutlet weak var horizontalLine_mainBox_spacing : NSLayoutConstraint! // horizontalLine、mainBox 水平间距约束
  25. @IBOutlet weak var horizontalLineHeight : NSLayoutConstraint! // horizontalLine 高度约束
  26. @IBOutlet weak var horizontalLineWidth : NSLayoutConstraint! // horizontalLine 宽度约束
  27. @IBOutlet weak var mainBox_textfield_spacing : NSLayoutConstraint! // mainBox、textfield 水平间距约束
  28. @IBOutlet weak var mainBox_verticalLine_spacing : NSLayoutConstraint! // mainBox、verticalLine 垂直间距约束
  29. @IBOutlet weak var textfield_button_spacing : NSLayoutConstraint! // textfield、button 水平间距约束
  30. @IBOutlet weak var verticalLine_mainBox_spacing : NSLayoutConstraint! // verticalLine、mainBox 垂直间距约束
  31. @IBOutlet weak var verticalLineWidth : NSLayoutConstraint! // verticalLine 宽度约束
  32. var buttonWidth = 20.0 // upButtonWidth、downButtonWidth 宽度
  33. var horizontalLine_mainBox = 0.0 // horizontalLine、mainBox 水平间距
  34. var hLineHeight = 1.0 // horizontalLine 高度
  35. var hLineWidth = 20.0 // horizontalLine 宽度
  36. var mainBox_textfield = 8.0 // mainBox、textfield 水平间距
  37. var mainBox_verticalLine = 0.0 // mainBox、verticalLine 垂直间距
  38. var textfield_button = 8.0 // textfield、button 水平间距
  39. var verticalLine_mainBox = 0.0 // verticalLine、mainBox 垂直间距
  40. var vLineWidth = 1.0 // verticalLine 宽度
  41. var height: Float = 32.0
  42. var width: Float = 72.0
  43. var text: String = "0"
  44. var textColor: NSColor = .black
  45. var font: NSFont = NSFont.systemFont(ofSize: 14.0)
  46. var background: NSColor = .clear
  47. var borderWidth: Float = 0.0
  48. var borderColor: NSColor = .clear
  49. var cornerRadius: Float = 0.0
  50. var upImage: NSImage = NSImage(named: "btn_arrow_gray_up_s_norm_on") ?? NSImage()
  51. var downImage: NSImage = NSImage(named: "btn_arrow_gray_down_s_norm_on") ?? NSImage()
  52. open weak var delete: KMStepperViewDelegate?
  53. var enabled: Bool = true // 是否可点击
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. // Do view setup here.
  57. horizontalLine.wantsLayer = true
  58. horizontalLine.layer?.backgroundColor = NSColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor
  59. verticalLine.wantsLayer = true
  60. verticalLine.layer?.backgroundColor = NSColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor
  61. updateUI()
  62. }
  63. // MARK: Private Methods
  64. func updateUI() -> Void {
  65. mainBox.fillColor = background
  66. mainBox.borderWidth = CGFloat(borderWidth)
  67. mainBox.cornerRadius = CGFloat(cornerRadius)
  68. mainBox.borderColor = borderColor
  69. textfield.stringValue = text
  70. textfield.textColor = textColor
  71. textfield.font = font
  72. upButton.image = upImage
  73. downButton.image = downImage
  74. upButton.isEnabled = enabled
  75. downButton.isEnabled = enabled
  76. textfield.isEnabled = enabled
  77. mainBoxHeight.constant = CGFloat(height)
  78. mainBoxWidth.constant = CGFloat(width)
  79. upButtonWidth.constant = buttonWidth
  80. downButtonWidth.constant = buttonWidth
  81. horizontalLine_mainBox_spacing.constant = horizontalLine_mainBox
  82. horizontalLineHeight.constant = hLineHeight
  83. horizontalLineWidth.constant = hLineWidth
  84. mainBox_textfield_spacing.constant = mainBox_textfield
  85. mainBox_verticalLine_spacing.constant = mainBox_verticalLine
  86. textfield_button_spacing.constant = textfield_button
  87. verticalLine_mainBox_spacing.constant = verticalLine_mainBox
  88. verticalLineWidth.constant = vLineWidth
  89. }
  90. // MARK: Action
  91. @IBAction func upButtonAction(_ sender: NSButton) {
  92. var float = text.stringToCGFloat()
  93. float += 0.1
  94. text = String(format: "%.2f", float)
  95. self.delete?.km_controlClick(self)
  96. }
  97. @IBAction func downButtonAction(_ sender: NSButton) {
  98. var float = text.stringToCGFloat()
  99. float -= 0.1
  100. text = String(format: "%.2f", float)
  101. self.delete?.km_controlClick(self)
  102. }
  103. }
  104. extension KMStepperView: NSTextFieldDelegate {
  105. func controlTextDidEndEditing(_ obj: Notification) {
  106. let object = obj.object as! NSTextField
  107. text = object.stringValue
  108. self.delete?.km_controlTextDidEndEditing(self)
  109. }
  110. func controlTextDidChange(_ obj: Notification) {
  111. let object = obj.object as! NSTextField
  112. text = object.stringValue
  113. self.delete?.km_controlTextDidChange(self)
  114. }
  115. }