KMNBotaSearchTopView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // KMNBotaSearchTopVIew.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/1.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. enum KMNBotaSearchTopItemKey: Int {
  10. case search = 1
  11. case replace = 2
  12. case `switch` = 3
  13. case previous = 4
  14. case next = 5
  15. }
  16. class KMNBotaSearchTopView: NSView, NibLoadable {
  17. @IBOutlet weak var topBox: NSBox!
  18. @IBOutlet weak var centerBox: NSBox!
  19. @IBOutlet weak var bottomBox: NSBox!
  20. @IBOutlet weak var centerHeightConst: NSLayoutConstraint!
  21. var itemClick: KMCommonClickBlock?
  22. var valueDidChange: KMValueDidChangeBlock?
  23. private var topHeight_: CGFloat = 40
  24. private var centerHeight_ : CGFloat = 72
  25. private var bottomHeight_: CGFloat = 32
  26. private var vSpace_: CGFloat = 8
  27. private lazy var searchButton_: ComponentButton = {
  28. let view = ComponentButton()
  29. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearch"))
  30. view.setTarget(self, action: #selector(searchAction))
  31. return view
  32. }()
  33. private lazy var searchInput_: NSTextField = {
  34. let view = NSTextField()
  35. view.placeholderString = KMLocalizedString("Search (⌥⌘F)")
  36. view.delegate = self
  37. return view
  38. }()
  39. private lazy var replaceButton_: ComponentButton = {
  40. let view = ComponentButton()
  41. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchReplace"))
  42. view.setTarget(self, action: #selector(replaceAction))
  43. return view
  44. }()
  45. private lazy var switchButton_: ComponentButton = {
  46. let view = ComponentButton()
  47. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchSwitch"))
  48. view.setTarget(self, action: #selector(switchAction))
  49. return view
  50. }()
  51. private lazy var centerView_: KMNSearchReplaceItemView = {
  52. let view = KMNSearchReplaceItemView()
  53. return view
  54. }()
  55. private lazy var resultLabel_: NSTextField = {
  56. let view = NSTextField(labelWithString: "")
  57. view.font = ComponentLibrary.shared.getFontFromKey("mac/body-xs-medium")
  58. return view
  59. }()
  60. private lazy var previousButton_: ComponentButton = {
  61. let view = ComponentButton()
  62. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchPreviousDisable"))
  63. view.setTarget(self, action: #selector(previousAction))
  64. return view
  65. }()
  66. private lazy var nextButton_: ComponentButton = {
  67. let view = ComponentButton()
  68. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchNext"))
  69. view.setTarget(self, action: #selector(nextActon))
  70. return view
  71. }()
  72. var resultLabel: NSTextField {
  73. get {
  74. return resultLabel_
  75. }
  76. }
  77. override func draw(_ dirtyRect: NSRect) {
  78. super.draw(dirtyRect)
  79. // Drawing code here.
  80. }
  81. override func awakeFromNib() {
  82. super.awakeFromNib()
  83. initSubviews()
  84. }
  85. func initSubviews() {
  86. topBox.borderWidth = 0
  87. topBox.contentView?.addSubview(searchButton_)
  88. searchButton_.km_add_leading_constraint(constant: 12)
  89. searchButton_.km_add_size_constraint(size: NSSize(width: 16, height: 16))
  90. searchButton_.km_add_centerY_constraint()
  91. topBox.contentView?.addSubview(searchInput_)
  92. searchInput_.km_add_leading_constraint(equalTo: searchButton_, attribute: .trailing, constant: 8)
  93. searchInput_.km_add_centerY_constraint()
  94. searchInput_.km_add_height_constraint(constant: 20)
  95. searchInput_.km_add_trailing_constraint(constant: -76)
  96. topBox.contentView?.addSubview(switchButton_)
  97. switchButton_.km_add_trailing_constraint(constant: -12)
  98. switchButton_.km_add_centerY_constraint()
  99. switchButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  100. topBox.contentView?.addSubview(replaceButton_)
  101. replaceButton_.km_add_trailing_constraint(equalTo: switchButton_, attribute: .leading, constant: -8)
  102. replaceButton_.km_add_centerY_constraint()
  103. replaceButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  104. centerBox.isHidden = true
  105. centerBox.borderWidth = 0
  106. centerBox.contentView = centerView_
  107. bottomBox.isHidden = true
  108. bottomBox.borderWidth = 0
  109. bottomBox.contentView?.addSubview(resultLabel_)
  110. resultLabel_.km_add_top_constraint(constant: 16)
  111. resultLabel_.km_add_leading_constraint(constant: 16)
  112. bottomBox.contentView?.addSubview(nextButton_)
  113. nextButton_.km_add_trailing_constraint(constant: -16)
  114. nextButton_.km_add_top_constraint(constant: 16)
  115. nextButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  116. bottomBox.contentView?.addSubview(previousButton_)
  117. previousButton_.km_add_trailing_constraint(equalTo: nextButton_, attribute: .leading, constant: -8)
  118. previousButton_.km_add_top_constraint(constant: 16)
  119. previousButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  120. }
  121. func showSearch() {
  122. centerBox.isHidden = true
  123. centerHeightConst.constant = 0
  124. }
  125. func showReplace() {
  126. centerBox.isHidden = false
  127. centerHeightConst.constant = centerHeight_
  128. }
  129. func showResult(type: KMNBotaSearchType) {
  130. bottomBox.isHidden = false
  131. }
  132. func hideResult(type: KMNBotaSearchType) {
  133. bottomBox.isHidden = true
  134. }
  135. func fetchContentHeight(type: KMNBotaSearchType, hasResult: Bool) -> CGFloat {
  136. var height: CGFloat = topHeight_
  137. if type == .search {
  138. } else if type == .replace {
  139. height += centerHeight_
  140. }
  141. height += (hasResult ? bottomHeight_+vSpace_ : 0)
  142. return height
  143. }
  144. // MARK: - Actions
  145. @objc func searchAction() {
  146. itemClick?(KMNBotaSearchTopItemKey.search.rawValue, searchButton_)
  147. }
  148. @objc func replaceAction() {
  149. itemClick?(KMNBotaSearchTopItemKey.replace.rawValue)
  150. }
  151. @objc func switchAction() {
  152. itemClick?(KMNBotaSearchTopItemKey.switch.rawValue)
  153. }
  154. @objc func previousAction() {
  155. itemClick?(KMNBotaSearchTopItemKey.previous.rawValue)
  156. }
  157. @objc func nextActon() {
  158. itemClick?(KMNBotaSearchTopItemKey.next.rawValue)
  159. }
  160. }
  161. extension KMNBotaSearchTopView: NSTextFieldDelegate {
  162. func controlTextDidChange(_ obj: Notification) {
  163. if searchInput_.isEqual(to: obj.object) {
  164. valueDidChange?(searchInput_, [.newKey : searchInput_.stringValue])
  165. }
  166. }
  167. }