Center search in 10.14 and above, keep it "centerish" in older systems

This commit is contained in:
Michael Starke
2020-01-21 15:12:25 +01:00
parent 77f3f23cfe
commit 228170c717
3 changed files with 85 additions and 67 deletions

View File

@@ -122,6 +122,11 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"];
self.toolbar.autosavesConfiguration = YES; self.toolbar.autosavesConfiguration = YES;
self.toolbar.allowsUserCustomization = YES; self.toolbar.allowsUserCustomization = YES;
if (@available(macOS 10.14, *)) {
self.toolbar.centeredItemIdentifier = MPToolbarItemIdentifierSearch;
} else {
// to not do any magic here
}
self.toolbar.delegate = self.toolbarDelegate; self.toolbar.delegate = self.toolbarDelegate;
self.window.toolbar = self.toolbar; self.window.toolbar = self.toolbar;
self.toolbarDelegate.toolbar = self.toolbar; self.toolbarDelegate.toolbar = self.toolbar;

View File

@@ -22,6 +22,18 @@
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierLock;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierAddGroup;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierAddEntry;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierDelete;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierAction;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierInspector;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierSearch;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierCopyUsername;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierCopyPassword;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierHistory;
APPKIT_EXTERN NSString *const MPToolbarItemIdentifierAutotype;
@class MPDocument; @class MPDocument;
@interface MPToolbarDelegate : NSObject <NSToolbarDelegate, NSTextFieldDelegate> @interface MPToolbarDelegate : NSObject <NSToolbarDelegate, NSTextFieldDelegate>

View File

@@ -38,17 +38,17 @@
#import "NSApplication+MPAdditions.h" #import "NSApplication+MPAdditions.h"
#import "MPAppDelegate.h" #import "MPAppDelegate.h"
NSString *const MPToolbarItemLock = @"TOOLBAR_LOCK"; NSString *const MPToolbarItemIdentifierLock = @"TOOLBAR_LOCK";
NSString *const MPToolbarItemAddGroup = @"TOOLBAR_ADD_GROUP"; NSString *const MPToolbarItemIdentifierAddGroup = @"TOOLBAR_ADD_GROUP";
NSString *const MPToolbarItemAddEntry = @"TOOLBAR_ADD_ENTRY"; NSString *const MPToolbarItemIdentifierAddEntry = @"TOOLBAR_ADD_ENTRY";
NSString *const MPToolbarItemDelete =@"TOOLBAR_DELETE"; NSString *const MPToolbarItemIdentifierDelete = @"TOOLBAR_DELETE";
NSString *const MPToolbarItemAction = @"TOOLBAR_ACTION"; NSString *const MPToolbarItemIdentifierAction = @"TOOLBAR_ACTION";
NSString *const MPToolbarItemInspector = @"TOOLBAR_INSPECTOR"; NSString *const MPToolbarItemIdentifierInspector = @"TOOLBAR_INSPECTOR";
NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; NSString *const MPToolbarItemIdentifierSearch = @"TOOLBAR_SEARCH";
NSString *const MPToolbarItemCopyUsername = @"TOOLBAR_COPY_USERNAME"; NSString *const MPToolbarItemIdentifierCopyUsername = @"TOOLBAR_COPY_USERNAME";
NSString *const MPToolbarItemCopyPassword = @"TOOLBAR_COPY_PASSWORD"; NSString *const MPToolbarItemIdentifierCopyPassword = @"TOOLBAR_COPY_PASSWORD";
NSString *const MPToolbarItemHistory = @"TOOLBAR_HISTORY"; NSString *const MPToolbarItemIdentifierHistory = @"TOOLBAR_HISTORY";
NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE"; NSString *const MPToolbarItemIdentifierAutotype = @"TOOLBAR_AUTOTYPE";
@interface MPToolbarDelegate() { @interface MPToolbarDelegate() {
MPAddEntryContextMenuDelegate *_addEntryMenuDelegate; MPAddEntryContextMenuDelegate *_addEntryMenuDelegate;
@@ -72,27 +72,28 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
if (self) { if (self) {
_didShowToolbarForSearch = NO; _didShowToolbarForSearch = NO;
_didAddSearchfieldForSearch = NO; _didAddSearchfieldForSearch = NO;
_toolbarIdentifiers = @[ MPToolbarItemAddEntry, _toolbarIdentifiers = @[ MPToolbarItemIdentifierAddEntry,
MPToolbarItemDelete, MPToolbarItemIdentifierDelete,
MPToolbarItemAddGroup, MPToolbarItemIdentifierAddGroup,
MPToolbarItemAction, MPToolbarItemIdentifierAction,
MPToolbarItemCopyPassword, MPToolbarItemIdentifierCopyPassword,
MPToolbarItemCopyUsername, MPToolbarItemIdentifierCopyUsername,
NSToolbarFlexibleSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
MPToolbarItemSearch, MPToolbarItemIdentifierSearch,
MPToolbarItemLock, MPToolbarItemIdentifierLock,
MPToolbarItemInspector, MPToolbarItemIdentifierInspector,
MPToolbarItemHistory, MPToolbarItemIdentifierHistory,
MPToolbarItemAutotype ]; MPToolbarItemIdentifierAutotype ];
_defaultToolbarIdentifiers = @[ MPToolbarItemAddEntry, _defaultToolbarIdentifiers = @[ MPToolbarItemIdentifierAddEntry,
MPToolbarItemDelete, MPToolbarItemIdentifierDelete,
MPToolbarItemAddGroup, MPToolbarItemIdentifierAddGroup,
MPToolbarItemAutotype, MPToolbarItemIdentifierAutotype,
MPToolbarItemAction, MPToolbarItemIdentifierAction,
NSToolbarFlexibleSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
MPToolbarItemSearch, MPToolbarItemIdentifierSearch,
MPToolbarItemLock, NSToolbarFlexibleSpaceItemIdentifier,
MPToolbarItemInspector ]; MPToolbarItemIdentifierLock,
MPToolbarItemIdentifierInspector ];
_toolbarImages = [self createToolbarImages]; _toolbarImages = [self createToolbarImages];
_toolbarItems = [[NSMutableDictionary alloc] initWithCapacity:[self.toolbarIdentifiers count]]; _toolbarItems = [[NSMutableDictionary alloc] initWithCapacity:[self.toolbarIdentifiers count]];
_addEntryMenuDelegate = [[MPAddEntryContextMenuDelegate alloc] init]; _addEntryMenuDelegate = [[MPAddEntryContextMenuDelegate alloc] init];
@@ -113,7 +114,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
item.label = itemLabel; item.label = itemLabel;
item.paletteLabel = itemLabel; item.paletteLabel = itemLabel;
if([itemIdentifier isEqualToString:MPToolbarItemAction]) { if([itemIdentifier isEqualToString:MPToolbarItemIdentifierAction]) {
NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 32) pullsDown:YES]; NSPopUpButton *popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 32) pullsDown:YES];
popupButton.bezelStyle = NSTexturedRoundedBezelStyle; popupButton.bezelStyle = NSTexturedRoundedBezelStyle;
popupButton.focusRingType = NSFocusRingTypeNone; popupButton.focusRingType = NSFocusRingTypeNone;
@@ -126,7 +127,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
NSMenu *menu = [[NSMenu alloc] init]; NSMenu *menu = [[NSMenu alloc] init];
NSMenuItem *actionImageItem = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]; NSMenuItem *actionImageItem = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
actionImageItem.image = self.toolbarImages[MPToolbarItemAction]; actionImageItem.image = self.toolbarImages[MPToolbarItemIdentifierAction];
[menu addItem:actionImageItem]; [menu addItem:actionImageItem];
NSArray *menuItems = [MPContextMenuHelper contextMenuItemsWithItems:MPContextMenuExtended|MPContextMenuShowGroupInOutline]; NSArray *menuItems = [MPContextMenuHelper contextMenuItemsWithItems:MPContextMenuExtended|MPContextMenuShowGroupInOutline];
for(NSMenuItem *item in menuItems) { for(NSMenuItem *item in menuItems) {
@@ -143,7 +144,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
item.menuFormRepresentation = menuRepresentation; item.menuFormRepresentation = menuRepresentation;
item.view = popupButton; item.view = popupButton;
} }
else if( [itemIdentifier isEqualToString:MPToolbarItemAddEntry]) { else if( [itemIdentifier isEqualToString:MPToolbarItemIdentifierAddEntry]) {
MPContextButton *button = [[MPContextButton alloc] initWithFrame:NSMakeRect(0, 0, 32, 32)]; MPContextButton *button = [[MPContextButton alloc] initWithFrame:NSMakeRect(0, 0, 32, 32)];
button.action = [self _actionForToolbarItemIdentifier:itemIdentifier]; button.action = [self _actionForToolbarItemIdentifier:itemIdentifier];
NSImage *image = self.toolbarImages[itemIdentifier]; NSImage *image = self.toolbarImages[itemIdentifier];
@@ -168,7 +169,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
item.menuFormRepresentation = menuRepresentation; item.menuFormRepresentation = menuRepresentation;
} }
else if( [itemIdentifier isEqualToString:MPToolbarItemSearch]){ else if( [itemIdentifier isEqualToString:MPToolbarItemIdentifierSearch]){
NSSearchField *searchField = [[NSSearchField alloc] init]; NSSearchField *searchField = [[NSSearchField alloc] init];
searchField.action = @selector(updateSearch:); searchField.action = @selector(updateSearch:);
NSSearchFieldCell *cell = searchField.cell; NSSearchFieldCell *cell = searchField.cell;
@@ -216,16 +217,16 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
} }
- (NSDictionary *)createToolbarImages { - (NSDictionary *)createToolbarImages {
NSDictionary *imageDict = @{ MPToolbarItemLock: [NSImage imageNamed:NSImageNameLockLockedTemplate], NSDictionary *imageDict = @{ MPToolbarItemIdentifierLock: [NSImage imageNamed:NSImageNameLockLockedTemplate],
MPToolbarItemAddEntry: [MPIconHelper icon:MPIconAddEntry], MPToolbarItemIdentifierAddEntry: [MPIconHelper icon:MPIconAddEntry],
MPToolbarItemAddGroup: [MPIconHelper icon:MPIconAddFolder], MPToolbarItemIdentifierAddGroup: [MPIconHelper icon:MPIconAddFolder],
MPToolbarItemCopyUsername : [MPIconHelper icon:MPIconIdentity], MPToolbarItemIdentifierCopyUsername : [MPIconHelper icon:MPIconIdentity],
MPToolbarItemCopyPassword : [MPIconHelper icon:MPIconPassword], MPToolbarItemIdentifierCopyPassword : [MPIconHelper icon:MPIconPassword],
MPToolbarItemDelete: [MPIconHelper icon:MPIconTrash], MPToolbarItemIdentifierDelete: [MPIconHelper icon:MPIconTrash],
MPToolbarItemAction: [NSImage imageNamed:NSImageNameActionTemplate], MPToolbarItemIdentifierAction: [NSImage imageNamed:NSImageNameActionTemplate],
MPToolbarItemInspector: [MPIconHelper icon:MPIconInfo], MPToolbarItemIdentifierInspector: [MPIconHelper icon:MPIconInfo],
MPToolbarItemHistory: [MPIconHelper icon:MPIconHistory], MPToolbarItemIdentifierHistory: [MPIconHelper icon:MPIconHistory],
MPToolbarItemAutotype : [MPIconHelper icon:MPIconKeyboard] MPToolbarItemIdentifierAutotype : [MPIconHelper icon:MPIconKeyboard]
}; };
return imageDict; return imageDict;
} }
@@ -260,17 +261,17 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
static NSDictionary *labelDict; static NSDictionary *labelDict;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
labelDict = @{ MPToolbarItemLock: NSLocalizedString(@"LOCK", @"Toolbar item to Lock the database"), labelDict = @{ MPToolbarItemIdentifierLock: NSLocalizedString(@"LOCK", @"Toolbar item to Lock the database"),
MPToolbarItemAction: NSLocalizedString(@"ACTION", @"Toolbar item with action menu"), MPToolbarItemIdentifierAction: NSLocalizedString(@"ACTION", @"Toolbar item with action menu"),
MPToolbarItemAddEntry: NSLocalizedString(@"NEW_ENTRY", @"Toolbar item new entry"), MPToolbarItemIdentifierAddEntry: NSLocalizedString(@"NEW_ENTRY", @"Toolbar item new entry"),
MPToolbarItemAddGroup: NSLocalizedString(@"NEW_GROUP", @"Toolbar item new group"), MPToolbarItemIdentifierAddGroup: NSLocalizedString(@"NEW_GROUP", @"Toolbar item new group"),
MPToolbarItemCopyPassword: NSLocalizedString(@"COPY_PASSWORD", @"Toolbar item copy password"), MPToolbarItemIdentifierCopyPassword: NSLocalizedString(@"COPY_PASSWORD", @"Toolbar item copy password"),
MPToolbarItemCopyUsername: NSLocalizedString(@"COPY_USERNAME", @"Toolbar item copy username"), MPToolbarItemIdentifierCopyUsername: NSLocalizedString(@"COPY_USERNAME", @"Toolbar item copy username"),
MPToolbarItemDelete: NSLocalizedString(@"DELETE", @"Toolbar item delete item"), MPToolbarItemIdentifierDelete: NSLocalizedString(@"DELETE", @"Toolbar item delete item"),
MPToolbarItemInspector: NSLocalizedString(@"INSPECTOR", @"Toolbar item toggle inspector"), MPToolbarItemIdentifierInspector: NSLocalizedString(@"INSPECTOR", @"Toolbar item toggle inspector"),
MPToolbarItemSearch: NSLocalizedString(@"SEARCH", @"Search input in Toolbar "), MPToolbarItemIdentifierSearch: NSLocalizedString(@"SEARCH", @"Search input in Toolbar "),
MPToolbarItemHistory: NSLocalizedString(@"SHOW_HISTORY", @"Toolbar item to toggle history display"), MPToolbarItemIdentifierHistory: NSLocalizedString(@"SHOW_HISTORY", @"Toolbar item to toggle history display"),
MPToolbarItemAutotype: NSLocalizedString(@"TOOLBAR_PERFORM_AUTOTYPE_FOR_ENTRY", @"Toolbar item to perform autotype") MPToolbarItemIdentifierAutotype: NSLocalizedString(@"TOOLBAR_PERFORM_AUTOTYPE_FOR_ENTRY", @"Toolbar item to perform autotype")
}; };
}); });
return labelDict[identifier]; return labelDict[identifier];
@@ -280,15 +281,15 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
static NSDictionary *actionDict; static NSDictionary *actionDict;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
actionDict = @{ MPToolbarItemLock: @(MPActionLock), actionDict = @{ MPToolbarItemIdentifierLock: @(MPActionLock),
MPToolbarItemAddEntry: @(MPActionAddEntry), MPToolbarItemIdentifierAddEntry: @(MPActionAddEntry),
MPToolbarItemAddGroup: @(MPActionAddGroup), MPToolbarItemIdentifierAddGroup: @(MPActionAddGroup),
MPToolbarItemDelete: @(MPActionDelete), MPToolbarItemIdentifierDelete: @(MPActionDelete),
MPToolbarItemCopyPassword: @(MPActionCopyPassword), MPToolbarItemIdentifierCopyPassword: @(MPActionCopyPassword),
MPToolbarItemCopyUsername: @(MPActionCopyUsername), MPToolbarItemIdentifierCopyUsername: @(MPActionCopyUsername),
MPToolbarItemInspector: @(MPActionToggleInspector), MPToolbarItemIdentifierInspector: @(MPActionToggleInspector),
MPToolbarItemHistory: @(MPActionShowEntryHistory), MPToolbarItemIdentifierHistory: @(MPActionShowEntryHistory),
MPToolbarItemAutotype: @(MPActionPerformAutotypeForSelectedEntry) MPToolbarItemIdentifierAutotype: @(MPActionPerformAutotypeForSelectedEntry)
}; };
}); });
MPActionType actionType = (MPActionType)[actionDict[identifier] integerValue]; MPActionType actionType = (MPActionType)[actionDict[identifier] integerValue];
@@ -321,9 +322,9 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
- (void)_didEnterSearch:(NSNotification *)notification { - (void)_didEnterSearch:(NSNotification *)notification {
/* We enter search. If there is no Item to search in the toolbar, we need to add it */ /* We enter search. If there is no Item to search in the toolbar, we need to add it */
NSArray *currentItems = self.toolbar.items; NSArray *currentItems = self.toolbar.items;
NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemSearch]; NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemIdentifierSearch];
if(!searchItem || ![currentItems containsObject:searchItem]) { if(!searchItem || ![currentItems containsObject:searchItem]) {
[self.toolbar insertItemWithItemIdentifier:MPToolbarItemSearch atIndex:[currentItems count]]; [self.toolbar insertItemWithItemIdentifier:MPToolbarItemIdentifierSearch atIndex:[currentItems count]];
_didAddSearchfieldForSearch = YES; _didAddSearchfieldForSearch = YES;
} }
/* Then we should make sure the toolbar is visible. Just to make life easier */ /* Then we should make sure the toolbar is visible. Just to make life easier */
@@ -350,7 +351,7 @@ NSString *const MPToolbarItemAutotype = @"TOOLBAR_AUTOTYPE";
[window makeFirstResponder:nil]; [window makeFirstResponder:nil];
} }
if(_didAddSearchfieldForSearch) { if(_didAddSearchfieldForSearch) {
NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemSearch]; NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemIdentifierSearch];
NSUInteger index = [self.toolbar.items indexOfObject:searchItem]; NSUInteger index = [self.toolbar.items indexOfObject:searchItem];
if(index != NSNotFound) { if(index != NSNotFound) {
[self.toolbar removeItemAtIndex:index]; [self.toolbar removeItemAtIndex:index];