diff --git a/Easydict/App/EZConst.h b/Easydict/App/EZConst.h index 75e130c93..e15575068 100644 --- a/Easydict/App/EZConst.h +++ b/Easydict/App/EZConst.h @@ -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; @@ -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 */ diff --git a/Easydict/Feature/EventMonitor/EZEventMonitor.m b/Easydict/Feature/EventMonitor/EZEventMonitor.m index 8e5f3304e..e82fc80f4 100644 --- a/Easydict/Feature/EventMonitor/EZEventMonitor.m +++ b/Easydict/Feature/EventMonitor/EZEventMonitor.m @@ -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) { diff --git a/Easydict/Feature/Utility/SystemUtility/EZSystemUtility.m b/Easydict/Feature/Utility/SystemUtility/EZSystemUtility.m index 633cc6662..49e178d44 100644 --- a/Easydict/Feature/Utility/SystemUtility/EZSystemUtility.m +++ b/Easydict/Feature/Utility/SystemUtility/EZSystemUtility.m @@ -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]; } diff --git a/Easydict/Feature/ViewController/View/CustomButton/EZReplaceTextButton/EZReplaceTextButton.m b/Easydict/Feature/ViewController/View/CustomButton/EZReplaceTextButton/EZReplaceTextButton.m index 7836f750a..749219256 100644 --- a/Easydict/Feature/ViewController/View/CustomButton/EZReplaceTextButton/EZReplaceTextButton.m +++ b/Easydict/Feature/ViewController/View/CustomButton/EZReplaceTextButton/EZReplaceTextButton.m @@ -1,6 +1,6 @@ // // EZReplaceTextButton.m -// +// // // Created by tisfeng on 2023/10/13. // @@ -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, @@ -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]; } @@ -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