Skip to content

Commit

Permalink
perf: use key event to replace selected text in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 15, 2023
1 parent 36473ef commit c77997f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Easydict/App/EZConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static NSTimeInterval const EZUpdateTableViewRowHeightAnimationDuration = 0.2;
static NSTimeInterval const EZNetWorkTimeoutInterval = 15.0;

// !!!: This floating window level shouldn't be higher than kCGModalPanelWindowLevel, otherwise it will cover system modal alert window.
static NSTimeInterval const EZFloatingWindowLevel = kCGModalPanelWindowLevel;
static CGFloat const EZFloatingWindowLevel = kCGModalPanelWindowLevel;

static NSInteger const EZEnglishWordMaxLength = 20;

Expand All @@ -52,5 +52,7 @@ static NSInteger const EZMaxThreeWordPhraseCount = 3;
/// Show word synonyms up to 5
static NSInteger const EZMaxFiveWordSynonymCount = 5;

static CGFloat const EZGetClipboardTextDelayTime = 0.1;


#endif /* EZConst_h */
2 changes: 1 addition & 1 deletion Easydict/Feature/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ - (void)getSelectedTextBySimulatedKey:(void (^)(NSString *_Nullable))completion
[self delayRecoverVolume];
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelayGetSelectedTextTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(EZGetClipboardTextDelayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSInteger newChangeCount = [pasteboard changeCount];
// If changeCount is equal to newChangeCount, it means that the copy value is nil.
if (changeCount == newChangeCount) {
Expand Down
4 changes: 4 additions & 0 deletions Easydict/Feature/Utility/SystemUtility/EZSystemUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ + (void)postCopyEvent {
}

+ (void)postPasteEvent {
// Disable local keyboard events while pasting.
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsFilterDuringSuppressionState(source, kCGEventFilterMaskPermitLocalMouseEvents | kCGEventFilterMaskPermitSystemDefinedEvents, kCGEventSuppressionStateSuppressionInterval);

[self postKeyboardEvent:kCGEventFlagMaskCommand virtualKey:kVK_ANSI_V keyDown:true];
[self postKeyboardEvent:kCGEventFlagMaskCommand virtualKey:kVK_ANSI_V keyDown:false];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// EZReplaceTextButton.m
//
//
//
// Created by tisfeng on 2023/10/13.
//
Expand Down Expand Up @@ -40,7 +40,7 @@ - (void)replaceSelectedText:(NSString *)replacementString {
NSRunningApplication *app = NSWorkspace.sharedWorkspace.frontmostApplication;
NSString *bundleID = app.bundleIdentifier;
NSString *textLengthRange = [EZLog textLengthRange:replacementString];

NSDictionary *parameters = @{
@"floating_window_type" : @(EZWindowManager.shared.floatingWindowType),
@"app_name" : app.localizedName,
Expand All @@ -51,11 +51,8 @@ - (void)replaceSelectedText:(NSString *)replacementString {

EZAppleScriptManager *appleScriptManager = [EZAppleScriptManager shared];
if ([appleScriptManager isKnownBrowser:bundleID]) {
[appleScriptManager replaceBrowserSelectedText:replacementString bundleID:bundleID completion:^(NSString * _Nullable result, NSError * _Nullable error) {
if (error) {
[self replaceSelectedTextByKey:replacementString];
}
}];
// Since browser environment is complex, use AppleScript to replace text may fail, so use key event to replace text.
[self replaceSelectedTextByKey:replacementString];
} else {
[self replaceSelectedTextByAccessibility:replacementString];
}
Expand Down Expand Up @@ -87,11 +84,15 @@ - (void)replaceSelectedTextByKey:(NSString *)replacementString {
MMLogInfo(@"Use Cmd+V to replace selected text, App: %@", app.localizedName);

NSString *lastText = [EZSystemUtility getLastPasteboardText];

[replacementString copyToPasteboard];
[EZSystemUtility postPasteEvent];
[replacementString copyToPasteboard];

[lastText copyToPasteboard];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(EZGetClipboardTextDelayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[EZSystemUtility postPasteEvent];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(EZGetClipboardTextDelayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[lastText copyToPasteboard];
});
});
}

@end

0 comments on commit c77997f

Please sign in to comment.