Push notifications out with more idiomatic semantics - controller as object, paramete...
[enjoyable.git] / Classes / EnjoyableApplicationDelegate.m
index aaa9f00..c66cf6b 100644 (file)
@@ -55,7 +55,6 @@
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
-    [self.inputController setup];
     [window makeKeyAndOrderFront:nil];
 }
 
     [self.mappingsController mappingPressed:sender];
 }
 
-- (void)addMappingsToMenu:(NSMenu *)menu withKeys:(BOOL)withKeys atIndex:(NSInteger)index {
-    static const NSUInteger MAXIMUM_ITEMS = 5;
+- (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:)
         item.state = mapping == self.mappingsController.currentMapping;
         [menu insertItem:item atIndex:index++];
         if (added == MAXIMUM_ITEMS && self.mappingsController.mappings.count > MAXIMUM_ITEMS + 1) {
-            NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:@"…"
+            NSString *msg = [NSString stringWithFormat:@"(and %lu more…)",
+                             self.mappingsController.mappings.count - MAXIMUM_ITEMS];
+            NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:msg
                                                          action:@selector(restoreWindowAndShowMappings:)
                                                   keyEquivalent:@""];
+            // There must be a represented object here so the item gets
+            // removed correctly when the menus are regenerated.
+            end.representedObject = mappings;
             end.target = self;
             [menu insertItem:end atIndex:index++];
             break;
 }
 
 - (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;
 - (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;
 }