KMWatermarkAdjectiveStepper.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMWatermarkAdjectiveStepper.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/18.
  6. //
  7. import Cocoa
  8. typealias KMWatermarkAdjectiveStepperValueDidChange = (_ value: Double) -> ()
  9. class KMWatermarkAdjectiveStepper: NSView {
  10. var textField = NSTextField()
  11. var stepper = NSStepper()
  12. var value: Double {
  13. get {
  14. return textField.doubleValue
  15. }
  16. set {
  17. textField.doubleValue = newValue
  18. stepper.doubleValue = newValue
  19. }
  20. }
  21. var enabled: Bool {
  22. get {
  23. return textField.isEnabled
  24. }
  25. set {
  26. textField.isEnabled = newValue
  27. textField.isEditable = newValue
  28. stepper.isEnabled = newValue
  29. }
  30. }
  31. var valueDidChange: KMWatermarkAdjectiveStepperValueDidChange!
  32. override init(frame frameRect: NSRect) {
  33. super.init(frame: frameRect)
  34. initSubViews()
  35. }
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. initSubViews()
  39. }
  40. func initSubViews() {
  41. addSubview(textField)
  42. addSubview(stepper)
  43. textField.focusRingType = .none
  44. textField.doubleValue = 0
  45. textField.delegate = self
  46. textField.backgroundColor = NSColor.clear
  47. textField.cell?.isBordered = false
  48. // textField.formatter = TextFieldFormatter()
  49. stepper.target = self
  50. stepper.action = #selector(valueChanged)
  51. stepper.doubleValue = 0.0
  52. stepper.minValue = -1000
  53. stepper.maxValue = 1000
  54. stepper.increment = 1
  55. // stepper.appearance = NSAppearance(named: .aqua)
  56. }
  57. override func layout() {
  58. super.layout()
  59. let width: CGFloat = NSWidth(self.bounds)
  60. let height: CGFloat = NSHeight(self.bounds)
  61. let stepperWidth: CGFloat = 12
  62. stepper.frame = NSMakeRect(width-stepperWidth,0, stepperWidth, height)
  63. let textFieldX: CGFloat = 4
  64. let textFieldHeight: CGFloat = 20
  65. textField.frame = NSMakeRect(textFieldX, (height-textFieldHeight)*0.5, width-stepperWidth-textFieldX, textFieldHeight)
  66. }
  67. @objc func valueChanged(sender: NSStepper) {
  68. textField.doubleValue = sender.doubleValue
  69. guard let callback = valueDidChange else {
  70. return
  71. }
  72. callback(textField.doubleValue)
  73. }
  74. }
  75. extension KMWatermarkAdjectiveStepper: NSTextFieldDelegate {
  76. func controlTextDidChange(_ obj: Notification) {
  77. if (textField.isEqual(to: obj.object)) {
  78. var value: Double = textField.doubleValue
  79. if (textField.doubleValue < stepper.minValue) {
  80. value = stepper.minValue
  81. }
  82. if (textField.doubleValue > stepper.maxValue) {
  83. value = stepper.maxValue
  84. }
  85. stepper.doubleValue = value
  86. // DispatchQueue.main.async {
  87. // self.textField.doubleValue = value
  88. // }
  89. guard let callback = valueDidChange else {
  90. return
  91. }
  92. callback(stepper.doubleValue)
  93. }
  94. }
  95. }