Generalize and share mapping menu (main, status, dock) behavior.
[enjoyable.git] / Classes / NJMappingMenuController.m
1 //
2 // NJMappingMenuController.m
3 // Enjoyable
4 //
5 // Created by Joe Wreschnig on 3/11/13.
6 //
7 //
8
9 #import "NJMappingMenuController.h"
10
11 #import "NJEvents.h"
12 #import "NJMapping.h"
13
14 #define MAXIMUM_MAPPINGS_IN_MENU 15
15
16 @implementation NJMappingMenuController
17
18 - (id)init {
19 if ((self = [super init])) {
20 NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
21 [center addObserver:self
22 selector:@selector(mappingsListDidChange:)
23 name:NJEventMappingListChanged
24 object:nil];
25 [center addObserver:self
26 selector:@selector(mappingDidChange:)
27 name:NJEventMappingChanged
28 object:nil];
29 [center addObserver:self
30 selector:@selector(eventTranslationActivated:)
31 name:NJEventTranslationActivated
32 object:nil];
33 [center addObserver:self
34 selector:@selector(eventTranslationDeactivated:)
35 name:NJEventTranslationDeactivated
36 object:nil];
37 }
38 return self;
39 }
40
41 - (void)dealloc {
42 [NSNotificationCenter.defaultCenter removeObserver:self];
43 }
44
45 - (void)_mappingWasChosen:(NSMenuItem *)sender {
46 NJMapping *mapping = sender.representedObject;
47 [self.delegate mappingWasChosen:mapping];
48 }
49
50 - (void)_mappingListWasChosen:(NSMenuItem *)sender {
51 [self.delegate mappingListShouldOpen];
52 }
53
54 - (void)mappingsListDidChange:(NSNotification *)note {
55 NSArray *mappings = note.userInfo[@"mappings"];
56 NJMapping *currentMapping = note.userInfo[@"mapping"];
57 NSMenuItem *toRemove;
58 while (self.menu.numberOfItems > self.firstMappingIndex
59 && (toRemove = [self.menu itemAtIndex:self.firstMappingIndex])
60 && ([toRemove.representedObject isKindOfClass:NJMapping.class]
61 || toRemove.representedObject == self.class))
62 [self.menu removeItemAtIndex:self.firstMappingIndex];
63
64 int added = 0;
65 NSUInteger index = self.firstMappingIndex;
66 for (NJMapping *mapping in mappings) {
67 NSString *keyEquiv = (++added < 10 && self.hasKeyEquivalents)
68 ? @(added).stringValue
69 : @"";
70 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
71 action:@selector(_mappingWasChosen:)
72 keyEquivalent:keyEquiv];
73 item.representedObject = mapping;
74 item.state = mapping == currentMapping;
75 item.target = self;
76 [self.menu insertItem:item atIndex:index++];
77 if (added == MAXIMUM_MAPPINGS_IN_MENU
78 && mappings.count > MAXIMUM_MAPPINGS_IN_MENU + 1) {
79 NSString *msg = [NSString stringWithFormat:@"(and %lu moreā€¦)",
80 mappings.count - MAXIMUM_MAPPINGS_IN_MENU];
81 NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:msg
82 action:@selector(_mappingListWasChosen:)
83 keyEquivalent:@""];
84 // There must be a represented object here so the item gets
85 // removed correctly when the menus are regenerated.
86 end.representedObject = self.class;
87 end.target = self;
88 [self.menu insertItem:end atIndex:index];
89 break;
90 }
91 }
92 }
93
94 - (void)mappingDidChange:(NSNotification *)note {
95 NJMapping *mapping = note.userInfo[@"mapping"];
96 for (NSMenuItem *item in self.menu.itemArray)
97 if ([item.representedObject isKindOfClass:NJMapping.class])
98 item.state = mapping == item.representedObject;
99 }
100
101 - (void)eventTranslationActivated:(NSNotification *)note {
102 self.eventTranslationToggle.title = @"Disable";
103 }
104
105 - (void)eventTranslationDeactivated:(NSNotification *)note {
106 self.eventTranslationToggle.title = @"Enable";
107 }
108
109
110
111 @end