diff --git a/MacPass/MPAppDelegate.h b/MacPass/MPAppDelegate.h index 5d09a909..55f2133d 100644 --- a/MacPass/MPAppDelegate.h +++ b/MacPass/MPAppDelegate.h @@ -24,6 +24,8 @@ APPKIT_EXTERN NSString *const MPDidChangeStoredKeyFilesSettings; +@class MPEntryContextMenuDelegate; + @interface MPAppDelegate : NSObject @property (strong) IBOutlet NSMenuItem *saveMenuItem; @@ -32,6 +34,8 @@ APPKIT_EXTERN NSString *const MPDidChangeStoredKeyFilesSettings; @property (strong) IBOutlet NSMenu *itemMenu; @property (strong) IBOutlet NSMenu *importMenu; +@property (strong, readonly) MPEntryContextMenuDelegate *itemActionMenuDelegate; + @property (nonatomic, assign) BOOL isAllowedToStoreKeyFile; - (IBAction)checkForUpdates:(id)sender; diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index 5c982f93..4062902d 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -55,8 +55,6 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi @private MPDockTileHelper *_dockTileHelper; MPUserNotificationCenterDelegate *_userNotificationCenterDelegate; - MPEntryContextMenuDelegate *_entryContextMenuDelegate; - BOOL _shouldOpenFile; // YES if app was started to open a } @@ -65,6 +63,8 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi @property (strong, nonatomic) MPPreferencesWindowController *preferencesController; @property (strong, nonatomic) MPPasswordCreatorViewController *passwordCreatorController; +@property (strong) MPEntryContextMenuDelegate *itemActionMenuDelegate; + @end @implementation MPAppDelegate @@ -81,7 +81,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi self = [super init]; if(self) { _userNotificationCenterDelegate = [[MPUserNotificationCenterDelegate alloc] init]; - _entryContextMenuDelegate = [[MPEntryContextMenuDelegate alloc] init]; + self.itemActionMenuDelegate = [[MPEntryContextMenuDelegate alloc] init]; /* We know that we do not use the variable after instantiation */ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" @@ -130,7 +130,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi for(NSMenuItem *item in [MPContextMenuHelper contextMenuItemsWithItems:MPContextMenuFull]) { [self.itemMenu addItem:item]; } - self.itemMenu.delegate = _entryContextMenuDelegate; + self.itemMenu.delegate = self.itemActionMenuDelegate; } #pragma mark - diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index f60ea31d..ef2abf91 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -42,6 +42,8 @@ #import "MPValueTransformerHelper.h" #import "MPEntryContextMenuDelegate.h" +#import "NSApplication+MPAdditions.h" + #import "KeePassKit/KeePassKit.h" #import "KPKNode+IconImage.h" @@ -68,8 +70,6 @@ NSString *const _MPTableStringCellView = @"StringCell"; NSString *const _MPTableSecurCellView = @"PasswordCell"; @interface MPEntryViewController () { - /* TODO unify delegation */ - MPEntryContextMenuDelegate *_menuDelegate; BOOL _isDisplayingContextBar; BOOL _didUnlock; } @@ -103,7 +103,6 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; _entryArrayController = [[NSArrayController alloc] init]; _dataSource = [[MPEntryTableDataSource alloc] init]; _dataSource.viewController = self; - _menuDelegate = [[MPEntryContextMenuDelegate alloc] init]; _contextBarViewController = [[MPContextBarViewController alloc] init]; [self _setupEntryBindings]; } @@ -604,7 +603,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; for(NSMenuItem *item in items) { [menu addItem:item]; } - menu.delegate = _menuDelegate; + menu.delegate = NSApp.mp_delegate.itemActionMenuDelegate; self.entryTable.menu = menu; } diff --git a/MacPass/MPToolbarDelegate.m b/MacPass/MPToolbarDelegate.m index 7f70597f..f03444c7 100644 --- a/MacPass/MPToolbarDelegate.m +++ b/MacPass/MPToolbarDelegate.m @@ -35,6 +35,9 @@ #import "MPDocumentWindowController.h" #import "MPDocument.h" +#import "NSApplication+MPAdditions.h" +#import "MPAppDelegate.h" + NSString *const MPToolbarItemLock = @"TOOLBAR_LOCK"; NSString *const MPToolbarItemAddGroup = @"TOOLBAR_ADD_GROUP"; NSString *const MPToolbarItemAddEntry = @"TOOLBAR_ADD_ENTRY"; @@ -49,7 +52,6 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE"; @interface MPToolbarDelegate() { MPAddEntryContextMenuDelegate *_addEntryMenuDelegate; - MPEntryContextMenuDelegate * _entryActionContextMenuDelegate; BOOL _didShowToolbarForSearch; BOOL _didAddSearchfieldForSearch; NSToolbarDisplayMode _displayModeBeforeSearch; @@ -94,7 +96,6 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE"; _toolbarImages = [self createToolbarImages]; _toolbarItems = [[NSMutableDictionary alloc] initWithCapacity:[self.toolbarIdentifiers count]]; _addEntryMenuDelegate = [[MPAddEntryContextMenuDelegate alloc] init]; - _entryActionContextMenuDelegate = [[MPEntryContextMenuDelegate alloc] init]; } return self; } @@ -137,7 +138,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE"; popupButton.frame = newFrame; popupButton.menu = menu; - menu.delegate = _entryActionContextMenuDelegate; + menu.delegate = NSApp.mp_delegate.itemActionMenuDelegate; item.menuFormRepresentation = menuRepresentation; item.view = popupButton; } diff --git a/MacPass/NSApplication+MPAdditions.h b/MacPass/NSApplication+MPAdditions.h index 49b0b415..2f7a1c9a 100644 --- a/MacPass/NSApplication+MPAdditions.h +++ b/MacPass/NSApplication+MPAdditions.h @@ -24,10 +24,13 @@ NS_ASSUME_NONNULL_BEGIN +@class MPAppDelegate; + @interface NSApplication (MPAdditions) @property (copy, readonly) NSString *applicationName; @property (copy, readonly, nullable) NSURL *applicationSupportDirectoryURL; +@property (nullable, readonly, weak) MPAppDelegate *mp_delegate; - (NSURL *_Nullable)applicationSupportDirectoryURL:(BOOL)create; - (void)relaunchAfterDelay:(CGFloat)seconds; diff --git a/MacPass/NSApplication+MPAdditions.m b/MacPass/NSApplication+MPAdditions.m index 71d19627..c9cc6915 100644 --- a/MacPass/NSApplication+MPAdditions.m +++ b/MacPass/NSApplication+MPAdditions.m @@ -24,6 +24,8 @@ @implementation NSApplication (MPAdditions) +@dynamic mp_delegate; + - (NSString *)applicationName { return [NSBundle.mainBundle.infoDictionary[@"CFBundleName"] copy]; } @@ -60,4 +62,8 @@ [self terminate:nil]; } +- (MPAppDelegate *)mp_delegate { + return (MPAppDelegate *)self.delegate; +} + @end