KMNSearchReplaceItemView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // KMNSearchReplaceItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNSearchReplacePopItemView: KMNSearchReplaceItemView {
  10. override func initSubviews() {
  11. addSubview(input)
  12. addSubview(replaceAllButton)
  13. addSubview(replaceButton)
  14. input.km_add_leading_constraint(constant: 12)
  15. input.km_add_top_constraint(constant: 0)
  16. input.km_add_trailing_constraint(constant: -12)
  17. input.km_add_height_constraint(constant: 32)
  18. replaceButton.km_add_height_constraint(constant: 32)
  19. replaceButton.km_add_trailing_constraint(constant: -12)
  20. replaceButton.km_add_bottom_constraint()
  21. replaceButton.km_add_width_constraint(constant: replaceButton.properties.propertyInfo.viewWidth)
  22. replaceAllButton.km_add_height_constraint(constant: 32)
  23. replaceAllButton.km_add_trailing_constraint(equalTo: replaceButton, attribute: .leading, constant: -8)
  24. replaceAllButton.km_add_bottom_constraint()
  25. replaceAllButton.km_add_width_constraint(constant: replaceAllButton.properties.propertyInfo.viewWidth)
  26. }
  27. }
  28. class KMNSearchReplaceItemView: NSView {
  29. private lazy var input_: ComponentInput = {
  30. let view = ComponentInput()
  31. let prop = ComponentInputProperty()
  32. prop.size = .s
  33. prop.showPrefix = true
  34. prop.placeholder = KMLocalizedString("Replace with...")
  35. view.properties = prop
  36. view.delegate = self
  37. return view
  38. }()
  39. private lazy var replaceAllButton_: ComponentButton = {
  40. let view = ComponentButton()
  41. let prop = ComponentButtonProperty()
  42. prop.type = .default_tertiary
  43. prop.size = .xxs
  44. prop.buttonText = KMLocalizedString("Replace All")
  45. prop.keepPressState = false
  46. view.properties = prop
  47. view.setTarget(self, action: #selector(replaceAllAction))
  48. return view
  49. }()
  50. private lazy var replaceButton_: ComponentButton = {
  51. let view = ComponentButton()
  52. let prop = ComponentButtonProperty()
  53. prop.type = .primary
  54. prop.size = .xxs
  55. prop.buttonText = KMLocalizedString("Replace")
  56. prop.keepPressState = false
  57. view.properties = prop
  58. view.setTarget(self, action: #selector(replaceAction))
  59. return view
  60. }()
  61. var input: ComponentInput {
  62. get {
  63. return input_
  64. }
  65. }
  66. var replaceAllButton: ComponentButton {
  67. get {
  68. return replaceAllButton_
  69. }
  70. }
  71. var replaceButton: ComponentButton {
  72. get {
  73. return replaceButton_
  74. }
  75. }
  76. var inputValue: String {
  77. get {
  78. return input.properties.text
  79. }
  80. set {
  81. input.properties.text = newValue
  82. input.reloadData()
  83. }
  84. }
  85. var itemClick: KMCommonClickBlock?
  86. var valueDidChange: KMValueDidChangeBlock?
  87. var inputDidEditBlock: KMEmptyBlock?
  88. convenience init() {
  89. self.init(frame: .init(x: 0, y: 0, width: 300, height: 60))
  90. initSubviews()
  91. }
  92. override func awakeFromNib() {
  93. super.awakeFromNib()
  94. initSubviews()
  95. }
  96. func initSubviews() {
  97. addSubview(input_)
  98. addSubview(replaceAllButton_)
  99. addSubview(replaceButton_)
  100. input_.km_add_leading_constraint(constant: 12)
  101. input_.km_add_top_constraint(constant: 8)
  102. input_.km_add_trailing_constraint(constant: -12)
  103. input_.km_add_height_constraint(constant: 32)
  104. replaceButton_.km_add_height_constraint(constant: 24)
  105. replaceButton_.km_add_trailing_constraint(constant: -12)
  106. replaceButton_.km_add_bottom_constraint()
  107. replaceButton_.km_add_width_constraint(constant: replaceButton_.properties.propertyInfo.viewWidth)
  108. replaceAllButton_.km_add_height_constraint(constant: 24)
  109. replaceAllButton_.km_add_trailing_constraint(equalTo: replaceButton_, attribute: .leading, constant: -8)
  110. replaceAllButton_.km_add_bottom_constraint()
  111. replaceAllButton_.km_add_width_constraint(constant: replaceAllButton_.properties.propertyInfo.viewWidth)
  112. }
  113. @objc func replaceAllAction() {
  114. itemClick?(1)
  115. }
  116. @objc func replaceAction() {
  117. itemClick?(2)
  118. }
  119. }
  120. extension KMNSearchReplaceItemView: ComponentInputDelegate {
  121. func componentInputDidChanged(inputView: ComponentInput) {
  122. valueDidChange?(inputView.properties.text, nil)
  123. }
  124. func componentInputDidEndEditing(inputView: ComponentInput) {
  125. inputDidEditBlock?()
  126. }
  127. }