diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index 1fd279d1..c875cdb4 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -33,7 +33,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { @private id _firstResponder; BOOL _saveAfterPasswordChange; - BOOL _didShowToolbarForSearch; } @property (strong) IBOutlet NSSplitView *splitView; @@ -58,7 +57,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { if( self ) { _firstResponder = nil; _saveAfterPasswordChange = NO; - _didShowToolbarForSearch = NO; _toolbarDelegate = [[MPToolbarDelegate alloc] init]; _outlineViewController = [[MPOutlineViewController alloc] init]; _entryViewController = [[MPEntryViewController alloc] init]; @@ -83,8 +81,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didRevertDocument:) name:MPDocumentDidRevertNotifiation object:document]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showEntries) name:MPDocumentDidUnlockDatabaseNotification object:document]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didEnterSearch:) name:MPDocumentDidEnterSearchNotification object:document]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidExitSearchNotification object:document]; [_entryViewController regsiterNotificationsForDocument:document]; [_inspectorViewController regsiterNotificationsForDocument:document]; @@ -95,9 +91,10 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { _toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; [_toolbar setAutosavesConfiguration:YES]; - [self.toolbar setAllowsUserCustomization:NO]; + [self.toolbar setAllowsUserCustomization:YES]; [self.toolbar setDelegate:self.toolbarDelegate]; [self.window setToolbar:self.toolbar]; + self.toolbarDelegate.toolbar = _toolbar; [self.splitView setTranslatesAutoresizingMaskIntoConstraints:NO]; @@ -168,26 +165,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { [self showPasswordInput]; } -- (void)_didEnterSearch:(NSNotificationCenter *)notification { - /* Hide again after search ? */ - NSWindow *window = [self window]; - NSToolbar *toolbar = [window toolbar]; - if(![toolbar isVisible]) { - _didShowToolbarForSearch = YES; - [toolbar setVisible:YES]; - } -} - -- (void)_didExitSearch:(NSNotification *)notification { - /* Hide again after search ? */ - NSWindow *window = [self window]; - NSToolbar *toolbar = [window toolbar]; - if(_didShowToolbarForSearch && [toolbar isVisible]) { - _didShowToolbarForSearch = NO; - [toolbar setVisible:NO]; - } -} - #pragma mark Actions - (void)saveDocument:(id)sender { _saveAfterPasswordChange = NO; diff --git a/MacPass/MPToolbarDelegate.h b/MacPass/MPToolbarDelegate.h index e3d2baf1..01ef1473 100644 --- a/MacPass/MPToolbarDelegate.h +++ b/MacPass/MPToolbarDelegate.h @@ -27,6 +27,7 @@ @interface MPToolbarDelegate : NSObject @property (weak, readonly) NSSearchField *searchField; +@property (weak) NSToolbar *toolbar; - (void)registerNotificationsForDocument:(MPDocument *)document; diff --git a/MacPass/MPToolbarDelegate.m b/MacPass/MPToolbarDelegate.m index b5979ece..e4f6a12f 100644 --- a/MacPass/MPToolbarDelegate.m +++ b/MacPass/MPToolbarDelegate.m @@ -44,6 +44,9 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; @interface MPToolbarDelegate() { MPAddEntryContextMenuDelegate *_entryMenuDelegate; + BOOL _didShowToolbarForSearch; + BOOL _didAddSearchfieldForSearch; + NSToolbarDisplayMode _displayModeBeforeSearch; } @property (strong) NSMutableDictionary *toolbarItems; @@ -62,7 +65,16 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; - (id)init { self = [super init]; if (self) { - _toolbarIdentifiers = @[ MPToolbarItemAddEntry, MPToolbarItemDelete, MPToolbarItemAddGroup, MPToolbarItemAction, NSToolbarFlexibleSpaceItemIdentifier, MPToolbarItemSearch, MPToolbarItemLock, MPToolbarItemInspector ]; + _didShowToolbarForSearch = NO; + _didAddSearchfieldForSearch = NO; + _toolbarIdentifiers = @[ MPToolbarItemAddEntry, + MPToolbarItemDelete, + MPToolbarItemAddGroup, + MPToolbarItemAction, + NSToolbarFlexibleSpaceItemIdentifier, + MPToolbarItemSearch, + MPToolbarItemLock, + MPToolbarItemInspector ]; _toolbarImages = [self createToolbarImages]; _toolbarItems = [[NSMutableDictionary alloc] initWithCapacity:[self.toolbarIdentifiers count]]; _entryMenuDelegate = [[MPAddEntryContextMenuDelegate alloc] init]; @@ -142,6 +154,7 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; NSSearchFieldCell *cell = [searchField cell]; [[cell cancelButtonCell] setAction:@selector(exitSearch:)]; [[cell cancelButtonCell] setTarget:nil]; + [searchField setRecentsAutosaveName:@"RecentEntrySearches"]; [item setView:searchField]; self.searchField = searchField; } @@ -224,6 +237,22 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; } - (void)_didEnterSearch:(NSNotification *)notification { + /* We enter search. If there is no Item to search in the toolbar, we need to add it */ + NSArray *currentItems = [self.toolbar items]; + NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemSearch]; + if(!searchItem || ![currentItems containsObject:searchItem]) { + [self.toolbar insertItemWithItemIdentifier:MPToolbarItemSearch atIndex:[currentItems count]]; + _didAddSearchfieldForSearch = YES; + } + /* Then we should make sure the toolbar is visible. Just to make life easier */ + if(![self.toolbar isVisible]) { + _didShowToolbarForSearch = YES; + [self.toolbar setVisible:YES]; + } + _displayModeBeforeSearch = [self.toolbar displayMode]; + if(_displayModeBeforeSearch == NSToolbarDisplayModeLabelOnly) { + [self.toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel]; + } [[self.searchField window] makeFirstResponder:self.searchField]; [self.searchField selectText:self]; } @@ -235,6 +264,21 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; if([window firstResponder] == [self.searchField currentEditor]) { [window makeFirstResponder:nil]; } + if(_didAddSearchfieldForSearch) { + NSToolbarItem *searchItem = self.toolbarItems[MPToolbarItemSearch]; + NSUInteger index = [[self.toolbar items] indexOfObject:searchItem]; + if(index != NSNotFound) { + [self.toolbar removeItemAtIndex:index]; + _didAddSearchfieldForSearch = NO; + } + } + if(_displayModeBeforeSearch != [self.toolbar displayMode]) { + [self.toolbar setDisplayMode:_displayModeBeforeSearch]; + } + if(_didShowToolbarForSearch && [self.toolbar isVisible]) { + _didShowToolbarForSearch = NO; + [self.toolbar setVisible:NO]; + } } @end