KMSearchReplaceWindowController.swift 26 KB

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