KMCoverButton.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // KMCoverButton.m
  3. // PDF Reader
  4. //
  5. // Created by tangchao on 2022/2/24.
  6. // Copyright © 2022 Kdan Mobile. All rights reserved.
  7. //
  8. #import "KMCoverButton.h"
  9. @interface KMCoverButton ()
  10. @property (nonatomic, strong) NSTrackingArea *area;
  11. @end
  12. @implementation KMCoverButton
  13. - (void)dealloc {
  14. [self removeTrackingArea:_area];
  15. }
  16. - (void)updateTrackingAreas {
  17. [super updateTrackingAreas];
  18. if (self.area) {
  19. [self removeTrackingArea:self.area];
  20. self.area = nil;
  21. }
  22. int opts = (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways);
  23. self.area = [[NSTrackingArea alloc] initWithRect:self.bounds options:opts owner:self userInfo:nil];
  24. [self addTrackingArea:self.area];
  25. }
  26. - (void)mouseEntered:(NSEvent *)event {
  27. if (self.coverAction) {
  28. self.coverAction(self, KMCoverActionEnter);
  29. }
  30. }
  31. - (void)mouseExited:(NSEvent *)event {
  32. if (self.coverAction) {
  33. self.coverAction(self, KMCoverActionExit);
  34. }
  35. }
  36. - (void)mouseMoved:(NSEvent *)event {
  37. if (self.coverAction) {
  38. self.coverAction(self, KMCoverActionMove);
  39. }
  40. }
  41. @end