Skip to content

Commit

Permalink
perf: improve dismissing pop button
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed May 6, 2024
1 parent 6cdd7b3 commit 18e476f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 12 additions & 9 deletions Easydict/objc/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,16 @@ - (void)recordSelectTextInfo {
/// Auto get selected text.
- (void)autoGetSelectedText:(BOOL)checkTextFrame {
if ([self enabledAutoSelectText]) {
// MMLogInfo(@"auto get selected text");
MMLogInfo(@"auto get selected text");

self.movedY = 0;
self.actionType = EZActionTypeAutoSelectQuery;

[self getSelectedText:checkTextFrame completion:^(NSString *_Nullable text) {
self.isPopButtonVisible = YES;

[self handleSelectedText:text];
}];
self.isPopButtonVisible = YES;
}
}

Expand All @@ -417,12 +419,10 @@ - (BOOL)enabledAutoSelectText {
}

- (void)handleSelectedText:(NSString *)text {
[self cancelDismissPop];

NSString *trimText = [text trim];
if (trimText.length > 0 && self.selectedTextBlock) {
[self cancelDismissPopButton];
self.selectedTextBlock(trimText);
[self cancelDismissPop];
}
}

Expand Down Expand Up @@ -759,9 +759,11 @@ - (void)handleMonitorEvent:(NSEvent *)event {
}
case NSEventTypeKeyDown: {
// ???: The debugging environment sometimes does not work and it seems that you have to move the application to the application directory to get it to work properly.
// MMLogInfo(@"key down");
// MMLogInfo(@"key down: %@, modifierFlags: %ld", event.characters, event.modifierFlags);

[self dismissPopButton];
if (self.isPopButtonVisible) {
[self dismissPopButton];
}
break;
}
case NSEventTypeScrollWheel: {
Expand Down Expand Up @@ -924,14 +926,15 @@ - (void)delayDismissPopButton:(NSTimeInterval)delayTime {
[self performSelector:@selector(dismissPopButton) withObject:nil afterDelay:delayTime];
}

- (void)cancelDismissPop {
- (void)cancelDismissPopButton {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissPopButton) object:nil];
}

- (void)dismissPopButton {
if (self.dismissPopButtonBlock) {
self.dismissPopButtonBlock();
}

self.isPopButtonVisible = NO;

[self stopCGEventTap];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ - (void)setupEventMonitor {
[self.eventMonitor setSelectedTextBlock:^(NSString *_Nonnull selectedText) {
mm_strongify(self);

// if ([self hasEasydictRunningInDebugMode]) {
// return;
// }
// MMLogInfo(@"auto get selected text successfully: %@", selectedText.truncated);

self.selectedText = selectedText ?: @"";
self.actionType = self.eventMonitor.actionType;
Expand Down

0 comments on commit 18e476f

Please sign in to comment.