KMNBotaSearchTopView.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. case replaceText
  16. case replaceAllText
  17. }
  18. class KMNBotaSearchTopView: NSView, NibLoadable {
  19. @IBOutlet weak var topBox: NSBox!
  20. @IBOutlet weak var centerBox: NSBox!
  21. @IBOutlet weak var bottomBox: NSBox!
  22. @IBOutlet weak var centerHeightConst: NSLayoutConstraint!
  23. var itemClick: KMCommonClickBlock?
  24. var valueDidChange: KMValueDidChangeBlock?
  25. private var topHeight_: CGFloat = 40
  26. private var centerHeight_ : CGFloat = 72
  27. private var bottomHeight_: CGFloat = 32
  28. private var vSpace_: CGFloat = 8
  29. private lazy var searchButton_: ComponentButton = {
  30. let view = ComponentButton()
  31. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearch"))
  32. view.setTarget(self, action: #selector(searchAction))
  33. return view
  34. }()
  35. private lazy var searchInput_: NSTextField = {
  36. let view = NSTextField()
  37. view.placeholderString = KMLocalizedString("Search (⌥⌘F)")
  38. view.isBordered = false
  39. view.drawsBackground = false
  40. view.delegate = self
  41. return view
  42. }()
  43. private lazy var replaceButton_: ComponentButton = {
  44. let view = ComponentButton()
  45. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchReplace"))
  46. view.setTarget(self, action: #selector(replaceAction))
  47. return view
  48. }()
  49. private lazy var switchButton_: ComponentButton = {
  50. let view = ComponentButton()
  51. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchSwitch"))
  52. view.setTarget(self, action: #selector(switchAction))
  53. return view
  54. }()
  55. private lazy var centerView_: KMNSearchReplaceItemView = {
  56. let view = KMNSearchReplaceItemView()
  57. return view
  58. }()
  59. private lazy var resultLabel_: NSTextField = {
  60. let view = NSTextField(labelWithString: "")
  61. view.font = ComponentLibrary.shared.getFontFromKey("mac/body-xs-medium")
  62. return view
  63. }()
  64. private lazy var previousButton_: ComponentButton = {
  65. let view = ComponentButton()
  66. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchPreviousDisable"))
  67. view.setTarget(self, action: #selector(previousAction))
  68. return view
  69. }()
  70. private lazy var nextButton_: ComponentButton = {
  71. let view = ComponentButton()
  72. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearchNext"))
  73. view.setTarget(self, action: #selector(nextActon))
  74. return view
  75. }()
  76. private lazy var bottomLine_: NSView = {
  77. let view = NSView()
  78. view.wantsLayer = true
  79. return view
  80. }()
  81. var searchInput: NSTextField {
  82. get {
  83. return searchInput_
  84. }
  85. }
  86. var replaceInput: ComponentInput {
  87. get {
  88. return centerView_.input
  89. }
  90. }
  91. var replaceButton: ComponentButton {
  92. get {
  93. return centerView_.replaceButton
  94. }
  95. }
  96. var replaceAllButton: ComponentButton {
  97. get {
  98. return centerView_.replaceAllButton
  99. }
  100. }
  101. var previousButton: ComponentButton {
  102. get {
  103. return previousButton_
  104. }
  105. }
  106. var nextButton: ComponentButton {
  107. get {
  108. return nextButton_
  109. }
  110. }
  111. var resultLabel: NSTextField {
  112. get {
  113. return resultLabel_
  114. }
  115. }
  116. var bottomLine: NSView {
  117. get {
  118. return bottomLine_
  119. }
  120. }
  121. var inputValue: String? {
  122. get {
  123. return searchInput_.stringValue
  124. }
  125. set {
  126. searchInput_.stringValue = newValue ?? ""
  127. }
  128. }
  129. var replaceText: String? {
  130. get {
  131. return centerView_.inputValue
  132. }
  133. set {
  134. centerView_.inputValue = newValue ?? ""
  135. }
  136. }
  137. override func draw(_ dirtyRect: NSRect) {
  138. super.draw(dirtyRect)
  139. // Drawing code here.
  140. }
  141. override func awakeFromNib() {
  142. super.awakeFromNib()
  143. initSubviews()
  144. }
  145. func initSubviews() {
  146. topBox.borderWidth = 0
  147. topBox.contentView?.addSubview(searchButton_)
  148. searchButton_.km_add_leading_constraint(constant: 12)
  149. searchButton_.km_add_size_constraint(size: NSSize(width: 16, height: 16))
  150. searchButton_.km_add_centerY_constraint()
  151. topBox.contentView?.addSubview(searchInput_)
  152. searchInput_.km_add_leading_constraint(equalTo: searchButton_, attribute: .trailing, constant: 8)
  153. searchInput_.km_add_centerY_constraint()
  154. searchInput_.km_add_height_constraint(constant: 20)
  155. searchInput_.km_add_trailing_constraint(constant: -76)
  156. topBox.contentView?.addSubview(switchButton_)
  157. switchButton_.km_add_trailing_constraint(constant: -12)
  158. switchButton_.km_add_centerY_constraint()
  159. switchButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  160. topBox.contentView?.addSubview(replaceButton_)
  161. replaceButton_.km_add_trailing_constraint(equalTo: switchButton_, attribute: .leading, constant: -8)
  162. replaceButton_.km_add_centerY_constraint()
  163. replaceButton_.km_add_size_constraint(size: NSSize(width: 24, height: 24))
  164. topBox.contentView?.addSubview(bottomLine_)
  165. bottomLine.km_add_leading_constraint()
  166. bottomLine.km_add_trailing_constraint()
  167. bottomLine.km_add_height_constraint(constant: 2)
  168. bottomLine.km_add_bottom_constraint()
  169. centerBox.isHidden = true
  170. centerBox.borderWidth = 0
  171. centerBox.contentView = centerView_
  172. centerView_.itemClick = { [unowned self] idx, _ in
  173. if idx == 1 {
  174. replaceAllButton.properties.state = .normal
  175. replaceAllButton.reloadData()
  176. itemClick?(KMNBotaSearchTopItemKey.replaceAllText.rawValue)
  177. } else if idx == 2 {
  178. replaceButton.properties.state = .normal
  179. replaceButton.reloadData()
  180. itemClick?(KMNBotaSearchTopItemKey.replaceText.rawValue)
  181. }
  182. }
  183. bottomBox.isHidden = true
  184. bottomBox.borderWidth = 0
  185. bottomBox.contentView?.addSubview(resultLabel_)
  186. resultLabel_.km_add_top_constraint(constant: 16)
  187. resultLabel_.km_add_leading_constraint(constant: 16)
  188. bottomBox.contentView?.addSubview(nextButton_)
  189. nextButton_.km_add_trailing_constraint(constant: -16)
  190. nextButton_.km_add_top_constraint(constant: 16)
  191. nextButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  192. bottomBox.contentView?.addSubview(previousButton_)
  193. previousButton_.km_add_trailing_constraint(equalTo: nextButton_, attribute: .leading, constant: -8)
  194. previousButton_.km_add_top_constraint(constant: 16)
  195. previousButton_.km_add_size_constraint(size: .init(width: 16, height: 16))
  196. }
  197. func showSearch() {
  198. centerBox.isHidden = true
  199. centerHeightConst.constant = 0
  200. }
  201. func showReplace() {
  202. centerBox.isHidden = false
  203. centerHeightConst.constant = centerHeight_
  204. }
  205. func showResult(type: KMNBotaSearchType) {
  206. bottomBox.isHidden = false
  207. }
  208. func hideResult(type: KMNBotaSearchType) {
  209. bottomBox.isHidden = true
  210. }
  211. func fetchContentHeight(type: KMNBotaSearchType, hasResult: Bool) -> CGFloat {
  212. var height: CGFloat = topHeight_
  213. if type == .search {
  214. } else if type == .replace {
  215. height += centerHeight_
  216. }
  217. height += (hasResult ? bottomHeight_+vSpace_ : 0)
  218. return height
  219. }
  220. // MARK: - Actions
  221. @objc func searchAction() {
  222. itemClick?(KMNBotaSearchTopItemKey.search.rawValue, searchButton_)
  223. }
  224. @objc func replaceAction() {
  225. itemClick?(KMNBotaSearchTopItemKey.replace.rawValue)
  226. }
  227. @objc func switchAction() {
  228. itemClick?(KMNBotaSearchTopItemKey.switch.rawValue)
  229. }
  230. @objc func previousAction() {
  231. itemClick?(KMNBotaSearchTopItemKey.previous.rawValue)
  232. }
  233. @objc func nextActon() {
  234. itemClick?(KMNBotaSearchTopItemKey.next.rawValue)
  235. }
  236. }
  237. extension KMNBotaSearchTopView: NSTextFieldDelegate {
  238. func controlTextDidChange(_ obj: Notification) {
  239. if searchInput_.isEqual(to: obj.object) {
  240. valueDidChange?(searchInput_, [.newKey : searchInput_.stringValue])
  241. }
  242. }
  243. }