KMSearchReplaceWindowController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx-1) as? KMSearchMode else {
  283. return
  284. }
  285. self.handdler.showIdx -= 1
  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. self.handdler.showIdx -= 1
  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 = 0
  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. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx+1) as? KMSearchMode else {
  335. return
  336. }
  337. self.handdler.showIdx += 1
  338. self.handdler.showSelection(model.selection)
  339. self._showIndexTip()
  340. } else {
  341. if let _ = self.currentSel {
  342. self.currentSel = self.handdler.pdfView?.document.findBackwordEditText()
  343. if let sel = self.currentSel {
  344. self.handdler.showIdx += 1
  345. self.handdler.showSelection(sel)
  346. self._showIndexTip()
  347. } else {
  348. _showNoResultsAlert()
  349. }
  350. } else {
  351. if self.finding_ {
  352. return
  353. }
  354. self.finding_ = true
  355. let searchS = self.searchItemView_.inputValue
  356. let opt = self.fetchSearchOptions()
  357. self._beginLoading()
  358. DispatchQueue.global().async {
  359. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  360. DispatchQueue.main.async {
  361. self._endLoading()
  362. self.finding_ = false
  363. let sel = datas?.first?.first
  364. if sel == nil {
  365. self._showNoResultsAlert()
  366. return
  367. }
  368. self.handdler.showIdx = 0
  369. var count = 0
  370. for i in datas ?? [] {
  371. count += i.count
  372. }
  373. self.handdler.resultCount = count
  374. self._showIndexTip()
  375. self.currentSel = sel
  376. self.handdler.showSelection(sel)
  377. }
  378. }
  379. }
  380. }
  381. }
  382. @objc private func _checkAction(_ sender: NSButton) {
  383. self.currentSel = nil
  384. }
  385. @objc private func _searchTabAction(_ sender: NSButton) {
  386. self.switchType(.search, animate: true)
  387. }
  388. @objc private func _replaceTabAction(_ sender: NSButton) {
  389. self.switchType(.replace, animate: true)
  390. }
  391. @objc private func _replaceAction(_ sender: NSButton) {
  392. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  393. if isEditing == false {
  394. NSSound.beep()
  395. return
  396. }
  397. if let sel = self.currentSel {
  398. let searchS = self.searchItemView_.inputValue
  399. let replaceS = self.replaceItemView_.inputValue
  400. let success = self.handdler.replace(searchS: searchS, replaceS: replaceS, sel: sel) { [weak self] newSel in
  401. self?.handdler.showSelection(newSel)
  402. }
  403. if success {
  404. // self.handdler.showSelection(sel)
  405. }
  406. } else { // 先查找
  407. if self.finding_ {
  408. return
  409. }
  410. self.finding_ = true
  411. let searchS = self.searchItemView_.inputValue
  412. let opt = self.fetchSearchOptions()
  413. self._beginLoading()
  414. DispatchQueue.global().async {
  415. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  416. DispatchQueue.main.async {
  417. self._endLoading()
  418. self.finding_ = false
  419. let sel = datas?.first?.first
  420. if sel == nil {
  421. self._showNoResultsAlert()
  422. return
  423. }
  424. self.currentSel = sel
  425. self.handdler.showSelection(sel)
  426. }
  427. }
  428. }
  429. }
  430. @objc private func _replaceAllAction(_ sender: NSButton) {
  431. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  432. if isEditing == false {
  433. NSSound.beep()
  434. return
  435. }
  436. let datas = self.handdler.pdfView?.document.findEditSelections() ?? []
  437. if datas.isEmpty {
  438. _showNoResultsAlert()
  439. return
  440. }
  441. if self.finding_ {
  442. return
  443. }
  444. self.finding_ = true
  445. let searchS = self.searchItemView_.inputValue
  446. let replaceS = self.replaceItemView_.inputValue
  447. self._beginLoading()
  448. DispatchQueue.global().async {
  449. self.handdler.pdfView?.document.replaceAllEditText(with: searchS, toReplace: replaceS)
  450. self.currentSel = nil
  451. DispatchQueue.main.async {
  452. self._endLoading()
  453. self.finding_ = false
  454. self.handdler.pdfView?.setHighlightedSelection(nil, animated: false)
  455. self.handdler.pdfView?.setNeedsDisplayForVisiblePages()
  456. }
  457. }
  458. }
  459. private func _showNoResultsAlert() {
  460. _ = _showAlert(style: .critical, message: KMLocalizedString("No related content found, please change keyword."), info: "", buttons: [KMLocalizedString("OK", comment: "")])
  461. }
  462. private func _showAlert(style: NSAlert.Style, message: String, info: String, buttons: [String]) -> NSApplication.ModalResponse {
  463. let alert = NSAlert()
  464. alert.alertStyle = style
  465. alert.messageText = message
  466. alert.informativeText = info
  467. for button in buttons {
  468. alert.addButton(withTitle: button)
  469. }
  470. return alert.runModal()
  471. }
  472. private func _showIndexTip() {
  473. DispatchQueue.main.async {
  474. if self.handdler.resultCount == 0 {
  475. self.searchItemView_.input.properties.rightText = ""
  476. } else {
  477. self.searchItemView_.input.properties.rightText = "\(self.handdler.showIdx+1)/\(self.handdler.resultCount)"
  478. }
  479. self.searchItemView_.input.reloadData()
  480. }
  481. }
  482. private func fetchSearchOptions() -> CPDFSearchOptions {
  483. var opt = CPDFSearchOptions()
  484. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  485. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  486. if isCase {
  487. opt.insert(.caseSensitive)
  488. }
  489. if isWholeWord {
  490. opt.insert(.matchWholeWord)
  491. }
  492. return opt
  493. }
  494. private func updateViewColor() {
  495. let isDark = KMAppearance.isDarkMode()
  496. if isDark {
  497. self.window?.contentView?.wantsLayer = true
  498. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#393C3E").cgColor
  499. } else {
  500. self.window?.contentView?.wantsLayer = true
  501. self.window?.contentView?.layer?.backgroundColor = .white
  502. }
  503. self.switchType(handdler.type)
  504. }
  505. func switchType(_ type: KMNBotaSearchType, animate: Bool = false) {
  506. if type == .replace {
  507. if IAPProductsManager.default().isAvailableAllFunction() == false {
  508. let winC = KMPurchaseCompareWindowController.sharedInstance()
  509. winC?.showWindow(nil)
  510. guard let win = winC?.window else {
  511. return
  512. }
  513. self.window?.addChildWindow(win, ordered: .above)
  514. return
  515. }
  516. }
  517. handdler.type = type
  518. if type == .search { // 248 112
  519. self.replaceBox.isHidden = true
  520. var frame = self.window?.frame ?? .zero
  521. let height: CGFloat = 112
  522. let heightOffset = frame.size.height - height
  523. frame.origin.y += heightOffset
  524. frame.size.height = height
  525. self.window?.setFrame(frame, display: true, animate: animate)
  526. self.window?.minSize = frame.size
  527. self.window?.maxSize = frame.size
  528. } else if type == .replace { // 388 208
  529. DispatchQueue.main.async {
  530. self.replaceBox.isHidden = false
  531. }
  532. var frame = self.window?.frame ?? .zero
  533. let height:CGFloat = 208
  534. let heightOffset = frame.size.height-height
  535. frame.origin.y += heightOffset
  536. frame.size.height = height
  537. self.window?.setFrame(frame, display: true, animate: animate)
  538. self.window?.minSize = frame.size
  539. self.window?.maxSize = frame.size
  540. // 将事件回调出去
  541. self.replaceCallback?()
  542. }
  543. }
  544. private func _beginLoading() {
  545. self.window?.contentView?.beginLoading()
  546. }
  547. private func _endLoading() {
  548. self.window?.contentView?.endLoading()
  549. }
  550. func startModal(_ sender: AnyObject?) {
  551. NSApp.stopModal()
  552. var modalCode: NSApplication.ModalResponse?
  553. if let _win = self.window {
  554. self._modalSession = NSApp.beginModalSession(for: _win)
  555. repeat {
  556. modalCode = NSApp.runModalSession(self._modalSession!)
  557. } while (modalCode == .continue)
  558. }
  559. }
  560. func endModal(_ sender: AnyObject?) {
  561. if let session = self._modalSession {
  562. NSApp.stopModal()
  563. NSApp.endModalSession(session)
  564. self.window?.orderOut(self)
  565. }
  566. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  567. self.window?.kmCurrentWindowC = nil
  568. }
  569. }
  570. func search(keyboard: String) {
  571. if keyboard.isEmpty {
  572. handdler.resultCount = 0
  573. _showIndexTip()
  574. return
  575. }
  576. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  577. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  578. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  579. if isEditing == false {
  580. if self.finding_ {
  581. return
  582. }
  583. self.finding_ = true
  584. self._beginLoading()
  585. self.handdler.search(keyword: keyboard, isCase: isCase, isWholeWord: isWholeWord, callback: { [weak self] datas in
  586. self?.finding_ = false
  587. self?._endLoading()
  588. guard let sels = self?.handdler.searchResults, sels.isEmpty == false else {
  589. self?._showNoResultsAlert()
  590. return
  591. }
  592. if let sel = sels.first?.selection {
  593. self?.handdler.showIdx = 0
  594. self?.handdler.resultCount = sels.count
  595. self?._showIndexTip()
  596. self?.handdler.showSelection(sel)
  597. }
  598. })
  599. } else {
  600. if self.finding_ {
  601. return
  602. }
  603. self.finding_ = true
  604. let searchS = keyboard
  605. let opt = self.fetchSearchOptions()
  606. self._beginLoading()
  607. DispatchQueue.global().async {
  608. let datas = self.handdler.pdfView?.document.findEditAllPageString(searchS, with: opt) ?? []
  609. DispatchQueue.main.async {
  610. self.finding_ = false
  611. self._endLoading()
  612. if datas.isEmpty {
  613. self._showNoResultsAlert()
  614. return
  615. }
  616. self.currentSel = datas.first?.first
  617. self.handdler.showIdx = 0
  618. var count = 0
  619. for i in datas {
  620. count += i.count
  621. }
  622. self.handdler.resultCount = count
  623. self._showIndexTip()
  624. if let sel = self.currentSel {
  625. self.handdler.showSelection(sel)
  626. }
  627. }
  628. }
  629. }
  630. }
  631. }
  632. extension KMSearchReplaceWindowController: ComponentGroupDelegate {
  633. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  634. if group == searchGroupView_ {
  635. guard let menuI = menuItemProperty else {
  636. return
  637. }
  638. let idx = group?.menuItemArr.firstIndex(of: menuI)
  639. if idx == 0 { // search
  640. switchType(.search)
  641. } else if idx == 1 { // replace
  642. switchType(.replace)
  643. } else if idx == 3 {
  644. let key = KMNSearchKey.wholeWords.botaSearch
  645. let value = KMDataManager.ud_bool(forKey: key)
  646. KMDataManager.ud_set(!value, forKey: key)
  647. currentSel = nil
  648. // currentModel_ = nil
  649. let data = searchItemView_.inputValue
  650. if data.isEmpty {
  651. search(keyboard: data)
  652. }
  653. } else if idx == 4 {
  654. let key = KMNSearchKey.caseSensitive.botaSearch
  655. let value = KMDataManager.ud_bool(forKey: key)
  656. KMDataManager.ud_set(!value, forKey: key)
  657. currentSel = nil
  658. // currentModel_ = nil
  659. let data = searchItemView_.inputValue
  660. if data.isEmpty {
  661. search(keyboard: data)
  662. }
  663. }
  664. }
  665. }
  666. }