KMBOTAOutlineView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // KMBOTAOutlineView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/2.
  6. //
  7. import Cocoa
  8. protocol KMBOTAOutlineViewDelegate: NSObjectProtocol {
  9. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didReloadData: KMBOTAOutlineItem)
  10. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didSelectItem: [KMBOTAOutlineItem])
  11. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, rightDidMoseDown: KMBOTAOutlineItem, event: NSEvent)
  12. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool
  13. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation
  14. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool
  15. }
  16. class KMBOTAOutlineView: BaseXibView {
  17. @IBOutlet weak var outlineView: KMOutlineView!
  18. @IBOutlet weak var scrollView: NSScrollView!
  19. weak var delegate: KMBOTAOutlineViewDelegate?
  20. var inputData: CPDFOutline? {
  21. didSet {
  22. self.reloadData(expandItemType: .none)
  23. }
  24. }
  25. var data: KMBOTAOutlineItem?
  26. var selectItems: [KMBOTAOutlineItem]?
  27. var dragPDFOutline: KMBOTAOutlineItem!
  28. var isSearchMode = false
  29. var searchKey = ""
  30. var wholeWords = false {
  31. didSet {
  32. if isValidSearchMode() == false {
  33. return
  34. }
  35. reloadData()
  36. outlineView.expandItem(nil, expandChildren: true)
  37. }
  38. }
  39. var caseSensitive = false {
  40. didSet {
  41. if isValidSearchMode() == false {
  42. return
  43. }
  44. reloadData()
  45. outlineView.expandItem(nil, expandChildren: true)
  46. }
  47. }
  48. override func draw(_ dirtyRect: NSRect) {
  49. super.draw(dirtyRect)
  50. // Drawing code here.
  51. }
  52. override func awakeFromNib() {
  53. super.awakeFromNib()
  54. self.setup()
  55. }
  56. func setup() {
  57. self.scrollView.backgroundColor(NSColor.km_init(hex: "#F2F9FF"))
  58. self.outlineView.registerForDraggedTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")])
  59. self.outlineView.delegate = self
  60. self.outlineView.dataSource = self
  61. self.outlineView.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none;
  62. self.outlineView.allowsMultipleSelection = true
  63. // self.outlineView.indentationPerLevel = 0
  64. outlineView.tocDelegate = self
  65. outlineView.hasImageToolTips = true
  66. }
  67. func reloadData(expandItemType: KMOutlineViewExpandItemType = .none) {
  68. if self.inputData != nil {
  69. //获取数据
  70. var tempData: KMBOTAOutlineItem = KMBOTAOutlineItem()
  71. if self.inputData!.numberOfChildren > 0 {
  72. let outline: CPDFOutline = self.inputData!
  73. tempData = self.addOutlineItem(outlineItem:tempData, outline: outline, expandItemType: expandItemType)
  74. } else {
  75. tempData.outline = CPDFOutline()
  76. if expandItemType == .collapse {
  77. tempData.isItemExpanded = false
  78. } else if (expandItemType == .expand) {
  79. tempData.isItemExpanded = true
  80. }
  81. }
  82. tempData.parent = nil
  83. self.data = tempData
  84. if isValidSearchMode() {
  85. self.reloadSearchChildren(item: self.data)
  86. }
  87. self.outlineView.reloadData()
  88. }
  89. self.delegate?.BOTAOutlineView(self, didReloadData: self.data ?? KMBOTAOutlineItem())
  90. }
  91. // 递归处理
  92. func reloadSearchChildren(item: KMBOTAOutlineItem?) {
  93. guard let theItem = item else {
  94. return
  95. }
  96. // 处理当前 item
  97. let models = self.fetchOutlines(for: theItem, searchString: searchKey)
  98. // 搜索数据
  99. theItem.searchChildren = models
  100. theItem.isItemExpanded = true
  101. if theItem.children.isEmpty { // 递归退出条件
  102. return
  103. }
  104. // 处理 childItem
  105. for childM in theItem.children {
  106. self.reloadSearchChildren(item: childM)
  107. }
  108. }
  109. func addOutlineItem(outlineItem: KMBOTAOutlineItem?, outline: CPDFOutline, expandItemType: KMOutlineViewExpandItemType = .none) -> KMBOTAOutlineItem {
  110. //添加根节点
  111. let item: KMBOTAOutlineItem = KMBOTAOutlineItem()
  112. item.outline = outline
  113. item.parent = outlineItem
  114. var items: [KMBOTAOutlineItem] = []
  115. if outline.numberOfChildren > 0 {
  116. for index in 0...outline.numberOfChildren - 1 {
  117. let children: CPDFOutline = outline.child(at: index)
  118. let childrenItem = self.addOutlineItem(outlineItem: item, outline: children, expandItemType: expandItemType)
  119. if expandItemType == .collapse {
  120. childrenItem.isItemExpanded = false
  121. } else if (expandItemType == .expand) {
  122. childrenItem.isItemExpanded = true
  123. }
  124. items.append(childrenItem)
  125. }
  126. }
  127. item.children = items
  128. return item
  129. }
  130. func updateUI() {
  131. }
  132. func updateLanguage() {
  133. }
  134. func hasContainString(_ searchString: String, rootOutline outline: CPDFOutline) -> Bool {
  135. var label = outline.label ?? ""
  136. var searchLabel = searchString
  137. if caseSensitive == false {
  138. label = label.lowercased()
  139. searchLabel = searchLabel.lowercased()
  140. }
  141. if label.contains(searchLabel) {
  142. if wholeWords {
  143. let words = label.words()
  144. return words.contains(searchLabel)
  145. }
  146. return true
  147. } else {
  148. var subHas = false
  149. for i in 0 ..< outline.numberOfChildren {
  150. if let subOutline = outline.child(at: i) {
  151. subHas = self.hasContainString(searchString, rootOutline: subOutline)
  152. } else {
  153. continue
  154. }
  155. if (subHas) {
  156. break
  157. }
  158. }
  159. return subHas
  160. }
  161. }
  162. func fetchOutlines(for item: KMBOTAOutlineItem, searchString: String) -> [KMBOTAOutlineItem] {
  163. var items: [KMBOTAOutlineItem] = []
  164. for childI in item.children {
  165. if self.hasContainString(searchString, rootOutline: childI.outline) {
  166. items.append(childI)
  167. }
  168. }
  169. return items
  170. }
  171. func isValidSearchMode() -> Bool {
  172. if isSearchMode == false {
  173. return false
  174. }
  175. if searchKey.isEmpty {
  176. return false
  177. }
  178. return true
  179. }
  180. }
  181. //MARK: NSOutlineViewDataSource,NSOutlineViewDelegate
  182. extension KMBOTAOutlineView : NSOutlineViewDataSource,NSOutlineViewDelegate {
  183. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  184. guard let rootModel = self.data else {
  185. return 0
  186. }
  187. guard let model = item as? KMBOTAOutlineItem else {
  188. if isValidSearchMode() { // 是否为搜索模块
  189. if self.hasContainString(searchKey, rootOutline: rootModel.outline) == false {
  190. return 0
  191. }
  192. return rootModel.searchChildren.count
  193. }
  194. return Int(rootModel.outline.numberOfChildren)
  195. }
  196. if isValidSearchMode() {
  197. return model.searchChildren.count
  198. }
  199. return model.children.count
  200. }
  201. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  202. guard let rootModel = self.data else {
  203. return ""
  204. }
  205. guard let model = item as? KMBOTAOutlineItem else {
  206. if isValidSearchMode() { // 是否为搜索模块
  207. if self.hasContainString(searchKey, rootOutline: rootModel.outline) == false {
  208. return ""
  209. }
  210. return rootModel.searchChildren.safe_element(for: index) as Any
  211. }
  212. return rootModel.children[index] as Any
  213. }
  214. if isValidSearchMode() {
  215. return model.searchChildren.safe_element(for: index) as Any
  216. }
  217. return model.children[index] as Any
  218. }
  219. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  220. guard let model = item as? KMBOTAOutlineItem else {
  221. return false
  222. }
  223. if isValidSearchMode() {
  224. let datas = self.fetchOutlines(for: model, searchString: searchKey)
  225. return !datas.isEmpty
  226. }
  227. return !model.children.isEmpty
  228. }
  229. func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
  230. if let item = item as? KMBOTAOutlineItem {
  231. if !item.isItemExpanded {
  232. item.isItemExpanded = true
  233. outlineView.animator().expandItem(item, expandChildren: true)
  234. return false
  235. }
  236. }
  237. return true
  238. }
  239. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  240. if let item = item as? KMBOTAOutlineItem {
  241. if item.isItemExpanded {
  242. item.isItemExpanded = false
  243. outlineView.animator().collapseItem(item, collapseChildren: true)
  244. return false
  245. }
  246. }
  247. return true
  248. }
  249. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  250. guard let model = item as? KMBOTAOutlineItem else {
  251. return nil
  252. }
  253. let cell : KMBOTAOutlineCellView = KMBOTAOutlineCellView.init()
  254. if isValidSearchMode() {
  255. var label = model.outline.label ?? ""
  256. let attri = NSMutableAttributedString(string: label, attributes: [
  257. .font : NSFont.SFProTextRegularFont(13),
  258. .foregroundColor : KMNColorTools.colorText_1()])
  259. var _searchKey = searchKey
  260. if caseSensitive == false {
  261. label = label.lowercased()
  262. _searchKey = _searchKey.lowercased()
  263. }
  264. let ranges = label.ranges(of: _searchKey)
  265. for range in ranges.nsRnage {
  266. attri.addAttributes([.font : NSFont.SFProTextBoldFont(13), .foregroundColor: KMNColorTools.colorPrimary_textLight()], range: range)
  267. }
  268. cell.titleLabel.attributedStringValue = attri
  269. cell.iconButton.isHidden = model.searchChildren.isEmpty
  270. } else {
  271. cell.titleLabel.stringValue = model.outline.label ?? ""
  272. if model.outline.numberOfChildren > 0 {
  273. cell.iconButton.isHidden = false
  274. } else {
  275. cell.iconButton.isHidden = true
  276. }
  277. }
  278. let isItemExpanded = model.isItemExpanded
  279. if isItemExpanded {
  280. cell.iconButton.image = NSImage(named: "KMImageNameArrowDown")
  281. } else {
  282. cell.iconButton.image = NSImage(named: "KMImageNameArrowRight")
  283. }
  284. cell.iconAction = { [unowned self] view in
  285. let rowIndex = outlineView.row(forItem: item)
  286. let rowView = outlineView.rowView(atRow: rowIndex, makeIfNecessary: false)
  287. self.didSelectItem(view: (rowView as? KMBOTAOutlineRowView), event: NSEvent())
  288. if self.selectItems?.count == 1 {
  289. self.needOpenOrCloseItem(oulineItem: (self.selectItems?.first)!)
  290. }
  291. }
  292. return cell
  293. }
  294. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  295. let rowView = KMBOTAOutlineRowView()
  296. rowView.model = item as? KMBOTAOutlineItem
  297. rowView.mouseDownCallback = { [unowned self] (view, event) in
  298. self.didSelectItem(view: view, event: event)
  299. }
  300. rowView.rightMouseCallback = { [unowned self] (view, event) in
  301. if !KMOCToolClass.arrayContains(array: self.selectItems, annotation: item) ||
  302. self.selectItems!.count == 1 {
  303. self.selectItem(outlineItem: item as! KMBOTAOutlineItem)
  304. }
  305. self.delegate?.BOTAOutlineView(self, rightDidMoseDown: item as! KMBOTAOutlineItem, event: event)
  306. }
  307. rowView.hoverCallback = { [unowned self] (mouseEntered, mouseBox) in
  308. self.outlineView.enumerateAvailableRowViews { view, row in
  309. if view is KMBOTAOutlineRowView {
  310. (view as? KMBOTAOutlineRowView)?.model?.hover = false
  311. (view as? KMBOTAOutlineRowView)?.reloadData()
  312. }
  313. }
  314. if mouseEntered {
  315. rowView.model?.hover = true
  316. } else {
  317. rowView.model?.hover = false
  318. }
  319. }
  320. return rowView
  321. }
  322. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  323. if item is KMBOTAOutlineItem {
  324. let tempItem: KMBOTAOutlineItem = item as! KMBOTAOutlineItem
  325. let string: NSString = tempItem.outline.label as NSString
  326. let paragraphStyle = NSMutableParagraphStyle()
  327. paragraphStyle.lineHeightMultiple = 1.32
  328. paragraphStyle.alignment = .left
  329. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  330. NSAttributedString.Key.font : NSFont.SFProTextRegularFont(14.0)]
  331. let size = string.boundingRect(with: NSMakeSize(outlineView.frame.size.width - 30, 200), options: NSString.DrawingOptions(rawValue: 3), attributes: attributes)
  332. return max(40, size.height + 16)
  333. }
  334. return 40
  335. }
  336. func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  337. guard let callBack = self.delegate else { return false}
  338. return callBack.BOTAOutlineView(self, writeItems: items, to: pasteboard)
  339. }
  340. func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  341. guard let callBack = self.delegate else { return NSDragOperation.init(rawValue: 0)}
  342. return callBack.BOTAOutlineView(self, validateDrop: info, proposedItem: item, proposedChildIndex: index)
  343. }
  344. func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  345. guard let callBack = self.delegate else { return false}
  346. return callBack.BOTAOutlineView(self, acceptDrop: info, item: item, childIndex: index)
  347. }
  348. func outlineViewSelectionDidChange(_ notification: Notification) {
  349. // if self.outlineView.selectedRow == -1 {
  350. // self.cancelSelect()
  351. // }
  352. }
  353. }
  354. extension KMBOTAOutlineView: KMTocOutlineViewDelegate {
  355. func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject? {
  356. if item == nil {
  357. return true as AnyObject
  358. }
  359. if let data = item as? KMBOTAOutlineItem {
  360. return data.outline.destination
  361. }
  362. return nil
  363. }
  364. }
  365. //MARK: Action
  366. extension KMBOTAOutlineView {
  367. @objc func expandAllComments(item: NSMenuItem) {
  368. self.reloadData(expandItemType: .expand)
  369. self.outlineView.reloadData()
  370. self.outlineView.expandItem(nil, expandChildren: true)
  371. }
  372. @objc func collapseAllComments(item: NSMenuItem) {
  373. self.reloadData(expandItemType: .collapse)
  374. self.outlineView.reloadData()
  375. self.outlineView.collapseItem(nil, collapseChildren: true)
  376. }
  377. func selectItem(outlineItem: KMBOTAOutlineItem) {
  378. let index = self.outlineView.row(forItem: outlineItem)
  379. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  380. self.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  381. }
  382. func selectIndex(index: Int) {
  383. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  384. self.didSelectItem(view: nil, event: NSEvent(), isNeedDelegate: false)
  385. }
  386. func cancelSelect() {
  387. guard let items = self.selectItems else { return }
  388. self.outlineView.deselectAll(nil)
  389. for model in items {
  390. model.select = false
  391. self.outlineView.reloadItem(model)
  392. }
  393. }
  394. func didSelectItem(view: KMBOTAOutlineRowView?, event: NSEvent, isNeedDelegate: Bool = true) {
  395. //当选中一个时
  396. if view != nil && (self.outlineView.selectedRowIndexes.count == 1 ||
  397. (!event.modifierFlags.contains(NSEvent.ModifierFlags.command) &&
  398. !event.modifierFlags.contains(NSEvent.ModifierFlags.shift))) {
  399. let rowView: KMBOTAOutlineRowView = view!
  400. let index = self.outlineView.row(for: rowView)
  401. self.outlineView.selectRowIndexes(IndexSet(integer: IndexSet.Element(index)), byExtendingSelection: false)
  402. }
  403. //原始数据置空
  404. if self.selectItems != nil {
  405. for item in self.selectItems! {
  406. item.select = false
  407. let index = self.outlineView.row(forItem: item)
  408. if index != -1 {
  409. if self.outlineView.rowView(atRow: index, makeIfNecessary: false) != nil {
  410. let rowView: KMBOTAOutlineRowView = self.outlineView.rowView(atRow: index, makeIfNecessary: false) as! KMBOTAOutlineRowView
  411. rowView.reloadData()
  412. }
  413. }
  414. }
  415. }
  416. //获取最新数据
  417. var items: [KMBOTAOutlineItem] = []
  418. for index in self.outlineView.selectedRowIndexes {
  419. if index != -1 {
  420. let item: KMBOTAOutlineItem = self.outlineView.item(atRow: index) as! KMBOTAOutlineItem
  421. item.select = true
  422. items.append(item)
  423. //刷新数据
  424. if self.outlineView.rowView(atRow: index, makeIfNecessary: false) != nil {
  425. let rowView: KMBOTAOutlineRowView = self.outlineView.rowView(atRow: index, makeIfNecessary: false) as! KMBOTAOutlineRowView
  426. rowView.reloadData()
  427. self.outlineView.reloadItem(item, reloadChildren: true)
  428. }
  429. }
  430. }
  431. self.selectItems = items
  432. // if self.selectItems?.count == 1 {
  433. // self.needOpenOrCloseItem(oulineItem: (self.selectItems?.first)!)
  434. // }
  435. if self.selectItems != nil && isNeedDelegate {
  436. self.delegate?.BOTAOutlineView(self, didSelectItem: self.selectItems!)
  437. }
  438. }
  439. func needOpenOrCloseItem(oulineItem: KMBOTAOutlineItem) {
  440. //只有一个选中项时开启关闭item
  441. if self.selectItems?.count == 1 {
  442. if self.outlineView.isItemExpanded(oulineItem) {
  443. self.outlineView.collapseItem(oulineItem)
  444. oulineItem.isItemExpanded = false
  445. } else {
  446. self.outlineView.expandItem(oulineItem)
  447. oulineItem.isItemExpanded = true
  448. }
  449. let row = self.outlineView.row(forItem: oulineItem)
  450. self.outlineView.reloadData(forRowIndexes: IndexSet(integer: row), columnIndexes: IndexSet(integer: 0))
  451. }
  452. }
  453. }