Format
Plain text
Post date
2015-06-04 21:02
Période de publication
Illimité
  1. @interface HMSourceListColoredView ()
  2. @property (strong, nonatomic) NSColor *backgroundColor;
  3. @property (nonatomic, getter=isObservingKeyState) BOOL observingKeyState;
  4. @end
  5. @implementation HMSourceListColoredView
  6. - (instancetype)initWithFrame:(NSRect)frameRect
  7. {
  8. self = [super initWithFrame:frameRect];
  9. if(floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_10) {
  10. NSVisualEffectView *view = [[NSVisualEffectView alloc] initWithFrame:self.frame];
  11. view.autoresizingMask = self.autoresizingMask;
  12. return (HMSourceListColoredView *)view;
  13. }
  14. return self;
  15. }
  16. - (void)dealloc
  17. {
  18. if (self.isObservingKeyState) {
  19. [[NSNotificationCenter defaultCenter] removeObserver:self
  20. name:NSWindowDidBecomeKeyNotification
  21. object:[self window]];
  22. [[NSNotificationCenter defaultCenter] removeObserver:self
  23. name:NSWindowDidResignKeyNotification
  24. object:[self window]];
  25. }
  26. }
  27. - (void)viewDidMoveToWindow
  28. {
  29. NSLog(@"Enter %s", __PRETTY_FUNCTION__);
  30. NSTableView *tableView = [[NSTableView alloc] init];
  31. [tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
  32. _backgroundColor = [tableView backgroundColor];
  33. [self addWindowKeyStateObservers];
  34. }
  35. - (void)addWindowKeyStateObservers
  36. {
  37. if (!self.isObservingKeyState) {
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:@selector(redisplay)
  40. name:NSWindowDidBecomeKeyNotification
  41. object:[self window]];
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(redisplay)
  44. name:NSWindowDidResignKeyNotification
  45. object:[self window]];
  46. }
  47. self.observingKeyState = YES;
  48. }
  49. - (void)redisplay
  50. {
  51. [self setNeedsDisplay:YES];
  52. }
  53. - (void)drawRect:(NSRect)dirtyRect
  54. {
  55. [_backgroundColor setFill];
  56. NSRectFill(dirtyRect);
  57. }
  58. @end
Télécharger Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text