KMThumbnailTableView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. override func highlightSelection(inClipRect clipRect: NSRect) {
  24. if self.selectionHighlightStyle == .regular {
  25. NSGraphicsContext.saveGraphicsState()
  26. var color = NSColor(red: 227.0/255.0, green: 238.0/255.0, blue: 250.0/255.0, alpha: 1)
  27. if KMAppearance.isSupportNewColor() {
  28. if KMAppearance.isDarkMode() {
  29. color = NSColor(red: 50.0/255.0, green: 68/255.0, blue: 92.0/255.0, alpha: 1)
  30. }
  31. }
  32. color.setFill()
  33. for row in self.selectedRowIndexes {
  34. __NSRectFill(self.rect(ofRow: row))
  35. }
  36. NSGraphicsContext.restoreGraphicsState()
  37. } else {
  38. super.highlightSelection(inClipRect: clipRect)
  39. }
  40. }
  41. override func preparedCell(atColumn column: Int, row: Int) -> NSCell? {
  42. let cell = super.preparedCell(atColumn: column, row: row)
  43. if self.selectionHighlightStyle == .regular && self.isRowSelected(row) && cell?.type == .textCellType {
  44. let attrString = cell?.attributedStringValue.mutableCopy() as? NSMutableAttributedString
  45. attrString?.addAttribute(.foregroundColor, value: NSColor.black, range: NSMakeRange(0, attrString?.length ?? 0))
  46. if attrString != nil {
  47. cell?.attributedStringValue = attrString!
  48. }
  49. }
  50. return cell
  51. }
  52. override func selectRowIndexes(_ indexes: IndexSet, byExtendingSelection extend: Bool) {
  53. super.selectRowIndexes(indexes, byExtendingSelection: extend)
  54. self._handleHighlightsChanged()
  55. }
  56. override func deselectRow(_ row: Int) {
  57. super.deselectRow(row)
  58. self._handleHighlightsChanged()
  59. }
  60. override func becomeFirstResponder() -> Bool {
  61. if super.becomeFirstResponder() {
  62. self._handleHighlightsChanged()
  63. return true
  64. }
  65. return false
  66. }
  67. override func resignFirstResponder() -> Bool {
  68. if super.resignFirstResponder() {
  69. self._handleHighlightsChanged()
  70. return true
  71. }
  72. return false
  73. }
  74. override func viewWillMove(toWindow newWindow: NSWindow?) {
  75. let oldWindow = self.window
  76. let names = [NSWindow.didBecomeMainNotification, NSWindow.didResignMainNotification, NSWindow.didBecomeKeyNotification, NSWindow.didResignKeyNotification]
  77. if (oldWindow != nil) {
  78. for name in names {
  79. NotificationCenter.default.removeObserver(self, name: name, object: oldWindow)
  80. }
  81. }
  82. if (newWindow != nil) {
  83. for name in names {
  84. NotificationCenter.default.addObserver(self, selector: #selector(_handleKeyOrMainStateChanged), name: name, object: newWindow)
  85. }
  86. }
  87. super.viewWillMove(toWindow: newWindow)
  88. }
  89. override func viewDidMoveToWindow() {
  90. super.viewDidMoveToWindow()
  91. if (self.window != nil) {
  92. self._handleKeyOrMainStateChanged(nil)
  93. }
  94. }
  95. override func mouseDown(with event: NSEvent) {
  96. if event.modifierFlags.contains(.command) {
  97. let row = self.row(at: event.locationInView(self))
  98. if row != -1 {
  99. if let data = self.thumbDelegate?.tableView?(self, commandSelectRow: row), data {
  100. return
  101. }
  102. }
  103. } else if event.modifierFlags.contains(.shift) {
  104. let row = self.row(at: event.locationInView(self))
  105. if row != -1 {
  106. if let data = self.thumbDelegate?.tableView?(self, shiftSelectRow: row), data {
  107. return
  108. }
  109. }
  110. }
  111. super.mouseDown(with: event)
  112. }
  113. }
  114. // MARK: - Private
  115. extension KMThumbnailTableView {
  116. @objc private func _handleKeyOrMainStateChanged(_ note: NSNotification?) {
  117. if self.selectionHighlightStyle == .sourceList {
  118. self._handleHighlightsChanged()
  119. } else {
  120. if let win = self.window, win.isMainWindow || win.isKeyWindow {
  121. self.backgroundColor = KMAppearance.viewBackgroundColor()
  122. } else {
  123. self.backgroundColor = .controlColor
  124. }
  125. self.needsDisplay = true
  126. }
  127. }
  128. @objc private func _handleHighlightsChanged() {
  129. if let _ = self.thumbDelegate?.tableView?(self, highlightLevelForRow: -1) {
  130. self.needsDisplay = true
  131. }
  132. }
  133. }