MPToolbarDelegate now takes care of the toolbar manipulation.

Toolbar is shown when search is started if it was hidden. If search revealed the toolbar, it will be hidden after the search is exited
If the toolbar was using text only mode, it switches to icon/text mode and then back to text mode after the search - there might be a better way to deal with this by simply using a menu for the search.
This commit is contained in:
michael starke
2014-02-28 20:36:10 +01:00
parent 7f03985eff
commit 0c4ddf3127
3 changed files with 48 additions and 26 deletions

View File

@@ -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;

View File

@@ -27,6 +27,7 @@
@interface MPToolbarDelegate : NSObject <NSToolbarDelegate>
@property (weak, readonly) NSSearchField *searchField;
@property (weak) NSToolbar *toolbar;
- (void)registerNotificationsForDocument:(MPDocument *)document;

View File

@@ -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