2 // NJMappingMenuController.m
5 // Created by Joe Wreschnig on 3/11/13.
9 #import "NJMappingMenuController.h"
14 #define MAXIMUM_MAPPINGS_IN_MENU 15
16 @implementation NJMappingMenuController
19 if ((self = [super init])) {
20 NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
21 [center addObserver:self
22 selector:@selector(mappingsListDidChange:)
23 name:NJEventMappingListChanged
25 [center addObserver:self
26 selector:@selector(mappingDidChange:)
27 name:NJEventMappingChanged
29 [center addObserver:self
30 selector:@selector(eventTranslationActivated:)
31 name:NJEventTranslationActivated
33 [center addObserver:self
34 selector:@selector(eventTranslationDeactivated:)
35 name:NJEventTranslationDeactivated
42 [NSNotificationCenter.defaultCenter removeObserver:self];
45 - (void)_mappingWasChosen:(NSMenuItem *)sender {
46 NJMapping *mapping = sender.representedObject;
47 [self.delegate mappingWasChosen:mapping];
50 - (void)_mappingListWasChosen:(NSMenuItem *)sender {
51 [self.delegate mappingListShouldOpen];
54 - (void)mappingsListDidChange:(NSNotification *)note {
55 NSArray *mappings = note.userInfo[NJMappingListKey];
56 NJMapping *currentMapping = note.userInfo[NJMappingKey];
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];
65 NSUInteger index = self.firstMappingIndex;
66 for (NJMapping *mapping in mappings) {
67 NSString *keyEquiv = (++added < 10 && self.hasKeyEquivalents)
68 ? @(added).stringValue
70 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
71 action:@selector(_mappingWasChosen:)
72 keyEquivalent:keyEquiv];
73 item.representedObject = mapping;
74 item.state = mapping == currentMapping;
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:NSLocalizedString(@"mapping overflow %lu",
80 @"menu item when mappings list overflows"),
81 mappings.count - MAXIMUM_MAPPINGS_IN_MENU];
82 NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:msg
83 action:@selector(_mappingListWasChosen:)
85 // There must be a represented object here so the item gets
86 // removed correctly when the menus are regenerated.
87 end.representedObject = self.class;
89 [self.menu insertItem:end atIndex:index];
95 - (void)mappingDidChange:(NSNotification *)note {
96 NJMapping *mapping = note.userInfo[NJMappingKey];
97 for (NSMenuItem *item in self.menu.itemArray)
98 if ([item.representedObject isKindOfClass:NJMapping.class])
99 item.state = mapping == item.representedObject;
102 - (void)eventTranslationActivated:(NSNotification *)note {
103 self.eventTranslationToggle.title = NSLocalizedString(@"Disable",
104 @"menu item text to disable event translation");
107 - (void)eventTranslationDeactivated:(NSNotification *)note {
108 self.eventTranslationToggle.title = NSLocalizedString(@"Enable",
109 @"menu item text to enable event translation");