KMSearchViewController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. self.previousSearchString = ""
  256. }
  257. @IBAction func searchHistoryAction(sender: NSMenuItem) {
  258. // self.delegate?.searchAction?(searchString: sender.title, isCase:self.isCase)
  259. self.searchTextField.stringValue = sender.title
  260. self.searchDoneAction()
  261. }
  262. @IBAction func caseSetAction(sender:Any) {
  263. self.isCase = !self.isCase
  264. UserDefaults.standard.set(self.isCase, forKey: "CPDFOfficeSearchIgnoreCaseKey")
  265. self.updateSearchMenu()
  266. self.searchDoneAction()
  267. }
  268. @IBAction func clearSearchHistoryAction(sender:Any) {
  269. UserDefaults.standard.removeObject(forKey: CPDFOfficeSearchHistoryKey)
  270. self.updateSearchMenu()
  271. //清空搜索词汇
  272. self.previousSearchString = ""
  273. }
  274. @IBAction func escButtonAction(_ sender: Any) {
  275. self.cancelSelect()
  276. }
  277. //MARK: 跳转
  278. //跳转到指定位置 若存在多个 则跳转最后一个
  279. func previewToSections() {
  280. if outlineView.selectedRowIndexes.count != 0 {
  281. var rows: [KMSearchMode] = []
  282. for index in outlineView.selectedRowIndexes {
  283. let model: KMSearchMode = outlineView.item(atRow: index) as! KMSearchMode
  284. rows.append(model)
  285. }
  286. self.toSearchModes(rows)
  287. }
  288. }
  289. func toSearchModes(_ searchModes: [KMSearchMode]) {
  290. self.selectAllSearchModel()
  291. var selections: [CPDFSelection] = []
  292. for model in searchModes {
  293. model.selection.setColor(NSColor(hex: "#FF5C00").withAlphaComponent(0.5))
  294. selections.append(model.selection)
  295. }
  296. self.toSections(selections)
  297. }
  298. func toSections(_ selections: [CPDFSelection]) {
  299. self.listView.go(to: selections.last, animated: true)
  300. // if selections.count == 1 {
  301. // self.listView.setHighlightedSelection(selections.first, animated: true)
  302. // } else {
  303. // self.listView.setHighlightedSelections(selections)
  304. // }
  305. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  306. }
  307. func didSelectItem(view: KMSearchTableRowView, event: NSEvent) {
  308. let rowView: KMSearchTableRowView = view
  309. if rowView.model.datas.count == 0 {
  310. //当选中一个时
  311. if self.outlineView.selectedRowIndexes.count == 1 || (!event.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  312. !event.modifierFlags.contains(NSEvent.ModifierFlags.shift)) {
  313. let index = self.outlineView.row(for: rowView)
  314. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  315. }
  316. //原始数据置空
  317. for model in self.selectItems {
  318. model.select = false
  319. self.outlineView.reloadItem(model)
  320. }
  321. //获取最新数据
  322. var items: [KMSearchMode] = []
  323. for index in self.outlineView.selectedRowIndexes {
  324. let model: KMSearchMode = self.outlineView.item(atRow: index) as! KMSearchMode
  325. model.select = true
  326. self.outlineView.reloadItem(model)
  327. items.append(model)
  328. }
  329. self.selectItems = items
  330. //刷新数据
  331. self.previewToSections()
  332. } else {
  333. let expanded = outlineView.isItemExpanded(outlineView.item(atRow: outlineView.selectedRow))
  334. if expanded {
  335. outlineView.collapseItem(outlineView.item(atRow: outlineView.selectedRow), collapseChildren: true)
  336. rowView.model.select = false
  337. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  338. } else {
  339. outlineView.expandItem(outlineView.item(atRow: outlineView.selectedRow), expandChildren: true)
  340. rowView.model.select = true
  341. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  342. }
  343. }
  344. }
  345. func cancelSelect() {
  346. self.outlineView.deselectAll(nil)
  347. for model in self.selectItems {
  348. model.select = false
  349. self.outlineView.reloadItem(model)
  350. }
  351. }
  352. }
  353. // MARK - NSOutlineViewDataSource,NSOutlineViewDelegate
  354. extension KMSearchViewController : NSOutlineViewDataSource,NSOutlineViewDelegate {
  355. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  356. let indexModel = item as? KMSearchMode
  357. var count = 0
  358. if indexModel == nil {
  359. count = self.sortResults.count;
  360. } else {
  361. count = indexModel?.datas.count ?? 0
  362. }
  363. if(count == 0) { //无数据时的图
  364. self.emptyView.isHidden = false
  365. } else {
  366. self.emptyView.isHidden = true
  367. }
  368. return count
  369. }
  370. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  371. let indexModel = item as? KMSearchMode
  372. var child = KMSearchMode()
  373. if indexModel == nil {
  374. child = self.sortResults[index];
  375. } else {
  376. child = indexModel?.datas[index] ?? KMSearchMode()
  377. }
  378. return child
  379. }
  380. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  381. let newitem = item as? KMSearchMode
  382. return newitem?.datas.count ?? 0 > 0
  383. }
  384. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item:Any) -> NSView? {
  385. let cell : KMSearchCellView = KMSearchCellView.init()
  386. let model : KMSearchMode = item as! KMSearchMode
  387. cell.model = model
  388. return cell
  389. }
  390. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  391. let rowView = KMSearchTableRowView()
  392. rowView.model = (item as! KMSearchMode)
  393. rowView.mouseDownCallback = { [unowned self] view, event in
  394. self.didSelectItem(view: view, event: event)
  395. }
  396. rowView.hoverCallback = { [unowned self] mouseEntered, mouseBox in
  397. self.outlineView.enumerateAvailableRowViews { view, row in
  398. if view is KMSearchTableRowView {
  399. (view as? KMSearchTableRowView)?.model.hover = false
  400. (view as? KMSearchTableRowView)?.reloadData()
  401. }
  402. }
  403. if mouseEntered {
  404. rowView.model.hover = true
  405. } else {
  406. rowView.model.hover = false
  407. }
  408. }
  409. return rowView
  410. }
  411. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  412. let model : KMSearchMode = item as! KMSearchMode
  413. if model.datas.count != 0 {
  414. return 40
  415. } else {
  416. let string: NSString = model.attributedString.string as NSString
  417. let text = string
  418. let size = CGSize(width: outlineView.frame.width - 32, height: 1000)
  419. let options = NSString.DrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
  420. let paragraphStyle = NSMutableParagraphStyle()
  421. paragraphStyle.firstLineHeadIndent = 10.0
  422. paragraphStyle.headIndent = 10.0
  423. paragraphStyle.lineBreakMode = .byCharWrapping
  424. paragraphStyle.lineHeightMultiple = 1.32
  425. let estimatedFrame = NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font: NSFont.SFProTextRegular(14.0),
  426. NSAttributedString.Key.paragraphStyle: paragraphStyle], context: nil)
  427. return estimatedFrame.size.height + 16
  428. }
  429. }
  430. // func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
  431. // return true
  432. // }
  433. func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
  434. return true
  435. }
  436. func outlineViewSelectionDidChange(_ notification: Notification) {
  437. if self.outlineView.selectedRow == -1 {
  438. self.cancelSelect()
  439. }
  440. }
  441. func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
  442. if let item = item as? KMSearchMode {
  443. if !item.select && item.datas.count > 0 {
  444. item.select = true
  445. outlineView.animator().expandItem(item, expandChildren: true)
  446. return false
  447. }
  448. }
  449. return true
  450. }
  451. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  452. if let item = item as? KMSearchMode {
  453. if item.select && item.datas.count > 0 {
  454. item.select = false
  455. outlineView.animator().collapseItem(item, collapseChildren: true)
  456. return false
  457. }
  458. }
  459. return true
  460. }
  461. // func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  462. // if self.outlineView.selectedRowIndexes.count > 1 {
  463. // return false
  464. // }
  465. //
  466. // let indexSet = [self.outlineView.clickedRow]
  467. // let indexSetData : Data = NSKeyedArchiver.archivedData(withRootObject: indexSet) as Data
  468. // pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  469. // pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  470. // return true
  471. // }
  472. //
  473. // func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  474. // var dragOperation = NSDragOperation.init(rawValue: 0)
  475. // if index > 0 {
  476. // dragOperation = NSDragOperation.move
  477. // }
  478. // return dragOperation
  479. // }
  480. //
  481. // func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  482. // if ((index < 0)) {
  483. // return false
  484. // }
  485. // let outline = item as! CPDFOutline
  486. // if outline.parent == nil {
  487. //
  488. // } else {
  489. //
  490. // }
  491. //
  492. // return true
  493. // }
  494. }
  495. //MARK: ControlTextDelegate
  496. extension KMSearchViewController: NSTextFieldDelegate {
  497. func controlTextDidEndEditing(_ obj: Notification) {
  498. let object = obj.object as! NSTextField
  499. if object == self.searchTextField {
  500. // self.searchDoneAction()
  501. // self.sortResults = []
  502. // self.reloadData()
  503. }
  504. }
  505. func controlTextDidChange(_ obj: Notification) {
  506. let object = obj.object as! NSTextField
  507. if object == self.searchTextField {
  508. if self.searchTextField.stringValue == "" {
  509. self.doneButton.isHidden = true
  510. self.searchResults = []
  511. self.reloadData()
  512. self.cancelAllSearchModel()
  513. }
  514. }
  515. }
  516. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  517. switch commandSelector {
  518. case #selector(NSResponder.insertNewline(_:)):
  519. if let inputView = control as? NSTextField {
  520. //当当前TextField按下enter
  521. if inputView == searchTextField {
  522. self.searchDoneAction()
  523. }
  524. }
  525. return true
  526. default:
  527. return false
  528. }
  529. }
  530. }
  531. extension KMSearchViewController: NSMenuDelegate, NSMenuItemValidation {
  532. //MARK: NSMenuItemValidation
  533. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  534. let action = menuItem.action
  535. if (action == #selector(addAnonationStyle)) {
  536. let model : KMSearchMode = self.outlineView.item(atRow: self.outlineView.clickedRow) as! KMSearchMode
  537. return model.datas.count == 0
  538. }
  539. return true
  540. }
  541. }
  542. class FocusAwareSearchTextField: NSSearchField {
  543. var onFocus: () -> Void = {}
  544. var onUnfocus: () -> Void = {}
  545. override func becomeFirstResponder() -> Bool {
  546. onFocus()
  547. let textView = window?.fieldEditor(true, for: nil) as? NSTextView
  548. textView?.insertionPointColor = NSColor.init(hex: "#252629")
  549. return super.becomeFirstResponder()
  550. }
  551. override func resignFirstResponder() -> Bool {
  552. onUnfocus()
  553. return super.resignFirstResponder()
  554. }
  555. }