KMSearchReplaceWindowController.swift 27 KB

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