Localization support. Change many names in NJKeyInputField to standard keyboard short...
[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[NJMappingListKey];
56 NJMapping *currentMapping = note.userInfo[NJMappingKey];
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: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:)
84 keyEquivalent:@""];
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;
88 end.target = self;
89 [self.menu insertItem:end atIndex:index];
90 break;
91 }
92 }
93 }
94
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;
100 }
101
102 - (void)eventTranslationActivated:(NSNotification *)note {
103 self.eventTranslationToggle.title = NSLocalizedString(@"Disable",
104 @"menu item text to disable event translation");
105 }
106
107 - (void)eventTranslationDeactivated:(NSNotification *)note {
108 self.eventTranslationToggle.title = NSLocalizedString(@"Enable",
109 @"menu item text to enable event translation");
110 }
111
112 @end