KMNBotaHeaderSearchView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // KMNBotaHeaderSearchView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/13.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNBotaHeaderSearchView: NSView, NibLoadable {
  10. @IBOutlet weak var leftBox: NSBox!
  11. @IBOutlet weak var searchBox: NSBox!
  12. @IBOutlet weak var rightBox: NSBox!
  13. @IBOutlet weak var bottomLine: NSView!
  14. var itemClick: KMCommonClickBlock?
  15. var valueDidChange: KMValueDidChangeBlock?
  16. var ignoreCase = false
  17. private var groupView_: ComponentGroup?
  18. private lazy var searchButton_: ComponentButton = {
  19. let view = ComponentButton()
  20. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaSearch"))
  21. return view
  22. }()
  23. private lazy var input_: NSTextField = {
  24. let view = NSTextField()
  25. view.drawsBackground = false
  26. view.isBordered = false
  27. view.placeholderString = KMLocalizedString("Search")
  28. return view
  29. }()
  30. private lazy var closeButton_: ComponentButton = {
  31. let view = ComponentButton()
  32. view.properties = ComponentButtonProperty(type: .text_gray_low, size: .xxs, state: .normal, onlyIcon: true, icon: NSImage(named: "KMImageNameBotaClose"))
  33. return view
  34. }()
  35. var input: NSTextField {
  36. get {
  37. return input_
  38. }
  39. }
  40. override func draw(_ dirtyRect: NSRect) {
  41. super.draw(dirtyRect)
  42. // Drawing code here.
  43. }
  44. override func awakeFromNib() {
  45. super.awakeFromNib()
  46. leftBox.borderWidth = 0
  47. leftBox.contentView = searchButton_
  48. searchButton_.setTarget(self, action: #selector(_searchAction))
  49. searchBox.borderWidth = 0
  50. searchBox.contentView = input_
  51. input_.delegate = self
  52. rightBox.borderWidth = 0
  53. rightBox.contentView = closeButton_
  54. closeButton_.setTarget(self, action: #selector(_closeAction))
  55. }
  56. func clearInputData() {
  57. input_.stringValue = ""
  58. }
  59. @objc private func _searchAction() {
  60. itemClick?(1, searchButton_)
  61. }
  62. @objc private func _closeAction() {
  63. itemClick?(2)
  64. }
  65. }
  66. extension KMNBotaHeaderSearchView: NSTextFieldDelegate {
  67. func controlTextDidChange(_ obj: Notification) {
  68. if input_.isEqual(to: obj.object) {
  69. valueDidChange?(input_,[.newKey : input_.stringValue])
  70. }
  71. }
  72. }
  73. extension KMNBotaHeaderSearchView: ComponentGroupDelegate {
  74. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  75. ignoreCase = !ignoreCase
  76. KMPrint("componentGroupDidSelect \(ignoreCase)")
  77. itemClick?(1)
  78. }
  79. func componentGroupDidDismiss(group: ComponentGroup?) {
  80. searchButton_.properties.state = .normal
  81. searchButton_.reloadData()
  82. }
  83. }