From 1315dd378437c84891c795c9927ff40d42d74cb3 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Mon, 4 Mar 2013 16:46:54 +0100 Subject: [PATCH] Remove mappingsChanged with a notification. MappingsController no longer needs to know about the app delegate. --- EnjoyableApplicationDelegate.h | 2 -- EnjoyableApplicationDelegate.m | 11 +++++++++-- NJEvents.h | 1 + NJMappingsController.m | 33 +++++++++++++++------------------ 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/EnjoyableApplicationDelegate.h b/EnjoyableApplicationDelegate.h index 3e74418..b77f799 100644 --- a/EnjoyableApplicationDelegate.h +++ b/EnjoyableApplicationDelegate.h @@ -18,6 +18,4 @@ @property (nonatomic, strong) IBOutlet NJOutputController *outputController; @property (nonatomic, strong) IBOutlet NJMappingsController *mappingsController; -- (void)mappingsChanged; - @end diff --git a/EnjoyableApplicationDelegate.m b/EnjoyableApplicationDelegate.m index 0dbea22..8afbb5b 100644 --- a/EnjoyableApplicationDelegate.m +++ b/EnjoyableApplicationDelegate.m @@ -28,6 +28,11 @@ selector:@selector(mappingDidChange:) name:NJEventMappingChanged object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(mappingListDidChange:) + name:NJEventMappingListChanged + object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(eventTranslationActivated:) @@ -48,6 +53,7 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification { [NSUserDefaults.standardUserDefaults synchronize]; + [NSNotificationCenter.defaultCenter removeObserver:self]; } - (void)eventTranslationActivated:(NSNotification *)note { @@ -67,12 +73,13 @@ NSLog(@"Ignoring application changes."); } -- (void)mappingsChanged { +- (void)mappingListDidChange:(NSNotification *)note { + NSArray *mappings = note.object; NSInteger removeFrom = mappingsMenuIndex; while (dockMenuBase.numberOfItems > removeFrom) [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1]; int added = 0; - for (NJMapping *mapping in self.mappingsController) { + for (NJMapping *mapping in mappings) { NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @""; [dockMenuBase addItemWithTitle:mapping.name action:@selector(chooseMapping:) diff --git a/NJEvents.h b/NJEvents.h index 84bdd82..ce87b24 100644 --- a/NJEvents.h +++ b/NJEvents.h @@ -7,5 +7,6 @@ // #define NJEventMappingChanged @"NJEventMappingChanged" +#define NJEventMappingListChanged @"NJEventMappingListChanged" #define NJEventTranslationActivated @"NJEventTranslationActivated" #define NJEventTranslationDeactivated @"NJEventTranslationDeactivated" diff --git a/NJMappingsController.m b/NJMappingsController.m index f1e068e..e2ac93c 100644 --- a/NJMappingsController.m +++ b/NJMappingsController.m @@ -7,7 +7,6 @@ #import "NJMappingsController.h" -#import "EnjoyableApplicationDelegate.h" #import "NJMapping.h" #import "NJMappingsController.h" #import "NJOutput.h" @@ -40,6 +39,14 @@ return idx < _mappings.count ? _mappings[idx] : nil; } +- (void)mappingsChanged { + [self save]; + [tableView reloadData]; + [NSNotificationCenter.defaultCenter + postNotificationName:NJEventMappingListChanged + object:_mappings]; +} + - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len { @@ -76,11 +83,9 @@ - (IBAction)addPressed:(id)sender { NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"]; [_mappings addObject:newMapping]; - [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged]; - [tableView reloadData]; - [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO]; - [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES]; + [self mappingsChanged]; [self activateMapping:newMapping]; + [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES]; } - (IBAction)removePressed:(id)sender { @@ -88,15 +93,12 @@ return; [_mappings removeObjectAtIndex:tableView.selectedRow]; - [tableView reloadData]; - [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged]; + [self mappingsChanged]; [self activateMapping:_mappings[0]]; - [self save]; } -(void)tableViewSelectionDidChange:(NSNotification *)notify { - if (tableView.selectedRow >= 0) - [self activateMapping:_mappings[tableView.selectedRow]]; + [self activateMapping:self[tableView.selectedRow]]; } - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index { @@ -108,9 +110,7 @@ forTableColumn:(NSTableColumn *)col row:(NSInteger)index { self[index].name = obj; - [self save]; - [tableView reloadData]; - [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged]; + [self mappingsChanged]; } - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { @@ -164,8 +164,7 @@ if (current >= newMappings.count) current = 0; _mappings = newMappings; - [tableView reloadData]; - [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged]; + [self mappingsChanged]; [self activateMapping:_mappings[current]]; } } @@ -248,11 +247,9 @@ mapping = mergeInto; } else { [_mappings addObject:mapping]; - [tableView reloadData]; } - [self save]; - [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged]; + [self mappingsChanged]; [self activateMapping:mapping]; [outputController loadCurrent]; -- 2.30.2