KMNBotaSearchTopView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. var inputValue: String? {
  78. get {
  79. return searchInput_.stringValue
  80. }
  81. set {
  82. searchInput_.stringValue = newValue ?? ""
  83. }
  84. }
  85. var replaceText: String? {
  86. get {
  87. return centerView_.inputValue
  88. }
  89. set {
  90. centerView_.inputValue = newValue ?? ""
  91. }
  92. }
  93. override func draw(_ dirtyRect: NSRect) {
  94. super.draw(dirtyRect)
  95. // Drawing code here.
  96. }
  97. override func awakeFromNib() {
  98. super.awakeFromNib()
  99. initSubviews()
  100. }
  101. func initSubviews() {
  102. topBox.borderWidth = 0
  103. topBox.contentView?.addSubview(searchButton_)
  104. searchButton_.km_add_leading_constraint(constant: 12)
  105. searchButton_.km_add_size_constraint(size: NSSize(width: 16, height: 16))
  106. searchButton_.km_add_centerY_constraint()
  107. topBox.contentView?.addSubview(searchInput_)
  108. searchInput_.km_add_leading_constraint(equalTo: searchButton_, attribute: .trailing, constant: 8)
  109. searchInput_.km_add_centerY_constraint()
  110. searchInput_.km_add_height_constraint(constant: 20)
  111. searchInput_.km_add_trailing_constraint(constant: -76)
  112. topBox.contentView?.addSubview(switchButton_)
  113. switchButton_.km_add_trailing_constraint(constant: -12)
  114. switchButton_.km_add_centerY_constraint()
  115. switchButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  116. topBox.contentView?.addSubview(replaceButton_)
  117. replaceButton_.km_add_trailing_constraint(equalTo: switchButton_, attribute: .leading, constant: -8)
  118. replaceButton_.km_add_centerY_constraint()
  119. replaceButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  120. centerBox.isHidden = true
  121. centerBox.borderWidth = 0
  122. centerBox.contentView = centerView_
  123. bottomBox.isHidden = true
  124. bottomBox.borderWidth = 0
  125. bottomBox.contentView?.addSubview(resultLabel_)
  126. resultLabel_.km_add_top_constraint(constant: 16)
  127. resultLabel_.km_add_leading_constraint(constant: 16)
  128. bottomBox.contentView?.addSubview(nextButton_)
  129. nextButton_.km_add_trailing_constraint(constant: -16)
  130. nextButton_.km_add_top_constraint(constant: 16)
  131. nextButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  132. bottomBox.contentView?.addSubview(previousButton_)
  133. previousButton_.km_add_trailing_constraint(equalTo: nextButton_, attribute: .leading, constant: -8)
  134. previousButton_.km_add_top_constraint(constant: 16)
  135. previousButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  136. }
  137. func showSearch() {
  138. centerBox.isHidden = true
  139. centerHeightConst.constant = 0
  140. }
  141. func showReplace() {
  142. centerBox.isHidden = false
  143. centerHeightConst.constant = centerHeight_
  144. }
  145. func showResult(type: KMNBotaSearchType) {
  146. bottomBox.isHidden = false
  147. }
  148. func hideResult(type: KMNBotaSearchType) {
  149. bottomBox.isHidden = true
  150. }
  151. func fetchContentHeight(type: KMNBotaSearchType, hasResult: Bool) -> CGFloat {
  152. var height: CGFloat = topHeight_
  153. if type == .search {
  154. } else if type == .replace {
  155. height += centerHeight_
  156. }
  157. height += (hasResult ? bottomHeight_+vSpace_ : 0)
  158. return height
  159. }
  160. // MARK: - Actions
  161. @objc func searchAction() {
  162. itemClick?(KMNBotaSearchTopItemKey.search.rawValue, searchButton_)
  163. }
  164. @objc func replaceAction() {
  165. itemClick?(KMNBotaSearchTopItemKey.replace.rawValue)
  166. }
  167. @objc func switchAction() {
  168. itemClick?(KMNBotaSearchTopItemKey.switch.rawValue)
  169. }
  170. @objc func previousAction() {
  171. itemClick?(KMNBotaSearchTopItemKey.previous.rawValue)
  172. }
  173. @objc func nextActon() {
  174. itemClick?(KMNBotaSearchTopItemKey.next.rawValue)
  175. }
  176. }
  177. extension KMNBotaSearchTopView: NSTextFieldDelegate {
  178. func controlTextDidChange(_ obj: Notification) {
  179. if searchInput_.isEqual(to: obj.object) {
  180. valueDidChange?(searchInput_, [.newKey : searchInput_.stringValue])
  181. }
  182. }
  183. }