From 423ee5a9d690a835cd8a8827e4c4df07cecf9dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 15 Mar 2017 21:31:00 +0000 Subject: [PATCH] Ensure UI code always runs on the main thread --- Sparkle/SUAutomaticUpdateDriver.m | 37 ++++++++------- Sparkle/SUUIBasedUpdateDriver.m | 76 ++++++++++++++++-------------- Sparkle/SUUpdatePermissionPrompt.m | 28 ++++++----- 3 files changed, 75 insertions(+), 66 deletions(-) diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 44fb18fd..400e8367 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -51,26 +51,29 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 self.interruptible = NO; [self invalidateShowUpdateAlertTimer]; - if (self.alert) { - [self.alert close]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.alert) { + [self.alert close]; + } - self.alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUAutomaticInstallationChoice choice) { - [self automaticUpdateAlertFinishedWithChoice:choice]; - }]; + self.alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUAutomaticInstallationChoice choice) { + [self automaticUpdateAlertFinishedWithChoice:choice]; + }]; - // If the app is a menubar app or the like, we need to focus it first and alter the - // update prompt to behave like a normal window. Otherwise if the window were hidden - // there may be no way for the application to be activated to make it visible again. - if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { - [[self.alert window] setHidesOnDeactivate:NO]; - [NSApp activateIgnoringOtherApps:YES]; - } + // If the app is a menubar app or the like, we need to focus it first and alter the + // update prompt to behave like a normal window. Otherwise if the window were hidden + // there may be no way for the application to be activated to make it visible again. + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { + [[self.alert window] setHidesOnDeactivate:NO]; + [NSApp activateIgnoringOtherApps:YES]; + } - if ([NSApp isActive]) - [[self.alert window] makeKeyAndOrderFront:self]; - else - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + if ([NSApp isActive]) { + [[self.alert window] makeKeyAndOrderFront:self]; + } else { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + } + }); } - (void)unarchiverDidFinish:(id)__unused ua diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 5e1de6f3..4df9ffda 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -66,33 +66,35 @@ return; } - self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { - [self updateAlertFinishedWithChoice:choice]; - }]; + dispatch_async(dispatch_get_main_queue(), ^{ + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { + [self updateAlertFinishedWithChoice:choice]; + }]; - id versDisp = nil; - if ([[updater delegate] respondsToSelector:@selector(versionDisplayerForUpdater:)]) { - versDisp = [[updater delegate] versionDisplayerForUpdater:self.updater]; - } - [self.updateAlert setVersionDisplayer:versDisp]; - - // If the app is a menubar app or the like, we need to focus it first and alter the - // update prompt to behave like a normal window. Otherwise if the window were hidden - // there may be no way for the application to be activated to make it visible again. - if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { - [[self.updateAlert window] setHidesOnDeactivate:NO]; - [NSApp activateIgnoringOtherApps:YES]; - } - - // Only show the update alert if the app is active; otherwise, we'll wait until it is. - if ([NSApp isActive]) { - NSWindow *window = [self.updateAlert window]; - if ([self shouldDisableKeyboardShortcutForInstallButton]) { - [self.updateAlert disableKeyboardShortcutForInstallButton]; + id versDisp = nil; + if ([[updater delegate] respondsToSelector:@selector(versionDisplayerForUpdater:)]) { + versDisp = [[updater delegate] versionDisplayerForUpdater:self.updater]; } - [window makeKeyAndOrderFront:self]; - } else - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + [self.updateAlert setVersionDisplayer:versDisp]; + + // If the app is a menubar app or the like, we need to focus it first and alter the + // update prompt to behave like a normal window. Otherwise if the window were hidden + // there may be no way for the application to be activated to make it visible again. + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { + [[self.updateAlert window] setHidesOnDeactivate:NO]; + [NSApp activateIgnoringOtherApps:YES]; + } + + // Only show the update alert if the app is active; otherwise, we'll wait until it is. + if ([NSApp isActive]) { + NSWindow *window = [self.updateAlert window]; + if ([self shouldDisableKeyboardShortcutForInstallButton]) { + [self.updateAlert disableKeyboardShortcutForInstallButton]; + } + [window makeKeyAndOrderFront:self]; + } else + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + }); } - (BOOL)shouldDisableKeyboardShortcutForInstallButton { @@ -313,20 +315,22 @@ - (void)showAlert:(NSAlert *)alert { - id updater = self.updater; - if ([[updater delegate] respondsToSelector:@selector(updaterWillShowModalAlert:)]) { - [[updater delegate] updaterWillShowModalAlert:self.updater]; - } + dispatch_sync(dispatch_get_main_queue(), ^{ + id updater = self.updater; + if ([[updater delegate] respondsToSelector:@selector(updaterWillShowModalAlert:)]) { + [[updater delegate] updaterWillShowModalAlert:self.updater]; + } - // When showing a modal alert we need to ensure that background applications - // are focused to inform the user since there is no dock icon to notify them. - if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [NSApp activateIgnoringOtherApps:YES]; } + // When showing a modal alert we need to ensure that background applications + // are focused to inform the user since there is no dock icon to notify them. + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [NSApp activateIgnoringOtherApps:YES]; } - [alert setIcon:[SUApplicationInfo bestIconForHost:self.host]]; - [alert runModal]; + [alert setIcon:[SUApplicationInfo bestIconForHost:self.host]]; + [alert runModal]; - if ([[updater delegate] respondsToSelector:@selector(updaterDidShowModalAlert:)]) - [[updater delegate] updaterDidShowModalAlert:self.updater]; + if ([[updater delegate] respondsToSelector:@selector(updaterDidShowModalAlert:)]) + [[updater delegate] updaterDidShowModalAlert:self.updater]; + }); } @end diff --git a/Sparkle/SUUpdatePermissionPrompt.m b/Sparkle/SUUpdatePermissionPrompt.m index 7f1c0055..d3773a7c 100644 --- a/Sparkle/SUUpdatePermissionPrompt.m +++ b/Sparkle/SUUpdatePermissionPrompt.m @@ -72,20 +72,22 @@ static NSString *const SUUpdatePermissionPromptTouchBarIndentifier = @"" SPARKLE + (void)promptWithHost:(SUHost *)host systemProfile:(NSArray *)profile reply:(void (^)(SUUpdatePermissionResponse *))reply { - // If this is a background application we need to focus it in order to bring the prompt - // to the user's attention. Otherwise the prompt would be hidden behind other applications and - // the user would not know why the application was paused. - if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; - } - - if (![NSApp modalWindow]) { // do not prompt if there is is another modal window on screen - SUUpdatePermissionPrompt *prompt = [[[self class] alloc] initWithHost:host systemProfile:profile reply:reply]; - NSWindow *window = [prompt window]; - if (window) { - [NSApp runModalForWindow:window]; + dispatch_async(dispatch_get_main_queue(), ^{ + // If this is a background application we need to focus it in order to bring the prompt + // to the user's attention. Otherwise the prompt would be hidden behind other applications and + // the user would not know why the application was paused. + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { + [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; } - } + + if (![NSApp modalWindow]) { // do not prompt if there is is another modal window on screen + SUUpdatePermissionPrompt *prompt = [[[self class] alloc] initWithHost:host systemProfile:profile reply:reply]; + NSWindow *window = [prompt window]; + if (window) { + [NSApp runModalForWindow:window]; + } + } + }); } - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self.host bundlePath]]; }