From 3fae21830d647cf9c208fc8f96e23efedccdbd0e Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Mon, 11 Mar 2013 02:01:08 +0100 Subject: [PATCH] Push notifications out with more idiomatic semantics - controller as object, parameter as userInfo. --- Classes/EnjoyableApplicationDelegate.m | 24 +++++++++++++++++------- Classes/NJMappingsController.m | 9 ++++++--- Classes/NJOutputController.m | 2 +- Info.plist | 2 +- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index 634f362..c66cf6b 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -124,10 +124,13 @@ [self.mappingsController mappingPressed:sender]; } -- (void)addMappingsToMenu:(NSMenu *)menu withKeys:(BOOL)withKeys atIndex:(NSInteger)index { +- (void)addMappings:(NSArray *)mappings + toMenu:(NSMenu *)menu + withKeys:(BOOL)withKeys + atIndex:(NSInteger)index { static const NSUInteger MAXIMUM_ITEMS = 15; int added = 0; - for (NJMapping *mapping in self.mappingsController) { + for (NJMapping *mapping in mappings) { NSString *keyEquiv = (++added < 10 && withKeys) ? @(added).stringValue : @""; NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name action:@selector(chooseMapping:) @@ -143,7 +146,7 @@ keyEquivalent:@""]; // There must be a represented object here so the item gets // removed correctly when the menus are regenerated. - end.representedObject = self.mappingsController.mappings; + end.representedObject = mappings; end.target = self; [menu insertItem:end atIndex:index++]; break; @@ -152,16 +155,20 @@ } - (void)mappingListDidChange:(NSNotification *)note { + NSArray *mappings = note.userInfo[@"mappings"]; while (mappingsMenu.lastItem.representedObject) [mappingsMenu removeLastItem]; - [self addMappingsToMenu:mappingsMenu withKeys:YES atIndex:mappingsMenu.numberOfItems]; + [self addMappings:mappings + toMenu:mappingsMenu + withKeys:YES + atIndex:mappingsMenu.numberOfItems]; while ([statusItemMenu itemAtIndex:2].representedObject) [statusItemMenu removeItemAtIndex:2]; - [self addMappingsToMenu:statusItemMenu withKeys:NO atIndex:2]; + [self addMappings:mappings toMenu:statusItemMenu withKeys:NO atIndex:2]; } - (void)mappingDidChange:(NSNotification *)note { - NJMapping *current = note.object; + NJMapping *current = note.userInfo[@"mapping"]; for (NSMenuItem *item in mappingsMenu.itemArray) if (item.representedObject) item.state = item.representedObject == current; @@ -184,7 +191,10 @@ - (NSMenu *)applicationDockMenu:(NSApplication *)sender { while (dockMenu.lastItem.representedObject) [dockMenu removeLastItem]; - [self addMappingsToMenu:dockMenu withKeys:NO atIndex:dockMenu.numberOfItems]; + [self addMappings:self.mappingsController.mappings + toMenu:dockMenu + withKeys:NO + atIndex:dockMenu.numberOfItems]; return dockMenu; } diff --git a/Classes/NJMappingsController.m b/Classes/NJMappingsController.m index 5d86186..ad88d73 100644 --- a/Classes/NJMappingsController.m +++ b/Classes/NJMappingsController.m @@ -51,7 +51,8 @@ [self updateInterfaceForCurrentMapping]; [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingListChanged - object:_mappings]; + object:self + userInfo:@{ @"mappings": _mappings }]; } - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state @@ -104,8 +105,10 @@ _manualMapping = mapping; _currentMapping = mapping; [self updateInterfaceForCurrentMapping]; - [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged - object:_currentMapping]; + [NSNotificationCenter.defaultCenter + postNotificationName:NJEventMappingChanged + object:self + userInfo:@{ @"mapping": _currentMapping }]; } - (IBAction)addPressed:(id)sender { diff --git a/Classes/NJOutputController.m b/Classes/NJOutputController.m index 5d90bee..cf758e6 100644 --- a/Classes/NJOutputController.m +++ b/Classes/NJOutputController.m @@ -280,7 +280,7 @@ } - (void)mappingListDidChange:(NSNotification *)note { - NSArray *mappings = note.object; + NSArray *mappings = note.userInfo[@"mappings"]; NJMapping *current = mappingPopup.selectedItem.representedObject; [mappingPopup.menu removeAllItems]; for (NJMapping *mapping in mappings) { diff --git a/Info.plist b/Info.plist index 9d3612e..ebc0a1d 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 108 + 109 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.30.2