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