Merge branch 'master' into feature/check_autotype_screen_recording_permission

This commit is contained in:
Michael Starke
2019-07-03 12:02:56 +02:00
2 changed files with 26 additions and 10 deletions

View File

@@ -56,7 +56,7 @@ NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
@property (copy) NSString *targetWindowTitle; // The title of the window that we are targeting
@property (strong) NSRunningApplication *previousApplication; // The application that was active before we got invoked
@property (assign) NSTimeInterval userActionRequested;
@property (strong) id applicationActivationObserver;
@end
@implementation MPAutotypeDaemon
@@ -108,6 +108,9 @@ static MPAutotypeDaemon *_sharedInstance;
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self];
if(self.applicationActivationObserver) {
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self.applicationActivationObserver name:NSWorkspaceDidActivateApplicationNotification object:nil];
}
[self unbind:NSStringFromSelector(@selector(enabled))];
[self unbind:NSStringFromSelector(@selector(hotKeyData))];
}
@@ -214,7 +217,7 @@ static MPAutotypeDaemon *_sharedInstance;
[self.matchSelectionWindow orderOut:self];
self.matchSelectionWindow = nil;
if(self.targetPID) {
[self _orderApplicationToFront:self.targetPID];
[self _orderApplicationToFront:self.targetPID forContext:nil];
}
}
@@ -319,10 +322,8 @@ static MPAutotypeDaemon *_sharedInstance;
return; // No context to work with
}
if([self _orderApplicationToFront:self.targetPID]) {
/* Sleep a bit after the app was activated */
/* TODO - we might be able to get a notification to check if the app actually was activated instead of guessing a waiting time */
usleep(1 * NSEC_PER_MSEC);
if(NO == [self _orderApplicationToFront:self.targetPID forContext:(MPAutotypeContext *)context]) {
return; // We will get called back when the application is in front - hopfully
}
useconds_t globalDelay = 0;
@@ -446,15 +447,30 @@ static MPAutotypeDaemon *_sharedInstance;
#pragma mark -
#pragma mark Application information
- (BOOL)_orderApplicationToFront:(pid_t)processIdentifier {
- (BOOL)_orderApplicationToFront:(pid_t)processIdentifier forContext:(MPAutotypeContext *)context {
NSRunningApplication *runingApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:processIdentifier];
NSRunningApplication *frontApplication = NSWorkspace.sharedWorkspace.frontmostApplication;
if(frontApplication.processIdentifier == processIdentifier) {
return NO;
return YES;
}
/* cleanup before to make sure everything is top notch */
if(self.applicationActivationObserver) {
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self.applicationActivationObserver name:NSWorkspaceDidActivateApplicationNotification object:nil];
self.applicationActivationObserver = nil;
}
self.applicationActivationObserver = [NSWorkspace.sharedWorkspace.notificationCenter addObserverForName:NSWorkspaceDidActivateApplicationNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
if(self.applicationActivationObserver) {
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self.applicationActivationObserver name:NSWorkspaceDidActivateApplicationNotification object:nil];
}
[self _performAutotypeForContext:context];
}];
[runingApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
return YES;
return NO;
}
- (void)_updateTargetInformationForFrontMostApplication {
[self _updateTargeInformationForApplication:NSWorkspace.sharedWorkspace.frontmostApplication];
}