KMSearchReplaceWindowController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 isMainWindow: Bool {
  10. // return true
  11. // }
  12. //
  13. // override var isKeyWindow: Bool {
  14. // return true
  15. // }
  16. override var canBecomeMain: Bool {
  17. return true
  18. }
  19. override var canBecomeKey: Bool {
  20. return true
  21. }
  22. }
  23. class KMSearchReplaceWindowController: NSWindowController {
  24. @IBOutlet weak var titleBarBox: NSBox!
  25. @IBOutlet weak var closeButton: NSButton!
  26. @IBOutlet weak var tabBox: NSBox!
  27. @IBOutlet weak var searchBox: NSBox!
  28. @IBOutlet weak var searchTitleLabel: NSTextField!
  29. @IBOutlet weak var searchInputBox: NSBox!
  30. @IBOutlet weak var searchInputView: NSTextField!
  31. @IBOutlet weak var matchWholeCheck: NSButton!
  32. @IBOutlet weak var caseSensitiveCheck: NSButton!
  33. @IBOutlet weak var previousButton: NSButton!
  34. @IBOutlet weak var nextButton: NSButton!
  35. @IBOutlet weak var replaceBox: NSBox!
  36. @IBOutlet weak var bottomBarBox: NSBox!
  37. @IBOutlet weak var replaceButton: NSButton!
  38. @IBOutlet weak var replaceAllButton: NSButton!
  39. private var _modalSession: NSApplication.ModalSession?
  40. private var handdler: KMSearchReplaceHanddler = KMSearchReplaceHanddler()
  41. deinit {
  42. KMPrint("KMSearchReplaceWindowController deinit.")
  43. }
  44. convenience init(with pdfView: CPDFView?) {
  45. self.init(windowNibName: "KMSearchReplaceWindowController")
  46. self.handdler.pdfView = pdfView
  47. }
  48. override func windowDidLoad() {
  49. super.windowDidLoad()
  50. self.initDefaultValue()
  51. }
  52. func initDefaultValue() {
  53. self.window?.isMovableByWindowBackground = true
  54. self.closeButton.imagePosition = .imageOnly
  55. self.closeButton.image = NSImage(named: "KMImageNameUXIconBtnCloseNor")
  56. self.closeButton.target = self
  57. self.closeButton.action = #selector(_closeAction)
  58. self.searchTitleLabel.stringValue = NSLocalizedString("Search", comment: "")
  59. self.searchInputView.delegate = self
  60. self.matchWholeCheck.title = NSLocalizedString("Whole Words Only", comment: "")
  61. self.caseSensitiveCheck.title = NSLocalizedString("Ignore Case", comment: "")
  62. self.previousButton.title = NSLocalizedString("Previous", comment: "")
  63. self.previousButton.target = self
  64. self.previousButton.action = #selector(_previousAction)
  65. self.nextButton.title = NSLocalizedString("Next", comment: "")
  66. self.nextButton.target = self
  67. self.nextButton.action = #selector(_nextAction)
  68. self.replaceButton.title = NSLocalizedString("Replace", comment: "")
  69. self.replaceAllButton.title = NSLocalizedString("Replace All", comment: "")
  70. }
  71. // MARK: - Actions
  72. @objc private func _closeAction(_ sender: NSButton) {
  73. self.endModal(sender)
  74. }
  75. @objc private func _previousAction(_ sender: NSButton) {
  76. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx+1) as? KMSearchMode else {
  77. return
  78. }
  79. self.handdler.showIdx += 1
  80. self.handdler.showSelection(model.selection)
  81. }
  82. @objc private func _nextAction(_ sender: NSButton) {
  83. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx-1) as? KMSearchMode else {
  84. return
  85. }
  86. self.handdler.showIdx -= 1
  87. self.handdler.showSelection(model.selection)
  88. }
  89. func startModal(_ sender: AnyObject?) {
  90. NSApp.stopModal()
  91. var modalCode: NSApplication.ModalResponse?
  92. if let _win = self.window {
  93. self._modalSession = NSApp.beginModalSession(for: _win)
  94. repeat {
  95. modalCode = NSApp.runModalSession(self._modalSession!)
  96. } while (modalCode == .continue)
  97. }
  98. }
  99. func endModal(_ sender: AnyObject?) {
  100. if let session = self._modalSession {
  101. NSApp.stopModal()
  102. NSApp.endModalSession(session)
  103. self.window?.orderOut(self)
  104. }
  105. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  106. self.window?.kmCurrentWindowC = nil
  107. }
  108. }
  109. }
  110. extension KMSearchReplaceWindowController: NSTextFieldDelegate {
  111. func controlTextDidEndEditing(_ obj: Notification) {
  112. }
  113. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  114. switch commandSelector {
  115. case #selector(NSResponder.insertNewline(_:)):
  116. if let inputView = control as? NSTextField {
  117. // //当当前TextField按下enter
  118. if inputView == self.searchInputView {
  119. self.handdler.search(keyword: self.searchInputView.stringValue, isCase: false, isWholeWord: false, callback: { [weak self] datas in
  120. if let sel = datas?.first?.selection {
  121. self?.handdler.showIdx = 0
  122. self?.handdler.showSelection(sel)
  123. }
  124. })
  125. }
  126. }
  127. return true
  128. default:
  129. return false
  130. }
  131. }
  132. }