KMThumbnailTableView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // KMThumbnailTableView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. /*
  9. @protocol SKThumbnailTableViewDelegate <SKTableViewDelegate>
  10. @optional
  11. - (NSUInteger)tableView:(NSTableView *)tableView highlightLevelForRow:(NSInteger)row;
  12. - (BOOL)tableView:(NSTableView *)tableView commandSelectRow:(NSInteger)rowIndex;
  13. - (BOOL)tableView:(NSTableView *)tableView shiftSelectRow:(NSInteger)rowIndex;
  14. @end
  15. */
  16. @objc protocol KMThumbnailTableViewDelegate: KMBotaTableViewDelegate {
  17. @objc optional func tableView(_ tableView: NSTableView, highlightLevelForRow row: Int) -> UInt
  18. @objc optional func tableView(_ tableView: NSTableView, commandSelectRow rowIndex: Int) -> Bool
  19. @objc optional func tableView(_ tableView: NSTableView, shiftSelectRow rowIndex: Int) -> Bool
  20. }
  21. class KMThumbnailTableView: KMBotaTableView {
  22. /*
  23. - (id <SKThumbnailTableViewDelegate>)delegate;
  24. - (void)setDelegate:(id <SKThumbnailTableViewDelegate>)newDelegate;
  25. */
  26. override func draw(_ dirtyRect: NSRect) {
  27. super.draw(dirtyRect)
  28. // Drawing code here.
  29. }
  30. /*
  31. - (void)dealloc {
  32. [[NSNotificationCenter defaultCenter] removeObserver:self];
  33. [super dealloc];
  34. }
  35. - (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect {
  36. // if ([[self delegate] respondsToSelector:@selector(tableView:highlightLevelForRow:)] &&
  37. // [self isRowSelected:row] == NO) {
  38. //
  39. // NSUInteger level = [[self delegate] tableView:self highlightLevelForRow:row];
  40. //
  41. // if (level < MAX_HIGHLIGHTS) {
  42. //
  43. // NSColor *color = [NSColor sourceListHighlightColorForView:self];
  44. // if (color) {
  45. // NSRect rect = [self rectOfRow:row];
  46. // if (NSIntersectsRect(rect, clipRect)) {
  47. // [NSGraphicsContext saveGraphicsState];
  48. // [[color colorWithAlphaComponent:0.1 * (MAX_HIGHLIGHTS - level)] setFill];
  49. // [NSBezierPath fillRect:rect];
  50. // [NSGraphicsContext restoreGraphicsState];
  51. // }
  52. // }
  53. // }
  54. // }
  55. [super drawRow:row clipRect:clipRect];
  56. }
  57. - (void)highlightSelectionInClipRect:(NSRect)clipRect {
  58. if ([self selectionHighlightStyle] == NSTableViewSelectionHighlightStyleRegular) {
  59. [NSGraphicsContext saveGraphicsState];
  60. NSColor *color = [NSColor colorWithRed:227.0/255.0 green:238.0/255.0 blue:250.0/255.0 alpha:1.0];
  61. if (@available(macOS 10.14, *)) {
  62. NSAppearanceName appearanceName = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
  63. if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) {
  64. color = [NSColor colorWithRed:50.0/255.0 green:68.0/255.0 blue:92.0/255.0 alpha:1.0];
  65. }
  66. }
  67. [color setFill];
  68. [[self selectedRowIndexes] enumerateIndexesUsingBlock:^(NSUInteger row, BOOL *stop){
  69. NSRectFill([self rectOfRow:row]);
  70. }];
  71. [NSGraphicsContext restoreGraphicsState];
  72. } else {
  73. [super highlightSelectionInClipRect:clipRect];
  74. }
  75. }
  76. - (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row {
  77. NSCell *cell = [super preparedCellAtColumn:column row:row];
  78. if ([self selectionHighlightStyle] == NSTableViewSelectionHighlightStyleRegular && [self isRowSelected:row] && [cell type] == NSTextCellType) {
  79. NSMutableAttributedString *attrString = [[cell attributedStringValue] mutableCopy];
  80. [attrString addAttribute:NSForegroundColorAttributeName
  81. value:[NSColor blackColor]
  82. range:NSMakeRange(0, [attrString length])];
  83. [cell setAttributedStringValue:attrString];
  84. [attrString release];
  85. }
  86. return cell;
  87. }
  88. - (void)handleHighlightsChanged {
  89. if ([[self delegate] respondsToSelector:@selector(tableView:highlightLevelForRow:)])
  90. [self setNeedsDisplay:YES];
  91. }
  92. - (void)selectRowIndexes:(NSIndexSet *)indexes byExtendingSelection:(BOOL)extendSelection {
  93. [super selectRowIndexes:indexes byExtendingSelection:extendSelection];
  94. [self handleHighlightsChanged];
  95. }
  96. - (void)deselectRow:(NSInteger)row {
  97. [super deselectRow:row];
  98. [self handleHighlightsChanged];
  99. }
  100. - (BOOL)becomeFirstResponder {
  101. if ([super becomeFirstResponder]) {
  102. [self handleHighlightsChanged];
  103. return YES;
  104. }
  105. return NO;
  106. }
  107. - (BOOL)resignFirstResponder {
  108. if ([super resignFirstResponder]) {
  109. [self handleHighlightsChanged];
  110. return YES;
  111. }
  112. return NO;
  113. }
  114. - (void)handleKeyOrMainStateChanged:(NSNotification *)note {
  115. if ([self selectionHighlightStyle] == NSTableViewSelectionHighlightStyleSourceList) {
  116. [self handleHighlightsChanged];
  117. } else {
  118. if ([[self window] isMainWindow] || [[self window] isKeyWindow])
  119. [self setBackgroundColor:[KMAppearance viewBackgroundColor]];
  120. else
  121. [self setBackgroundColor:[NSColor controlColor]];
  122. [self setNeedsDisplay:YES];
  123. }
  124. }
  125. - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
  126. NSWindow *oldWindow = [self window];
  127. NSArray *names = [NSArray arrayWithObjects:NSWindowDidBecomeMainNotification, NSWindowDidResignMainNotification, NSWindowDidBecomeKeyNotification, NSWindowDidResignKeyNotification, nil];
  128. if (oldWindow) {
  129. for (NSString *name in names)
  130. [[NSNotificationCenter defaultCenter] removeObserver:self name:name object:oldWindow];
  131. }
  132. if (newWindow) {
  133. for (NSString *name in names)
  134. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyOrMainStateChanged:) name:name object:newWindow];
  135. }
  136. [super viewWillMoveToWindow:newWindow];
  137. }
  138. - (void)viewDidMoveToWindow {
  139. [super viewDidMoveToWindow];
  140. if ([self window])
  141. [self handleKeyOrMainStateChanged:nil];
  142. }
  143. - (void)mouseDown:(NSEvent *)theEvent {
  144. if (([theEvent modifierFlags] & NSCommandKeyMask) && [[self delegate] respondsToSelector:@selector(tableView:commandSelectRow:)]) {
  145. NSInteger row = [self rowAtPoint:[theEvent locationInView:self]];
  146. if (row != -1 && [[self delegate] tableView:self commandSelectRow:row])
  147. return;
  148. } else if (([theEvent modifierFlags] & NSShiftKeyMask) && [[self delegate] respondsToSelector:@selector(tableView:shiftSelectRow:)]) {
  149. NSInteger row = [self rowAtPoint:[theEvent locationInView:self]];
  150. if (row != -1 && [[self delegate] tableView:self shiftSelectRow:row])
  151. return;
  152. }
  153. [super mouseDown:theEvent];
  154. }
  155. - (id <SKThumbnailTableViewDelegate>)delegate { return (id <SKThumbnailTableViewDelegate>)[super delegate]; }
  156. - (void)setDelegate:(id <SKThumbnailTableViewDelegate>)newDelegate { [super setDelegate:newDelegate]; }
  157. */
  158. }