KMNSearchReplaceSearchItemView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // KMNSearchReplaceSearchItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/3.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNSearchReplaceSearchItemView: NSView {
  10. private lazy var previousButton_: ComponentButton = {
  11. let view = ComponentButton()
  12. view.properties = ComponentButtonProperty(type: .gray, size: .s, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchPreviousDisable"))
  13. view.setTarget(self, action: #selector(previousAction))
  14. return view
  15. }()
  16. private lazy var nextButton_: ComponentButton = {
  17. let view = ComponentButton()
  18. view.properties = ComponentButtonProperty(type: .gray, size: .s, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchNext"))
  19. view.setTarget(self, action: #selector(nextActon))
  20. return view
  21. }()
  22. private lazy var input_: ComponentInput = {
  23. let view = ComponentInput()
  24. let prop = ComponentInputProperty()
  25. prop.size = .s
  26. prop.showPrefix = true
  27. prop.showSuffix = true
  28. view.properties = prop
  29. view.delegate = self
  30. return view
  31. }()
  32. var input: ComponentInput {
  33. get {
  34. return input_
  35. }
  36. }
  37. var inputValue: String {
  38. set {
  39. input_.properties.text = newValue
  40. input_.reloadData()
  41. }
  42. get {
  43. return input_.properties.text
  44. }
  45. }
  46. var previousButton: ComponentButton {
  47. get {
  48. return previousButton_
  49. }
  50. }
  51. var nextButton: ComponentButton {
  52. get {
  53. return nextButton_
  54. }
  55. }
  56. var itemClick: KMCommonClickBlock?
  57. var valueDidChange: KMValueDidChangeBlock?
  58. var inputDidEditBlock: KMEmptyBlock?
  59. convenience init() {
  60. self.init(frame: .init(x: 0, y: 0, width: 300, height: 44))
  61. }
  62. override func awakeFromNib() {
  63. super.awakeFromNib()
  64. initSubviews()
  65. }
  66. override init(frame frameRect: NSRect) {
  67. super.init(frame: frameRect)
  68. initSubviews()
  69. }
  70. required init?(coder: NSCoder) {
  71. super.init(coder: coder)
  72. initSubviews()
  73. }
  74. func initSubviews() {
  75. addSubview(nextButton_)
  76. addSubview(previousButton_)
  77. addSubview(input_)
  78. nextButton_.km_add_size_constraint(size: .init(width: 32, height: 32))
  79. nextButton_.km_add_top_constraint(constant: 0)
  80. nextButton_.km_add_trailing_constraint(constant: -24)
  81. previousButton_.km_add_size_constraint(size: .init(width: 32, height: 32))
  82. previousButton_.km_add_top_constraint(constant: 0)
  83. previousButton_.km_add_trailing_constraint(equalTo: nextButton_, attribute: .leading, constant: -8)
  84. input_.km_add_top_constraint(constant: 0)
  85. input_.km_add_leading_constraint(constant: 24)
  86. input_.km_add_height_constraint(constant: 32)
  87. input_.km_add_trailing_constraint(equalTo: previousButton_, attribute: .leading, constant: -8)
  88. }
  89. @objc func previousAction() {
  90. itemClick?(1)
  91. }
  92. @objc func nextActon() {
  93. itemClick?(2)
  94. }
  95. }
  96. extension KMNSearchReplaceSearchItemView: ComponentInputDelegate {
  97. func componentInputDidChanged(inputView: ComponentInput) {
  98. valueDidChange?(inputView.properties.text, nil)
  99. }
  100. func componentInputDidEndEditing(inputView: ComponentInput) {
  101. inputDidEditBlock?()
  102. }
  103. func componentInputDidLeftIconClicked(inputView: ComponentInput) {
  104. itemClick?(3)
  105. }
  106. }