diff --git a/Sources/OpenKey/macOS/ModernKey/OpenKey.mm b/Sources/OpenKey/macOS/ModernKey/OpenKey.mm index 4292db2d..1e6b6c14 100644 --- a/Sources/OpenKey/macOS/ModernKey/OpenKey.mm +++ b/Sources/OpenKey/macOS/ModernKey/OpenKey.mm @@ -57,6 +57,7 @@ NSArray* _unicodeCompoundApp = @[@"com.apple.", @"com.google.Chrome", @"com.brave.Browser", @"com.microsoft.edgemac.Dev", @"com.microsoft.edgemac.Beta", @"com.microsoft.Edge.Dev", @"com.microsoft.Edge"]; + NSArray* _recommendWorkaroundDisabledApp = @[@"com.apple.Spotlight"]; CGEventSourceRef myEventSource = NULL; vKeyHookState* pData; @@ -173,6 +174,28 @@ BOOL containUnicodeCompoundApp(NSString* topApp) { } return false; } + + BOOL isSpotlightVisible() { + NSArray *windows = CFBridgingRelease(CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, + kCGNullWindowID)); + for (NSDictionary *window in windows) { + if ([[window objectForKey:(__bridge NSString *)kCGWindowOwnerName] isEqualToString:@"Spotlight"]) { + return true; + } + } + return false; + } + + BOOL shouldUseRecommendWorkaround(NSString* topApp) { + if (!vFixRecommendBrowser) return false; + if (isSpotlightVisible()) return false; + if (topApp == nil) return true; + return ![_recommendWorkaroundDisabledApp containsObject:topApp]; + } + + BOOL shouldUseSelectionReplacement(NSString* topApp) { + return isSpotlightVisible() || [_recommendWorkaroundDisabledApp containsObject:topApp]; + } void saveSmartSwitchKeyData() { getSmartSwitchKeySaveData(savedSmartSwitchKeyData); @@ -520,7 +543,7 @@ void switchLanguage() { void handleMacro() { //fix autocomplete - if (vFixRecommendBrowser) { + if (shouldUseRecommendWorkaround(FRONT_APP)) { SendEmptyCharacter(); pData->backspaceCount++; } @@ -716,7 +739,7 @@ CGEventRef OpenKeyCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef e } else if (pData->code == vWillProcess || pData->code == vRestore || pData->code == vRestoreAndStartNewSession) { //handle result signal //fix autocomplete - if (vFixRecommendBrowser && pData->extCode != 4) { + if (shouldUseRecommendWorkaround(FRONT_APP) && pData->extCode != 4) { if (vFixChromiumBrowser && [_unicodeCompoundApp containsObject:FRONT_APP]) { if (pData->backspaceCount > 0) { SendShiftAndLeftArrow(); @@ -729,6 +752,13 @@ CGEventRef OpenKeyCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef e } } + + if (shouldUseSelectionReplacement(FRONT_APP) && pData->backspaceCount > 0) { + for (_i = 0; _i < pData->backspaceCount; _i++) { + SendShiftAndLeftArrow(); + } + pData->backspaceCount = 0; + } //send backspace if (pData->backspaceCount > 0 && pData->backspaceCount < MAX_BUFF) { diff --git a/Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m b/Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m index 2d1b665b..97883f1a 100644 --- a/Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m +++ b/Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m @@ -74,17 +74,12 @@ +(BOOL)initEventTap { // Enable the event tap. CGEventTapEnable(eventTap, true); - // Set it all running. - CFRunLoopRun(); - return YES; } +(BOOL)stopEventTap { if (_isInited) { //release all object - CFRunLoopStop(CFRunLoopGetCurrent()); - - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode); + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CFRelease(runLoopSource); runLoopSource = nil; diff --git a/Sources/OpenKey/macOS/ModernKey/ViewController.m b/Sources/OpenKey/macOS/ModernKey/ViewController.m index d4c5b73c..386d3872 100644 --- a/Sources/OpenKey/macOS/ModernKey/ViewController.m +++ b/Sources/OpenKey/macOS/ModernKey/ViewController.m @@ -48,6 +48,7 @@ @implementation ViewController { __weak IBOutlet NSButton *CustomBeepSound; NSArray* tabviews, *tabbuttons; NSRect tabViewRect; + NSView* tabButtonBackground; } - (void)viewDidLoad { @@ -66,6 +67,16 @@ - (void)viewDidLoad { //set correct tabgroup tabviews = [NSArray arrayWithObjects:self.tabviewPrimary, self.tabviewMacro, self.tabviewSystem, self.tabviewInfo, nil]; tabbuttons = [NSArray arrayWithObjects:self.tabbuttonPrimary, self.tabbuttonMacro, self.tabbuttonSystem, self.tabbuttonInfo, nil]; + NSButton* firstTabButton = [tabbuttons objectAtIndex:0]; + NSRect tabButtonBackgroundRect = firstTabButton.frame; + for (NSButton* button in tabbuttons) { + tabButtonBackgroundRect = NSUnionRect(tabButtonBackgroundRect, button.frame); + } + tabButtonBackgroundRect = NSInsetRect(tabButtonBackgroundRect, -2, -2); + tabButtonBackground = [[NSView alloc] initWithFrame:tabButtonBackgroundRect]; + [tabButtonBackground setWantsLayer:YES]; + tabButtonBackground.layer.backgroundColor = [[NSColor windowBackgroundColor] CGColor]; + [self.view addSubview:tabButtonBackground]; tabViewRect = self.tabviewPrimary.frame; for (NSBox* b in tabviews) { b.frame = tabViewRect; @@ -137,6 +148,11 @@ -(void)showTab:(NSInteger)index { NSButton* button = [tabbuttons objectAtIndex:index]; [button setState:NSControlStateValueOn]; + + [self.view addSubview:tabButtonBackground positioned:NSWindowAbove relativeTo:b]; + for (NSButton* tabButton in tabbuttons) { + [self.view addSubview:tabButton positioned:NSWindowAbove relativeTo:nil]; + } } - (IBAction)onTabButton:(NSButton *)sender {