KMTableRowView.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // KMTableRowView.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by zhipeng jiang on 2020/11/25.
  6. //
  7. #import "KMTableRowView.h"
  8. @implementation KMTableRowView
  9. - (instancetype)init {
  10. self = [super init];
  11. if (self) {
  12. [self addTrackingArea];
  13. self.selectionInset = NSEdgeInsetsZero;
  14. self.selectionRadius = 0.f;
  15. }
  16. return self;
  17. }
  18. - (void)setHasBottomLine:(BOOL)hasBottomLine {
  19. _hasBottomLine = hasBottomLine;
  20. [self setNeedsDisplay:YES];
  21. }
  22. - (void)setBottomLineColor:(NSColor *)bottomLineColor {
  23. _bottomLineColor = bottomLineColor;
  24. [self setNeedsDisplay:YES];
  25. }
  26. #pragma mark - Private Methods
  27. - (void)addTrackingArea {
  28. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways |NSTrackingMouseMoved owner:self userInfo:nil];
  29. [self addTrackingArea:trackingArea];
  30. }
  31. - (void)drawRect:(NSRect)dirtyRect {
  32. [super drawRect:dirtyRect];
  33. if (self.hasBottomLine) {
  34. NSColor *color = self.bottomLineColor;
  35. if (color == nil) {
  36. color = [NSColor lightGrayColor];
  37. }
  38. [color setStroke];
  39. [color setFill];
  40. NSRect lineRect = NSMakeRect(0, NSHeight(self.bounds)-1, NSWidth(self.bounds), 1);
  41. NSBezierPath *linePath = [NSBezierPath bezierPathWithRoundedRect:lineRect xRadius:0 yRadius:0];
  42. [linePath fill];
  43. [linePath stroke];
  44. }
  45. }
  46. - (void)drawSelectionInRect:(NSRect)dirtyRect {
  47. if (!self.selectionBackgroundColorBlock) {
  48. // [super drawSelectionInRect:dirtyRect];
  49. NSRect selectionRect = self.bounds;
  50. [[NSColor lightGrayColor] setFill];
  51. NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:0 yRadius:0];
  52. [selectionPath fill];
  53. return;
  54. }
  55. // NSRect selectionRect = NSInsetRect(self.bounds, 0, 0);
  56. NSRect selectionRect = NSMakeRect(self.selectionInset.left, self.selectionInset.top, NSWidth(self.bounds)-self.selectionInset.left-self.selectionInset.right, NSHeight(self.bounds)-self.selectionInset.top-self.selectionInset.bottom);
  57. NSColor *color = self.selectionBackgroundColorBlock();
  58. [color setStroke];
  59. [color setFill];
  60. NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:self.selectionRadius yRadius:self.selectionRadius];
  61. [selectionPath fill];
  62. [selectionPath stroke];
  63. }
  64. - (void)mouseEntered:(NSEvent *)event {
  65. [super mouseEntered:event];
  66. }
  67. @end