KMEraserController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // KMEraserController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMEraserController: NSViewController {
  10. @IBOutlet var eraserBGView: NSView!
  11. @IBOutlet var eraserLabel: NSTextField!
  12. @IBOutlet var eraserSlider: ComponentSlider!
  13. @IBOutlet var eraserSelect: ComponentSelect!
  14. //MARK: - func
  15. override func viewDidAppear() {
  16. super.viewDidAppear()
  17. eraserSlider.reloadData()
  18. }
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do view setup here.
  22. setupProperty()
  23. }
  24. func setupProperty() {
  25. eraserLabel.stringValue = KMLocalizedString("Line")
  26. eraserLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  27. eraserLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  28. eraserSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  29. eraserSlider.delegate = self
  30. eraserSelect.properties = ComponentSelectProperties(size: .s,
  31. state: .normal,
  32. creatable: true,
  33. text: "2",
  34. textUnit: " pt",
  35. regexString: "0123456789 pt")
  36. if true {
  37. var opacityItems: [ComponentMenuitemProperty] = []
  38. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  39. let item = ComponentMenuitemProperty(type: .normal, text: string)
  40. opacityItems.append(item)
  41. }
  42. eraserSelect.updateMenuItemsArr(opacityItems)
  43. }
  44. eraserSelect.delegate = self
  45. }
  46. func reloadData() {
  47. let value = CPDFAnnotationConfig.getDefaultFloatValue(forKey: CEraserkNoteLineWidthKey)
  48. let percent: CGFloat = (CGFloat(value) - 6)/12
  49. eraserSlider.properties.percent = percent
  50. eraserSlider.reloadData()
  51. eraserSelect.properties.text = String(format: "%.0f%@", CGFloat(value), " pt")
  52. eraserSelect.reloadData()
  53. }
  54. }
  55. //MARK: - ComponentSliderDelegate
  56. extension KMEraserController: ComponentSliderDelegate {
  57. func componentSliderDidUpdate(_ view: ComponentSlider) {
  58. if view == eraserSlider {
  59. let percent = view.properties.percent * 12 + 6
  60. CPDFAnnotationConfig.setDefaultFloatValue(percent, toKey: CEraserkNoteLineWidthKey)
  61. }
  62. reloadData()
  63. }
  64. }
  65. //MARK: - ComponentSelectDelegate
  66. extension KMEraserController: ComponentSelectDelegate {
  67. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  68. if let result = menuItemProperty?.text {
  69. let result = result.stringByDeleteCharString(view?.properties.textUnit ?? "")
  70. var value = result.stringToCGFloat()
  71. if value > 18 {
  72. value = 18
  73. } else if value < 1 {
  74. value = 1
  75. }
  76. CPDFAnnotationConfig.setDefaultFloatValue(value, toKey: CEraserkNoteLineWidthKey)
  77. }
  78. reloadData()
  79. }
  80. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  81. if let result = text {
  82. if view == eraserSelect {
  83. var value = result.stringToCGFloat()
  84. if value > 18 {
  85. value = 18
  86. } else if value < 1 {
  87. value = 1
  88. }
  89. CPDFAnnotationConfig.setDefaultFloatValue(value, toKey: CEraserkNoteLineWidthKey)
  90. }
  91. }
  92. reloadData()
  93. }
  94. }