diff --git a/MacPass/KPKNode+IconImage.m b/MacPass/KPKNode+IconImage.m index 9395d7d9..2d374dcd 100644 --- a/MacPass/KPKNode+IconImage.m +++ b/MacPass/KPKNode+IconImage.m @@ -63,7 +63,7 @@ if(self.timeInfo.isExpired) { const BOOL isGroup = [self isKindOfClass:[KPKGroup class]]; - return [[MPIconHelper icon:(isGroup ? MPIconExpiredGroup : MPIconExpiredEntry)] imageWithTintColor:NSColor.systemOrangeColor]; + return [[MPIconHelper icon:(isGroup ? MPIconExpiredGroup : MPIconExpiredEntry)] imageWithTintColor:NSColor.systemRedColor]; } if(self.icon) { return self.icon.image; diff --git a/MacPass/MPAddEntryContextMenuDelegate.m b/MacPass/MPAddEntryContextMenuDelegate.m index bfd28666..013c2cfb 100644 --- a/MacPass/MPAddEntryContextMenuDelegate.m +++ b/MacPass/MPAddEntryContextMenuDelegate.m @@ -21,41 +21,17 @@ // #import "MPAddEntryContextMenuDelegate.h" -#import "MPDocument.h" -#import "MPDocumentWindowController.h" -#import "MPActionHelper.h" -#import "KPKNode+IconImage.h" +#import "MPContextMenuHelper.h" #import "KeePassKit/KeePassKit.h" -#define EDIT_TEMPLATES_ITEM_TAG 10; - @implementation MPAddEntryContextMenuDelegate - (void)menuNeedsUpdate:(NSMenu *)menu { - /* - The Method is rather brute force - It's possible nicer to cache the entries and just update - the menu entries, that actually need updating - */ - MPDocument *document = NSDocumentController.sharedDocumentController.currentDocument; [menu removeAllItems]; - [menu addItemWithTitle:NSLocalizedString(@"EDIT_TEMPLATE_GROUP", "Menu item on the add entry context menu to edit template groups") action:[MPActionHelper actionOfType:MPActionEditTemplateGroup] keyEquivalent:@""]; - - [menu addItem:[NSMenuItem separatorItem]]; - for(KPKEntry *entry in document.templates.childEntries) { - NSString *templateMask = NSLocalizedString(@"NEW_ENTRY_WITH_TEMPLATE_%@", "Submenu to add an entry via template"); - NSMenuItem *templateItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:templateMask, entry.title] - action:@selector(createEntryFromTemplate:) - keyEquivalent:@""]; - templateItem.image = [entry.iconImage copy]; - templateItem.image.size = NSMakeSize(14, 14); - templateItem.representedObject = entry.uuid; - [menu addItem:templateItem]; - } - /* If there are no entries, add a note as disabled menu item */ - if(menu.itemArray.count == 2) { - [menu addItemWithTitle:NSLocalizedString(@"NO_TEMPLATES", "Menu item added to show that no templates are defined") action:NULL keyEquivalent:@""]; + NSArray *items = [MPContextMenuHelper contextMenuItemsWithCreateFromTemplateEntriesItems]; + for(NSMenuItem *item in items) { + [menu addItem:item]; } } diff --git a/MacPass/MPContextMenuHelper.h b/MacPass/MPContextMenuHelper.h index 9d46358b..dc0db111 100644 --- a/MacPass/MPContextMenuHelper.h +++ b/MacPass/MPContextMenuHelper.h @@ -37,11 +37,11 @@ typedef NS_OPTIONS(NSUInteger, MPContextMenuItemsFlags) { }; @interface MPContextMenuHelper : NSTableCellView - /* Creates an array of menuitems to be used as a menu Automatically sets up actions, so you need to take care of the responder chain */ + (NSArray *)contextMenuItemsWithItems:(MPContextMenuItemsFlags)flags; ++ (NSArray *)contextMenuItemsWithCreateFromTemplateEntriesItems; @end diff --git a/MacPass/MPContextMenuHelper.m b/MacPass/MPContextMenuHelper.m index 8fce1f2f..24b1205d 100644 --- a/MacPass/MPContextMenuHelper.m +++ b/MacPass/MPContextMenuHelper.m @@ -22,9 +22,13 @@ #import "MPContextMenuHelper.h" #import "MPActionHelper.h" - +#import "MPDocument.h" #import "MPFlagsHelper.h" +#import "KPKNode+IconImage.h" + +#import + static void MPContextmenuHelperBeginSection(NSMutableArray *items) { if(items.count > 0) { [items addObject:[NSMenuItem separatorItem]]; @@ -33,6 +37,38 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) { @implementation MPContextMenuHelper ++ (NSArray *)contextMenuItemsWithCreateFromTemplateEntriesItems { + /* + The Method is rather brute force + It's possible nicer to cache the entries and just update + the menu entries, that actually need updating + */ + + NSMutableArray *items = [[NSMutableArray alloc] init]; + NSMenuItem *editItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"EDIT_TEMPLATE_GROUP", "Menu item on the add entry context menu to edit template groups") action:[MPActionHelper actionOfType:MPActionEditTemplateGroup] keyEquivalent:@""]; + + [items addObject:editItem]; + MPContextmenuHelperBeginSection(items); + + MPDocument *document = NSDocumentController.sharedDocumentController.currentDocument; + for(KPKEntry *entry in document.templates.childEntries) { + NSString *templateMask = NSLocalizedString(@"NEW_ENTRY_WITH_TEMPLATE_%@", "Submenu to add an entry via template"); + NSMenuItem *templateItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:templateMask, entry.title] + action:@selector(createEntryFromTemplate:) + keyEquivalent:@""]; + templateItem.image = [entry.iconImage copy]; + templateItem.image.size = NSMakeSize(14, 14); + templateItem.representedObject = entry.uuid; + [items addObject:templateItem]; + } + /* If there are no entries, add a note as disabled menu item */ + if(items.count == 2) { + NSMenuItem *noItemsItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"NO_TEMPLATES", "Menu item added to show that no templates are defined") action:NULL keyEquivalent:@""]; + [items addObject:noItemsItem]; + } + return [items copy]; +} + + (NSArray *)contextMenuItemsWithItems:(MPContextMenuItemsFlags)flags { BOOL const insertCreate = MPIsFlagSetInOptions(MPContextMenuCreate, flags); @@ -44,7 +80,7 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) { BOOL const insertHistory = MPIsFlagSetInOptions(MPContextMenuHistory, flags); BOOL const insertShowGroupInOutline = MPIsFlagSetInOptions(MPContextMenuShowGroupInOutline, flags); - NSMutableArray *items = [NSMutableArray arrayWithCapacity:10]; + NSMutableArray *items = [NSMutableArray arrayWithCapacity:15]; if(insertCreate) { NSMenuItem *newGroup = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"NEW_GROUP", @"Menu item to create a new group") @@ -54,7 +90,20 @@ static void MPContextmenuHelperBeginSection(NSMutableArray *items) { action:[MPActionHelper actionOfType:MPActionAddEntry] keyEquivalent:@"n"]; + [items addObjectsFromArray:@[ newGroup, newEntry ]]; + /* + NSMenuItem *newEntryFromTemplate = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"NEW_ENTRY_FROM_TEMPLATE", @"Submen to create entries from tempaltes") + action:NULL + keyEquivalent:@""]; + newEntryFromTemplate.submenu = [[NSMenu alloc] init]; + NSArray *templateItems = [self contextMenuItemsWithCreateFromTemplateEntriesItems]; + for(NSMenuItem *item in templateItems) { + [newEntryFromTemplate.submenu addItem:item]; + } + [items addObjectsFromArray:@[ newGroup, newEntry, newEntryFromTemplate ]]; + */ + } if(insertDuplicate) { MPContextmenuHelperBeginSection(items);