KMNSearchReplaceItemView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // KMImagenameBotaSearchInputPrefiex
  36. view.properties = prop
  37. view.delegate = self
  38. return view
  39. }()
  40. private lazy var replaceAllButton_: ComponentButton = {
  41. let view = ComponentButton()
  42. let prop = ComponentButtonProperty()
  43. prop.type = .default_tertiary
  44. prop.size = .xxs
  45. prop.buttonText = KMLocalizedString("Replace All")
  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. view.properties = prop
  57. view.setTarget(self, action: #selector(replaceAction))
  58. return view
  59. }()
  60. var input: ComponentInput {
  61. get {
  62. return input_
  63. }
  64. }
  65. var replaceAllButton: ComponentButton {
  66. get {
  67. return replaceAllButton_
  68. }
  69. }
  70. var replaceButton: ComponentButton {
  71. get {
  72. return replaceButton_
  73. }
  74. }
  75. var inputValue: String {
  76. get {
  77. return input.properties.text
  78. }
  79. set {
  80. input.properties.text = newValue
  81. input.reloadData()
  82. }
  83. }
  84. var itemClick: KMCommonClickBlock?
  85. var valueDidChange: KMValueDidChangeBlock?
  86. var inputDidEditBlock: KMEmptyBlock?
  87. convenience init() {
  88. self.init(frame: .init(x: 0, y: 0, width: 300, height: 60))
  89. initSubviews()
  90. }
  91. override func awakeFromNib() {
  92. super.awakeFromNib()
  93. initSubviews()
  94. }
  95. func initSubviews() {
  96. addSubview(input_)
  97. addSubview(replaceAllButton_)
  98. addSubview(replaceButton_)
  99. input_.km_add_leading_constraint(constant: 12)
  100. input_.km_add_top_constraint(constant: 8)
  101. input_.km_add_trailing_constraint(constant: -12)
  102. input_.km_add_height_constraint(constant: 32)
  103. replaceButton_.km_add_height_constraint(constant: 24)
  104. replaceButton_.km_add_trailing_constraint(constant: -12)
  105. replaceButton_.km_add_bottom_constraint()
  106. replaceButton_.km_add_width_constraint(constant: replaceButton_.properties.propertyInfo.viewWidth)
  107. replaceAllButton_.km_add_height_constraint(constant: 24)
  108. replaceAllButton_.km_add_trailing_constraint(equalTo: replaceButton_, attribute: .leading, constant: -8)
  109. replaceAllButton_.km_add_bottom_constraint()
  110. replaceAllButton_.km_add_width_constraint(constant: replaceAllButton_.properties.propertyInfo.viewWidth)
  111. }
  112. @objc func replaceAllAction() {
  113. itemClick?(1)
  114. }
  115. @objc func replaceAction() {
  116. itemClick?(2)
  117. }
  118. }
  119. extension KMNSearchReplaceItemView: ComponentInputDelegate {
  120. func componentInputDidChanged(inputView: ComponentInput) {
  121. valueDidChange?(inputView.properties.text, nil)
  122. }
  123. func componentInputDidEndEditing(inputView: ComponentInput) {
  124. inputDidEditBlock?()
  125. }
  126. }