From 51bb22a9b7b75970bd16f4ecae2b1c9462fff4c8 Mon Sep 17 00:00:00 2001 From: michael starke Date: Wed, 26 Feb 2014 21:17:35 +0100 Subject: [PATCH] Moving more stuff about context bar into the document away from the EntryViewController. Some refactorings considering notification registration. --- MacPass.xcodeproj/project.pbxproj | 6 +++++ MacPass/MPContextBarViewController.h | 15 +++---------- MacPass/MPContextBarViewController.m | 33 ++++++++++------------------ MacPass/MPDocument+HistoryBrowsing.h | 6 +++++ MacPass/MPDocument+HistoryBrowsing.m | 12 ++++++++++ MacPass/MPDocument+Search.m | 8 +++++++ MacPass/MPDocument.h | 3 +++ MacPass/MPDocument.m | 5 ++++- MacPass/MPDocumentWindowController.m | 4 ++-- MacPass/MPEntryViewController.h | 2 +- MacPass/MPEntryViewController.m | 15 +++++++------ MacPass/MPInspectorViewController.m | 2 +- MacPass/MPNotifications.h | 1 - MacPass/MPNotifications.m | 1 - MacPass/MPToolbarDelegate.h | 2 +- MacPass/MPToolbarDelegate.m | 14 +++++++++--- 16 files changed, 78 insertions(+), 51 deletions(-) diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index c2c19cae..2101c822 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -159,6 +159,7 @@ 4C68456D17BC227B00FCDBFC /* KPKWindowAssociation.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C68456C17BC227B00FCDBFC /* KPKWindowAssociation.m */; }; 4C68456F17BC2A0700FCDBFC /* WelcomeWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C68456E17BC2A0700FCDBFC /* WelcomeWindow.xib */; }; 4C69A73A16D589DF00EC1B1A /* HNHGradientView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C69A73916D589DF00EC1B1A /* HNHGradientView.m */; }; + 4C6B7C7D18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C6B7C7C18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.m */; }; 4C6BEA3117A88E6C00CF69A8 /* MPStripLineBreaksTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C6BEA3017A88E6C00CF69A8 /* MPStripLineBreaksTransformer.m */; }; 4C6C72C417C01E4200768849 /* NSMutableData+KeePassKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C6C72C317C01E4200768849 /* NSMutableData+KeePassKit.m */; }; 4C6D1D25178579570014C5A5 /* 48_FolderTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 4C6D1D24178579570014C5A5 /* 48_FolderTemplate.pdf */; }; @@ -624,6 +625,8 @@ 4C68456E17BC2A0700FCDBFC /* WelcomeWindow.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = WelcomeWindow.xib; sourceTree = ""; }; 4C69A73816D589DF00EC1B1A /* HNHGradientView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HNHGradientView.h; sourceTree = ""; }; 4C69A73916D589DF00EC1B1A /* HNHGradientView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HNHGradientView.m; sourceTree = ""; }; + 4C6B7C7B18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MPDocument+HistoryBrowsing.h"; sourceTree = ""; }; + 4C6B7C7C18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MPDocument+HistoryBrowsing.m"; sourceTree = ""; }; 4C6BEA2F17A88E6C00CF69A8 /* MPStripLineBreaksTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPStripLineBreaksTransformer.h; sourceTree = ""; }; 4C6BEA3017A88E6C00CF69A8 /* MPStripLineBreaksTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPStripLineBreaksTransformer.m; sourceTree = ""; }; 4C6C72C217C01E4200768849 /* NSMutableData+KeePassKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableData+KeePassKit.h"; sourceTree = ""; }; @@ -1225,6 +1228,8 @@ 4C1FA07A18231900003A3F8C /* MPDocument+Autotype.m */, 4C15B74418BCA3B1003F8008 /* MPDocument+Search.h */, 4C15B74518BCA3B1003F8008 /* MPDocument+Search.m */, + 4C6B7C7B18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.h */, + 4C6B7C7C18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.m */, ); name = Model; sourceTree = ""; @@ -2093,6 +2098,7 @@ 4CAD747F15B887FD00104512 /* DDXMLNode.m in Sources */, 4C37A84015B8B474005EF8EE /* MPOutlineDataSource.m in Sources */, 4CA0B2F915BCAF6700654E32 /* MPGeneralSettingsController.m in Sources */, + 4C6B7C7D18BE7EB0001D5D77 /* MPDocument+HistoryBrowsing.m in Sources */, 4CA0B2FC15BCAF8600654E32 /* MPSettingsWindowController.m in Sources */, 4C83814215BF4677001AE468 /* MPDocumentWindowController.m in Sources */, 4C2E382316D1421B00037A9D /* MPIconHelper.m in Sources */, diff --git a/MacPass/MPContextBarViewController.h b/MacPass/MPContextBarViewController.h index a4edf3c0..be6873e9 100644 --- a/MacPass/MPContextBarViewController.h +++ b/MacPass/MPContextBarViewController.h @@ -9,25 +9,16 @@ #import "MPViewController.h" #import "MPDocument+Search.h" -@protocol MPContextBarDelegate - -@optional -- (void)contextBarDidChangeFilter; -- (void)contextBarDidExitFilter; -- (void)contextBarDidExitHistory; -- (void)contextBarShouldEmptyTrash; -@end - @class HNHGradientView; +@class MPDocument; @interface MPContextBarViewController : MPViewController -@property (nonatomic, readonly) BOOL hasFilter; -@property (nonatomic, weak) id delegate; @property (weak) NSView *nextKeyView; +- (void)registerNotificationsForDocument:(MPDocument *)document; + - (IBAction)toggleFilterSpace:(id)sender; -- (IBAction)exitFilter:(id)sender; - (BOOL)showsFilter; - (BOOL)showsHistory; diff --git a/MacPass/MPContextBarViewController.m b/MacPass/MPContextBarViewController.m index a347e652..a23cd4e2 100644 --- a/MacPass/MPContextBarViewController.m +++ b/MacPass/MPContextBarViewController.m @@ -59,10 +59,6 @@ typedef NS_ENUM(NSUInteger, MPContextTab) { _delegateRespondsToDidExitHistory = NO; _delegateRespondsToShouldEmptyTrash = NO; _delegateRespondsToDidChangeFilter = NO; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_updateFilterButtons) - name:MPDocumentDidChangeSearchFlags - object:nil]; } return self; } @@ -94,15 +90,6 @@ typedef NS_ENUM(NSUInteger, MPContextTab) { } #pragma mark Properties -- (void)setDelegate:(id)delegate { - if(self.delegate != delegate) { - _delegate = delegate; - _delegateRespondsToDidChangeFilter = [_delegate respondsToSelector:@selector(contextBarDidChangeFilter)]; - _delegateRespondsToDidExitFilter = [_delegate respondsToSelector:@selector(contextBarDidExitFilter)]; - _delegateRespondsToDidExitHistory = [_delegate respondsToSelector:@selector(contextBarDidExitHistory)]; - _delegateRespondsToShouldEmptyTrash = [_delegate respondsToSelector:@selector(contextBarShouldEmptyTrash)]; - } -} - (void)showFilter { /* Select text if already visible */ @@ -111,13 +98,11 @@ typedef NS_ENUM(NSUInteger, MPContextTab) { } - (void)showHistory { - [self exitFilter:self]; self.activeTab = MPContextTabHistory; [self _updateBindings]; } - (void)showTrash { - [self exitFilter:self]; self.activeTab = MPContextTabTrash; [self _updateBindings]; } @@ -134,6 +119,18 @@ typedef NS_ENUM(NSUInteger, MPContextTab) { return self.activeTab == MPContextTabTrash; } +- (void)registerNotificationsForDocument:(MPDocument *)document { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_updateFilterButtons) + name:MPDocumentDidChangeSearchFlags + object:document]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(showFilter) + name:MPDocumentDidEnterSearchNotification + object:document]; + +} + /* - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector { if(commandSelector == @selector(insertNewline:)) { @@ -143,12 +140,6 @@ typedef NS_ENUM(NSUInteger, MPContextTab) { } */ -- (void)_didChangeFilter { - if(_delegateRespondsToDidChangeFilter) { - [self.delegate contextBarDidChangeFilter]; - } -} - #pragma mark UI Helper - (void)_updateBindings { // only the entry view has to be bound, the rest not diff --git a/MacPass/MPDocument+HistoryBrowsing.h b/MacPass/MPDocument+HistoryBrowsing.h index e5a18417..7acd9a86 100644 --- a/MacPass/MPDocument+HistoryBrowsing.h +++ b/MacPass/MPDocument+HistoryBrowsing.h @@ -8,6 +8,12 @@ #import "MPDocument.h" +FOUNDATION_EXPORT NSString *const MPDocumentDidEnterHistoryNotification; +FOUNDATION_EXPORT NSString *const MPDocumentDidExitHistoryNotification; + @interface MPDocument (HistoryBrowsing) +- (IBAction)showHistory:(id)sender; +- (IBAction)exitHistory:(id)sender; + @end diff --git a/MacPass/MPDocument+HistoryBrowsing.m b/MacPass/MPDocument+HistoryBrowsing.m index 08e22c02..a42b14c9 100644 --- a/MacPass/MPDocument+HistoryBrowsing.m +++ b/MacPass/MPDocument+HistoryBrowsing.m @@ -8,6 +8,18 @@ #import "MPDocument+HistoryBrowsing.h" +NSString *const MPDocumentDidEnterHistoryNotification = @"MPDocumentDidEnterHistoryNotification"; +NSString *const MPDocumentDidExitHistoryNotification = @"MPDocumentDidExitHistoryNotification"; + @implementation MPDocument (HistoryBrowsing) +- (void)showHistory:(id)sender { + [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidEnterHistoryNotification object:self]; +} + +- (void)exitHistory:(id)sender { + [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidExitHistoryNotification object:self]; +} + + @end diff --git a/MacPass/MPDocument+Search.m b/MacPass/MPDocument+Search.m index a71b4e1a..ae7d9dab 100644 --- a/MacPass/MPDocument+Search.m +++ b/MacPass/MPDocument+Search.m @@ -22,15 +22,23 @@ NSString *const MPDocumentDidExitSearchNotification = @"com.hicknhack.macpass. #pragma mark Actions - (void)performFindPanelAction:(id)sender { + self.hasSearch = YES; + NSWindow *window = [[self windowControllers][0] window]; + NSToolbar *toolbar = [window toolbar]; + if(![toolbar isVisible]) { + [toolbar setVisible:YES]; + } [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidEnterSearchNotification object:self]; } - (void)updateSearch:(id)sender { + self.hasSearch = YES; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidChangeSearchNotification object:self]; } - (void)exitSearch:(id)sender { self.searchString = nil; + self.hasSearch = NO; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidExitSearchNotification object:self]; } diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index ee4c504c..41563f06 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -29,6 +29,8 @@ APPKIT_EXTERN NSString *const MPDocumentDidRevertNotifiation; APPKIT_EXTERN NSString *const MPDocumentDidLockDatabaseNotification; APPKIT_EXTERN NSString *const MPDocumentDidUnlockDatabaseNotification; +FOUNDATION_EXTERN NSString *const MPDocumentCurrentItemChangedNotification; + APPKIT_EXTERN NSString *const MPDocumentEntryKey; APPKIT_EXTERN NSString *const MPDocumentGroupKey; @@ -77,6 +79,7 @@ typedef NS_OPTIONS(NSUInteger, MPEntrySearchFlags) { */ @property (nonatomic, assign) MPEntrySearchFlags activeFlags; @property (nonatomic, copy) NSString *searchString; +@property (nonatomic, assign) BOOL hasSearch; + (KPKVersion)versionForFileType:(NSString *)fileType; diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 405e350f..5d7b1d85 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -48,6 +48,8 @@ NSString *const MPDocumentDidRevertNotifiation = @"com.hicknhack.macp NSString *const MPDocumentDidLockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidLockDatabaseNotification"; NSString *const MPDocumentDidUnlockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidUnlockDatabaseNotification"; +NSString *const MPDocumentCurrentItemChangedNotification = @"com.hicknhack.macpass.MPDocumentCurrentItemChangedNotification"; + NSString *const MPDocumentEntryKey = @"MPDocumentEntryKey"; NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; @@ -112,6 +114,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey _didLockFile = NO; _readOnly = NO; _activeFlags = MPEntrySearchTitles; + _hasSearch = NO; self.tree = [KPKTree templateTree]; } return self; @@ -350,7 +353,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey - (void)setSelectedItem:(id)selectedItem { if(_selectedItem != selectedItem) { _selectedItem = selectedItem; - [[NSNotificationCenter defaultCenter] postNotificationName:MPCurrentItemChangedNotification object:self]; + [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentCurrentItemChangedNotification object:self]; } } - (void)setTree:(KPKTree *)tree { diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index 61daccda..e3466146 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -86,13 +86,13 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { [_entryViewController regsiterNotificationsForDocument:document]; [_inspectorViewController regsiterNotificationsForDocument:document]; [_outlineViewController regsiterNotificationsForDocument:document]; - [_toolbarDelegate regsiterNotificationsForDocument:document]; + [_toolbarDelegate registerNotificationsForDocument:document]; _toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; [_toolbar setAutosavesConfiguration:YES]; - [self.toolbar setAllowsUserCustomization:YES]; + [self.toolbar setAllowsUserCustomization:NO]; [self.toolbar setDelegate:self.toolbarDelegate]; [self.window setToolbar:self.toolbar]; diff --git a/MacPass/MPEntryViewController.h b/MacPass/MPEntryViewController.h index a1393970..7ffb4981 100644 --- a/MacPass/MPEntryViewController.h +++ b/MacPass/MPEntryViewController.h @@ -28,7 +28,7 @@ typedef NS_ENUM( NSUInteger, MPCopyContentTypeTag) { @class MPDocumentWindowController; @class MPDocument; -@interface MPEntryViewController : MPViewController +@interface MPEntryViewController : MPViewController @property (weak,readonly) NSTableView *entryTable; @property (readonly, strong) NSArrayController *entryArrayController; diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index b4f2dc19..5deaf7af 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -194,7 +194,7 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; - (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeCurrentItem:) - name:MPCurrentItemChangedNotification + name:MPDocumentCurrentItemChangedNotification object:document]; [[NSNotificationCenter defaultCenter] addObserver:self @@ -207,6 +207,11 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; object:document]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_updateContextBar) + name:MPDocumentDidExitSearchNotification + object:document]; + } #pragma mark NSTableViewDelgate @@ -309,7 +314,7 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; if(document.selectedItem == document.selectedGroup) { /* If we change to a group selection, we should clear the filter */ if(_isDisplayingContextBar) { - [self.contextBarViewController exitFilter:self]; + [document exitSearch:self]; } else if([[self.entryArrayController content] count] > 0) { KPKEntry *entry = [[self.entryArrayController content] lastObject]; @@ -365,10 +370,6 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; [self _showContextBar]; } -- (void)clearFilter:(id)sender { - [self.contextBarViewController exitFilter:sender]; -} - #pragma mark ContextBar - (void)_showTrashBar { [self.contextBarViewController showTrash]; @@ -377,7 +378,7 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; - (void)_updateContextBar { MPDocument *document = [[self windowController] document]; - if(![self.contextBarViewController hasFilter]) { + if(!document.hasSearch) { BOOL showTrash = document.useTrash && (document.selectedGroup == document.trash || [document isItemTrashed:document.selectedItem]); if(showTrash) { [self _showTrashBar]; diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index c2e1219b..904d36de 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -110,7 +110,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { - (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeCurrentItem:) - name:MPCurrentItemChangedNotification + name:MPDocumentCurrentItemChangedNotification object:document]; [_entryViewController setupBindings:document]; [_groupViewController setupBindings:document]; diff --git a/MacPass/MPNotifications.h b/MacPass/MPNotifications.h index b4771c30..c73ebc8f 100644 --- a/MacPass/MPNotifications.h +++ b/MacPass/MPNotifications.h @@ -12,6 +12,5 @@ #import FOUNDATION_EXPORT NSString *const MPDidActivateViewNotification; -FOUNDATION_EXPORT NSString *const MPCurrentItemChangedNotification; #endif diff --git a/MacPass/MPNotifications.m b/MacPass/MPNotifications.m index e9b4c267..bf94ce73 100644 --- a/MacPass/MPNotifications.m +++ b/MacPass/MPNotifications.m @@ -9,4 +9,3 @@ #import "MPNotifications.h" NSString *const MPDidActivateViewNotification = @"com.hicknhack.macpass.MPDidBecomeFirstResonderNotification"; -NSString *const MPCurrentItemChangedNotification = @"com.hicknhack.macpass.MPCurrentItemChangedNotification"; \ No newline at end of file diff --git a/MacPass/MPToolbarDelegate.h b/MacPass/MPToolbarDelegate.h index 5b270f4b..e3d2baf1 100644 --- a/MacPass/MPToolbarDelegate.h +++ b/MacPass/MPToolbarDelegate.h @@ -28,6 +28,6 @@ @property (weak, readonly) NSSearchField *searchField; -- (void)regsiterNotificationsForDocument:(MPDocument *)document; +- (void)registerNotificationsForDocument:(MPDocument *)document; @end diff --git a/MacPass/MPToolbarDelegate.m b/MacPass/MPToolbarDelegate.m index 6287ceb4..45d4735b 100644 --- a/MacPass/MPToolbarDelegate.m +++ b/MacPass/MPToolbarDelegate.m @@ -187,9 +187,9 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; } -- (void)regsiterNotificationsForDocument:(MPDocument *)document { +- (void)registerNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidExitSearchNotification object:document]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidEnterSearchNotification object:document]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didEnterSearch:) name:MPDocumentDidEnterSearchNotification object:document]; } - (NSString *)_localizedLabelForToolbarItemIdentifier:(NSString *)identifier { @@ -223,9 +223,17 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; return [MPActionHelper actionOfType:actionType]; } +- (void)_didEnterSearch:(NSNotification *)notification { + [[self.searchField window] makeFirstResponder:self.searchField]; +} + - (void)_didExitSearch:(NSNotification *)notification { [self.searchField setStringValue:@""]; - [[self.searchField window] makeFirstResponder:nil]; + NSWindow *window = [self.searchField window]; + /* Resign first responder form search field only if it was the first responder */ + if([window firstResponder] == self.searchField ) { + [window makeFirstResponder:nil]; + } } @end