From 411360deb2bd18e305caf9b7b231c140676b5ea3 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 25 Apr 2021 15:48:20 -0700 Subject: [PATCH] Allow check for updates menu item to be invoked outside of settings Previously check for updates was only in responder chain of the settings window controller. Now it's in responder chain of the entire application. --- TestApplication/SUTestApplicationDelegate.m | 57 +++++++++++++++-- .../SUUpdateSettingsWindowController.h | 4 +- .../SUUpdateSettingsWindowController.m | 61 +------------------ 3 files changed, 56 insertions(+), 66 deletions(-) diff --git a/TestApplication/SUTestApplicationDelegate.m b/TestApplication/SUTestApplicationDelegate.m index 84aff79a..a15b60c3 100644 --- a/TestApplication/SUTestApplicationDelegate.m +++ b/TestApplication/SUTestApplicationDelegate.m @@ -12,16 +12,20 @@ #import "SUTestWebServer.h" #import "TestAppHelperProtocol.h" #import "ed25519.h" // Run `git submodule update --init` if you get an error here +#import +#import "SUPopUpTitlebarUserDriver.h" @interface SUTestApplicationDelegate () -@property (nonatomic) NSWindowController *updateSettingsWindowController; +@property (nonatomic) SPUUpdater *updater; +@property (nonatomic) SUUpdateSettingsWindowController *updateSettingsWindowController; @property (nonatomic) SUTestWebServer *webServer; @end @implementation SUTestApplicationDelegate +@synthesize updater = _updater; @synthesize updateSettingsWindowController = _updateSettingsWindowController; @synthesize webServer = _webServer; @@ -194,10 +198,40 @@ static NSString * const UPDATED_VERSION = @"2.0"; } self.webServer = webServer; - // Show the Settings window - self.updateSettingsWindowController = [[SUUpdateSettingsWindowController alloc] initWithCustomUserDriver:shiftKeyHeldDown]; - - [self.updateSettingsWindowController showWindow:nil]; + // Set up updater and the updater settings window + { + self.updateSettingsWindowController = [[SUUpdateSettingsWindowController alloc] init]; + + NSWindow *settingsWindow = self.updateSettingsWindowController.window; + + NSBundle *hostBundle = [NSBundle mainBundle]; + NSBundle *applicationBundle = hostBundle; + + id userDriver; + if (shiftKeyHeldDown) { + userDriver = [[SUPopUpTitlebarUserDriver alloc] initWithWindow:settingsWindow]; + } else { + userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:nil]; + } + + SPUUpdater *updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:applicationBundle userDriver:userDriver delegate:nil]; + + self.updater = updater; + self.updateSettingsWindowController.updater = updater; + + NSError *updaterError = nil; + if (![updater startUpdater:&updaterError]) { + NSLog(@"Failed to start updater with error: %@", updaterError); + + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = @"Updater Error"; + alert.informativeText = @"The Updater failed to start. For detailed error information, check the Console.app log."; + [alert addButtonWithTitle:@"OK"]; + [alert runModal]; + } + + [self.updateSettingsWindowController showWindow:nil]; + } }); }]; } @@ -222,4 +256,17 @@ static NSString * const UPDATED_VERSION = @"2.0"; [self.webServer close]; } +- (IBAction)checkForUpdates:(id __unused)sender +{ + [self.updater checkForUpdates]; +} + +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + if (menuItem.action == @selector(checkForUpdates:)) { + return self.updater.canCheckForUpdates; + } + return YES; +} + @end diff --git a/TestApplication/SUUpdateSettingsWindowController.h b/TestApplication/SUUpdateSettingsWindowController.h index de1656bc..322e475b 100644 --- a/TestApplication/SUUpdateSettingsWindowController.h +++ b/TestApplication/SUUpdateSettingsWindowController.h @@ -8,8 +8,10 @@ #import +@class SPUUpdater; + @interface SUUpdateSettingsWindowController : NSWindowController -- (instancetype)initWithCustomUserDriver:(BOOL)customUserDriver; +@property (nonatomic) SPUUpdater *updater; @end diff --git a/TestApplication/SUUpdateSettingsWindowController.m b/TestApplication/SUUpdateSettingsWindowController.m index 39949539..c55ef4df 100644 --- a/TestApplication/SUUpdateSettingsWindowController.m +++ b/TestApplication/SUUpdateSettingsWindowController.m @@ -8,74 +8,15 @@ #import "SUUpdateSettingsWindowController.h" #import -#import "SUPopUpTitlebarUserDriver.h" - -@interface SUUpdateSettingsWindowController () - -@property (nonatomic) SPUUpdater *updater; -@property (nonatomic, readonly) BOOL customUserDriver; -@property (nonatomic) SPUStandardUserDriver *userDriver; - -@end +// This class binds to various updater properties in the nib @implementation SUUpdateSettingsWindowController @synthesize updater = _updater; -@synthesize customUserDriver = _customUserDriver; -@synthesize userDriver = _userDriver; - -- (instancetype)initWithCustomUserDriver:(BOOL)customUserDriver -{ - self = [super init]; - if (self != nil) { - _customUserDriver = customUserDriver; - } - return self; -} - (NSString *)windowNibName { return NSStringFromClass([self class]); } -- (void)windowDidLoad -{ - NSBundle *hostBundle = [NSBundle mainBundle]; - NSBundle *applicationBundle = hostBundle; - - id userDriver; - if (self.customUserDriver) { - userDriver = [[SUPopUpTitlebarUserDriver alloc] initWithWindow:self.window]; - } else { - userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:nil]; - } - - self.userDriver = userDriver; - self.updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:applicationBundle userDriver:userDriver delegate:nil]; - - NSError *updaterError = nil; - if (![self.updater startUpdater:&updaterError]) { - NSLog(@"Failed to start updater with error: %@", updaterError); - - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = @"Updater Error"; - alert.informativeText = @"The Updater failed to start. For detailed error information, check the Console.app log."; - [alert addButtonWithTitle:@"OK"]; - [alert runModal]; - } -} - -- (IBAction)checkForUpdates:(id __unused)sender -{ - [self.updater checkForUpdates]; -} - -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem -{ - if (menuItem.action == @selector(checkForUpdates:)) { - return self.updater.canCheckForUpdates; - } - return YES; -} - @end