From c46dba59c3c6d602456b7668cb77151a0be8cddd Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Wed, 24 Apr 2019 15:57:20 +0200 Subject: [PATCH] do not show welcome window when we are asked to open a file via double-click in the Finder --- MacPass/MPAppDelegate.m | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index d9d364d9..9bad8060 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -51,6 +51,13 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDidChangeStoredKeyFilesSettings"; +typedef NS_OPTIONS(NSInteger, MPAppDelegateNotifcationState) { + MPAppDelegateDidReceiveNoNotification = 0, + MPAppDelegateDidReceiveApplicationDidRestoreWindowsNotification = 1, + MPAppDelegateDidReceiveApplicationDidFinsishLaunchinNotification = 2, + MPAppdelegateDidReceiveAllNotifications = (MPAppDelegateDidReceiveApplicationDidRestoreWindowsNotification | MPAppDelegateDidReceiveApplicationDidFinsishLaunchinNotification) +}; + @interface MPAppDelegate () { @private MPDockTileHelper *_dockTileHelper; @@ -62,6 +69,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi @property (strong) IBOutlet NSWindow *passwordCreatorWindow; @property (strong, nonatomic) MPPreferencesWindowController *preferencesController; @property (strong, nonatomic) MPPasswordCreatorViewController *passwordCreatorController; +@property (assign, nonatomic) MPAppDelegateNotifcationState notificationState; @property (strong) MPEntryContextMenuDelegate *itemActionMenuDelegate; @@ -83,6 +91,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi _userNotificationCenterDelegate = [[MPUserNotificationCenterDelegate alloc] init]; self.itemActionMenuDelegate = [[MPEntryContextMenuDelegate alloc] init]; _shouldOpenFile = NO; + self.notificationState = MPAppDelegateDidReceiveNoNotification; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_applicationDidFinishRestoringWindows:) @@ -119,6 +128,15 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi } } +- (void)setNotificationState:(MPAppDelegateNotifcationState)notificationState { + if(notificationState != self.notificationState) { + _notificationState = notificationState; + if(MPAppdelegateDidReceiveAllNotifications == (self.notificationState & MPAppdelegateDidReceiveAllNotifications)) { + [self _applicationDidFinishLaunchingAndDidRestoreWindows]; + } + } +} + - (void)awakeFromNib { _isAllowedToStoreKeyFile = NO; /* Update the … at the save menu */ @@ -164,11 +182,6 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi return [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyOpenEmptyDatabaseOnLaunch]; } - -- (void)applicationWillFinishLaunching:(NSNotification *)notification { - -} - - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { return [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyQuitOnLastWindowClose]; } @@ -206,6 +219,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi /* Disable updates if in debug or nosparkle */ [SUUpdater sharedUpdater]; #endif + self.notificationState |= MPAppDelegateDidReceiveApplicationDidFinsishLaunchinNotification; } #pragma mark - @@ -290,7 +304,6 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi } - (void)showWelcomeWindow { - NSApp.dockTile.badgeLabel = @"WELCOME"; if(!self.welcomeWindow) { self.welcomeWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable @@ -335,8 +348,12 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi #pragma mark - #pragma mark Private Helper - (void)_applicationDidFinishRestoringWindows:(NSNotification *)notification { + self.notificationState |= MPAppDelegateDidReceiveApplicationDidRestoreWindowsNotification; +} + +- (void)_applicationDidFinishLaunchingAndDidRestoreWindows { NSArray *documents = NSDocumentController.sharedDocumentController.documents; - BOOL restoredWindows = documents.count > 0; + BOOL hasOpenDocuments = documents.count > 0; for(NSDocument *document in documents) { for(NSWindowController *windowController in document.windowControllers) { @@ -345,8 +362,8 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi } BOOL reopen = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; - BOOL showWelcomeScreen = !restoredWindows && !_shouldOpenFile; - if(reopen && !restoredWindows && !_shouldOpenFile) { + BOOL showWelcomeScreen = !hasOpenDocuments && !_shouldOpenFile; + if(reopen && !hasOpenDocuments && !_shouldOpenFile) { showWelcomeScreen = ![((MPDocumentController *)NSDocumentController.sharedDocumentController) reopenLastDocument]; } if(showWelcomeScreen) {