KMThumbnailTableView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // KMThumbnailTableView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. @objc protocol KMThumbnailTableViewDelegate: KMBotaTableViewDelegate {
  9. @objc optional func tableView(_ tableView: NSTableView, highlightLevelForRow row: Int) -> UInt
  10. @objc optional func tableView(_ tableView: NSTableView, commandSelectRow rowIndex: Int) -> Bool
  11. @objc optional func tableView(_ tableView: NSTableView, shiftSelectRow rowIndex: Int) -> Bool
  12. }
  13. class KMThumbnailTableView: KMBotaTableView {
  14. weak var thumbDelegate: KMThumbnailTableViewDelegate?
  15. override func draw(_ dirtyRect: NSRect) {
  16. super.draw(dirtyRect)
  17. // Drawing code here.
  18. }
  19. deinit {
  20. KMPrint("KMThumbnailTableView deinit.")
  21. NotificationCenter.default.removeObserver(self)
  22. }
  23. /*
  24. - (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect {
  25. // if ([[self delegate] respondsToSelector:@selector(tableView:highlightLevelForRow:)] &&
  26. // [self isRowSelected:row] == NO) {
  27. //
  28. // NSUInteger level = [[self delegate] tableView:self highlightLevelForRow:row];
  29. //
  30. // if (level < MAX_HIGHLIGHTS) {
  31. //
  32. // NSColor *color = [NSColor sourceListHighlightColorForView:self];
  33. // if (color) {
  34. // NSRect rect = [self rectOfRow:row];
  35. // if (NSIntersectsRect(rect, clipRect)) {
  36. // [NSGraphicsContext saveGraphicsState];
  37. // [[color colorWithAlphaComponent:0.1 * (MAX_HIGHLIGHTS - level)] setFill];
  38. // [NSBezierPath fillRect:rect];
  39. // [NSGraphicsContext restoreGraphicsState];
  40. // }
  41. // }
  42. // }
  43. // }
  44. [super drawRow:row clipRect:clipRect];
  45. }
  46. */
  47. override func highlightSelection(inClipRect clipRect: NSRect) {
  48. if self.selectionHighlightStyle == .regular {
  49. NSGraphicsContext.saveGraphicsState()
  50. var color = NSColor(red: 227.0/255.0, green: 238.0/255.0, blue: 250.0/255.0, alpha: 1)
  51. if KMAppearance.isSupportNewColor() {
  52. if KMAppearance.isDarkMode() {
  53. color = NSColor(red: 50.0/255.0, green: 68/255.0, blue: 92.0/255.0, alpha: 1)
  54. }
  55. }
  56. color.setFill()
  57. for row in self.selectedRowIndexes {
  58. __NSRectFill(self.rect(ofRow: row))
  59. }
  60. NSGraphicsContext.restoreGraphicsState()
  61. } else {
  62. super.highlightSelection(inClipRect: clipRect)
  63. }
  64. }
  65. override func preparedCell(atColumn column: Int, row: Int) -> NSCell? {
  66. let cell = super.preparedCell(atColumn: column, row: row)
  67. if self.selectionHighlightStyle == .regular && self.isRowSelected(row) && cell?.type == .textCellType {
  68. var attrString = cell?.attributedStringValue.mutableCopy() as? NSMutableAttributedString
  69. attrString?.addAttribute(.foregroundColor, value: NSColor.black, range: NSMakeRange(0, attrString?.length ?? 0))
  70. if attrString != nil {
  71. cell?.attributedStringValue = attrString!
  72. }
  73. }
  74. return cell
  75. }
  76. override func selectRowIndexes(_ indexes: IndexSet, byExtendingSelection extend: Bool) {
  77. super.selectRowIndexes(indexes, byExtendingSelection: extend)
  78. self._handleHighlightsChanged()
  79. }
  80. override func deselectRow(_ row: Int) {
  81. super.deselectRow(row)
  82. self._handleHighlightsChanged()
  83. }
  84. override func becomeFirstResponder() -> Bool {
  85. if super.becomeFirstResponder() {
  86. self._handleHighlightsChanged()
  87. return true
  88. }
  89. return false
  90. }
  91. override func resignFirstResponder() -> Bool {
  92. if super.resignFirstResponder() {
  93. self._handleHighlightsChanged()
  94. return true
  95. }
  96. return false
  97. }
  98. override func viewWillMove(toWindow newWindow: NSWindow?) {
  99. let oldWindow = self.window
  100. let names = [NSWindow.didBecomeMainNotification, NSWindow.didResignMainNotification, NSWindow.didBecomeKeyNotification, NSWindow.didResignKeyNotification]
  101. if (oldWindow != nil) {
  102. for name in names {
  103. NotificationCenter.default.removeObserver(self, name: name, object: oldWindow)
  104. }
  105. }
  106. if (newWindow != nil) {
  107. for name in names {
  108. NotificationCenter.default.addObserver(self, selector: #selector(_handleKeyOrMainStateChanged), name: name, object: newWindow)
  109. }
  110. }
  111. super.viewWillMove(toWindow: newWindow)
  112. }
  113. override func viewDidMoveToWindow() {
  114. super.viewDidMoveToWindow()
  115. if (self.window != nil) {
  116. self._handleKeyOrMainStateChanged(nil)
  117. }
  118. }
  119. override func mouseDown(with event: NSEvent) {
  120. if event.modifierFlags.contains(.command) {
  121. let row = self.row(at: event.locationInView(self))
  122. if row != -1 {
  123. if let data = self.thumbDelegate?.tableView?(self, commandSelectRow: row), data {
  124. return
  125. }
  126. }
  127. } else if event.modifierFlags.contains(.shift) {
  128. let row = self.row(at: event.locationInView(self))
  129. if row != -1 {
  130. if let data = self.thumbDelegate?.tableView?(self, shiftSelectRow: row), data {
  131. return
  132. }
  133. }
  134. }
  135. super.mouseDown(with: event)
  136. }
  137. }
  138. // MARK: - Private
  139. extension KMThumbnailTableView {
  140. @objc private func _handleKeyOrMainStateChanged(_ note: NSNotification?) {
  141. if self.selectionHighlightStyle == .sourceList {
  142. self._handleHighlightsChanged()
  143. } else {
  144. if let win = self.window, win.isMainWindow || win.isKeyWindow {
  145. self.backgroundColor = KMAppearance.viewBackgroundColor()
  146. } else {
  147. self.backgroundColor = .controlColor
  148. }
  149. self.needsDisplay = true
  150. }
  151. }
  152. @objc private func _handleHighlightsChanged() {
  153. if let _ = self.thumbDelegate?.tableView?(self, highlightLevelForRow: -1) {
  154. self.needsDisplay = true
  155. }
  156. }
  157. }