KMSearchReplaceWindowController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. }
  17. class KMSearchReplaceWindowController: KMNBaseWindowController {
  18. @IBOutlet weak var titleBarBox: NSBox!
  19. @IBOutlet weak var tabBox: NSBox!
  20. @IBOutlet weak var searchBox: NSBox!
  21. @IBOutlet weak var replaceBox: NSBox!
  22. var replaceCallback: (() -> Void)?
  23. var itemClick: KMCommonClickBlock?
  24. var closeCallback: (() -> Void)?
  25. private var _modalSession: NSApplication.ModalSession?
  26. var handdler: KMNSearchHanddler = KMNSearchHanddler() {
  27. didSet {
  28. if handdler.searchKey?.count ?? 0 > 0 && handdler.searchResults.count == 0 {
  29. self.search(keyboard: handdler.searchKey ?? "")
  30. } else {
  31. self.reloadData()
  32. }
  33. }
  34. }
  35. private var currentSel: CPDFSelection?
  36. private lazy var titleBarView_: KMNSearchReplaceTitleBarView = {
  37. let view = KMNSearchReplaceTitleBarView()
  38. return view
  39. }()
  40. private lazy var searchItemView_: KMNSearchReplaceSearchItemView = {
  41. let view = KMNSearchReplaceSearchItemView()
  42. return view
  43. }()
  44. private lazy var replaceItemView_: KMNSearchReplacePopItemView = {
  45. let view = KMNSearchReplacePopItemView()
  46. return view
  47. }()
  48. var previousButton: ComponentButton {
  49. get {
  50. return searchItemView_.previousButton
  51. }
  52. }
  53. var nextButton: ComponentButton {
  54. get {
  55. return searchItemView_.nextButton
  56. }
  57. }
  58. var replaceAllButton: ComponentButton {
  59. get {
  60. return replaceItemView_.replaceAllButton
  61. }
  62. }
  63. var replaceButton: ComponentButton {
  64. get {
  65. return replaceItemView_.replaceButton
  66. }
  67. }
  68. private var searchGroupView_: ComponentGroup?
  69. convenience init(with pdfView: CPDFView?, type: KMNBotaSearchType) {
  70. self.init(windowNibName: "KMSearchReplaceWindowController")
  71. self.handdler.pdfView = pdfView
  72. handdler.type = type
  73. }
  74. override func windowDidLoad() {
  75. super.windowDidLoad()
  76. self.initDefaultValue()
  77. self.switchType(handdler.type)
  78. }
  79. @objc func didMoveNotification(_ noti: Notification) {
  80. let window = self.window
  81. let frame = window?.frame ?? .zero
  82. let supFrame = window?.parent?.frame ?? .zero
  83. KMPrint("frame: \(frame)")
  84. KMPrint("supFrame: \(supFrame)")
  85. }
  86. func initDefaultValue() {
  87. window?.isMovableByWindowBackground = true
  88. window?.contentView?.wantsLayer = true
  89. window?.contentView?.layer?.cornerRadius = ComponentLibrary.shared.getComponentValueFromKey("radius/m") as? CGFloat ?? 8
  90. window?.contentView?.layer?.masksToBounds = true
  91. window?.backgroundColor = .clear
  92. titleBarBox.boxType = .custom
  93. titleBarBox.borderWidth = 0
  94. titleBarBox.contentView = titleBarView_
  95. titleBarView_.itemClick = { [weak self] idx, _ in
  96. if idx == 1 {
  97. self?._closeAction(NSButton())
  98. } else if idx == 2 {
  99. self?._closeAction(NSButton())
  100. self?.itemClick?(1, self?.handdler)
  101. } else if idx == 3 {
  102. self?.switchType(.search, animate: true)
  103. self?.itemClick?(3, self?.handdler)
  104. if self?.handdler.searchKey?.count ?? 0 > 0 {
  105. self?.search(keyboard: self?.handdler.searchKey ?? "")
  106. }
  107. } else if idx == 4 {
  108. self?.switchType(.replace, animate: true)
  109. self?.itemClick?(4, self?.handdler)
  110. if self?.handdler.searchKey?.count ?? 0 > 0 {
  111. self?.search(keyboard: self?.handdler.searchKey ?? "")
  112. }
  113. }
  114. }
  115. searchBox.borderWidth = 0
  116. searchBox.contentView = searchItemView_
  117. searchItemView_.itemClick = { [unowned self] idx, _ in
  118. if idx == 1 { // Previous
  119. _previousAction(NSButton())
  120. } else if idx == 2 { // next
  121. _nextAction(NSButton())
  122. } else if idx == 3 {
  123. showSearchGroupView(sender: ComponentButton())
  124. }
  125. }
  126. searchItemView_.valueDidChange = { [unowned self] value, _ in
  127. if let data = value as? String {
  128. handdler.searchKey = data
  129. search(keyboard: data)
  130. updateButtonState()
  131. }
  132. }
  133. searchItemView_.inputDidEditBlock = { [unowned self] in
  134. updateButtonState()
  135. let value = searchItemView_.inputValue
  136. if value.isEmpty {
  137. } else {
  138. currentSel = nil
  139. }
  140. }
  141. searchItemView_.input.properties.showSuffix = false
  142. searchItemView_.input.reloadData()
  143. replaceBox.borderWidth = 0
  144. replaceBox.contentView = replaceItemView_
  145. replaceItemView_.itemClick = { [unowned self] idx, _ in
  146. if idx == 1 {
  147. _replaceAllAction(NSButton())
  148. } else if idx == 2 {
  149. _replaceAction(NSButton())
  150. }
  151. }
  152. replaceItemView_.valueDidChange = { [unowned self] value, _ in
  153. if let data = value as? String {
  154. handdler.replaceKey = data
  155. }
  156. }
  157. updateButtonState()
  158. if searchItemView_.inputValue.isEmpty {
  159. } else {
  160. self.currentSel = nil
  161. }
  162. }
  163. override func updateUILanguage() {
  164. super.updateUILanguage()
  165. }
  166. override func updateUIThemeColor() {
  167. super.updateUIThemeColor()
  168. KMMainThreadExecute {
  169. self.searchItemView_.input.properties.leftIcon = NSImage(named: "KMImageNameBotaSearch")
  170. self.searchItemView_.input.reloadData()
  171. self.replaceItemView_.input.properties.leftIcon = NSImage(named: "KMImagenameBotaSearchInputPrefiex")
  172. self.replaceItemView_.input.reloadData()
  173. self.updateViewColor()
  174. }
  175. }
  176. func updateButtonState() {
  177. let value = searchItemView_.inputValue
  178. if value.isEmpty || self.handdler.searchResults.isEmpty {
  179. previousButton.properties.isDisabled = true
  180. previousButton.reloadData()
  181. nextButton.properties.isDisabled = true
  182. nextButton.reloadData()
  183. replaceButton.properties.isDisabled = true
  184. replaceButton.reloadData()
  185. replaceAllButton.properties.isDisabled = true
  186. replaceAllButton.reloadData()
  187. } else {
  188. replaceButton.properties.isDisabled = false
  189. replaceButton.reloadData()
  190. replaceAllButton.properties.isDisabled = false
  191. replaceAllButton.reloadData()
  192. previousButton.properties.isDisabled = (self.handdler.showIdx == 0) ? true : false
  193. previousButton.reloadData()
  194. nextButton.properties.isDisabled = ((self.handdler.showIdx == self.handdler.resultCount - 1) || self.handdler.resultCount == 0) ? true : false
  195. nextButton.reloadData()
  196. }
  197. }
  198. private func updateViewColor() {
  199. let isDark = KMAppearance.isDarkMode()
  200. if isDark {
  201. self.window?.contentView?.wantsLayer = true
  202. self.window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  203. self.window?.contentView?.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/popUp"), 1, 8)
  204. } else {
  205. self.window?.contentView?.wantsLayer = true
  206. self.window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  207. self.window?.contentView?.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/popUp"), 1, 8)
  208. }
  209. self.switchType(handdler.type)
  210. }
  211. func switchType(_ type: KMNBotaSearchType, animate: Bool = false) {
  212. if type == .replace {
  213. if KMMemberInfo.shared.isLogin == false {
  214. KMLoginWindowsController.shared.showWindow(nil)
  215. return
  216. }
  217. }
  218. handdler.type = type
  219. self.titleBarView_.type = type
  220. if type == .search { // 248 112
  221. self.replaceBox.isHidden = true
  222. var frame = self.window?.frame ?? .zero
  223. let height: CGFloat = 156
  224. let heightOffset = frame.size.height - height
  225. frame.origin.y += heightOffset
  226. frame.size.height = height
  227. self.window?.setFrame(frame, display: true, animate: animate)
  228. self.window?.minSize = frame.size
  229. self.window?.maxSize = frame.size
  230. } else if type == .replace { // 388 208
  231. DispatchQueue.main.async {
  232. self.replaceBox.isHidden = false
  233. }
  234. var frame = self.window?.frame ?? .zero
  235. let height:CGFloat = 252
  236. let heightOffset = frame.size.height-height
  237. frame.origin.y += heightOffset
  238. frame.size.height = height
  239. self.window?.setFrame(frame, display: true, animate: animate)
  240. self.window?.minSize = frame.size
  241. self.window?.maxSize = frame.size
  242. // 将事件回调出去
  243. self.replaceCallback?()
  244. }
  245. }
  246. func search(keyboard: String) {
  247. if keyboard.isEmpty {
  248. self.handdler.clear()
  249. self.reloadData()
  250. return
  251. }
  252. let isCase = KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch)
  253. let isWholeWord = KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch)
  254. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  255. self._beginLoading()
  256. self.handdler.search(keyword: keyboard, isCase: isCase, isWholeWord: isWholeWord, isEdit: isEditing, callback: { [weak self] datas in
  257. self?._endLoading()
  258. if datas.count == 0 {
  259. self?._showNoResultsAlert()
  260. }
  261. self?.reloadData()
  262. })
  263. }
  264. func reloadData() {
  265. let handdler = self.handdler
  266. searchItemView_.inputValue = handdler.searchKey ?? ""
  267. replaceItemView_.inputValue = handdler.replaceKey ?? ""
  268. if handdler.showIdx != 0 {
  269. let model = self.handdler.searchResults[handdler.showIdx]
  270. self.handdler.showSelection(model.selection)
  271. } else {
  272. let sels = handdler.searchResults
  273. if let sel = sels.first?.selection {
  274. handdler.showSelection(sel)
  275. } else {
  276. handdler.showSelection(CPDFSelection())
  277. }
  278. }
  279. self._showIndexTip()
  280. self.updateButtonState()
  281. }
  282. }
  283. //MARK: Actions
  284. extension KMSearchReplaceWindowController {
  285. @objc 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 index = self.handdler.previous()
  292. if index < self.handdler.searchResults.count && index >= 0{
  293. let model = self.handdler.searchResults[index]
  294. self.handdler.showSelection(model.selection)
  295. self._showIndexTip()
  296. self.updateButtonState()
  297. }
  298. }
  299. @objc public func _nextAction(_ sender: NSButton?) {
  300. let index = self.handdler.next()
  301. if index < self.handdler.searchResults.count && index >= 0 {
  302. let model = self.handdler.searchResults[index]
  303. self.handdler.showSelection(model.selection)
  304. self._showIndexTip()
  305. self.updateButtonState()
  306. }
  307. }
  308. @objc private func _checkAction(_ sender: NSButton) {
  309. self.currentSel = nil
  310. }
  311. @objc private func _searchTabAction(_ sender: NSButton) {
  312. self.switchType(.search, animate: true)
  313. }
  314. @objc private func _replaceTabAction(_ sender: NSButton) {
  315. self.switchType(.replace, animate: true)
  316. }
  317. @objc private func _replaceAction(_ sender: NSButton) {
  318. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  319. if isEditing == false {
  320. NSSound.beep()
  321. return
  322. }
  323. guard let selection = self.currentSel else { return }
  324. let searchS = self.searchItemView_.inputValue
  325. let replaceS = self.replaceItemView_.inputValue
  326. self.handdler.replace(searchS: searchS, replaceS: replaceS, sel: selection) { [weak self] newSel in
  327. self?.handdler.showSelection(newSel)
  328. }
  329. }
  330. @objc private func _replaceAllAction(_ sender: NSButton) {
  331. let isEditing = self.handdler.pdfView?.isEditing() ?? false
  332. if isEditing == false {
  333. NSSound.beep()
  334. return
  335. }
  336. let datas = self.handdler.pdfView?.document.findEditSelections() ?? []
  337. if datas.isEmpty {
  338. _showNoResultsAlert()
  339. return
  340. }
  341. let searchS = self.searchItemView_.inputValue
  342. let replaceS = self.replaceItemView_.inputValue
  343. self._beginLoading()
  344. DispatchQueue.global().async {
  345. self.handdler.pdfView?.document.replaceAllEditText(with: searchS, toReplace: replaceS)
  346. self.currentSel = nil
  347. DispatchQueue.main.async {
  348. self._endLoading()
  349. self.handdler.pdfView?.setHighlightedSelection(nil, animated: false)
  350. self.handdler.pdfView?.setNeedsDisplayForVisiblePages()
  351. }
  352. }
  353. }
  354. }
  355. //MARK: Alert
  356. extension KMSearchReplaceWindowController {
  357. private func _showNoResultsAlert() {
  358. _ = _showAlert(style: .critical, message: KMLocalizedString("No related content found, please change keyword."), info: "", buttons: [KMLocalizedString("OK", comment: "")])
  359. }
  360. private func _showAlert(style: NSAlert.Style, message: String, info: String, buttons: [String]) -> NSApplication.ModalResponse {
  361. let alert = NSAlert()
  362. alert.alertStyle = style
  363. alert.messageText = message
  364. alert.informativeText = info
  365. for button in buttons {
  366. alert.addButton(withTitle: button)
  367. }
  368. return alert.runModal()
  369. }
  370. private func _showIndexTip() {
  371. DispatchQueue.main.async {
  372. if self.handdler.resultCount == 0 {
  373. self.searchItemView_.input.properties.rightText = ""
  374. } else {
  375. self.searchItemView_.input.properties.rightText = "\(self.handdler.showIdx+1)/\(self.handdler.resultCount)"
  376. }
  377. self.searchItemView_.input.reloadData()
  378. }
  379. }
  380. }
  381. //MARK: ComponentGroupDelegate
  382. extension KMSearchReplaceWindowController: ComponentGroupDelegate {
  383. func showSearchGroupView(sender: ComponentButton) {
  384. var viewHeight: CGFloat = 8
  385. var menuItemArr: [ComponentMenuitemProperty] = []
  386. let titles = ["Search", "Find and Replace", "", "Whole Words", "Case Sensitive"]
  387. for i in titles {
  388. if i.isEmpty {
  389. let menuI = ComponentMenuitemProperty.divider()
  390. menuItemArr.append(menuI)
  391. viewHeight += 8
  392. } else {
  393. let menuI = ComponentMenuitemProperty(text: KMLocalizedString(i))
  394. menuItemArr.append(menuI)
  395. viewHeight += 36
  396. }
  397. }
  398. if handdler.type == .search {
  399. menuItemArr.first?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  400. } else if handdler.type == .replace {
  401. let info = menuItemArr.safe_element(for: 1) as? ComponentMenuitemProperty
  402. info?.righticon = NSImage(named: "KMNImageNameMenuSelect")
  403. }
  404. if let info = menuItemArr.safe_element(for: 3) as? ComponentMenuitemProperty {
  405. if KMDataManager.ud_bool(forKey: KMNSearchKey.wholeWords.botaSearch) {
  406. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  407. }
  408. }
  409. if let info = menuItemArr.last {
  410. if KMDataManager.ud_bool(forKey: KMNSearchKey.caseSensitive.botaSearch) {
  411. info.righticon = NSImage(named: "KMNImageNameMenuSelect")
  412. }
  413. }
  414. let groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  415. searchGroupView_ = groupView
  416. groupView?.groupDelegate = self
  417. groupView?.frame = CGRectMake(310, 0, 200, viewHeight)
  418. groupView?.updateGroupInfo(menuItemArr)
  419. let senderView = self.searchItemView_.input
  420. var point = senderView.convert(senderView.frame.origin, to: nil)
  421. point.y -= viewHeight
  422. point.y -= 30
  423. groupView?.showWithPoint(point, relativeTo: senderView)
  424. // searchGroupTarget = sender
  425. }
  426. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  427. if group == searchGroupView_ {
  428. guard let menuI = menuItemProperty else {
  429. return
  430. }
  431. let idx = group?.menuItemArr.firstIndex(of: menuI)
  432. if idx == 0 { // search
  433. switchType(.search)
  434. } else if idx == 1 { // replace
  435. switchType(.replace)
  436. } else if idx == 3 {
  437. let key = KMNSearchKey.wholeWords.botaSearch
  438. let value = KMDataManager.ud_bool(forKey: key)
  439. KMDataManager.ud_set(!value, forKey: key)
  440. currentSel = nil
  441. let data = searchItemView_.inputValue
  442. if data.isEmpty {
  443. search(keyboard: data)
  444. }
  445. } else if idx == 4 {
  446. let key = KMNSearchKey.caseSensitive.botaSearch
  447. let value = KMDataManager.ud_bool(forKey: key)
  448. KMDataManager.ud_set(!value, forKey: key)
  449. currentSel = nil
  450. let data = searchItemView_.inputValue
  451. if data.isEmpty {
  452. search(keyboard: data)
  453. }
  454. }
  455. }
  456. }
  457. }
  458. //MARK: Load
  459. extension KMSearchReplaceWindowController {
  460. private func _beginLoading() {
  461. self.window?.contentView?.beginLoading()
  462. }
  463. private func _endLoading() {
  464. self.window?.contentView?.endLoading()
  465. }
  466. }
  467. //MARK: Modal
  468. extension KMSearchReplaceWindowController {
  469. func startModal(_ sender: AnyObject?) {
  470. NSApp.stopModal()
  471. var modalCode: NSApplication.ModalResponse?
  472. if let _win = self.window {
  473. self._modalSession = NSApp.beginModalSession(for: _win)
  474. repeat {
  475. modalCode = NSApp.runModalSession(self._modalSession!)
  476. } while (modalCode == .continue)
  477. }
  478. }
  479. func endModal(_ sender: AnyObject?) {
  480. if let session = self._modalSession {
  481. NSApp.stopModal()
  482. NSApp.endModalSession(session)
  483. self.window?.orderOut(self)
  484. }
  485. if let winC = self.window?.kmCurrentWindowC, winC.isEqual(to: self) {
  486. self.window?.kmCurrentWindowC = nil
  487. }
  488. }
  489. }