KMSearchViewController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. self.resultTextField.stringValue = "Reslut:\(self.searchResults.count)"
  160. self.listView.setHighlightedSelection(nil, animated: true)
  161. self.outlineView.reloadData()
  162. }
  163. }
  164. //MARK: Search
  165. extension KMSearchViewController: NSSearchFieldDelegate {
  166. private func searchDoneAction() {
  167. var searchs : [String] = UserDefaults.standard.object(forKey: CPDFOfficeSearchHistoryKey) as? [String] ?? []
  168. let searchString = self.searchTextField.stringValue
  169. if searchString != "" && (self.previousSearchString != searchString || self.isCase != self.previousCase) {
  170. //缓存搜索词汇是否重复
  171. if searchs.contains(searchString) {
  172. searchs.removeObject(searchString)
  173. }
  174. if searchs.count == 10 {
  175. searchs.remove(at: 9)
  176. }
  177. searchs.insert((self.searchTextField.stringValue), at: 0)
  178. UserDefaults.standard.set(searchs, forKey: CPDFOfficeSearchHistoryKey)
  179. self.updateSearchMenu()
  180. self.delegate?.searchAction?(searchString: self.searchTextField.stringValue,isCase:self.isCase)
  181. self.doneButton.isHidden = false
  182. for model in self.sortResults {
  183. model.select = true
  184. }
  185. self.outlineView.expandItem(nil, expandChildren: true)
  186. self.selectAllSearchModel()
  187. self.previousSearchString = searchString
  188. self.previousCase = self.isCase
  189. }
  190. //移除响应
  191. NSApplication.shared.mainWindow?.makeFirstResponder(self)
  192. }
  193. func selectAllSearchModel() {
  194. //需要延时2s 不然不会高亮
  195. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [unowned self] in
  196. //高亮所有注释
  197. var selections: [CPDFSelection] = []
  198. for model in self.sortResults {
  199. for item in model.datas {
  200. item.selection.setColor(NSColor(hex: "#FFE600").withAlphaComponent(0.5))
  201. selections.append(item.selection)
  202. }
  203. }
  204. self.listView.setHighlightedSelections(selections)
  205. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  206. // }
  207. }
  208. func cancelAllSearchModel() {
  209. self.listView.setHighlightedSelections(nil)
  210. self.listView.setHighlightedSelection(nil, animated: true)
  211. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  212. self.outlineView.deselectAll(nil)
  213. }
  214. }
  215. //MARK: Action
  216. extension KMSearchViewController {
  217. @IBAction func addAnonationStyle(sender: NSMenuItem) {
  218. let selectRowIndexs = self.selectedRowIndexs()
  219. if selectRowIndexs.count > 0 {
  220. var newAnnonations : [CPDFAnnotation] = []
  221. for selectRow in selectRowIndexs {
  222. let searchModel = self.outlineView.item(atRow: selectRow) as! KMSearchMode
  223. if searchModel.datas.count > 0 { //选了到了1页
  224. for search in searchModel.datas {
  225. let selection = search.selection
  226. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? CAnnotationType.unkown, selection: selection, page: selection.page, bounds: selection.bounds)
  227. self.listView.setNeedsDisplayAnnotationViewFor(selection.page)
  228. if annotation != nil {
  229. newAnnonations.append(annotation!)
  230. }
  231. }
  232. } else { //选到页码里的条数
  233. let selection = searchModel.selection
  234. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? CAnnotationType.unkown, selection: selection, page: selection.page, bounds: selection.bounds)
  235. self.listView.setNeedsDisplayAnnotationViewFor(selection.page)
  236. if annotation != nil {
  237. newAnnonations.append(annotation!)
  238. }
  239. }
  240. }
  241. self.listView.updateActiveAnnotations(newAnnonations)
  242. }
  243. }
  244. @IBAction func doneSearchAction(_ sender: Any) {
  245. self.searchTextField.stringValue = ""
  246. self.searchResults = [];
  247. self.reloadData()
  248. self.delegate?.searchDoneAction?(viewController: self)
  249. self.doneButton.isHidden = true
  250. }
  251. @IBAction func searchHistoryAction(sender: NSMenuItem) {
  252. // self.delegate?.searchAction?(searchString: sender.title, isCase:self.isCase)
  253. self.searchTextField.stringValue = sender.title
  254. self.searchDoneAction()
  255. }
  256. @IBAction func caseSetAction(sender:Any) {
  257. self.isCase = !self.isCase
  258. UserDefaults.standard.set(self.isCase, forKey: "CPDFOfficeSearchIgnoreCaseKey")
  259. self.updateSearchMenu()
  260. self.searchDoneAction()
  261. }
  262. @IBAction func clearSearchHistoryAction(sender:Any) {
  263. UserDefaults.standard.removeObject(forKey: CPDFOfficeSearchHistoryKey)
  264. self.updateSearchMenu()
  265. }
  266. @IBAction func escButtonAction(_ sender: Any) {
  267. self.cancelSelect()
  268. }
  269. //MARK: 跳转
  270. //跳转到指定位置 若存在多个 则跳转最后一个
  271. func previewToSections() {
  272. if outlineView.selectedRowIndexes.count != 0 {
  273. var rows: [KMSearchMode] = []
  274. for index in outlineView.selectedRowIndexes {
  275. let model: KMSearchMode = outlineView.item(atRow: index) as! KMSearchMode
  276. rows.append(model)
  277. }
  278. self.toSearchModes(rows)
  279. }
  280. }
  281. func toSearchModes(_ searchModes: [KMSearchMode]) {
  282. self.selectAllSearchModel()
  283. var selections: [CPDFSelection] = []
  284. for model in searchModes {
  285. model.selection.setColor(NSColor(hex: "#FF5C00").withAlphaComponent(0.5))
  286. selections.append(model.selection)
  287. }
  288. self.toSections(selections)
  289. }
  290. func toSections(_ selections: [CPDFSelection]) {
  291. self.listView.go(to: selections.last, animated: true)
  292. // if selections.count == 1 {
  293. // self.listView.setHighlightedSelection(selections.first, animated: true)
  294. // } else {
  295. // self.listView.setHighlightedSelections(selections)
  296. // }
  297. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  298. }
  299. func didSelectItem(view: KMSearchTableRowView, event: NSEvent) {
  300. let rowView: KMSearchTableRowView = view
  301. if rowView.model.datas.count == 0 {
  302. //当选中一个时
  303. if self.outlineView.selectedRowIndexes.count == 1 || (!event.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  304. !event.modifierFlags.contains(NSEvent.ModifierFlags.shift)) {
  305. let index = self.outlineView.row(for: rowView)
  306. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  307. }
  308. //原始数据置空
  309. for model in self.selectItems {
  310. model.select = false
  311. self.outlineView.reloadItem(model)
  312. }
  313. //获取最新数据
  314. var items: [KMSearchMode] = []
  315. for index in self.outlineView.selectedRowIndexes {
  316. let model: KMSearchMode = self.outlineView.item(atRow: index) as! KMSearchMode
  317. model.select = true
  318. self.outlineView.reloadItem(model)
  319. items.append(model)
  320. }
  321. self.selectItems = items
  322. //刷新数据
  323. self.previewToSections()
  324. } else {
  325. let expanded = outlineView.isItemExpanded(outlineView.item(atRow: outlineView.selectedRow))
  326. if expanded {
  327. outlineView.collapseItem(outlineView.item(atRow: outlineView.selectedRow), collapseChildren: true)
  328. rowView.model.select = false
  329. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  330. } else {
  331. outlineView.expandItem(outlineView.item(atRow: outlineView.selectedRow), expandChildren: true)
  332. rowView.model.select = true
  333. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  334. }
  335. }
  336. }
  337. func cancelSelect() {
  338. self.outlineView.deselectAll(nil)
  339. for model in self.selectItems {
  340. model.select = false
  341. self.outlineView.reloadItem(model)
  342. }
  343. }
  344. }
  345. // MARK - NSOutlineViewDataSource,NSOutlineViewDelegate
  346. extension KMSearchViewController : NSOutlineViewDataSource,NSOutlineViewDelegate {
  347. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  348. let indexModel = item as? KMSearchMode
  349. var count = 0
  350. if indexModel == nil {
  351. count = self.sortResults.count;
  352. } else {
  353. count = indexModel?.datas.count ?? 0
  354. }
  355. if(count == 0) { //无数据时的图
  356. self.emptyView.isHidden = false
  357. } else {
  358. self.emptyView.isHidden = true
  359. }
  360. return count
  361. }
  362. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  363. let indexModel = item as? KMSearchMode
  364. var child = KMSearchMode()
  365. if indexModel == nil {
  366. child = self.sortResults[index];
  367. } else {
  368. child = indexModel?.datas[index] ?? KMSearchMode()
  369. }
  370. return child
  371. }
  372. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  373. let newitem = item as? KMSearchMode
  374. return newitem?.datas.count ?? 0 > 0
  375. }
  376. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item:Any) -> NSView? {
  377. let cell : KMSearchCellView = KMSearchCellView.init()
  378. let model : KMSearchMode = item as! KMSearchMode
  379. cell.model = model
  380. return cell
  381. }
  382. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  383. let rowView = KMSearchTableRowView()
  384. rowView.model = (item as! KMSearchMode)
  385. rowView.mouseDownCallback = { [unowned self] view, event in
  386. self.didSelectItem(view: view, event: event)
  387. }
  388. rowView.hoverCallback = { [unowned self] mouseEntered, mouseBox in
  389. self.outlineView.enumerateAvailableRowViews { view, row in
  390. if view is KMSearchTableRowView {
  391. (view as? KMSearchTableRowView)?.model.hover = false
  392. (view as? KMSearchTableRowView)?.reloadData()
  393. }
  394. }
  395. if mouseEntered {
  396. rowView.model.hover = true
  397. } else {
  398. rowView.model.hover = false
  399. }
  400. }
  401. return rowView
  402. }
  403. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  404. let model : KMSearchMode = item as! KMSearchMode
  405. if model.datas.count != 0 {
  406. return 40
  407. } else {
  408. let string: NSString = model.attributedString.string as NSString
  409. let text = string
  410. let size = CGSize(width: outlineView.frame.width - 32, height: 1000)
  411. let options = NSString.DrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
  412. let paragraphStyle = NSMutableParagraphStyle()
  413. paragraphStyle.firstLineHeadIndent = 10.0
  414. paragraphStyle.headIndent = 10.0
  415. paragraphStyle.lineBreakMode = .byCharWrapping
  416. paragraphStyle.lineHeightMultiple = 1.32
  417. let estimatedFrame = NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font: NSFont.SFProTextRegular(14.0),
  418. NSAttributedString.Key.paragraphStyle: paragraphStyle], context: nil)
  419. return estimatedFrame.size.height + 16
  420. }
  421. }
  422. // func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
  423. // return true
  424. // }
  425. func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
  426. return true
  427. }
  428. func outlineViewSelectionDidChange(_ notification: Notification) {
  429. if self.outlineView.selectedRow == -1 {
  430. self.cancelSelect()
  431. }
  432. }
  433. func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
  434. if let item = item as? KMSearchMode {
  435. if !item.select && item.datas.count > 0 {
  436. item.select = true
  437. outlineView.animator().expandItem(item, expandChildren: true)
  438. return false
  439. }
  440. }
  441. return true
  442. }
  443. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  444. if let item = item as? KMSearchMode {
  445. if item.select && item.datas.count > 0 {
  446. item.select = false
  447. outlineView.animator().collapseItem(item, collapseChildren: true)
  448. return false
  449. }
  450. }
  451. return true
  452. }
  453. // func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  454. // if self.outlineView.selectedRowIndexes.count > 1 {
  455. // return false
  456. // }
  457. //
  458. // let indexSet = [self.outlineView.clickedRow]
  459. // let indexSetData : Data = NSKeyedArchiver.archivedData(withRootObject: indexSet) as Data
  460. // pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  461. // pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  462. // return true
  463. // }
  464. //
  465. // func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  466. // var dragOperation = NSDragOperation.init(rawValue: 0)
  467. // if index > 0 {
  468. // dragOperation = NSDragOperation.move
  469. // }
  470. // return dragOperation
  471. // }
  472. //
  473. // func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  474. // if ((index < 0)) {
  475. // return false
  476. // }
  477. // let outline = item as! CPDFOutline
  478. // if outline.parent == nil {
  479. //
  480. // } else {
  481. //
  482. // }
  483. //
  484. // return true
  485. // }
  486. }
  487. //MARK: ControlTextDelegate
  488. extension KMSearchViewController: NSTextFieldDelegate {
  489. func controlTextDidEndEditing(_ obj: Notification) {
  490. let object = obj.object as! NSTextField
  491. if object == self.searchTextField {
  492. // self.searchDoneAction()
  493. // self.sortResults = []
  494. // self.reloadData()
  495. }
  496. }
  497. func controlTextDidChange(_ obj: Notification) {
  498. let object = obj.object as! NSTextField
  499. if object == self.searchTextField {
  500. if self.searchTextField.stringValue == "" {
  501. self.doneButton.isHidden = true
  502. self.searchResults = []
  503. self.reloadData()
  504. self.cancelAllSearchModel()
  505. }
  506. }
  507. }
  508. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  509. switch commandSelector {
  510. case #selector(NSResponder.insertNewline(_:)):
  511. if let inputView = control as? NSTextField {
  512. //当当前TextField按下enter
  513. if inputView == searchTextField {
  514. print("按下 enter")
  515. self.searchDoneAction()
  516. }
  517. }
  518. return true
  519. default:
  520. return false
  521. }
  522. }
  523. }
  524. extension KMSearchViewController: NSMenuDelegate, NSMenuItemValidation {
  525. //MARK: NSMenuItemValidation
  526. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  527. let action = menuItem.action
  528. if (action == #selector(addAnonationStyle)) {
  529. let model : KMSearchMode = self.outlineView.item(atRow: self.outlineView.clickedRow) as! KMSearchMode
  530. return model.datas.count == 0
  531. }
  532. return true
  533. }
  534. }
  535. class FocusAwareSearchTextField: NSSearchField {
  536. var onFocus: () -> Void = {}
  537. var onUnfocus: () -> Void = {}
  538. override func becomeFirstResponder() -> Bool {
  539. onFocus()
  540. let textView = window?.fieldEditor(true, for: nil) as? NSTextView
  541. textView?.insertionPointColor = NSColor.init(hex: "#252629")
  542. return super.becomeFirstResponder()
  543. }
  544. override func resignFirstResponder() -> Bool {
  545. onUnfocus()
  546. return super.resignFirstResponder()
  547. }
  548. }