123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //
- // KMSearchReplaceWindowController.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/8/7.
- //
- import Cocoa
- class KMSearchReplaceWindowController_Window: NSWindow {
- // override var isMainWindow: Bool {
- // return true
- // }
- //
- // override var isKeyWindow: Bool {
- // return true
- // }
-
- override var canBecomeMain: Bool {
- return true
- }
-
- override var canBecomeKey: Bool {
- return true
- }
- }
- class KMSearchReplaceWindowController: NSWindowController {
- @IBOutlet weak var titleBarBox: NSBox!
- @IBOutlet weak var closeButton: NSButton!
-
- @IBOutlet weak var tabBox: NSBox!
-
- @IBOutlet weak var searchBox: NSBox!
- @IBOutlet weak var searchTitleLabel: NSTextField!
- @IBOutlet weak var searchInputBox: NSBox!
- @IBOutlet weak var searchInputView: NSTextField!
- @IBOutlet weak var matchWholeCheck: NSButton!
- @IBOutlet weak var caseSensitiveCheck: NSButton!
- @IBOutlet weak var previousButton: NSButton!
- @IBOutlet weak var nextButton: NSButton!
-
- @IBOutlet weak var replaceBox: NSBox!
- @IBOutlet weak var bottomBarBox: NSBox!
- @IBOutlet weak var replaceButton: NSButton!
- @IBOutlet weak var replaceAllButton: NSButton!
-
- private var _modalSession: NSApplication.ModalSession?
-
- private var handdler: KMSearchReplaceHanddler = KMSearchReplaceHanddler()
-
- deinit {
- KMPrint("KMSearchReplaceWindowController deinit.")
- }
-
- convenience init(with pdfView: CPDFView?) {
- self.init(windowNibName: "KMSearchReplaceWindowController")
-
- self.handdler.pdfView = pdfView
- }
- override func windowDidLoad() {
- super.windowDidLoad()
- self.initDefaultValue()
- }
-
- func initDefaultValue() {
- self.window?.isMovableByWindowBackground = true
-
- self.closeButton.imagePosition = .imageOnly
- self.closeButton.image = NSImage(named: "KMImageNameUXIconBtnCloseNor")
- self.closeButton.target = self
- self.closeButton.action = #selector(_closeAction)
-
- self.searchTitleLabel.stringValue = NSLocalizedString("Search", comment: "")
- self.searchInputView.delegate = self
-
- self.matchWholeCheck.title = NSLocalizedString("Whole Words Only", comment: "")
- self.caseSensitiveCheck.title = NSLocalizedString("Ignore Case", comment: "")
- self.previousButton.title = NSLocalizedString("Previous", comment: "")
- self.previousButton.target = self
- self.previousButton.action = #selector(_previousAction)
- self.nextButton.title = NSLocalizedString("Next", comment: "")
- self.nextButton.target = self
- self.nextButton.action = #selector(_nextAction)
-
- self.replaceButton.title = NSLocalizedString("Replace", comment: "")
- self.replaceAllButton.title = NSLocalizedString("Replace All", comment: "")
- }
-
- // MARK: - Actions
-
- @objc private func _closeAction(_ sender: NSButton) {
- self.endModal(sender)
- }
-
- @objc private func _previousAction(_ sender: NSButton) {
- guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx+1) as? KMSearchMode else {
- return
- }
- self.handdler.showIdx += 1
- self.handdler.showSelection(model.selection)
- }
-
- @objc private func _nextAction(_ sender: NSButton) {
- guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx-1) as? KMSearchMode else {
- return
- }
- self.handdler.showIdx -= 1
- self.handdler.showSelection(model.selection)
- }
-
- func startModal(_ sender: AnyObject?) {
- NSApp.stopModal()
-
- var modalCode: NSApplication.ModalResponse?
- if let _win = self.window {
- self._modalSession = NSApp.beginModalSession(for: _win)
- repeat {
- modalCode = NSApp.runModalSession(self._modalSession!)
- } while (modalCode == .continue)
- }
- }
-
- func endModal(_ sender: AnyObject?) {
- if let session = self._modalSession {
- NSApp.stopModal()
- NSApp.endModalSession(session)
- self.window?.orderOut(self)
- }
- if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
- self.window?.kmCurrentWindowC = nil
- }
- }
-
- }
- extension KMSearchReplaceWindowController: NSTextFieldDelegate {
- func controlTextDidEndEditing(_ obj: Notification) {
-
- }
-
- func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
- switch commandSelector {
- case #selector(NSResponder.insertNewline(_:)):
- if let inputView = control as? NSTextField {
- // //当当前TextField按下enter
- if inputView == self.searchInputView {
- self.handdler.search(keyword: self.searchInputView.stringValue, isCase: false, isWholeWord: false, callback: { [weak self] datas in
- if let sel = datas?.first?.selection {
- self?.handdler.showIdx = 0
- self?.handdler.showSelection(sel)
- }
- })
- }
- }
- return true
- default:
- return false
- }
- }
- }
|