KMSearchReplaceWindowController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //
  2. // KMSearchReplaceWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/8/7.
  6. //
  7. import Cocoa
  8. class KMSearchReplaceWindowController_Window: NSWindow {
  9. override var canBecomeMain: Bool {
  10. return true
  11. }
  12. override var canBecomeKey: Bool {
  13. return true
  14. }
  15. }
  16. @objc enum KMSearchReplaceType: Int {
  17. case none = 0
  18. case search = 1
  19. case replace = 2
  20. }
  21. class KMSearchReplaceWindowController: NSWindowController {
  22. @IBOutlet weak var titleBarBox: NSBox!
  23. @IBOutlet weak var closeButton: NSButton!
  24. @IBOutlet weak var tabBox: NSBox!
  25. @IBOutlet weak var searchTabButton: NSButton!
  26. @IBOutlet weak var replaceTabButton: NSButton!
  27. @IBOutlet weak var tabBottomLine: NSBox!
  28. @IBOutlet weak var tabSelectedLine: NSBox!
  29. @IBOutlet weak var tabSelectedLineLeftConst: NSLayoutConstraint!
  30. @IBOutlet weak var searchBox: NSBox!
  31. @IBOutlet weak var searchTitleLabel: NSTextField!
  32. @IBOutlet weak var searchInputBox: NSBox!
  33. @IBOutlet weak var searchInputView: NSTextField!
  34. @IBOutlet weak var matchWholeCheck: NSButton!
  35. @IBOutlet weak var caseSensitiveCheck: NSButton!
  36. @IBOutlet weak var previousButton: NSButton!
  37. @IBOutlet weak var nextButton: NSButton!
  38. @IBOutlet weak var replaceBox: NSBox!
  39. @IBOutlet weak var replaceTitleLabel: NSTextField!
  40. @IBOutlet weak var replaceInputBox: NSBox!
  41. @IBOutlet weak var replaceInputView: NSTextField!
  42. @IBOutlet weak var bottomBarBox: NSBox!
  43. @IBOutlet weak var replaceButton: NSButton!
  44. @IBOutlet weak var replaceAllButton: NSButton!
  45. var replaceCallback: (() -> Void)?
  46. private var _modalSession: NSApplication.ModalSession?
  47. private var handdler: KMSearchReplaceHanddler = KMSearchReplaceHanddler()
  48. private var type_: KMSearchReplaceType = .search
  49. private var currentSel: CPDFSelection?
  50. deinit {
  51. KMPrint("KMSearchReplaceWindowController deinit.")
  52. DistributedNotificationCenter.default().removeObserver(self)
  53. }
  54. convenience init(with pdfView: CPDFView?, type: KMSearchReplaceType) {
  55. self.init(windowNibName: "KMSearchReplaceWindowController")
  56. self.handdler.pdfView = pdfView
  57. self.type_ = type
  58. }
  59. override func windowDidLoad() {
  60. super.windowDidLoad()
  61. self.initDefaultValue()
  62. self.switchType(self.type_)
  63. self.updateViewColor()
  64. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  65. }
  66. func initDefaultValue() {
  67. self.window?.isMovableByWindowBackground = true
  68. self.titleBarBox.boxType = .custom
  69. self.titleBarBox.borderWidth = 0
  70. self.closeButton.imagePosition = .imageOnly
  71. self.closeButton.image = NSImage(named: "KMImageNameUXIconBtnCloseNor")
  72. self.closeButton.target = self
  73. self.closeButton.action = #selector(_closeAction)
  74. self.searchTabButton.target = self
  75. self.searchTabButton.action = #selector(_searchTabAction)
  76. self.searchTabButton.title = " \(NSLocalizedString("Search", comment: ""))"
  77. self.searchTabButton.image = NSImage(named: "KMImageNameSearchIcon")
  78. self.searchTabButton.imagePosition = .imageLeft
  79. // self.searchTabButton.imageHugsTitle = true
  80. self.replaceTabButton.target = self
  81. self.replaceTabButton.action = #selector(_replaceTabAction)
  82. self.replaceTabButton.title = " \(NSLocalizedString("Replace", comment: ""))"
  83. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceIcon")
  84. self.replaceTabButton.imagePosition = .imageLeft
  85. self.tabSelectedLine.borderWidth = 0
  86. self.tabSelectedLine.fillColor = NSColor(hex: "#4982E6")
  87. self.searchBox.borderWidth = 0
  88. // #0E1114
  89. self.searchTitleLabel.stringValue = NSLocalizedString("Search", comment: "")
  90. self.searchTitleLabel.font = NSFont.SFProTextBoldFont(14)
  91. self.searchInputBox.cornerRadius = 0
  92. self.searchInputView.drawsBackground = false
  93. self.searchInputView.isBordered = false
  94. self.searchInputView.delegate = self
  95. self.matchWholeCheck.title = NSLocalizedString("Whole Words Only", comment: "")
  96. self.matchWholeCheck.target = self
  97. self.matchWholeCheck.action = #selector(_checkAction)
  98. self.caseSensitiveCheck.title = NSLocalizedString("Ignore Case", comment: "")
  99. self.caseSensitiveCheck.target = self
  100. self.caseSensitiveCheck.action = #selector(_checkAction)
  101. self.previousButton.title = NSLocalizedString("Next", comment: "")
  102. self.previousButton.target = self
  103. self.previousButton.action = #selector(_nextAction)
  104. self.nextButton.title = NSLocalizedString("Previous", comment: "")
  105. self.nextButton.target = self
  106. self.nextButton.action = #selector(_previousAction)
  107. self.replaceBox.borderWidth = 0
  108. self.replaceTitleLabel.stringValue = NSLocalizedString("Replace", comment: "")
  109. self.replaceTitleLabel.font = NSFont.SFProTextBoldFont(14)
  110. self.replaceInputBox.cornerRadius = 0
  111. self.replaceInputView.drawsBackground = false
  112. self.replaceInputView.isBordered = false
  113. self.replaceInputView.delegate = self
  114. self.bottomBarBox.borderWidth = 0
  115. self.replaceButton.title = NSLocalizedString("Replace", comment: "")
  116. self.replaceButton.target = self
  117. self.replaceButton.action = #selector(_replaceAction)
  118. self.replaceAllButton.title = NSLocalizedString("Replace All", comment: "")
  119. self.replaceAllButton.target = self
  120. self.replaceAllButton.action = #selector(_replaceAllAction)
  121. }
  122. // MARK: - Actions
  123. @objc private func _closeAction(_ sender: NSButton) {
  124. self.endModal(sender)
  125. }
  126. @objc private func _previousAction(_ sender: NSButton) {
  127. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  128. if isEditing == false {
  129. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx-1) as? KMSearchMode else {
  130. return
  131. }
  132. self.handdler.showIdx -= 1
  133. self.handdler.showSelection(model.selection)
  134. } else {
  135. if let _ = self.currentSel {
  136. self.currentSel = self.handdler.pdfView?.document.findForwardEditText()
  137. if let sel = self.currentSel {
  138. self.handdler.showSelection(sel)
  139. } else {
  140. let alert = NSAlert()
  141. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  142. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  143. alert.runModal()
  144. }
  145. } else {
  146. let searchS = self.searchInputView.stringValue
  147. let opt = self.fetchSearchOptions()
  148. self._beginLoading()
  149. DispatchQueue.global().async {
  150. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  151. DispatchQueue.main.async {
  152. self._endLoading()
  153. let sel = datas?.first?.first
  154. if sel == nil {
  155. let alert = NSAlert()
  156. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  157. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  158. alert.runModal()
  159. return
  160. }
  161. self.currentSel = sel
  162. self.handdler.showSelection(sel)
  163. }
  164. }
  165. }
  166. }
  167. }
  168. @objc private func _nextAction(_ sender: NSButton) {
  169. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  170. if isEditing == false {
  171. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx+1) as? KMSearchMode else {
  172. return
  173. }
  174. self.handdler.showIdx += 1
  175. self.handdler.showSelection(model.selection)
  176. } else {
  177. if let _ = self.currentSel {
  178. self.currentSel = self.handdler.pdfView?.document.findBackwordEditText()
  179. if let sel = self.currentSel {
  180. self.handdler.showSelection(sel)
  181. } else {
  182. let alert = NSAlert()
  183. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  184. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  185. alert.runModal()
  186. }
  187. } else {
  188. let searchS = self.searchInputView.stringValue
  189. let opt = self.fetchSearchOptions()
  190. self._beginLoading()
  191. DispatchQueue.global().async {
  192. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  193. DispatchQueue.main.async {
  194. self._endLoading()
  195. let sel = datas?.first?.first
  196. if sel == nil {
  197. let alert = NSAlert()
  198. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  199. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  200. alert.runModal()
  201. return
  202. }
  203. self.currentSel = sel
  204. self.handdler.showSelection(sel)
  205. }
  206. }
  207. }
  208. }
  209. }
  210. @objc private func _checkAction(_ sender: NSButton) {
  211. self.currentSel = nil
  212. }
  213. @objc private func _searchTabAction(_ sender: NSButton) {
  214. self.switchType(.search, animate: true)
  215. }
  216. @objc private func _replaceTabAction(_ sender: NSButton) {
  217. self.switchType(.replace, animate: true)
  218. }
  219. @objc private func _replaceAction(_ sender: NSButton) {
  220. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  221. if isEditing == false {
  222. NSSound.beep()
  223. return
  224. }
  225. if let sel = self.currentSel {
  226. let searchS = self.searchInputView.stringValue
  227. let replaceS = self.replaceInputView.stringValue
  228. let success = self.handdler.replace(searchS: searchS, replaceS: replaceS, sel: sel) { [weak self] newSel in
  229. self?.handdler.showSelection(newSel)
  230. }
  231. if success {
  232. self.handdler.showSelection(sel)
  233. }
  234. } else { // 先查找
  235. let searchS = self.searchInputView.stringValue
  236. let opt = self.fetchSearchOptions()
  237. self._beginLoading()
  238. DispatchQueue.global().async {
  239. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  240. DispatchQueue.main.async {
  241. self._endLoading()
  242. let sel = datas?.first?.first
  243. if sel == nil {
  244. let alert = NSAlert()
  245. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  246. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  247. alert.runModal()
  248. return
  249. }
  250. self.currentSel = sel
  251. self.handdler.showSelection(sel)
  252. }
  253. }
  254. }
  255. }
  256. @objc private func _replaceAllAction(_ sender: NSButton) {
  257. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  258. if isEditing == false {
  259. NSSound.beep()
  260. return
  261. }
  262. let datas = self.handdler.pdfView?.document.findEditSelections() ?? []
  263. if datas.isEmpty {
  264. let alert = NSAlert()
  265. alert.informativeText = NSLocalizedString("The search item was not found.", comment: "")
  266. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  267. alert.beginSheetModal(for: NSApp.mainWindow!)
  268. return
  269. }
  270. let searchS = self.searchInputView.stringValue
  271. let replaceS = self.replaceInputView.stringValue
  272. self._beginLoading()
  273. DispatchQueue.global().async {
  274. self.handdler.pdfView?.document.replaceAllEditText(with: searchS, toReplace: replaceS)
  275. self.currentSel = nil
  276. DispatchQueue.main.async {
  277. self._endLoading()
  278. self.handdler.pdfView?.setHighlightedSelection(nil, animated: false)
  279. self.handdler.pdfView?.setNeedsDisplayForVisiblePages()
  280. }
  281. }
  282. }
  283. private func fetchSearchOptions() -> CPDFSearchOptions {
  284. var opt = CPDFSearchOptions()
  285. let isCase = self.caseSensitiveCheck.state == .off
  286. if isCase {
  287. opt.insert(.caseSensitive)
  288. }
  289. let isWholeWord = self.matchWholeCheck.state == .on
  290. if isWholeWord {
  291. opt.insert(.matchWholeWord)
  292. }
  293. return opt
  294. }
  295. private func updateViewColor() {
  296. let isDark = KMAppearance.isDarkMode()
  297. if isDark {
  298. self.window?.backgroundColor = NSColor(hex: "#393C3E")
  299. self.searchInputBox.borderColor = NSColor(hex: "#56585A")
  300. self.replaceInputBox.borderColor = NSColor(hex: "#56585A")
  301. } else {
  302. self.window?.backgroundColor = .white
  303. self.searchInputBox.borderColor = NSColor(hex: "#DADBDE")
  304. self.replaceInputBox.borderColor = NSColor(hex: "#DADBDE")
  305. }
  306. self.switchType(self.type_)
  307. }
  308. func switchType(_ type: KMSearchReplaceType, animate: Bool = false) {
  309. if type == .replace {
  310. if IAPProductsManager.default().isAvailableAllFunction() == false {
  311. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  312. return
  313. }
  314. }
  315. self.type_ = type
  316. let isDark = KMAppearance.isDarkMode()
  317. var selectedColor = NSColor(hex: "0E1114")
  318. var unSelectedColor = NSColor(hex: "757780")
  319. if isDark {
  320. selectedColor = .white
  321. unSelectedColor = NSColor(hex: "#7E7F85")
  322. }
  323. if type == .search { // 248
  324. self.tabSelectedLineLeftConst.animator().constant = 24
  325. self.searchTabButton.setTitleColor(selectedColor)
  326. self.searchTabButton.image = NSImage(named: "KMImageNameSearchIcon")
  327. self.replaceTabButton.setTitleColor(unSelectedColor)
  328. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceUnselectedIcon")
  329. // DispatchQueue.main.async {
  330. self.replaceBox.isHidden = true
  331. self.bottomBarBox.isHidden = true
  332. // }
  333. var frame = self.window?.frame ?? .zero
  334. let height: CGFloat = 248+20
  335. let heightOffset = frame.size.height - height
  336. frame.origin.y += heightOffset
  337. frame.size.height = height
  338. self.window?.setFrame(frame, display: true, animate: animate)
  339. self.window?.minSize = frame.size
  340. self.window?.maxSize = frame.size
  341. } else if type == .replace { // 388
  342. self.tabSelectedLineLeftConst.animator().constant = 140
  343. self.searchTabButton.setTitleColor(unSelectedColor)
  344. self.searchTabButton.image = NSImage(named: "KMImageNameSearchUnselectedIcon")
  345. self.replaceTabButton.setTitleColor(selectedColor)
  346. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceIcon")
  347. DispatchQueue.main.async {
  348. self.replaceBox.isHidden = false
  349. self.bottomBarBox.isHidden = false
  350. }
  351. var frame = self.window?.frame ?? .zero
  352. let height:CGFloat = 388
  353. let heightOffset = frame.size.height-height
  354. frame.origin.y += heightOffset
  355. frame.size.height = height
  356. self.window?.setFrame(frame, display: true, animate: animate)
  357. self.window?.minSize = frame.size
  358. self.window?.maxSize = frame.size
  359. // 将事件回调出去
  360. self.replaceCallback?()
  361. }
  362. }
  363. private func _beginLoading() {
  364. self.window?.contentView?.beginLoading()
  365. }
  366. private func _endLoading() {
  367. self.window?.contentView?.endLoading()
  368. }
  369. func startModal(_ sender: AnyObject?) {
  370. NSApp.stopModal()
  371. var modalCode: NSApplication.ModalResponse?
  372. if let _win = self.window {
  373. self._modalSession = NSApp.beginModalSession(for: _win)
  374. repeat {
  375. modalCode = NSApp.runModalSession(self._modalSession!)
  376. } while (modalCode == .continue)
  377. }
  378. }
  379. func endModal(_ sender: AnyObject?) {
  380. if let session = self._modalSession {
  381. NSApp.stopModal()
  382. NSApp.endModalSession(session)
  383. self.window?.orderOut(self)
  384. }
  385. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  386. self.window?.kmCurrentWindowC = nil
  387. }
  388. }
  389. // MARK: - Noti Methods
  390. @objc func themeChanged(_ notification: Notification) {
  391. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  392. self.updateViewColor()
  393. }
  394. }
  395. }
  396. extension KMSearchReplaceWindowController: NSTextFieldDelegate {
  397. func controlTextDidEndEditing(_ obj: Notification) {
  398. }
  399. func controlTextDidChange(_ obj: Notification) {
  400. if self.searchInputView.isEqual(to: obj.object) { // 搜索输入框
  401. if self.searchInputView.stringValue.isEmpty {
  402. self.previousButton.isEnabled = false
  403. self.nextButton.isEnabled = false
  404. self.replaceButton.isEnabled = false
  405. self.replaceAllButton.isEnabled = false
  406. } else {
  407. self.previousButton.isEnabled = true
  408. self.nextButton.isEnabled = true
  409. self.replaceButton.isEnabled = true
  410. self.replaceAllButton.isEnabled = true
  411. }
  412. }
  413. }
  414. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  415. switch commandSelector {
  416. case #selector(NSResponder.insertNewline(_:)):
  417. if let inputView = control as? NSTextField {
  418. // //当当前TextField按下enter
  419. if inputView == self.searchInputView {
  420. let isCase = self.caseSensitiveCheck.state == .off
  421. let isWholeWord = self.matchWholeCheck.state == .on
  422. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  423. if isEditing == false {
  424. self._beginLoading()
  425. self.handdler.search(keyword: self.searchInputView.stringValue, isCase: isCase, isWholeWord: isWholeWord, callback: { [weak self] datas in
  426. self?._endLoading()
  427. if let sel = datas?.first?.selection {
  428. self?.handdler.showIdx = 0
  429. self?.handdler.showSelection(sel)
  430. }
  431. })
  432. } else {
  433. let searchS = self.searchInputView.stringValue
  434. let opt = self.fetchSearchOptions()
  435. self._beginLoading()
  436. DispatchQueue.global().async {
  437. let datas = self.handdler.pdfView?.document.findEditAllPageString(searchS, with: opt) ?? []
  438. DispatchQueue.main.async {
  439. self._endLoading()
  440. if datas.isEmpty {
  441. let alert = NSAlert()
  442. alert.informativeText = NSLocalizedString("The search item was not found.", comment: "")
  443. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  444. alert.beginSheetModal(for: NSApp.mainWindow!)
  445. return
  446. }
  447. self.currentSel = datas.first?.first
  448. if let sel = self.currentSel {
  449. self.handdler.showSelection(sel)
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
  456. return true
  457. default:
  458. return false
  459. }
  460. }
  461. }