KMSearchViewController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. //
  2. // KMSearchViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/11/17.
  6. //
  7. import Cocoa
  8. @objc protocol KMSearchViewControllerDelegate {
  9. @objc optional func searchDoneAction(viewController:KMSearchViewController)
  10. @objc optional func searchAction(searchString:String, isCase:Bool)
  11. }
  12. class CSearchFieldCustomCell : NSSearchFieldCell {
  13. required init(coder: NSCoder) {
  14. super.init(coder: coder)
  15. let cancelCell = self.cancelButtonCell
  16. let cancelImage = NSImage(named: "KMImageNameTriBtnClear")
  17. cancelImage?.size = NSMakeSize(16, 16)
  18. cancelCell?.image = cancelImage
  19. cancelCell?.alternateImage = cancelImage
  20. let searchCell = self.searchButtonCell
  21. let searchImage = NSImage(named: "KMImageNameSearchLeftImage")
  22. searchImage?.size = NSMakeSize(16, 16)
  23. searchCell?.image = searchImage
  24. searchCell?.alternateImage = searchImage
  25. }
  26. override func resetCancelButtonCell() {
  27. super.resetCancelButtonCell()
  28. }
  29. }
  30. class KMSearchViewController: NSViewController {
  31. let CPDFOfficeSearchHistoryKey = "CPDFOfficeSearchHistoryKey"
  32. @IBOutlet weak var findTipTextField: NSTextField!
  33. @IBOutlet weak var resultTextField: NSTextField!
  34. @IBOutlet weak var allTipTextField: NSTextField!
  35. @IBOutlet weak var lineView: NSView!
  36. @IBOutlet weak var searchCotentView: NSView!
  37. @IBOutlet weak var searchTextField: FocusAwareSearchTextField!
  38. @IBOutlet weak var doneButton: NSButton!
  39. @IBOutlet weak var outlineView: KMOutlineView!
  40. var isCase = false
  41. let searchFieldMenu = NSMenu()
  42. @IBOutlet weak var emptyView: NSView!
  43. @IBOutlet weak var tipTitleLabel: NSTextField!
  44. @IBOutlet weak var searResultLabel: NSTextField!
  45. //select 多选
  46. var selectItems: [KMSearchMode] = []
  47. var listView : CPDFListView!
  48. var searchResults : [KMSearchMode] = []
  49. var sortResults : [KMSearchMode] = []
  50. var isCancelCell : String = ""
  51. var previousSearchString: String = ""
  52. var previousCase: Bool = true
  53. open weak var delegate: KMSearchViewControllerDelegate?
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. // Do view setup here.
  57. self.setup()
  58. }
  59. override func viewDidAppear() {
  60. super.viewDidAppear()
  61. NSApplication.shared.mainWindow?.makeFirstResponder(self.searchTextField)
  62. }
  63. override func viewWillDisappear() {
  64. super.viewWillDisappear()
  65. self.cancelAllSearchModel()
  66. }
  67. override func viewWillAppear() {
  68. super.viewWillAppear()
  69. self.selectAllSearchModel()
  70. }
  71. func setup() {
  72. self.view.backgroundColor(NSColor(hex: "#F7F8FA"))
  73. //空状态
  74. self.emptyView.isHidden = true
  75. self.searResultLabel.stringValue = NSLocalizedString("No Search Results", comment: "")
  76. self.searResultLabel.font = NSFont.SFProTextRegular(14.0)
  77. self.searResultLabel.textColor = NSColor(hex: "#616469")
  78. self.tipTitleLabel.font = NSFont.SFProTextRegular(12.0)
  79. self.tipTitleLabel.textColor = NSColor(hex: "#94989C")
  80. let paragraphStyle = NSMutableParagraphStyle()
  81. paragraphStyle.lineHeightMultiple = 1.32
  82. paragraphStyle.alignment = .center
  83. self.tipTitleLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString("Search text can be entered above", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  84. self.lineView.backgroundColor(NSColor(hex: "#EDEEF0"))
  85. //搜索
  86. self.searchCotentView.backgroundColor(NSColor(hex: "#FFFFFF"))
  87. self.searchCotentView.border()
  88. self.searchTextField.delegate = self
  89. self.searchTextField.border(NSColor(hex: "#FFFFFF"), 1, 4)
  90. self.searchTextField.textColor = NSColor(hex: "#252629")
  91. self.searchTextField.font = NSFont.SFProTextRegular(14.0)
  92. let search = UserDefaults.standard.object(forKey: "CPDFOfficeSearchIgnoreCaseKey")
  93. if search == nil {
  94. self.isCase = true
  95. } else {
  96. self.isCase = UserDefaults.standard.bool(forKey: "CPDFOfficeSearchIgnoreCaseKey")
  97. }
  98. self.previousCase = self.isCase
  99. self.findTipTextField.stringValue = NSLocalizedString("Search", comment: "")
  100. self.findTipTextField.font = NSFont.SFProTextSemibold(14.0)
  101. self.findTipTextField.textColor = NSColor(hex: "#252629")
  102. self.doneButton.isHidden = true
  103. self.doneButton.title = NSLocalizedString("Done", comment: "")
  104. self.doneButton.backgroundColor(NSColor(hex: "#1770F4"))
  105. self.doneButton.font = NSFont.SFProTextRegular(12.0)
  106. self.doneButton.contentTintColor = NSColor(hex: "#FFFFFF")
  107. self.doneButton.border(NSColor(hex: "#1770F4"), 0, 4)
  108. //显示
  109. self.allTipTextField.stringValue = NSLocalizedString("All", comment: "")
  110. self.allTipTextField.font = NSFont.SFProTextSemibold(11.0)
  111. self.allTipTextField.textColor = NSColor(hex: "#252629")
  112. self.resultTextField.stringValue = NSLocalizedString("Results", comment: "") + ":"
  113. self.resultTextField.font = NSFont.SFProTextSemibold(11.0)
  114. self.resultTextField.textColor = NSColor(hex: "#94989C")
  115. self.outlineView.allowsMultipleSelection = true
  116. self.outlineView.indentationPerLevel = 0
  117. self.updateSearchMenu()
  118. (self.searchTextField.cell! as! NSSearchFieldCell).placeholderString = NSLocalizedString("Search PDF", comment: "")
  119. self.reloadData()
  120. }
  121. private func updateSearchMenu() {
  122. searchFieldMenu.removeAllItems()
  123. let item = searchFieldMenu.addItem(withTitle: NSLocalizedString("Ignore Case", comment: ""), action: #selector(caseSetAction), target: self)
  124. if self.isCase {
  125. item?.state = .on
  126. } else {
  127. item?.state = .off
  128. }
  129. let searchs : [String] = UserDefaults.standard.object(forKey: CPDFOfficeSearchHistoryKey) as? [String] ?? []
  130. if searchs.count > 0 {
  131. searchFieldMenu.addItem(NSMenuItem.separator())
  132. searchFieldMenu.addItem(withTitle: NSLocalizedString("Search History", comment: ""), action: nil, target: self)
  133. for search in searchs {
  134. searchFieldMenu.addItem(withTitle: search, action: #selector(searchHistoryAction), target: self)
  135. }
  136. searchFieldMenu.addItem(NSMenuItem.separator())
  137. searchFieldMenu.addItem(withTitle: NSLocalizedString("Clear Search History", comment: ""), action: #selector(clearSearchHistoryAction), target: self)
  138. self.searchFieldMenu.font = NSFont.SFProTextRegular(13.0)
  139. }
  140. (self.searchTextField.cell! as! NSSearchFieldCell).searchMenuTemplate = searchFieldMenu
  141. // let menus : NSMenu = NSMenu(title: "")
  142. // menus.addItem(withTitle: NSLocalizedString("Add Crice", comment: ""), action: #selector(addAnonationStyle), target: self, tag: CAnnotationType.circle.rawValue)
  143. // menus.addItem(withTitle: NSLocalizedString("Add Square", comment: ""), action: #selector(addAnonationStyle), target: self, tag: CAnnotationType.square.rawValue)
  144. // menus.addItem(withTitle: NSLocalizedString("Add Highlight", comment: ""), action: #selector(addAnonationStyle), target: self, tag: CAnnotationType.highlight.rawValue)
  145. // menus.addItem(withTitle: NSLocalizedString("Add Underline", comment: ""), action: #selector(addAnonationStyle), target: self, tag: CAnnotationType.underline.rawValue)
  146. // menus.addItem(withTitle: NSLocalizedString("Add Strikeththrough", comment: ""), action: #selector(addAnonationStyle), target: self, tag: CAnnotationType.strikeOut.rawValue)
  147. // self.outlineView.menu = menus
  148. }
  149. func selectedRowIndexs() -> IndexSet {
  150. let clickRow = self.outlineView.clickedRow
  151. var selectedRowIndexs = self.outlineView.selectedRowIndexes
  152. if(clickRow != -1 && !selectedRowIndexs.contains(clickRow)) {
  153. selectedRowIndexs = [clickRow]
  154. }
  155. return selectedRowIndexs
  156. }
  157. public func reloadData() {
  158. self.sortResults = KMSearchMode.sortSearchResult(results: self.searchResults)
  159. if self.searchResults.count > 1 {
  160. self.resultTextField.stringValue = "Results:\(self.searchResults.count)"
  161. } else {
  162. self.resultTextField.stringValue = "Result:\(self.searchResults.count)"
  163. }
  164. self.listView.setHighlightedSelection(nil, animated: true)
  165. self.outlineView.reloadData()
  166. }
  167. }
  168. //MARK: Search
  169. extension KMSearchViewController: NSSearchFieldDelegate {
  170. private func searchDoneAction() {
  171. var searchs : [String] = UserDefaults.standard.object(forKey: CPDFOfficeSearchHistoryKey) as? [String] ?? []
  172. let searchString = self.searchTextField.stringValue
  173. if searchString != "" && (self.previousSearchString != searchString || self.isCase != self.previousCase) {
  174. //缓存搜索词汇是否重复
  175. if searchs.contains(searchString) {
  176. searchs.removeObject(searchString)
  177. }
  178. if searchs.count == 10 {
  179. searchs.remove(at: 9)
  180. }
  181. searchs.insert((self.searchTextField.stringValue), at: 0)
  182. UserDefaults.standard.set(searchs, forKey: CPDFOfficeSearchHistoryKey)
  183. self.updateSearchMenu()
  184. self.delegate?.searchAction?(searchString: self.searchTextField.stringValue,isCase:self.isCase)
  185. self.doneButton.isHidden = false
  186. for model in self.sortResults {
  187. model.select = true
  188. }
  189. self.outlineView.expandItem(nil, expandChildren: true)
  190. self.selectAllSearchModel()
  191. self.previousSearchString = searchString
  192. self.previousCase = self.isCase
  193. }
  194. //移除响应
  195. NSApplication.shared.mainWindow?.makeFirstResponder(self)
  196. }
  197. func selectAllSearchModel() {
  198. //需要延时2s 不然不会高亮
  199. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [unowned self] in
  200. //高亮所有注释
  201. var selections: [CPDFSelection] = []
  202. for model in self.sortResults {
  203. for item in model.datas {
  204. item.selection.setColor(NSColor(hex: "#FFE600").withAlphaComponent(0.5))
  205. selections.append(item.selection)
  206. }
  207. }
  208. self.listView.setHighlightedSelections(selections)
  209. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  210. // }
  211. }
  212. func cancelAllSearchModel() {
  213. self.listView.setHighlightedSelections(nil)
  214. self.listView.setHighlightedSelection(nil, animated: true)
  215. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  216. self.outlineView.deselectAll(nil)
  217. }
  218. }
  219. //MARK: Action
  220. extension KMSearchViewController {
  221. @IBAction func addAnonationStyle(sender: NSMenuItem) {
  222. let selectRowIndexs = self.selectedRowIndexs()
  223. if selectRowIndexs.count > 0 {
  224. var newAnnonations : [CPDFAnnotation] = []
  225. for selectRow in selectRowIndexs {
  226. let searchModel = self.outlineView.item(atRow: selectRow) as! KMSearchMode
  227. if searchModel.datas.count > 0 { //选了到了1页
  228. for search in searchModel.datas {
  229. let selection = search.selection
  230. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? CAnnotationType.unkown, selection: selection, page: selection.page, bounds: selection.bounds)
  231. self.listView.setNeedsDisplayAnnotationViewFor(selection.page)
  232. if annotation != nil {
  233. newAnnonations.append(annotation!)
  234. }
  235. }
  236. } else { //选到页码里的条数
  237. let selection = searchModel.selection
  238. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? CAnnotationType.unkown, selection: selection, page: selection.page, bounds: selection.bounds)
  239. self.listView.setNeedsDisplayAnnotationViewFor(selection.page)
  240. if annotation != nil {
  241. newAnnonations.append(annotation!)
  242. }
  243. }
  244. }
  245. self.listView.updateActiveAnnotations(newAnnonations)
  246. }
  247. }
  248. @IBAction func doneSearchAction(_ sender: Any) {
  249. self.searchTextField.stringValue = ""
  250. self.searchResults = [];
  251. self.reloadData()
  252. self.delegate?.searchDoneAction?(viewController: self)
  253. self.doneButton.isHidden = true
  254. }
  255. @IBAction func searchHistoryAction(sender: NSMenuItem) {
  256. // self.delegate?.searchAction?(searchString: sender.title, isCase:self.isCase)
  257. self.searchTextField.stringValue = sender.title
  258. self.searchDoneAction()
  259. }
  260. @IBAction func caseSetAction(sender:Any) {
  261. self.isCase = !self.isCase
  262. UserDefaults.standard.set(self.isCase, forKey: "CPDFOfficeSearchIgnoreCaseKey")
  263. self.updateSearchMenu()
  264. self.searchDoneAction()
  265. }
  266. @IBAction func clearSearchHistoryAction(sender:Any) {
  267. UserDefaults.standard.removeObject(forKey: CPDFOfficeSearchHistoryKey)
  268. self.updateSearchMenu()
  269. }
  270. @IBAction func escButtonAction(_ sender: Any) {
  271. self.cancelSelect()
  272. }
  273. //MARK: 跳转
  274. //跳转到指定位置 若存在多个 则跳转最后一个
  275. func previewToSections() {
  276. if outlineView.selectedRowIndexes.count != 0 {
  277. var rows: [KMSearchMode] = []
  278. for index in outlineView.selectedRowIndexes {
  279. let model: KMSearchMode = outlineView.item(atRow: index) as! KMSearchMode
  280. rows.append(model)
  281. }
  282. self.toSearchModes(rows)
  283. }
  284. }
  285. func toSearchModes(_ searchModes: [KMSearchMode]) {
  286. self.selectAllSearchModel()
  287. var selections: [CPDFSelection] = []
  288. for model in searchModes {
  289. model.selection.setColor(NSColor(hex: "#FF5C00").withAlphaComponent(0.5))
  290. selections.append(model.selection)
  291. }
  292. self.toSections(selections)
  293. }
  294. func toSections(_ selections: [CPDFSelection]) {
  295. self.listView.go(to: selections.last, animated: true)
  296. // if selections.count == 1 {
  297. // self.listView.setHighlightedSelection(selections.first, animated: true)
  298. // } else {
  299. // self.listView.setHighlightedSelections(selections)
  300. // }
  301. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  302. }
  303. func didSelectItem(view: KMSearchTableRowView, event: NSEvent) {
  304. let rowView: KMSearchTableRowView = view
  305. if rowView.model.datas.count == 0 {
  306. //当选中一个时
  307. if self.outlineView.selectedRowIndexes.count == 1 || (!event.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  308. !event.modifierFlags.contains(NSEvent.ModifierFlags.shift)) {
  309. let index = self.outlineView.row(for: rowView)
  310. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  311. }
  312. //原始数据置空
  313. for model in self.selectItems {
  314. model.select = false
  315. self.outlineView.reloadItem(model)
  316. }
  317. //获取最新数据
  318. var items: [KMSearchMode] = []
  319. for index in self.outlineView.selectedRowIndexes {
  320. let model: KMSearchMode = self.outlineView.item(atRow: index) as! KMSearchMode
  321. model.select = true
  322. self.outlineView.reloadItem(model)
  323. items.append(model)
  324. }
  325. self.selectItems = items
  326. //刷新数据
  327. self.previewToSections()
  328. } else {
  329. let expanded = outlineView.isItemExpanded(outlineView.item(atRow: outlineView.selectedRow))
  330. if expanded {
  331. outlineView.collapseItem(outlineView.item(atRow: outlineView.selectedRow), collapseChildren: true)
  332. rowView.model.select = false
  333. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  334. } else {
  335. outlineView.expandItem(outlineView.item(atRow: outlineView.selectedRow), expandChildren: true)
  336. rowView.model.select = true
  337. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  338. }
  339. }
  340. }
  341. func cancelSelect() {
  342. self.outlineView.deselectAll(nil)
  343. for model in self.selectItems {
  344. model.select = false
  345. self.outlineView.reloadItem(model)
  346. }
  347. }
  348. }
  349. // MARK - NSOutlineViewDataSource,NSOutlineViewDelegate
  350. extension KMSearchViewController : NSOutlineViewDataSource,NSOutlineViewDelegate {
  351. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  352. let indexModel = item as? KMSearchMode
  353. var count = 0
  354. if indexModel == nil {
  355. count = self.sortResults.count;
  356. } else {
  357. count = indexModel?.datas.count ?? 0
  358. }
  359. if(count == 0) { //无数据时的图
  360. self.emptyView.isHidden = false
  361. } else {
  362. self.emptyView.isHidden = true
  363. }
  364. return count
  365. }
  366. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  367. let indexModel = item as? KMSearchMode
  368. var child = KMSearchMode()
  369. if indexModel == nil {
  370. child = self.sortResults[index];
  371. } else {
  372. child = indexModel?.datas[index] ?? KMSearchMode()
  373. }
  374. return child
  375. }
  376. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  377. let newitem = item as? KMSearchMode
  378. return newitem?.datas.count ?? 0 > 0
  379. }
  380. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item:Any) -> NSView? {
  381. let cell : KMSearchCellView = KMSearchCellView.init()
  382. let model : KMSearchMode = item as! KMSearchMode
  383. cell.model = model
  384. return cell
  385. }
  386. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  387. let rowView = KMSearchTableRowView()
  388. rowView.model = (item as! KMSearchMode)
  389. rowView.mouseDownCallback = { [unowned self] view, event in
  390. self.didSelectItem(view: view, event: event)
  391. }
  392. rowView.hoverCallback = { [unowned self] mouseEntered, mouseBox in
  393. self.outlineView.enumerateAvailableRowViews { view, row in
  394. if view is KMSearchTableRowView {
  395. (view as? KMSearchTableRowView)?.model.hover = false
  396. (view as? KMSearchTableRowView)?.reloadData()
  397. }
  398. }
  399. if mouseEntered {
  400. rowView.model.hover = true
  401. } else {
  402. rowView.model.hover = false
  403. }
  404. }
  405. return rowView
  406. }
  407. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  408. let model : KMSearchMode = item as! KMSearchMode
  409. if model.datas.count != 0 {
  410. return 40
  411. } else {
  412. let string: NSString = model.attributedString.string as NSString
  413. let text = string
  414. let size = CGSize(width: outlineView.frame.width - 32, height: 1000)
  415. let options = NSString.DrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
  416. let paragraphStyle = NSMutableParagraphStyle()
  417. paragraphStyle.firstLineHeadIndent = 10.0
  418. paragraphStyle.headIndent = 10.0
  419. paragraphStyle.lineBreakMode = .byCharWrapping
  420. paragraphStyle.lineHeightMultiple = 1.32
  421. let estimatedFrame = NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font: NSFont.SFProTextRegular(14.0),
  422. NSAttributedString.Key.paragraphStyle: paragraphStyle], context: nil)
  423. return estimatedFrame.size.height + 16
  424. }
  425. }
  426. // func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
  427. // return true
  428. // }
  429. func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
  430. return true
  431. }
  432. func outlineViewSelectionDidChange(_ notification: Notification) {
  433. if self.outlineView.selectedRow == -1 {
  434. self.cancelSelect()
  435. }
  436. }
  437. func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
  438. if let item = item as? KMSearchMode {
  439. if !item.select && item.datas.count > 0 {
  440. item.select = true
  441. outlineView.animator().expandItem(item, expandChildren: true)
  442. return false
  443. }
  444. }
  445. return true
  446. }
  447. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  448. if let item = item as? KMSearchMode {
  449. if item.select && item.datas.count > 0 {
  450. item.select = false
  451. outlineView.animator().collapseItem(item, collapseChildren: true)
  452. return false
  453. }
  454. }
  455. return true
  456. }
  457. // func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  458. // if self.outlineView.selectedRowIndexes.count > 1 {
  459. // return false
  460. // }
  461. //
  462. // let indexSet = [self.outlineView.clickedRow]
  463. // let indexSetData : Data = NSKeyedArchiver.archivedData(withRootObject: indexSet) as Data
  464. // pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  465. // pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  466. // return true
  467. // }
  468. //
  469. // func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  470. // var dragOperation = NSDragOperation.init(rawValue: 0)
  471. // if index > 0 {
  472. // dragOperation = NSDragOperation.move
  473. // }
  474. // return dragOperation
  475. // }
  476. //
  477. // func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  478. // if ((index < 0)) {
  479. // return false
  480. // }
  481. // let outline = item as! CPDFOutline
  482. // if outline.parent == nil {
  483. //
  484. // } else {
  485. //
  486. // }
  487. //
  488. // return true
  489. // }
  490. }
  491. //MARK: ControlTextDelegate
  492. extension KMSearchViewController: NSTextFieldDelegate {
  493. func controlTextDidEndEditing(_ obj: Notification) {
  494. let object = obj.object as! NSTextField
  495. if object == self.searchTextField {
  496. // self.searchDoneAction()
  497. // self.sortResults = []
  498. // self.reloadData()
  499. }
  500. }
  501. func controlTextDidChange(_ obj: Notification) {
  502. let object = obj.object as! NSTextField
  503. if object == self.searchTextField {
  504. if self.searchTextField.stringValue == "" {
  505. self.doneButton.isHidden = true
  506. self.searchResults = []
  507. self.reloadData()
  508. self.cancelAllSearchModel()
  509. }
  510. }
  511. }
  512. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  513. switch commandSelector {
  514. case #selector(NSResponder.insertNewline(_:)):
  515. if let inputView = control as? NSTextField {
  516. //当当前TextField按下enter
  517. if inputView == searchTextField {
  518. self.searchDoneAction()
  519. }
  520. }
  521. return true
  522. default:
  523. return false
  524. }
  525. }
  526. }
  527. extension KMSearchViewController: NSMenuDelegate, NSMenuItemValidation {
  528. //MARK: NSMenuItemValidation
  529. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  530. let action = menuItem.action
  531. if (action == #selector(addAnonationStyle)) {
  532. let model : KMSearchMode = self.outlineView.item(atRow: self.outlineView.clickedRow) as! KMSearchMode
  533. return model.datas.count == 0
  534. }
  535. return true
  536. }
  537. }
  538. class FocusAwareSearchTextField: NSSearchField {
  539. var onFocus: () -> Void = {}
  540. var onUnfocus: () -> Void = {}
  541. override func becomeFirstResponder() -> Bool {
  542. onFocus()
  543. let textView = window?.fieldEditor(true, for: nil) as? NSTextView
  544. textView?.insertionPointColor = NSColor.init(hex: "#252629")
  545. return super.becomeFirstResponder()
  546. }
  547. override func resignFirstResponder() -> Bool {
  548. onUnfocus()
  549. return super.resignFirstResponder()
  550. }
  551. }