From a57fe08f8bc48d02d9002accca08cf46bd64e645 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 17 Nov 2023 00:26:43 +0800 Subject: [PATCH] perf: reduce the handling of listening mouse events to reduce the burden on CUP(#223) --- .../Feature/EventMonitor/EZEventMonitor.m | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/Easydict/Feature/EventMonitor/EZEventMonitor.m b/Easydict/Feature/EventMonitor/EZEventMonitor.m index eb60739b3..7d68567b6 100644 --- a/Easydict/Feature/EventMonitor/EZEventMonitor.m +++ b/Easydict/Feature/EventMonitor/EZEventMonitor.m @@ -65,6 +65,7 @@ @interface EZEventMonitor () @property (nonatomic, assign) CFMachPortRef eventTap; @property (nonatomic, assign) EZTriggerType frontmostAppTriggerType; +@property (nonatomic, assign) BOOL isPopButtonVisible; @end @@ -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; } } @@ -726,7 +730,6 @@ - (BOOL)isSupportEmptyCopy { - (void)handleMonitorEvent:(NSEvent *)event { // NSLog(@"type: %ld", event.type); - switch (event.type) { case NSEventTypeLeftMouseUp: { if ([self checkIfLeftMouseDragged]) { @@ -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; } @@ -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; } @@ -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) { @@ -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]; } @@ -931,6 +947,7 @@ - (void)dismissPopButton { if (self.dismissPopButtonBlock) { self.dismissPopButtonBlock(); } + self.isPopButtonVisible = NO; }