KMSearchReplaceWindowController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. //
  2. // KMSearchReplaceWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/8/7.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMSearchReplaceWindowController_Window: NSWindow {
  10. override var canBecomeMain: Bool {
  11. return true
  12. }
  13. override var canBecomeKey: Bool {
  14. return true
  15. }
  16. // override func setFrame(from string: NSWindow.PersistableFrameDescriptor) {
  17. // super.setFrame(from: string)
  18. // }
  19. //
  20. // override func setFrame(_ frameRect: NSRect, display flag: Bool) {
  21. // let frame = frameRect
  22. //// let supFrame = self.parent?.frame ?? .zero
  23. //
  24. //// KMPrint("frame: \(frame)")
  25. //// KMPrint("supFrame: \(supFrame)")
  26. //
  27. // var theFrame = frame
  28. // theFrame.origin.x = max(frame.origin.x, 100)
  29. // theFrame.origin.y = max(frame.origin.y, 100)
  30. //
  31. // super.setFrame(theFrame, display: flag)
  32. // }
  33. //
  34. // override func setFrame(_ frameRect: NSRect, display displayFlag: Bool, animate animateFlag: Bool) {
  35. // super.setFrame(frameRect, display: displayFlag, animate: animateFlag)
  36. // }
  37. //
  38. // override func setFrameOrigin(_ point: NSPoint) {
  39. // super.setFrameOrigin(point)
  40. // }
  41. }
  42. class KMSearchReplaceWindowController: KMNBaseWindowController {
  43. @IBOutlet weak var titleBarBox: NSBox!
  44. @IBOutlet weak var tabBox: NSBox!
  45. @IBOutlet weak var searchBox: NSBox!
  46. @IBOutlet weak var replaceBox: NSBox!
  47. var replaceCallback: (() -> Void)?
  48. var itemClick: KMCommonClickBlock?
  49. var closeCallback: (() -> Void)?
  50. private var _modalSession: NSApplication.ModalSession?
  51. private var handdler = KMNSearchHanddler()
  52. private var currentSel: CPDFSelection?
  53. private var finding_ = false
  54. private lazy var titleBarView_: KMNSearchReplaceTitleBarView = {
  55. let view = KMNSearchReplaceTitleBarView()
  56. return view
  57. }()
  58. private lazy var searchItemView_: KMNSearchReplaceSearchItemView = {
  59. let view = KMNSearchReplaceSearchItemView()
  60. return view
  61. }()
  62. private lazy var replaceItemView_: KMNSearchReplacePopItemView = {
  63. let view = KMNSearchReplacePopItemView()
  64. return view
  65. }()
  66. var previousButton: ComponentButton {
  67. get {
  68. return searchItemView_.previousButton
  69. }
  70. }
  71. var nextButton: ComponentButton {
  72. get {
  73. return searchItemView_.nextButton
  74. }
  75. }
  76. var replaceAllButton: ComponentButton {
  77. get {
  78. return replaceItemView_.replaceAllButton
  79. }
  80. }
  81. var replaceButton: ComponentButton {
  82. get {
  83. return replaceItemView_.replaceButton
  84. }
  85. }
  86. private var searchGroupView_: ComponentGroup?
  87. convenience init(with pdfView: CPDFView?, type: KMNBotaSearchType) {
  88. self.init(windowNibName: "KMSearchReplaceWindowController")
  89. self.handdler.pdfView = pdfView
  90. handdler.type = type
  91. }
  92. override func windowDidLoad() {
  93. super.windowDidLoad()
  94. self.initDefaultValue()
  95. self.switchType(handdler.type)
  96. // NotificationCenter.default.addObserver(self, selector: #selector(didMoveNotification), name: NSWindow.didMoveNotification, object: self.window)
  97. // NotificationCenter.default.addObserver(self, selector: #selector(didMoveNotification), name: NSWindow.willMoveNotification, object: self.window)
  98. }
  99. @objc func didMoveNotification(_ noti: Notification) {
  100. let window = self.window
  101. let frame = window?.frame ?? .zero
  102. let supFrame = window?.parent?.frame ?? .zero
  103. KMPrint("frame: \(frame)")
  104. KMPrint("supFrame: \(supFrame)")
  105. // var theFrame = frame
  106. // theFrame.origin.x = max(frame.origin.x, supFrame.origin.x)
  107. // theFrame.origin.y = max(frame.origin.y, supFrame.origin.y)
  108. //
  109. // self.window?.setFrame(theFrame, display: true)
  110. // self.window?.setFrameOrigin(theFrame.origin)
  111. }
  112. func initDefaultValue() {
  113. window?.isMovableByWindowBackground = true
  114. window?.contentView?.wantsLayer = true
  115. window?.contentView?.layer?.cornerRadius = ComponentLibrary.shared.getComponentValueFromKey("radius/m") as? CGFloat ?? 8
  116. window?.contentView?.layer?.masksToBounds = true
  117. window?.backgroundColor = .clear
  118. titleBarBox.boxType = .custom
  119. titleBarBox.borderWidth = 0
  120. titleBarBox.contentView = titleBarView_
  121. titleBarView_.titleLabel.font = .SFProTextRegularFont(14)
  122. titleBarView_.itemClick = { [weak self] idx, _ in
  123. if idx == 1 {
  124. self?._closeAction(NSButton())
  125. } else if idx == 2 {
  126. self?._closeAction(NSButton())
  127. self?.itemClick?(1, self?.handdler)
  128. }
  129. }
  130. searchBox.borderWidth = 0
  131. searchBox.contentView = searchItemView_
  132. searchItemView_.itemClick = { [unowned self] idx, _ in
  133. if idx == 1 { // Previous
  134. _previousAction(NSButton())
  135. } else if idx == 2 { // next
  136. _nextAction(NSButton())
  137. } else if idx == 3 {
  138. showSearchGroupView(sender: ComponentButton())
  139. }
  140. }
  141. searchItemView_.valueDidChange = { [unowned self] value, _ in
  142. if let data = value as? String {
  143. handdler.searchKey = data
  144. search(keyboard: data)
  145. updateButtonStatus()
  146. }
  147. }
  148. searchItemView_.inputDidEditBlock = { [unowned self] in
  149. updateButtonStatus()
  150. let value = searchItemView_.inputValue
  151. if value.isEmpty {
  152. } else {
  153. currentSel = nil
  154. }
  155. }
  156. searchItemView_.input.properties.showSuffix = false
  157. searchItemView_.input.reloadData()
  158. replaceBox.borderWidth = 0
  159. replaceBox.contentView = replaceItemView_
  160. replaceItemView_.itemClick = { [unowned self] idx, _ in
  161. if idx == 1 {
  162. _replaceAllAction(NSButton())
  163. } else if idx == 2 {
  164. _replaceAction(NSButton())
  165. }
  166. }
  167. replaceItemView_.valueDidChange = { [unowned self] value, _ in
  168. if let data = value as? String {
  169. handdler.replaceKey = data
  170. }
  171. }
  172. updateButtonStatus()
  173. if searchItemView_.inputValue.isEmpty {
  174. } else {
  175. self.currentSel = nil
  176. }
  177. }
  178. override func updateUILanguage() {
  179. super.updateUILanguage()
  180. KMMainThreadExecute {
  181. self.titleBarView_.titleLabel.stringValue = KMLocalizedString("Search")
  182. }
  183. }
  184. override func updateUIThemeColor() {
  185. super.updateUIThemeColor()
  186. KMMainThreadExecute {
  187. self.titleBarView_.titleLabel.textColor = KMNColorTools.colorText_1()
  188. self.searchItemView_.input.properties.leftIcon = NSImage(named: "KMImageNameBotaSearch")
  189. self.searchItemView_.input.reloadData()
  190. self.replaceItemView_.input.properties.leftIcon = NSImage(named: "KMImagenameBotaSearchInputPrefiex")
  191. self.replaceItemView_.input.reloadData()
  192. self.updateViewColor()
  193. }
  194. }
  195. func updateButtonStatus() {
  196. let value = searchItemView_.inputValue
  197. if value.isEmpty {
  198. previousButton.properties.isDisabled = true
  199. previousButton.reloadData()
  200. nextButton.properties.isDisabled = true
  201. nextButton.reloadData()
  202. replaceButton.properties.isDisabled = true
  203. replaceButton.reloadData()
  204. replaceAllButton.properties.isDisabled = true
  205. replaceAllButton.reloadData()
  206. } else {
  207. previousButton.properties.isDisabled = false
  208. previousButton.reloadData()
  209. nextButton.properties.isDisabled = false
  210. nextButton.reloadData()
  211. replaceButton.properties.isDisabled = false
  212. replaceButton.reloadData()
  213. replaceAllButton.properties.isDisabled = false
  214. replaceAllButton.reloadData()
  215. }
  216. }
  217. func update(keyborad: String?, replaceKey: String?, results: [KMSearchMode]) {
  218. searchItemView_.inputValue = keyborad ?? ""
  219. replaceItemView_.inputValue = replaceKey ?? ""
  220. handdler.searchKey = keyborad
  221. handdler.replaceKey = replaceKey
  222. if results.isEmpty == false {
  223. handdler.searchResults = results
  224. self.currentSel = results.first?.selection
  225. if let sel = self.currentSel {
  226. self.handdler.showSelection(sel)
  227. }
  228. }
  229. updateButtonStatus()
  230. }
  231. // MARK: - Group View
  232. func showSearchGroupView(sender: ComponentButton) {
  233. var viewHeight: CGFloat = 8
  234. var menuItemArr: [ComponentMenuitemProperty] = []
  235. let titles = ["Search", "Find and Replace", "", "Whole Words", "Case Sensitive"]
  236. for i in titles {
  237. if i.isEmpty {
  238. let menuI = ComponentMenuitemProperty.divider()
  239. menuItemArr.append(menuI)
  240. viewHeight += 8
  241. } else {
  242. let menuI = ComponentMenuitemProperty(text: KMLocalizedString(i))
  243. menuItemArr.append(menuI)
  244. viewHeight += 36
  245. }
  246. }
  247. if handdler.type == .search {
  248. menuItemArr.first?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  249. } else if handdler.type == .replace {
  250. let info = menuItemArr.safe_element(for: 1) as? ComponentMenuitemProperty
  251. info?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  252. }
  253. if let info = menuItemArr.safe_element(for: 3) as? ComponentMenuitemProperty {
  254. if KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch) {
  255. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  256. }
  257. }
  258. if let info = menuItemArr.last {
  259. if KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch) {
  260. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  261. }
  262. }
  263. let groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  264. searchGroupView_ = groupView
  265. groupView?.groupDelegate = self
  266. groupView?.frame = CGRectMake(310, 0, 200, viewHeight)
  267. groupView?.updateGroupInfo(menuItemArr)
  268. let senderView = self.searchItemView_.input
  269. var point = senderView.convert(senderView.frame.origin, to: nil)
  270. point.y -= viewHeight
  271. point.y -= 30
  272. groupView?.showWithPoint(point, relativeTo: senderView)
  273. // searchGroupTarget = sender
  274. }
  275. // MARK: - Actions
  276. @objc private func _closeAction(_ sender: NSButton) {
  277. self.window?.orderOut(nil)
  278. self.handdler.clearData()
  279. self.closeCallback?()
  280. }
  281. @objc private func _previousAction(_ sender: NSButton) {
  282. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  283. if isEditing == false {
  284. let index = self.handdler.previous()
  285. let model = self.handdler.searchResults[index]
  286. self.handdler.showSelection(model.selection)
  287. self._showIndexTip()
  288. } else {
  289. if let _ = self.currentSel {
  290. self.currentSel = self.handdler.pdfView?.document.findForwardEditText()
  291. if let sel = self.currentSel {
  292. let index = self.handdler.previous()
  293. self.handdler.showSelection(sel)
  294. self._showIndexTip()
  295. } else {
  296. _showNoResultsAlert()
  297. return
  298. }
  299. } else {
  300. if self.finding_ {
  301. return
  302. }
  303. self.finding_ = true
  304. let searchS = self.searchItemView_.inputValue
  305. let opt = self.fetchSearchOptions()
  306. self._beginLoading()
  307. DispatchQueue.global().async {
  308. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  309. DispatchQueue.main.async {
  310. self._endLoading()
  311. self.finding_ = false
  312. let sel = datas?.first?.first
  313. if sel == nil {
  314. self._showNoResultsAlert()
  315. return
  316. }
  317. self.currentSel = sel
  318. self.handdler.showIdx = 1
  319. var count = 0
  320. for i in datas ?? [] {
  321. count += i.count
  322. }
  323. self.handdler.resultCount = count
  324. self._showIndexTip()
  325. self.handdler.showSelection(sel)
  326. }
  327. }
  328. }
  329. }
  330. }
  331. @objc private func _nextAction(_ sender: NSButton) {
  332. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  333. if isEditing == false {
  334. let index = self.handdler.next()
  335. let model = self.handdler.searchResults[index]
  336. self.handdler.showSelection(model.selection)
  337. self._showIndexTip()
  338. } else {
  339. if let _ = self.currentSel {
  340. self.currentSel = self.handdler.pdfView?.document.findBackwordEditText()
  341. if let sel = self.currentSel {
  342. let index = self.handdler.next()
  343. self.handdler.showSelection(sel)
  344. self._showIndexTip()
  345. } else {
  346. _showNoResultsAlert()
  347. }
  348. } else {
  349. if self.finding_ {
  350. return
  351. }
  352. self.finding_ = true
  353. let searchS = self.searchItemView_.inputValue
  354. let opt = self.fetchSearchOptions()
  355. self._beginLoading()
  356. DispatchQueue.global().async {
  357. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  358. DispatchQueue.main.async {
  359. self._endLoading()
  360. self.finding_ = false
  361. let sel = datas?.first?.first
  362. if sel == nil {
  363. self._showNoResultsAlert()
  364. return
  365. }
  366. self.handdler.showIdx = 1
  367. var count = 0
  368. for i in datas ?? [] {
  369. count += i.count
  370. }
  371. self.handdler.resultCount = count
  372. self._showIndexTip()
  373. self.currentSel = sel
  374. self.handdler.showSelection(sel)
  375. }
  376. }
  377. }
  378. }
  379. }
  380. @objc private func _checkAction(_ sender: NSButton) {
  381. self.currentSel = nil
  382. }
  383. @objc private func _searchTabAction(_ sender: NSButton) {
  384. self.switchType(.search, animate: true)
  385. }
  386. @objc private func _replaceTabAction(_ sender: NSButton) {
  387. self.switchType(.replace, animate: true)
  388. }
  389. @objc private func _replaceAction(_ sender: NSButton) {
  390. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  391. if isEditing == false {
  392. NSSound.beep()
  393. return
  394. }
  395. if let sel = self.currentSel {
  396. let searchS = self.searchItemView_.inputValue
  397. let replaceS = self.replaceItemView_.inputValue
  398. let success = self.handdler.replace(searchS: searchS, replaceS: replaceS, sel: sel) { [weak self] newSel in
  399. self?.handdler.showSelection(newSel)
  400. }
  401. if success {
  402. // self.handdler.showSelection(sel)
  403. }
  404. } else { // 先查找
  405. if self.finding_ {
  406. return
  407. }
  408. self.finding_ = true
  409. let searchS = self.searchItemView_.inputValue
  410. let opt = self.fetchSearchOptions()
  411. self._beginLoading()
  412. DispatchQueue.global().async {
  413. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  414. DispatchQueue.main.async {
  415. self._endLoading()
  416. self.finding_ = false
  417. let sel = datas?.first?.first
  418. if sel == nil {
  419. self._showNoResultsAlert()
  420. return
  421. }
  422. self.currentSel = sel
  423. self.handdler.showSelection(sel)
  424. }
  425. }
  426. }
  427. }
  428. @objc private func _replaceAllAction(_ sender: NSButton) {
  429. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  430. if isEditing == false {
  431. NSSound.beep()
  432. return
  433. }
  434. let datas = self.handdler.pdfView?.document.findEditSelections() ?? []
  435. if datas.isEmpty {
  436. _showNoResultsAlert()
  437. return
  438. }
  439. if self.finding_ {
  440. return
  441. }
  442. self.finding_ = true
  443. let searchS = self.searchItemView_.inputValue
  444. let replaceS = self.replaceItemView_.inputValue
  445. self._beginLoading()
  446. DispatchQueue.global().async {
  447. self.handdler.pdfView?.document.replaceAllEditText(with: searchS, toReplace: replaceS)
  448. self.currentSel = nil
  449. DispatchQueue.main.async {
  450. self._endLoading()
  451. self.finding_ = false
  452. self.handdler.pdfView?.setHighlightedSelection(nil, animated: false)
  453. self.handdler.pdfView?.setNeedsDisplayForVisiblePages()
  454. }
  455. }
  456. }
  457. private func _showNoResultsAlert() {
  458. _ = _showAlert(style: .critical, message: KMLocalizedString("No related content found, please change keyword."), info: "", buttons: [KMLocalizedString("OK", comment: "")])
  459. }
  460. private func _showAlert(style: NSAlert.Style, message: String, info: String, buttons: [String]) -> NSApplication.ModalResponse {
  461. let alert = NSAlert()
  462. alert.alertStyle = style
  463. alert.messageText = message
  464. alert.informativeText = info
  465. for button in buttons {
  466. alert.addButton(withTitle: button)
  467. }
  468. return alert.runModal()
  469. }
  470. private func _showIndexTip() {
  471. DispatchQueue.main.async {
  472. if self.handdler.resultCount == 0 {
  473. self.searchItemView_.input.properties.rightText = ""
  474. } else {
  475. self.searchItemView_.input.properties.rightText = "\(self.handdler.showIdx+1)/\(self.handdler.resultCount)"
  476. }
  477. self.searchItemView_.input.reloadData()
  478. }
  479. }
  480. private func fetchSearchOptions() -> CPDFSearchOptions {
  481. var opt = CPDFSearchOptions()
  482. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  483. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  484. if isCase {
  485. opt.insert(.caseSensitive)
  486. }
  487. if isWholeWord {
  488. opt.insert(.matchWholeWord)
  489. }
  490. return opt
  491. }
  492. private func updateViewColor() {
  493. let isDark = KMAppearance.isDarkMode()
  494. if isDark {
  495. self.window?.contentView?.wantsLayer = true
  496. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#393C3E").cgColor
  497. } else {
  498. self.window?.contentView?.wantsLayer = true
  499. self.window?.contentView?.layer?.backgroundColor = .white
  500. }
  501. self.switchType(handdler.type)
  502. }
  503. func switchType(_ type: KMNBotaSearchType, animate: Bool = false) {
  504. if type == .replace {
  505. if IAPProductsManager.default().isAvailableAllFunction() == false {
  506. let winC = KMPurchaseCompareWindowController.sharedInstance()
  507. winC?.showWindow(nil)
  508. guard let win = winC?.window else {
  509. return
  510. }
  511. self.window?.addChildWindow(win, ordered: .above)
  512. return
  513. }
  514. }
  515. handdler.type = type
  516. if type == .search { // 248 112
  517. self.replaceBox.isHidden = true
  518. var frame = self.window?.frame ?? .zero
  519. let height: CGFloat = 112
  520. let heightOffset = frame.size.height - height
  521. frame.origin.y += heightOffset
  522. frame.size.height = height
  523. self.window?.setFrame(frame, display: true, animate: animate)
  524. self.window?.minSize = frame.size
  525. self.window?.maxSize = frame.size
  526. } else if type == .replace { // 388 208
  527. DispatchQueue.main.async {
  528. self.replaceBox.isHidden = false
  529. }
  530. var frame = self.window?.frame ?? .zero
  531. let height:CGFloat = 208
  532. let heightOffset = frame.size.height-height
  533. frame.origin.y += heightOffset
  534. frame.size.height = height
  535. self.window?.setFrame(frame, display: true, animate: animate)
  536. self.window?.minSize = frame.size
  537. self.window?.maxSize = frame.size
  538. // 将事件回调出去
  539. self.replaceCallback?()
  540. }
  541. }
  542. private func _beginLoading() {
  543. self.window?.contentView?.beginLoading()
  544. }
  545. private func _endLoading() {
  546. self.window?.contentView?.endLoading()
  547. }
  548. func startModal(_ sender: AnyObject?) {
  549. NSApp.stopModal()
  550. var modalCode: NSApplication.ModalResponse?
  551. if let _win = self.window {
  552. self._modalSession = NSApp.beginModalSession(for: _win)
  553. repeat {
  554. modalCode = NSApp.runModalSession(self._modalSession!)
  555. } while (modalCode == .continue)
  556. }
  557. }
  558. func endModal(_ sender: AnyObject?) {
  559. if let session = self._modalSession {
  560. NSApp.stopModal()
  561. NSApp.endModalSession(session)
  562. self.window?.orderOut(self)
  563. }
  564. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  565. self.window?.kmCurrentWindowC = nil
  566. }
  567. }
  568. func search(keyboard: String) {
  569. if keyboard.isEmpty {
  570. handdler.resultCount = 0
  571. _showIndexTip()
  572. return
  573. }
  574. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  575. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  576. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  577. if isEditing == false {
  578. if self.finding_ {
  579. return
  580. }
  581. self.finding_ = true
  582. self._beginLoading()
  583. self.handdler.search(keyword: keyboard, isCase: isCase, isWholeWord: isWholeWord, callback: { [weak self] datas in
  584. self?.finding_ = false
  585. self?._endLoading()
  586. guard let sels = self?.handdler.searchResults, sels.isEmpty == false else {
  587. self?._showNoResultsAlert()
  588. return
  589. }
  590. if let sel = sels.first?.selection {
  591. self?.handdler.showIdx = 1
  592. self?.handdler.resultCount = sels.count
  593. self?._showIndexTip()
  594. self?.handdler.showSelection(sel)
  595. }
  596. })
  597. } else {
  598. if self.finding_ {
  599. return
  600. }
  601. self.finding_ = true
  602. let searchS = keyboard
  603. let opt = self.fetchSearchOptions()
  604. self._beginLoading()
  605. DispatchQueue.global().async {
  606. let datas = self.handdler.pdfView?.document.findEditAllPageString(searchS, with: opt) ?? []
  607. DispatchQueue.main.async {
  608. self.finding_ = false
  609. self._endLoading()
  610. if datas.isEmpty {
  611. self._showNoResultsAlert()
  612. return
  613. }
  614. self.currentSel = datas.first?.first
  615. self.handdler.showIdx = 1
  616. var count = 0
  617. for i in datas {
  618. count += i.count
  619. }
  620. self.handdler.resultCount = count
  621. self._showIndexTip()
  622. if let sel = self.currentSel {
  623. self.handdler.showSelection(sel)
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. extension KMSearchReplaceWindowController: ComponentGroupDelegate {
  631. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  632. if group == searchGroupView_ {
  633. guard let menuI = menuItemProperty else {
  634. return
  635. }
  636. let idx = group?.menuItemArr.firstIndex(of: menuI)
  637. if idx == 0 { // search
  638. switchType(.search)
  639. } else if idx == 1 { // replace
  640. switchType(.replace)
  641. } else if idx == 3 {
  642. let key = KMNSearchKey.wholeWords.botaSearch
  643. let value = KMDataManager.ud_bool(forKey: key)
  644. KMDataManager.ud_set(!value, forKey: key)
  645. currentSel = nil
  646. // currentModel_ = nil
  647. let data = searchItemView_.inputValue
  648. if data.isEmpty {
  649. search(keyboard: data)
  650. }
  651. } else if idx == 4 {
  652. let key = KMNSearchKey.caseSensitive.botaSearch
  653. let value = KMDataManager.ud_bool(forKey: key)
  654. KMDataManager.ud_set(!value, forKey: key)
  655. currentSel = nil
  656. // currentModel_ = nil
  657. let data = searchItemView_.inputValue
  658. if data.isEmpty {
  659. search(keyboard: data)
  660. }
  661. }
  662. }
  663. }
  664. }