Ensure UI code always runs on the main thread

This commit is contained in:
Kornel Lesiński committed 2017-03-15 21:31:11 +00:00
1 parent 36139c57bc
commit 423ee5a9d6
3 files changed
+75 -66

No files matched your search

+20 -17
View File
@@ -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
+40 -36
View File
@@ -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<SUVersionDisplay> 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<SUVersionDisplay> 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<SUUpdaterPrivate> updater = self.updater;
if ([[updater delegate] respondsToSelector:@selector(updaterWillShowModalAlert:)]) {
[[updater delegate] updaterWillShowModalAlert:self.updater];
}
dispatch_sync(dispatch_get_main_queue(), ^{
id<SUUpdaterPrivate> 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
+15 -13
View File
@@ -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]]; }