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: KMBaseXibView {
  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. override 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. setFindString(firstString)
  75. lastChangeCount = findPboard.changeCount
  76. didChange = false
  77. }
  78. }
  79. }
  80. private func windowDidResignKey() {
  81. updateFindPboard()
  82. }
  83. private func updateFindPboard() {
  84. if didChange {
  85. let findPboard = NSPasteboard(name: NSPasteboard.Name.find)
  86. findPboard.clearContents()
  87. findPboard.writeObjects([findString! as NSPasteboardWriting])
  88. lastChangeCount = findPboard.changeCount
  89. didChange = false
  90. }
  91. }
  92. func setFindString(_ newFindString: String?) {
  93. if findString != newFindString {
  94. findString = newFindString
  95. didChange = true
  96. }
  97. }
  98. func findForward(_ forward: Bool) {
  99. var found = true
  100. if let findStringLength = findString?.count, findStringLength > 0 {
  101. guard let searchAction = searchAction else { return }
  102. found = searchAction(self,findString!,forward)
  103. updateFindPboard()
  104. if var words = UserDefaults.standard.object(forKey: "kmDocumentSearchWordArrays") as? [String] {
  105. if let index = words.firstIndex(of: findString!) {
  106. words.remove(at: index)
  107. }
  108. words.insert(findString!, at: 0)
  109. if words.count > 10 {
  110. words.removeLast()
  111. }
  112. UserDefaults.standard.set(words, forKey: "kmDocumentSearchWordArrays")
  113. UserDefaults.standard.synchronize()
  114. refreshSearchBarMenu()
  115. }
  116. }
  117. messageField.isHidden = found
  118. }
  119. @IBAction func find(_ sender: Any) {
  120. findForward((sender as AnyObject).selectedTag() == 1)
  121. }
  122. @IBAction func buttonItemClick_ShowAll(_ sender: NSButton) {
  123. self.findString = findField.stringValue
  124. showAllAction?(self, self.findString ?? "", findField.selectedTag() == 1)
  125. }
  126. @IBAction func remove(_ sender: Any) {
  127. doneAction?(self)
  128. }
  129. @IBAction func toggleCaseInsensitiveFind(_ sender: Any) {
  130. // let caseInsensitive = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey)
  131. // UserDefaults.standard.set(!caseInsensitive, forKey: SKCaseInsensitiveFindKey)
  132. let caseInsensitive = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey)
  133. UserDefaults.standard.set(!caseInsensitive, forKey: SKCaseInsensitiveFindKey)
  134. }
  135. @objc func clearSearchWordHistory(_ sender: Any) {
  136. UserDefaults.standard.removeObject(forKey: "kmDocumentSearchWordArrays")
  137. UserDefaults.standard.synchronize()
  138. refreshSearchBarMenu()
  139. }
  140. @objc func searchPDFKeyWord(_ item: NSMenuItem) {
  141. let word = item.representedObject as? String ?? ""
  142. findField.stringValue = word
  143. findString = word
  144. findForward(true)
  145. }
  146. // override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  147. // if menuItem.action == #selector(toggleCaseInsensitiveFind(_:)) {
  148. // menuItem.state = UserDefaults.standard.bool(forKey: SKCaseInsensitiveFindKey) ? .on : .off
  149. // return true
  150. // }
  151. // return true
  152. // }
  153. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  154. if commandSelector == #selector(cancelOperation(_:)) {
  155. doneButton.performClick(nil)
  156. return true
  157. }
  158. return false
  159. }
  160. }
  161. extension KMSearchFindView: NSSearchFieldDelegate {
  162. func searchFieldDidEndSearching(_ sender: NSSearchField) {
  163. print("searchFieldDidEndSearching")
  164. }
  165. func controlTextDidEndEditing(_ obj: Notification) {
  166. let fied: NSTextField = obj.object as! NSTextField;
  167. if fied.isEqual(self.findField) {
  168. print("controlTextDidEndEditing")
  169. findString = self.findField.stringValue
  170. self.find(self.findField as Any)
  171. }
  172. }
  173. }