KMBotaSearchViewController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. //
  2. // KMBotaSearchViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. extension KMNSearchKey.wholeWords {
  10. static let botaSearch = "BotaSearchWholeWordsKey"
  11. }
  12. extension KMNSearchKey.caseSensitive {
  13. static let botaSearch = "BotaSearchCaseSensitiveKey"
  14. }
  15. enum KMNBotaSearchType: Int {
  16. case none = 0
  17. case search = 1
  18. case replace = 2
  19. }
  20. @objc protocol KMBotaSearchViewControllerDelegate: NSObjectProtocol {
  21. @objc optional func switchSearchPopWindow(controller: KMBotaSearchViewController)
  22. }
  23. class KMBotaSearchViewController: KMNBotaBaseViewController {
  24. @IBOutlet weak var searchField: NSSearchField!
  25. @IBOutlet weak var segmentedControl: KMSegmentedControl!
  26. @IBOutlet weak var topView: NSBox!
  27. @IBOutlet weak var topHeightConst: NSLayoutConstraint!
  28. var contentView: NSView? {
  29. didSet {
  30. if let view = self.contentView {
  31. self.box.contentView = view
  32. }
  33. }
  34. }
  35. @IBOutlet weak var emptyBox: NSBox!
  36. @IBOutlet weak var searchBox: KMBox!
  37. @IBOutlet weak var searchResultsView: NSView!
  38. @IBOutlet weak var searchResultsLabel: NSTextField!
  39. @IBOutlet weak var searchDomeButton: NSButton!
  40. @IBOutlet weak var box: NSBox!
  41. @IBOutlet weak var emptySearchLabel: NSTextField!
  42. @IBOutlet weak var searchLabel: NSTextField!
  43. @IBOutlet weak var searchTips: NSTextField!
  44. @IBOutlet weak var pageLabel: NSTextField!
  45. @IBOutlet var scrollView: NSScrollView!
  46. @IBOutlet weak var tableView: KMBotaTableView!
  47. private lazy var topContentView_: KMNBotaSearchTopView? = {
  48. let view = KMNBotaSearchTopView.createFromNib()
  49. return view
  50. }()
  51. private var emptyView_: ComponentEmpty = {
  52. let view = ComponentEmpty()
  53. view.properties = ComponentEmptyProperty(emptyType: .noSearch, state: .normal, text: KMLocalizedString("No Results"), subText: KMLocalizedString(""))
  54. return view
  55. }()
  56. private var menuGroupView_: ComponentGroup?
  57. private var menuSections_: [CPDFSelection] = []
  58. private var menuType_: CAnnotationType = .circle
  59. var handdler = KMNSearchHanddler()
  60. weak var delegate: KMBotaSearchViewControllerDelegate?
  61. var searchResults : [KMBotaSearchSectionModel] = [] {
  62. didSet {
  63. self.updataLeftSideFindView()
  64. }
  65. }
  66. private var datas: [Any] = []
  67. private var searchResultIndex_: Int = -1
  68. override func loadView() {
  69. super.loadView()
  70. topView.borderWidth = 0
  71. topView.fillColor = .clear
  72. topView.contentView = topContentView_
  73. topContentView_?.itemClick = { [unowned self] idx, params in
  74. if idx == KMNBotaSearchTopItemKey.search.rawValue {
  75. if let data = params.first as? ComponentButton {
  76. showSearchGroupView(sender: data)
  77. }
  78. } else if idx == KMNBotaSearchTopItemKey.replace.rawValue {
  79. if handdler.type == .search {
  80. handdler.type = .replace
  81. showReplaceView()
  82. } else {
  83. handdler.type = .search
  84. showSearchView()
  85. }
  86. } else if idx == KMNBotaSearchTopItemKey.switch.rawValue {
  87. delegate?.switchSearchPopWindow?(controller: self)
  88. } else if idx == KMNBotaSearchTopItemKey.previous.rawValue {
  89. tableViewMoveUp(tableView)
  90. } else if idx == KMNBotaSearchTopItemKey.next.rawValue {
  91. tableViewMoveDown(tableView)
  92. }
  93. }
  94. showSearchView()
  95. topContentView_?.valueDidChange = { [unowned self] sender, info in
  96. guard let string = info?[.newKey] as? String else {
  97. return
  98. }
  99. self.search(keyword: string) { [unowned self] results in
  100. searchResults = results ?? []
  101. showResult()
  102. tableView.reloadData()
  103. }
  104. }
  105. emptyBox.contentView?.addSubview(emptyView_)
  106. emptyView_.km_add_top_constraint(constant: 232)
  107. emptyView_.km_add_bottom_constraint()
  108. emptyView_.km_add_leading_constraint()
  109. emptyView_.km_add_trailing_constraint()
  110. self.emptySearchLabel.stringValue = KMLocalizedString("")
  111. self.emptySearchLabel.textColor = KMAppearance.Layout.h1Color()
  112. self.emptyBox.fillColor = KMAppearance.Layout.l0Color()
  113. contentView = tableView.enclosingScrollView
  114. tableView.menuClickedAction = { [unowned self] point in
  115. let idxs = self.tableView.selectedRowIndexes.count
  116. let convertP = self.tableView.convert(point, from: nil)
  117. let row = self.tableView.row(at: convertP)
  118. if row == -1 {
  119. return NSMenu()
  120. }
  121. let hideNotes = handdler.hideNotes()
  122. let allowsNotes = handdler.allowsNotes()
  123. if hideNotes || allowsNotes == false {
  124. return NSMenu()
  125. }
  126. guard let model = self.datas[row] as? KMSearchMode else {
  127. return NSMenu()
  128. }
  129. var viewHeight: CGFloat = 0
  130. let items: [String] = ["Add New Circle", "Add New Rectangle", "Add New Highlight", "Add New Underline", "Add New Strikethrough"]
  131. var menuItemArr: [ComponentMenuitemProperty] = []
  132. for value in items {
  133. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  134. itemSelected: false,
  135. isDisabled: false,
  136. keyEquivalent: nil,
  137. text: KMLocalizedString(value),
  138. identifier: value)
  139. menuItemArr.append(properties_Menuitem)
  140. viewHeight += 36
  141. }
  142. self.menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  143. self.menuGroupView_?.clickedAutoHide = false
  144. self.menuGroupView_?.groupDelegate = self
  145. self.menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  146. self.menuGroupView_?.updateGroupInfo(menuItemArr)
  147. self.menuSections_ = [model.selection]
  148. self.menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: self.tableView)
  149. return NSMenu()
  150. }
  151. }
  152. override func viewDidLoad() {
  153. super.viewDidLoad()
  154. self.tableView.delegate = self
  155. self.tableView.dataSource = self
  156. self.tableView.botaDelegate = self
  157. }
  158. override func viewDidAppear() {
  159. super.viewDidAppear()
  160. // self.searchField.becomeFirstResponder()
  161. }
  162. override func updateUILanguage() {
  163. super.updateUILanguage()
  164. KMMainThreadExecute {
  165. self.topContentView_?.resultLabel.stringValue = KMLocalizedString("Result:") + " " + "\(self.handdler.searchResults.count)"
  166. self.tableView.reloadData()
  167. }
  168. }
  169. override func updateUIThemeColor() {
  170. super.updateUIThemeColor()
  171. KMMainThreadExecute {
  172. self.view.wantsLayer = true
  173. let color = KMNColorTools.colorBg_layoutMiddle()
  174. self.view.layer?.backgroundColor = color.cgColor
  175. self.tableView.backgroundColor = color
  176. self.topContentView_?.resultLabel.textColor = KMNColorTools.colorText_3()
  177. self.tableView.reloadData()
  178. }
  179. }
  180. func showSearchView() {
  181. topContentView_?.showSearch()
  182. topHeightConst.constant = topContentView_?.fetchContentHeight(type: handdler.type, hasResult: handdler.searchResults.isEmpty == false) ?? 0
  183. }
  184. func showReplaceView() {
  185. topContentView_?.showReplace()
  186. topHeightConst.constant = topContentView_?.fetchContentHeight(type: handdler.type, hasResult: handdler.searchResults.isEmpty == false) ?? 0
  187. }
  188. func showResult() {
  189. topContentView_?.showResult(type: handdler.type)
  190. topContentView_?.resultLabel.stringValue = KMLocalizedString("Result:") + " " + "\(self.handdler.searchResults.count)"
  191. topHeightConst.constant = topContentView_?.fetchContentHeight(type: handdler.type, hasResult: searchResults.isEmpty == false) ?? 0
  192. }
  193. func update(keyborad: String?, replaceKey: String?, results: [KMSearchMode]) {
  194. // searchItemView_.inputValue = keyborad
  195. // replaceItemView_.inputValue = replaceKey
  196. //
  197. // if results.isEmpty == false {
  198. // handdler.searchResults = results
  199. // self.currentSel = results.first?.selection
  200. // if let sel = self.currentSel {
  201. // self.handdler.showSelection(sel)
  202. // }
  203. // }
  204. //
  205. // updateButtonStatus()
  206. }
  207. func search(keyword: String, callback: @escaping (([KMBotaSearchSectionModel]?) -> Void)) {
  208. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  209. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  210. handdler.search(keyword: keyword, isCase: isCase, isWholeWord: isWholeWord, callback: callback)
  211. }
  212. // MARK: - Group View
  213. func showSearchGroupView(sender: ComponentButton) {
  214. var viewHeight: CGFloat = 8
  215. var menuItemArr: [ComponentMenuitemProperty] = []
  216. let titles = ["Search", "Find and Replace", "", "Whole Words", "Case Sensitive"]
  217. for i in titles {
  218. if i.isEmpty {
  219. let menuI = ComponentMenuitemProperty.divider()
  220. menuItemArr.append(menuI)
  221. viewHeight += 8
  222. } else {
  223. let menuI = ComponentMenuitemProperty(text: KMLocalizedString(i))
  224. menuItemArr.append(menuI)
  225. viewHeight += 36
  226. }
  227. }
  228. if handdler.type == .search {
  229. menuItemArr.first?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  230. } else if handdler.type == .replace {
  231. let info = menuItemArr.safe_element(for: 1) as? ComponentMenuitemProperty
  232. info?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  233. }
  234. if let info = menuItemArr.safe_element(for: 3) as? ComponentMenuitemProperty {
  235. if KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch) {
  236. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  237. }
  238. }
  239. if let info = menuItemArr.last {
  240. if KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch) {
  241. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  242. }
  243. }
  244. let groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  245. searchGroupView = groupView
  246. groupView?.groupDelegate = self
  247. groupView?.frame = CGRectMake(310, 0, 200, viewHeight)
  248. groupView?.updateGroupInfo(menuItemArr)
  249. var point = sender.convert(sender.frame.origin, to: nil)
  250. point.y -= viewHeight
  251. groupView?.showWithPoint(point, relativeTo: sender)
  252. searchGroupTarget = sender
  253. }
  254. @IBAction func searchDomeButtonAtion(_ sender: AnyObject) {
  255. self.searchField.isHidden = true
  256. self.searchDomeButton.isHidden = true
  257. self.searchBox.isHidden = false
  258. }
  259. @objc func goToSelectedFindResults(_ sender: AnyObject?) {
  260. // guard let olView = sender as? NSTableView, olView.clickedRow != -1 else {
  261. // NSSound.beep()
  262. // return
  263. // }
  264. // self.updateFindResultHighlightsForDirection(.directSelection)
  265. }
  266. @objc func addAnnotationsForSelections(_ sender: NSMenuItem) {
  267. for selection in menuSections_ {
  268. // self.listView?.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? .circle, selection: selection, page: selection.page, bounds: selection.bounds)
  269. }
  270. }
  271. func updataLeftSideFindView() {
  272. if (self.searchResults.count > 0) {
  273. self.emptyBox.isHidden = true
  274. // self.searchResultsView.isHidden = false
  275. // self.searchResultsLabel.stringValue = String(format: KMLocalizedString("%ld Results"), self.searchResults.count)
  276. } else {
  277. self.emptyBox.isHidden = false
  278. // self.searchResultsView.isHidden = true
  279. }
  280. }
  281. func updateFindResultHighlightsForDirection(_ direction: NSWindow.SelectionDirection) {
  282. var findResults: [KMSearchMode] = handdler.searchResults
  283. if (findResults.count == 0) {
  284. handdler.showSelection(nil)
  285. } else {
  286. if direction == .directSelection {
  287. self.searchResultIndex_ = 0
  288. } else if (direction == .selectingNext) {
  289. self.searchResultIndex_ += 1
  290. if self.searchResultIndex_ >= findResults.count {
  291. self.searchResultIndex_ = 0
  292. }
  293. } else if (direction == .selectingPrevious) {
  294. self.searchResultIndex_ -= 1
  295. if self.searchResultIndex_ < 0 {
  296. self.searchResultIndex_ = findResults.count-1
  297. }
  298. }
  299. let currentSel = findResults[self.searchResultIndex_].selection
  300. if currentSel.hasCharacters() {
  301. let page = currentSel.safeFirstPage()
  302. var rect = NSZeroRect
  303. for model in findResults {
  304. if let data = page, model.selection.pages().contains(data) {
  305. rect = NSUnionRect(rect, model.selection.bounds(for: data))
  306. }
  307. }
  308. let FIND_RESULT_MARGIN = 50.0
  309. rect = NSIntersectionRect(NSInsetRect(rect, -FIND_RESULT_MARGIN, -FIND_RESULT_MARGIN), page?.bounds(for: .cropBox) ?? .zero)
  310. handdler.pdfView?.go(to: page)
  311. handdler.pdfView?.go(to: rect, on: page)
  312. }
  313. if currentSel.hasCharacters() {
  314. let bColor = NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5)
  315. let color = NSColor(red: 219/255.0, green: 220/255.0, blue: 3/255.0, alpha: 0.5)
  316. handdler.pdfView?.setHighlight(currentSel, forBorderColor: .clear, fill: color, animated: true)
  317. handdler.pdfView?.go(to: currentSel, animated: true)
  318. handdler.pdfView?.setCurrentSelection(currentSel, animate: true)
  319. }
  320. // let mode = self.listView?.toolMode ?? .none
  321. // if mode == .moveToolMode || mode == .magnifyToolMode || mode == .selectToolMode {
  322. // self.listView?.setCurrentSelection(nil, animate: false)
  323. // }
  324. }
  325. }
  326. }
  327. // MARK: - NSTableViewDelegate, NSTableViewDataSource
  328. extension KMBotaSearchViewController: NSTableViewDelegate, NSTableViewDataSource {
  329. func numberOfRows(in tableView: NSTableView) -> Int {
  330. var datas: [Any] = []
  331. for sectionM in self.handdler.searchSectionResults {
  332. if sectionM.items.count > 0 {
  333. datas.append(sectionM)
  334. if sectionM.isExpand == false {
  335. continue
  336. }
  337. for item in sectionM.items {
  338. datas.append(item)
  339. }
  340. }
  341. }
  342. self.datas = datas
  343. return datas.count
  344. }
  345. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  346. let model = self.datas[row]
  347. if let data = model as? KMBotaSearchSectionModel {
  348. var cell = tableView.makeView(withIdentifier: KMSectionCellView.km_identifier, owner: nil) as? KMSectionCellView
  349. if cell == nil {
  350. cell = KMSectionCellView.createFromNib()
  351. }
  352. cell?.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  353. cell?.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  354. cell?.titleLabel.textColor = KMNColorTools.colorText_1()
  355. cell?.countLabel.textColor = KMNColorTools.colorText_3()
  356. cell?.isExpand = data.isExpand
  357. let pageIndex = data.pageIndex
  358. cell?.titleLabel.stringValue = KMLocalizedString("Page") + " \(pageIndex + 1)"
  359. cell?.countLabel.stringValue = "\(data.itemCount)"
  360. cell?.bottomLine.wantsLayer = true
  361. cell?.bottomLine.layer?.backgroundColor = KMNColorTools.colorBorder_divider().cgColor
  362. cell?.itemClick = { [weak self] idx, _ in
  363. if idx == 1 { // 收取 & 展开
  364. data.isExpand = !data.isExpand
  365. self?.tableView.reloadData()
  366. }
  367. }
  368. return cell
  369. }
  370. if let data = model as? KMSearchMode {
  371. var cell = tableView.makeView(withIdentifier: KMNBotaSearchCellView.km_identifier, owner: self) as? KMNBotaSearchCellView
  372. if cell == nil {
  373. cell = KMNBotaSearchCellView()
  374. }
  375. cell?.label.attributedStringValue = data.attributedString
  376. return cell
  377. }
  378. return nil
  379. }
  380. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  381. let model = datas[row]
  382. if model is KMBotaSearchSectionModel {
  383. return 40.0
  384. }
  385. if let data = model as? KMSearchMode {
  386. let width = NSWidth(self.view.frame)
  387. let rect = data.attributedString.boundingRect(with: .init(width: width-24*2, height: CGFLOAT_MAX), options: [.usesLineFragmentOrigin, .usesLineFragmentOrigin])
  388. return rect.size.height + 12 * 2
  389. }
  390. return 40.0
  391. }
  392. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  393. let rowView = KMBotaTableRowView()
  394. return rowView
  395. }
  396. func tableViewSelectionDidChange(_ notification: Notification) {
  397. let row = self.tableView.selectedRow
  398. if row < 0 || row >= datas.count {
  399. return
  400. }
  401. guard let model = datas[row] as? KMSearchMode else {
  402. return
  403. }
  404. let isEditing = handdler.pdfView?.isEditing() ?? false
  405. if isEditing {
  406. handdler.showSelection(model.selection)
  407. return
  408. }
  409. DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
  410. self.handdler.showSelection(model.selection)
  411. }
  412. }
  413. func tableView(_ aTableView: NSTableView, copyRowsWithIndexes rowIndexes: IndexSet) {
  414. if IAPProductsManager.default().isAvailableAllFunction() == false {
  415. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  416. return
  417. }
  418. var string = ""
  419. for idx in rowIndexes {
  420. if idx < 0 || idx >= datas.count {
  421. continue
  422. }
  423. guard let model = datas[idx] as? KMSearchMode else {
  424. continue
  425. }
  426. let match = model.selection
  427. string.append("* ")
  428. // [string appendFormat:NSLocalizedString(@"Page %@", @""), [match firstPageLabel]];
  429. string = string.appendingFormat(KMLocalizedString("Page %@"), "\(match.safeFirstPage()?.pageIndex() ?? 0)")
  430. // [string appendFormat:@"", [[match contextString] string]];
  431. string = string.appendingFormat(": %@\n", match.string() ?? "")
  432. }
  433. let pboard = NSPasteboard.general
  434. pboard.clearContents()
  435. pboard.writeObjects([string as NSPasteboardWriting])
  436. }
  437. }
  438. // MARK: - KMBotaTableViewDelegate
  439. extension KMBotaSearchViewController: KMBotaTableViewDelegate {
  440. func tableView(_ aTableView: NSTableView, canCopyRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  441. return rowIndexes.count > 0
  442. }
  443. func tableViewMoveRight(_ aTableView: NSTableView) {
  444. updateFindResultHighlightsForDirection(.selectingNext)
  445. }
  446. func tableViewMoveUp(_ aTableView: NSTableView) {
  447. self.tableView.km_safe_selectRowIndexes(.init(integer: self.tableView.selectedRow-1), byExtendingSelection: false)
  448. self.tableView.scrollRowToVisible(self.tableView.selectedRow)
  449. }
  450. func tableViewMoveDown(_ aTableView: NSTableView) {
  451. self.tableView.km_safe_selectRowIndexes(.init(integer: self.tableView.selectedRow+1), byExtendingSelection: false)
  452. self.tableView.scrollRowToVisible(self.tableView.selectedRow)
  453. }
  454. func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
  455. if rowIndex < 0 || rowIndex >= datas.count {
  456. return nil
  457. }
  458. guard let model = datas[rowIndex] as? KMSearchMode else {
  459. return nil
  460. }
  461. let selection = model.selection
  462. let x = selection.bounds.origin.x + NSWidth(selection.bounds) * 0.5
  463. let y = selection.bounds.origin.y + NSHeight(selection.bounds) * 0.5
  464. let point = NSPoint(x: x, y: y)
  465. return CPDFDestination(document: handdler.pdfDocument(), pageIndex: Int(model.selectionPageIndex), at: point, zoom: handdler.scaleFactor() ?? 0)
  466. }
  467. }
  468. //MARK: - ComponentGroupDelegate
  469. extension KMBotaSearchViewController: ComponentGroupDelegate {
  470. func componentGroupDidDismiss(group: ComponentGroup?) {
  471. if group == menuGroupView_ {
  472. group?.removeFromSuperview()
  473. menuGroupView_ = nil
  474. } else if group == searchGroupView {
  475. searchGroupTarget?.properties.state = .normal
  476. searchGroupTarget?.reloadData()
  477. searchGroupTarget = nil
  478. }
  479. }
  480. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  481. if group == menuGroupView_ {
  482. if let selItem = menuItemProperty {
  483. let index = group?.menuItemArr.firstIndex(of: selItem)
  484. if index == 0 {
  485. menuType_ = .circle
  486. addAnnotationsForSelections(NSMenuItem())
  487. } else if index == 1 {
  488. menuType_ = .square
  489. addAnnotationsForSelections(NSMenuItem())
  490. } else if index == 2 {
  491. menuType_ = .highlight
  492. addAnnotationsForSelections(NSMenuItem())
  493. } else if index == 3 {
  494. menuType_ = .underline
  495. addAnnotationsForSelections(NSMenuItem())
  496. } else if index == 4 {
  497. menuType_ = .strikeOut
  498. addAnnotationsForSelections(NSMenuItem())
  499. }
  500. group?.removeFromSuperview()
  501. }
  502. } else if group == searchGroupView {
  503. guard let menuI = menuItemProperty else {
  504. return
  505. }
  506. let idx = group?.menuItemArr.firstIndex(of: menuI)
  507. if idx == 0 { // search
  508. } else if idx == 1 { // replace
  509. } else if idx == 3 {
  510. let key = KMNSearchKey.wholeWords.botaSearch
  511. let value = KMDataManager.ud_bool(forKey: key)
  512. KMDataManager.ud_set(!value, forKey: key)
  513. if let data = topContentView_?.inputValue, data.isEmpty {
  514. search(keyword: data) { [weak self] results in
  515. self?.searchResults = results ?? []
  516. self?.showResult()
  517. self?.tableView.reloadData()
  518. }
  519. }
  520. } else if idx == 4 {
  521. let key = KMNSearchKey.caseSensitive.botaSearch
  522. let value = KMDataManager.ud_bool(forKey: key)
  523. KMDataManager.ud_set(!value, forKey: key)
  524. if let data = topContentView_?.inputValue, data.isEmpty {
  525. search(keyword: data) { [weak self] results in
  526. self?.searchResults = results ?? []
  527. self?.showResult()
  528. self?.tableView.reloadData()
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }