KMTocOutlineView.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // KMTocOutlineView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/17.
  6. //
  7. import Cocoa
  8. @objc protocol KMTocOutlineViewDelegate: KMCustomOutlineViewDelegate {
  9. @objc optional func outlineView(_ anOutlineView: NSOutlineView, highlightLevelForRow row: Int) -> Int
  10. @objc optional func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject?
  11. }
  12. class KMTocOutlineView: KMCustomOutlineView {
  13. weak var tocDelegate: KMTocOutlineViewDelegate? {
  14. didSet {
  15. self._rebuildTrackingAreas()
  16. }
  17. }
  18. var hasImageToolTips: Bool {
  19. get {
  20. return self._kmTrackingAreas != nil
  21. }
  22. set {
  23. if (newValue && self._kmTrackingAreas == nil) {
  24. self._kmTrackingAreas = []
  25. if (self.window != nil) {
  26. self._rebuildTrackingAreas()
  27. }
  28. } else if (newValue == false && self._kmTrackingAreas != nil) {
  29. if self.window != nil {
  30. self._removeTrackingAreas()
  31. }
  32. }
  33. }
  34. }
  35. private var _kmTrackingAreas: [NSTrackingArea]?
  36. private let MAX_HIGHLIGHTS = 5
  37. deinit {
  38. KMPrint("KMTocOutlineView deinit.")
  39. NotificationCenter.default.removeObserver(self)
  40. }
  41. override func draw(_ dirtyRect: NSRect) {
  42. super.draw(dirtyRect)
  43. }
  44. override func drawRow(_ row: Int, clipRect: NSRect) {
  45. // if (RUNNING_BEFORE(10_10) && [[self delegate] respondsToSelector:@selector(outlineView:highlightLevelForRow:)] &&
  46. // [self isRowSelected:row] == NO) {
  47. if let level = self.tocDelegate?.outlineView?(self, highlightLevelForRow: row), self.isRowSelected(row) == false {
  48. if (level < MAX_HIGHLIGHTS) {
  49. var color = NSColor(red: 0.556, green: 0.615, blue: 0.748, alpha: 1.0)
  50. let rect = self.rect(ofRow: row)
  51. if (NSIntersectsRect(rect, clipRect)) {
  52. NSGraphicsContext.saveGraphicsState()
  53. color.withAlphaComponent(0.1 * (CGFloat(MAX_HIGHLIGHTS) - level.cgFloat)).setFill()
  54. NSBezierPath.fill(rect)
  55. NSGraphicsContext.restoreGraphicsState()
  56. }
  57. }
  58. }
  59. super.drawRow(row, clipRect: clipRect)
  60. }
  61. override func highlightSelection(inClipRect clipRect: NSRect) {
  62. if self.selectionHighlightStyle == .regular {
  63. NSGraphicsContext.saveGraphicsState()
  64. NSColor(red: 0.556, green: 0.615, blue: 0.748, alpha: 1.0).setFill()
  65. for i in self.selectedRowIndexes {
  66. __NSRectFill(self.rect(ofRow: i))
  67. }
  68. NSGraphicsContext.restoreGraphicsState()
  69. } else {
  70. super.highlightSelection(inClipRect: clipRect)
  71. }
  72. }
  73. override func preparedCell(atColumn column: Int, row: Int) -> NSCell? {
  74. let cell = super.preparedCell(atColumn: column, row: row)
  75. if self.selectionHighlightStyle == .regular && self.isRowSelected(row) && cell?.type == .textCellType {
  76. if var attrString = cell?.attributedStringValue.mutableCopy() as? NSMutableAttributedString {
  77. let font = NSFontManager.shared.convert(cell?.font ?? .systemFont(ofSize: 12), toHaveTrait: .boldFontMask)
  78. attrString.addAttributes([.font : font], range: NSMakeRange(0, attrString.length))
  79. cell?.attributedStringValue = attrString
  80. }
  81. }
  82. return cell
  83. }
  84. override func selectRowIndexes(_ indexes: IndexSet, byExtendingSelection extend: Bool) {
  85. super.selectRowIndexes(indexes, byExtendingSelection: extend)
  86. self._handleHighlightsChanged()
  87. }
  88. override func deselectRow(_ row: Int) {
  89. super.deselectRow(row)
  90. self._handleHighlightsChanged()
  91. }
  92. override func becomeFirstResponder() -> Bool {
  93. if super.becomeFirstResponder() {
  94. self._handleHighlightsChanged()
  95. return true
  96. }
  97. return false
  98. }
  99. override func resignFirstResponder() -> Bool {
  100. if super.resignFirstResponder() {
  101. self._handleHighlightsChanged()
  102. return true
  103. }
  104. return false
  105. }
  106. override func updateTrackingAreas() {
  107. super.updateTrackingAreas()
  108. self._rebuildTrackingAreas()
  109. }
  110. override func noteNumberOfRowsChanged() {
  111. super.noteNumberOfRowsChanged()
  112. self._rebuildTrackingAreas()
  113. }
  114. override func viewDidMoveToWindow() {
  115. super.viewDidMoveToWindow()
  116. if self.window != nil {
  117. self._handleKeyOrMainStateChanged(nil)
  118. }
  119. }
  120. override func viewWillMove(toWindow newWindow: NSWindow?) {
  121. let oldWindow = self.window
  122. let names = [NSWindow.didBecomeMainNotification, NSWindow.didResignMainNotification, NSWindow.didBecomeKeyNotification, NSWindow.didResignKeyNotification]
  123. if oldWindow != nil {
  124. for name in names {
  125. NotificationCenter.default.removeObserver(self, name: name, object: oldWindow)
  126. }
  127. }
  128. if newWindow != nil {
  129. for name in names {
  130. NotificationCenter.default.addObserver(self, selector: #selector(_handleKeyOrMainStateChanged), name: name, object: newWindow)
  131. }
  132. }
  133. super.viewWillMove(toWindow: newWindow)
  134. }
  135. override func keyDown(with event: NSEvent) {
  136. let eventChar = event.firstCharacter()
  137. let modifiers = event.standardPDFListViewModifierFlags()
  138. if (eventChar == NSLeftArrowFunctionKey && modifiers == (NSEvent.ModifierFlags.command.rawValue + NSEvent.ModifierFlags.option.rawValue)) {
  139. self.collapseItem(nil, collapseChildren: true)
  140. } else if (eventChar == NSRightArrowFunctionKey && modifiers == (NSEvent.ModifierFlags.command.rawValue + NSEvent.ModifierFlags.option.rawValue)) {
  141. self.expandItem(nil, expandChildren: true)
  142. } else {
  143. super.keyDown(with: event)
  144. }
  145. }
  146. override func mouseEntered(with event: NSEvent) {
  147. if (self._kmTrackingAreas == nil) {
  148. return
  149. }
  150. let userInfo = event.trackingArea?.userInfo
  151. if let rowNumber = userInfo?["row"] as? Int {
  152. var item = self.item(atRow: rowNumber)
  153. if let context = self.tocDelegate?.outlineView?(self, imageContextForItem: item) as? KMImageToolTipContext {
  154. KMImageToolTipWindow.shared.showForImageContext(context, at: .zero)
  155. }
  156. }
  157. }
  158. override func mouseExited(with event: NSEvent) {
  159. let userInfo = event.trackingArea?.userInfo
  160. if let row = userInfo?["row"] {
  161. KMImageToolTipWindow.shared.fadeOut()
  162. }
  163. }
  164. }
  165. // MARK: - Private Methods
  166. extension KMTocOutlineView {
  167. private func _handleHighlightsChanged() {
  168. // if (RUNNING_BEFORE(10_10) && [[self delegate] respondsToSelector:@selector(outlineView:highlightLevelForRow:)])
  169. if let _ = self.tocDelegate?.outlineView?(self, highlightLevelForRow: -1) {
  170. self.needsDisplay = true
  171. }
  172. }
  173. @objc private func _handleKeyOrMainStateChanged(_ note: NSNotification?) {
  174. if self.selectionHighlightStyle == .sourceList {
  175. self._handleHighlightsChanged()
  176. } else {
  177. if let win = self.window, win.isMainWindow || win.isKeyWindow {
  178. // self.backgroundColor = NSColor(calibratedRed: 0.839216, green: 0.866667, blue: 0.898039, alpha: 1)
  179. } else {
  180. // self.backgroundColor = .controlColor
  181. }
  182. self.needsDisplay = true
  183. }
  184. }
  185. private func _rebuildTrackingAreas() {
  186. if self._kmTrackingAreas == nil || self.tocDelegate?.outlineView?(self, imageContextForItem: nil) == nil {
  187. return
  188. }
  189. self._removeTrackingAreas()
  190. if (self.window != nil) {
  191. let visibleRect = self.visibleRect
  192. let rowRange = self.rows(in: visibleRect)
  193. // NSUInteger row;
  194. for row in rowRange.location ..< NSMaxRange(rowRange) {
  195. self._addTrackingAreaForRow(row)
  196. }
  197. }
  198. }
  199. private func _removeTrackingAreas() {
  200. if (self._kmTrackingAreas == nil) {
  201. return
  202. }
  203. for area in self._kmTrackingAreas! {
  204. self.removeTrackingArea(area)
  205. }
  206. self._kmTrackingAreas?.removeAll()
  207. }
  208. private func _addTrackingAreaForRow(_ row: Int) {
  209. if (self._kmTrackingAreas == nil) {
  210. return
  211. }
  212. let userInfo = ["row" : row]
  213. let area = NSTrackingArea(rect: self.rect(ofRow: row), options: [.mouseEnteredAndExited, .activeInActiveApp], owner: self, userInfo: userInfo)
  214. self.addTrackingArea(area)
  215. self._kmTrackingAreas?.append(area)
  216. }
  217. }