Skip to content

Commit

Permalink
perf: reduce the handling of listening mouse events to reduce the bur…
Browse files Browse the repository at this point in the history
…den on CUP(tisfeng#223)
  • Loading branch information
tisfeng committed Nov 17, 2023
1 parent e4745e2 commit a57fe08
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions Easydict/Feature/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ @interface EZEventMonitor ()
@property (nonatomic, assign) CFMachPortRef eventTap;

@property (nonatomic, assign) EZTriggerType frontmostAppTriggerType;
@property (nonatomic, assign) BOOL isPopButtonVisible;

@end

Expand Down Expand Up @@ -390,11 +391,14 @@ - (void)recordSelectTextInfo {
/// Auto get selected text.
- (void)autoGetSelectedText:(BOOL)checkTextFrame {
if ([self enabledAutoSelectText]) {
// NSLog(@"auto get selected text");

self.movedY = 0;
self.actionType = EZActionTypeAutoSelectQuery;
[self getSelectedText:checkTextFrame completion:^(NSString *_Nullable text) {
[self handleSelectedText:text];
}];
self.isPopButtonVisible = YES;
}
}

Expand Down Expand Up @@ -726,7 +730,6 @@ - (BOOL)isSupportEmptyCopy {
- (void)handleMonitorEvent:(NSEvent *)event {
// NSLog(@"type: %ld", event.type);


switch (event.type) {
case NSEventTypeLeftMouseUp: {
if ([self checkIfLeftMouseDragged]) {
Expand All @@ -738,10 +741,17 @@ - (void)handleMonitorEvent:(NSEvent *)event {
break;
}
case NSEventTypeLeftMouseDown: {
// NSLog(@"mouse down");

// Record some mouse event except dragged event.
[self updateRecordedEvents:event];

[self handleLeftMouseDownEvent:event];
break;
}
case NSEventTypeLeftMouseDragged: {
// Record dragged event.
[self updateRecordedEvents:event];
// NSLog(@"NSEventTypeLeftMouseDragged");
break;
}
Expand All @@ -759,20 +769,24 @@ - (void)handleMonitorEvent:(NSEvent *)event {
break;
}
case NSEventTypeScrollWheel: {
CGFloat deltaY = event.scrollingDeltaY;
self.movedY += deltaY;
// NSLog(@"movedY: %.1f", self.movedY);

CGFloat maxDeltaY = 80;
if (fabs(self.movedY) > maxDeltaY) {
[self dismissPopButton];
if (self.isPopButtonVisible) {
CGFloat deltaY = event.scrollingDeltaY;
self.movedY += deltaY;
// NSLog(@"movedY: %.1f", self.movedY);

CGFloat maxDeltaY = 80;
if (fabs(self.movedY) > maxDeltaY) {
[self dismissPopButton];
}
}
break;
}
case NSEventTypeMouseMoved: {
// Hide the button after exceeding a certain range of selected text frame.
if (![self isMouseInPopButtonExpandedFrame]) {
[self dismissPopButton];
if (self.isPopButtonVisible) {
// Hide the button after exceeding a certain range of selected text frame.
if (![self isMouseInPopButtonExpandedFrame]) {
[self dismissPopButton];
}
}
break;
}
Expand Down Expand Up @@ -801,13 +815,15 @@ - (void)handleMonitorEvent:(NSEvent *)event {

default:
// NSLog(@"default type: %ld", event.type);
[self dismissPopButton];

if (self.isPopButtonVisible) {
[self dismissPopButton];
}
break;
}

[self updateRecoredEvents:event];
}


- (void)dismissWindowsIfMouseLocationOutsideFloatingWindow {
EZWindowManager *windowManager = EZWindowManager.shared;
if (windowManager.floatingWindowType == EZWindowTypeMini) {
Expand All @@ -834,7 +850,7 @@ - (BOOL)checkIfMouseLocationInWindow:(NSWindow *)window {
}

// If recoredEevents count > kRecoredEeventCount, remove the first one
- (void)updateRecoredEvents:(NSEvent *)event {
- (void)updateRecordedEvents:(NSEvent *)event {
if (self.recordEvents.count >= kRecordEventCount) {
[self.recordEvents removeObjectAtIndex:0];
}
Expand Down Expand Up @@ -931,6 +947,7 @@ - (void)dismissPopButton {
if (self.dismissPopButtonBlock) {
self.dismissPopButtonBlock();
}
self.isPopButtonVisible = NO;
}


Expand Down

0 comments on commit a57fe08

Please sign in to comment.