KMSearchFindView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // KMSearchFindView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/1/17.
  6. //
  7. import Cocoa
  8. typealias KMSearchFindViewDoneAction = (_ view: KMSearchFindView) -> Void
  9. typealias KMSearchFindViewSearchAction = (_ view: KMSearchFindView, _ searchString: String, _ forward: Bool) -> Bool
  10. typealias KMSearchFindViewShowAllAction = (_ view: KMSearchFindView, _ searchString: String, _ forward: Bool) -> Void
  11. class KMSearchFindView: BaseXibView {
  12. @IBOutlet var findField: NSSearchField!
  13. @IBOutlet var messageField: NSTextField!
  14. @IBOutlet var doneButton: NSButton!
  15. @IBOutlet var navigationButton: NSSegmentedControl!
  16. @IBOutlet var showAllButton: NSButton!
  17. var ownerController: AnyObject?
  18. var findString: String?
  19. private var lastChangeCount: Int = 0
  20. private var didChange: Bool = false
  21. private var animating: Bool = false
  22. var showAllAction: KMSearchFindViewShowAllAction?
  23. var doneAction: KMSearchFindViewDoneAction?
  24. var searchAction: KMSearchFindViewSearchAction?
  25. func setup() {
  26. findField.delegate = self
  27. refreshSearchBarMenu()
  28. self.showAllButton.title = NSLocalizedString("Show All", comment: "")
  29. self.doneButton.title = NSLocalizedString("Done", comment: "")
  30. }
  31. private func refreshSearchBarMenu() {
  32. var words = UserDefaults.standard.object(forKey: "kmDocumentSearchWordArrays") as? [String]
  33. if words == nil {
  34. UserDefaults.standard.setValue([], forKey: "kmDocumentSearchWordArrays")
  35. words = []
  36. }
  37. guard let words = words else { return }
  38. let searchMenu = NSMenu()
  39. for word in words {
  40. var tword = word
  41. if word.count > 15 {
  42. tword = String(word.prefix(15)) + "..."
  43. }
  44. let item = searchMenu.addItem(withTitle: tword, action: #selector(searchPDFKeyWord(_:)), keyEquivalent: "")
  45. item.representedObject = tword
  46. }
  47. let attrited = [NSAttributedString.Key.font: NSFont(name: "Helvetica", size: 12.0)!]
  48. if words.count > 0 {
  49. let attributedString = NSAttributedString(string: NSLocalizedString("Search History", comment: ""), attributes: attrited)
  50. let item = NSMenuItem()
  51. item.attributedTitle = attributedString
  52. searchMenu.insertItem(item, at: 0)
  53. searchMenu.addItem(NSMenuItem.separator())
  54. let attributedString1 = NSAttributedString(string: NSLocalizedString("Clear Search History", comment: ""), attributes: attrited)
  55. let item1 = NSMenuItem()
  56. item1.attributedTitle = attributedString1
  57. item1.action = #selector(clearSearchWordHistory(_:))
  58. item1.target = self
  59. searchMenu.addItem(item1)
  60. searchMenu.insertItem(NSMenuItem.separator(), at: 0)
  61. }
  62. let attributedString2 = NSAttributedString(string: NSLocalizedString("Ignore Case", comment: ""), attributes: attrited)
  63. let ignoreItem = NSMenuItem()
  64. ignoreItem.attributedTitle = attributedString2
  65. ignoreItem.action = #selector(toggleCaseInsensitiveFind(_:))
  66. ignoreItem.target = self
  67. searchMenu.insertItem(ignoreItem, at: 0)
  68. (findField.cell! as! NSSearchFieldCell).searchMenuTemplate = searchMenu
  69. }
  70. private func windowDidBecomeKey() {
  71. let findPboard = NSPasteboard(name: NSPasteboard.Name.find)
  72. if lastChangeCount < findPboard.changeCount {
  73. if let strings = findPboard.readObjects(forClasses: [NSString.self], options: [:]) as? [String], let firstString = strings.first {
  74. lastChangeCount = findPboard.changeCount
  75. didChange = false
  76. }
  77. }
  78. }
  79. private func windowDidResignKey() {
  80. updateFindPboard()
  81. }
  82. private func updateFindPboard() {
  83. if didChange {
  84. let findPboard = NSPasteboard(name: NSPasteboard.Name.find)
  85. findPboard.clearContents()
  86. findPboard.writeObjects([findString! as NSPasteboardWriting])
  87. lastChangeCount = findPboard.changeCount
  88. didChange = false
  89. }
  90. }
  91. func findForward(_ forward: Bool) {
  92. var found = true
  93. if let findStringLength = findString?.count, findStringLength > 0 {
  94. guard let searchAction = searchAction else { return }
  95. found = searchAction(self,findString!,forward)
  96. updateFindPboard()
  97. if var words = UserDefaults.standard.object(forKey: "kmDocumentSearchWordArrays") as? [String] {
  98. if let index = words.firstIndex(of: findString!) {
  99. words.remove(at: index)
  100. }
  101. words.insert(findString!, at: 0)
  102. if words.count > 10 {
  103. words.removeLast()
  104. }
  105. UserDefaults.standard.set(words, forKey: "kmDocumentSearchWordArrays")
  106. UserDefaults.standard.synchronize()
  107. refreshSearchBarMenu()
  108. }
  109. }
  110. messageField.isHidden = found
  111. }
  112. @IBAction func find(_ sender: Any) {
  113. findForward((sender as AnyObject).selectedTag() == 1)
  114. }
  115. @IBAction func buttonItemClick_ShowAll(_ sender: NSButton) {
  116. self.findString = findField.stringValue
  117. showAllAction?(self, self.findString ?? "", findField.selectedTag() == 1)
  118. }
  119. @IBAction func remove(_ sender: Any) {
  120. doneAction?(self)
  121. }
  122. @IBAction func toggleCaseInsensitiveFind(_ sender: Any) {
  123. // let caseInsensitive = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey)
  124. // UserDefaults.standard.set(!caseInsensitive, forKey: SKCaseInsensitiveFindKey)
  125. let caseInsensitive = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey)
  126. UserDefaults.standard.set(!caseInsensitive, forKey: SKCaseInsensitiveFindKey)
  127. }
  128. @objc func clearSearchWordHistory(_ sender: Any) {
  129. UserDefaults.standard.removeObject(forKey: "kmDocumentSearchWordArrays")
  130. UserDefaults.standard.synchronize()
  131. refreshSearchBarMenu()
  132. }
  133. @objc func searchPDFKeyWord(_ item: NSMenuItem) {
  134. let word = item.representedObject as? String ?? ""
  135. findField.stringValue = word
  136. findString = word
  137. findForward(true)
  138. }
  139. @objc func searchString(_ string: String) {
  140. let word = string
  141. findField.stringValue = word
  142. findString = word
  143. findForward(true)
  144. }
  145. // override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  146. // if menuItem.action == #selector(toggleCaseInsensitiveFind(_:)) {
  147. // menuItem.state = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey) ? .on : .off
  148. // return true
  149. // }
  150. // return true
  151. // }
  152. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  153. if commandSelector == #selector(cancelOperation(_:)) {
  154. doneButton.performClick(nil)
  155. return true
  156. }
  157. return false
  158. }
  159. }
  160. extension KMSearchFindView: NSSearchFieldDelegate {
  161. func searchFieldDidEndSearching(_ sender: NSSearchField) {
  162. print("searchFieldDidEndSearching")
  163. }
  164. func controlTextDidEndEditing(_ obj: Notification) {
  165. let fied: NSTextField = obj.object as! NSTextField;
  166. if fied.isEqual(self.findField) {
  167. print("controlTextDidEndEditing")
  168. findString = self.findField.stringValue
  169. self.find(self.findField as Any)
  170. }
  171. }
  172. }