KMSearchReplaceWindowController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. }
  53. convenience init(with pdfView: CPDFView?, type: KMSearchReplaceType) {
  54. self.init(windowNibName: "KMSearchReplaceWindowController")
  55. self.handdler.pdfView = pdfView
  56. self.type_ = type
  57. }
  58. override func windowDidLoad() {
  59. super.windowDidLoad()
  60. self.initDefaultValue()
  61. self.switchType(self.type_)
  62. }
  63. func initDefaultValue() {
  64. self.window?.isMovableByWindowBackground = true
  65. self.titleBarBox.boxType = .custom
  66. self.titleBarBox.borderWidth = 0
  67. self.closeButton.imagePosition = .imageOnly
  68. self.closeButton.image = NSImage(named: "KMImageNameUXIconBtnCloseNor")
  69. self.closeButton.target = self
  70. self.closeButton.action = #selector(_closeAction)
  71. self.searchTabButton.target = self
  72. self.searchTabButton.action = #selector(_searchTabAction)
  73. self.searchTabButton.title = " \(NSLocalizedString("Search", comment: ""))"
  74. self.searchTabButton.image = NSImage(named: "KMImageNameSearchIcon")
  75. self.searchTabButton.imagePosition = .imageLeft
  76. // self.searchTabButton.imageHugsTitle = true
  77. self.replaceTabButton.target = self
  78. self.replaceTabButton.action = #selector(_replaceTabAction)
  79. self.replaceTabButton.title = " \(NSLocalizedString("Replace", comment: ""))"
  80. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceIcon")
  81. self.replaceTabButton.imagePosition = .imageLeft
  82. self.tabSelectedLine.borderWidth = 0
  83. self.tabSelectedLine.fillColor = NSColor(hex: "#4982E6")
  84. self.searchBox.borderWidth = 0
  85. self.searchTitleLabel.stringValue = NSLocalizedString("Search", comment: "")
  86. self.searchInputBox.cornerRadius = 0
  87. self.searchInputView.drawsBackground = false
  88. self.searchInputView.isBordered = false
  89. self.searchInputView.delegate = self
  90. self.matchWholeCheck.title = NSLocalizedString("Whole Words Only", comment: "")
  91. self.caseSensitiveCheck.title = NSLocalizedString("Ignore Case", comment: "")
  92. self.previousButton.title = NSLocalizedString("Previous", comment: "")
  93. self.previousButton.target = self
  94. self.previousButton.action = #selector(_previousAction)
  95. self.nextButton.title = NSLocalizedString("Next", comment: "")
  96. self.nextButton.target = self
  97. self.nextButton.action = #selector(_nextAction)
  98. self.replaceBox.borderWidth = 0
  99. self.replaceTitleLabel.stringValue = NSLocalizedString("Replace", comment: "")
  100. self.replaceInputBox.cornerRadius = 0
  101. self.replaceInputView.drawsBackground = false
  102. self.replaceInputView.isBordered = false
  103. self.replaceInputView.delegate = self
  104. self.bottomBarBox.borderWidth = 0
  105. self.replaceButton.title = NSLocalizedString("Replace", comment: "")
  106. self.replaceButton.target = self
  107. self.replaceButton.action = #selector(_replaceAction)
  108. self.replaceAllButton.title = NSLocalizedString("Replace All", comment: "")
  109. self.replaceAllButton.target = self
  110. self.replaceAllButton.action = #selector(_replaceAllAction)
  111. }
  112. // MARK: - Actions
  113. @objc private func _closeAction(_ sender: NSButton) {
  114. self.endModal(sender)
  115. }
  116. @objc private func _previousAction(_ sender: NSButton) {
  117. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  118. if isEditing == false {
  119. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx+1) as? KMSearchMode else {
  120. return
  121. }
  122. self.handdler.showIdx += 1
  123. self.handdler.showSelection(model.selection)
  124. } else {
  125. if let _ = self.currentSel {
  126. self.currentSel = self.handdler.pdfView?.document.findForwardEditText()
  127. if let sel = self.currentSel {
  128. self.handdler.showSelection(sel)
  129. } else {
  130. let alert = NSAlert()
  131. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  132. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  133. alert.runModal()
  134. }
  135. } else {
  136. let searchS = self.searchInputView.stringValue
  137. let opt = self.fetchSearchOptions()
  138. DispatchQueue.global().async {
  139. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  140. DispatchQueue.main.async {
  141. let sel = datas?.first?.first
  142. if sel == nil {
  143. let alert = NSAlert()
  144. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  145. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  146. alert.runModal()
  147. return
  148. }
  149. self.currentSel = sel
  150. self.handdler.showSelection(sel)
  151. }
  152. }
  153. }
  154. }
  155. }
  156. @objc private func _nextAction(_ sender: NSButton) {
  157. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  158. if isEditing == false {
  159. guard let model = self.handdler.searchResults.safe_element(for: self.handdler.showIdx-1) as? KMSearchMode else {
  160. return
  161. }
  162. self.handdler.showIdx -= 1
  163. self.handdler.showSelection(model.selection)
  164. } else {
  165. if let _ = self.currentSel {
  166. self.currentSel = self.handdler.pdfView?.document.findBackwordEditText()
  167. if let sel = self.currentSel {
  168. self.handdler.showSelection(sel)
  169. } else {
  170. let alert = NSAlert()
  171. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  172. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  173. alert.runModal()
  174. }
  175. } else {
  176. let searchS = self.searchInputView.stringValue
  177. let opt = self.fetchSearchOptions()
  178. DispatchQueue.global().async {
  179. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  180. DispatchQueue.main.async {
  181. let sel = datas?.first?.first
  182. if sel == nil {
  183. let alert = NSAlert()
  184. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  185. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  186. alert.runModal()
  187. return
  188. }
  189. self.currentSel = sel
  190. self.handdler.showSelection(sel)
  191. }
  192. }
  193. }
  194. }
  195. }
  196. @objc private func _searchTabAction(_ sender: NSButton) {
  197. self.switchType(.search, animate: true)
  198. }
  199. @objc private func _replaceTabAction(_ sender: NSButton) {
  200. self.switchType(.replace, animate: true)
  201. }
  202. @objc private func _replaceAction(_ sender: NSButton) {
  203. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  204. if isEditing == false {
  205. NSSound.beep()
  206. return
  207. }
  208. if let sel = self.currentSel {
  209. let searchS = self.searchInputView.stringValue
  210. let replaceS = self.replaceInputView.stringValue
  211. let success = self.handdler.replace(searchS: searchS, replaceS: replaceS, sel: sel) { [weak self] newSel in
  212. self?.handdler.showSelection(newSel)
  213. }
  214. if success {
  215. self.handdler.showSelection(sel)
  216. }
  217. } else { // 先查找
  218. let searchS = self.searchInputView.stringValue
  219. let opt = self.fetchSearchOptions()
  220. DispatchQueue.global().async {
  221. let datas = self.handdler.pdfView?.document.startFindEditText(from: nil, with: searchS, options: opt)
  222. DispatchQueue.main.async {
  223. let sel = datas?.first?.first
  224. if sel == nil {
  225. let alert = NSAlert()
  226. alert.messageText = NSLocalizedString("The search item was not found.", comment: "")
  227. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  228. alert.runModal()
  229. return
  230. }
  231. self.currentSel = sel
  232. self.handdler.showSelection(sel)
  233. }
  234. }
  235. }
  236. }
  237. @objc private func _replaceAllAction(_ sender: NSButton) {
  238. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  239. if isEditing == false {
  240. NSSound.beep()
  241. return
  242. }
  243. let datas = self.handdler.pdfView?.document.findEditSelections() ?? []
  244. if datas.isEmpty {
  245. let alert = NSAlert()
  246. alert.informativeText = NSLocalizedString("The search item was not found.", comment: "")
  247. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  248. alert.beginSheetModal(for: NSApp.mainWindow!)
  249. return
  250. }
  251. let searchS = self.searchInputView.stringValue
  252. let replaceS = self.replaceInputView.stringValue
  253. DispatchQueue.global().async {
  254. self.handdler.pdfView?.document.replaceAllEditText(with: searchS, toReplace: replaceS)
  255. self.currentSel = nil
  256. DispatchQueue.main.async {
  257. self.handdler.pdfView?.setHighlightedSelection(nil, animated: false)
  258. self.handdler.pdfView?.setNeedsDisplayForVisiblePages()
  259. }
  260. }
  261. }
  262. private func fetchSearchOptions() -> CPDFSearchOptions {
  263. var opt = CPDFSearchOptions()
  264. let isCase = self.caseSensitiveCheck.state == .on
  265. if isCase {
  266. opt.insert(.caseSensitive)
  267. }
  268. let isWholeWord = self.matchWholeCheck.state == .on
  269. if isWholeWord {
  270. opt.insert(.matchWholeWord)
  271. }
  272. return opt
  273. }
  274. func switchType(_ type: KMSearchReplaceType, animate: Bool = false) {
  275. if type == .replace {
  276. if IAPProductsManager.default().isAvailableAllFunction() == false {
  277. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  278. return
  279. }
  280. }
  281. self.type_ = type
  282. let selectedColor = NSColor(hex: "0E1114")
  283. let unSelectedColor = NSColor(hex: "757780")
  284. if type == .search { // 248
  285. self.tabSelectedLineLeftConst.constant = 24
  286. self.searchTabButton.setTitleColor(selectedColor)
  287. self.searchTabButton.image = NSImage(named: "KMImageNameSearchIcon")
  288. self.replaceTabButton.setTitleColor(unSelectedColor)
  289. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceUnselectedIcon")
  290. // DispatchQueue.main.async {
  291. self.replaceBox.isHidden = true
  292. self.bottomBarBox.isHidden = true
  293. // }
  294. var frame = self.window?.frame ?? .zero
  295. let height: CGFloat = 248+20
  296. let heightOffset = frame.size.height - height
  297. frame.origin.y += heightOffset
  298. frame.size.height = height
  299. self.window?.setFrame(frame, display: true, animate: animate)
  300. self.window?.minSize = frame.size
  301. self.window?.maxSize = frame.size
  302. } else if type == .replace { // 388
  303. self.tabSelectedLineLeftConst.constant = 140
  304. self.searchTabButton.setTitleColor(unSelectedColor)
  305. self.searchTabButton.image = NSImage(named: "KMImageNameSearchUnselectedIcon")
  306. self.replaceTabButton.setTitleColor(selectedColor)
  307. self.replaceTabButton.image = NSImage(named: "KMImageNameReplaceIcon")
  308. DispatchQueue.main.async {
  309. self.replaceBox.isHidden = false
  310. self.bottomBarBox.isHidden = false
  311. }
  312. var frame = self.window?.frame ?? .zero
  313. let height:CGFloat = 388
  314. let heightOffset = frame.size.height-height
  315. frame.origin.y += heightOffset
  316. frame.size.height = height
  317. self.window?.setFrame(frame, display: true, animate: animate)
  318. self.window?.minSize = frame.size
  319. self.window?.maxSize = frame.size
  320. // 将事件回调出去
  321. self.replaceCallback?()
  322. }
  323. }
  324. func startModal(_ sender: AnyObject?) {
  325. NSApp.stopModal()
  326. var modalCode: NSApplication.ModalResponse?
  327. if let _win = self.window {
  328. self._modalSession = NSApp.beginModalSession(for: _win)
  329. repeat {
  330. modalCode = NSApp.runModalSession(self._modalSession!)
  331. } while (modalCode == .continue)
  332. }
  333. }
  334. func endModal(_ sender: AnyObject?) {
  335. if let session = self._modalSession {
  336. NSApp.stopModal()
  337. NSApp.endModalSession(session)
  338. self.window?.orderOut(self)
  339. }
  340. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  341. self.window?.kmCurrentWindowC = nil
  342. }
  343. }
  344. }
  345. extension KMSearchReplaceWindowController: NSTextFieldDelegate {
  346. func controlTextDidEndEditing(_ obj: Notification) {
  347. }
  348. func controlTextDidChange(_ obj: Notification) {
  349. if self.searchInputView.isEqual(to: obj.object) { // 搜索输入框
  350. if self.searchInputView.stringValue.isEmpty {
  351. self.previousButton.isEnabled = false
  352. self.nextButton.isEnabled = false
  353. self.replaceButton.isEnabled = false
  354. self.replaceAllButton.isEnabled = false
  355. } else {
  356. self.previousButton.isEnabled = true
  357. self.nextButton.isEnabled = true
  358. self.replaceButton.isEnabled = true
  359. self.replaceAllButton.isEnabled = true
  360. }
  361. }
  362. }
  363. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  364. switch commandSelector {
  365. case #selector(NSResponder.insertNewline(_:)):
  366. if let inputView = control as? NSTextField {
  367. // //当当前TextField按下enter
  368. if inputView == self.searchInputView {
  369. let isCase = self.caseSensitiveCheck.state == .on
  370. let isWholeWord = self.matchWholeCheck.state == .on
  371. self.handdler.search(keyword: self.searchInputView.stringValue, isCase: isCase, isWholeWord: isWholeWord, callback: { [weak self] datas in
  372. if let sel = datas?.first?.selection {
  373. self?.handdler.showIdx = 0
  374. self?.handdler.showSelection(sel)
  375. }
  376. })
  377. }
  378. }
  379. return true
  380. default:
  381. return false
  382. }
  383. }
  384. }