Improvements to mappings controller. Move array munging into a separate category...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 10 Mar 2013 10:54:36 +0000 (11:54 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 10 Mar 2013 10:54:36 +0000 (11:54 +0100)
Categories/NSMutableArray+MoveObject.h
Categories/NSMutableArray+MoveObject.m
Classes/NJMappingsController.h
Classes/NJMappingsController.m
Classes/NJOutputController.m
Info.plist
Resources/English.lproj/MainMenu.xib

index c1c60d28a466b41615a84baf068405023645b5b1..ce62234b524a5815faf117945314b375b83f1641 100644 (file)
 @interface NSMutableArray (MoveObject)
 
 - (void)moveObjectAtIndex:(NSUInteger)src toIndex:(NSUInteger)dst;
+    // Move the object at index src to (pre-move) index dst. Other
+    // objects shift up or down as necessary to make room, as in
+    // insertObject:atIndex:. Because the object is also removed from
+    // the source index, its resulting index may be one less than dst.
+
+- (BOOL)moveFirstwards:(id)object upTo:(NSUInteger)minIndex;
+- (BOOL)moveLastwards:(id)object upTo:(NSUInteger)maxIndex;
+    // Move an object one step towards the first or last position in
+    // the array, up to a minimum or maximum index. Returns YES if the
+    // array changed; NO if the object was not in the array or was
+    // already at the minimum/maximum index.
+
+- (BOOL)moveFirstwards:(id)object;
+- (BOOL)moveLastwards:(id)object;
+    // Move an object towards the first or last position in the array.
+    // Returns YES if the array changed; NO if the object was not in
+    // the array or if the object was already in the first/last
+    // position.
+
 
 @end
index 8d9a4ee6978e927c5ca9e42375720f7871de4d30..6c7987e252b8c20f176a655f4e8e99e5d705ed96 100644 (file)
     [self insertObject:obj atIndex:dst > src ? dst - 1 : dst];
 }
 
+- (BOOL)moveFirstwards:(id)object upTo:(NSUInteger)minIndex {
+    NSUInteger idx = [self indexOfObject:object];
+    if (idx > minIndex && idx != NSNotFound) {
+        [self exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+- (BOOL)moveLastwards:(id)object upTo:(NSUInteger)maxIndex {
+    maxIndex = MIN(self.count - 1, maxIndex);
+    NSUInteger idx = [self indexOfObject:object];
+    if (idx < maxIndex && idx != NSNotFound) {
+        [self exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+- (BOOL)moveFirstwards:(id)object {
+    return [self moveFirstwards:object upTo:0];
+}
+
+- (BOOL)moveLastwards:(id)object {
+    return [self moveLastwards:object upTo:NSNotFound];
+}
+
+
 @end
index bca6c1f944be154dab4bd92d21ec8dd562895471..dcb44b004152e748ba63a9f7d44dca2dd527f595 100644 (file)
@@ -17,7 +17,6 @@
 {
     IBOutlet NSButton *removeButton;
     IBOutlet NSTableView *tableView;
-    IBOutlet NJOutputController *outputController;
     IBOutlet NSButton *popoverActivate;
     IBOutlet NSPopover *popover;
     IBOutlet NSButton *moveUp;
index 2ee423905cde287893e165ada1e395d00ce25312..de66fb888f5199bdd348b40289ffa6412da2558a 100644 (file)
 #import "NJMapping.h"
 #import "NJMappingsController.h"
 #import "NJOutput.h"
-#import "NJOutputController.h"
 #import "NJEvents.h"
 
 #define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
 
 @implementation NJMappingsController {
     NSMutableArray *_mappings;
-    NJMapping *manualMapping;
-    NSString *draggingName;
+    NJMapping *_manualMapping;
 }
 
 - (id)init {
     if ((self = [super init])) {
         _mappings = [[NSMutableArray alloc] init];
         _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
-        manualMapping = _currentMapping;
+        _manualMapping = _currentMapping;
         [_mappings addObject:_currentMapping];
     }
     return self;
@@ -50,7 +48,6 @@
 - (void)mappingsChanged {
     [self save];
     [tableView reloadData];
-    popoverActivate.title = _currentMapping.name;
     [self updateInterfaceForCurrentMapping];
     [NSNotificationCenter.defaultCenter
         postNotificationName:NJEventMappingListChanged
 }
 
 - (void)activateMappingForProcess:(NSRunningApplication *)app {
-    NJMapping *oldMapping = manualMapping;
+    NJMapping *oldMapping = _manualMapping;
     NSArray *names = app.possibleMappingNames;
     BOOL found = NO;
     for (NSString *name in names) {
         NJMapping *mapping = self[name];
         if (mapping) {
-            [self activateMapping:self[name]];
+            [self activateMapping:mapping];
             found = YES;
             break;
         }
             [self mappingsChanged];
         }
     }
-    manualMapping = oldMapping;
+    _manualMapping = oldMapping;
 }
 
 - (void)updateInterfaceForCurrentMapping {
     NSUInteger selected = [_mappings indexOfObject:_currentMapping];
-    [removeButton setEnabled:selected != 0];
-    [moveDown setEnabled:selected && selected != _mappings.count - 1];
-    [moveUp setEnabled:selected > 1];
+    removeButton.enabled = selected != 0;
+    moveUp.enabled = selected > 1;
+    moveDown.enabled = selected && selected != _mappings.count - 1;
     popoverActivate.title = _currentMapping.name;
     [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
     [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
 
 - (void)activateMapping:(NJMapping *)mapping {
     if (!mapping)
-        mapping = manualMapping;
+        mapping = _manualMapping;
     if (mapping == _currentMapping)
         return;
     NSLog(@"Switching to mapping %@.", mapping.name);
-    manualMapping = mapping;
+    _manualMapping = mapping;
     _currentMapping = mapping;
     [self updateInterfaceForCurrentMapping];
-    [outputController loadCurrent];
     [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
                                                       object:_currentMapping];
 }
     }
 }
 
-- (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
-    NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
-    [stream open];
-    NSDictionary *serialization = !*error
-        ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
-        : nil;
-    [stream close];
-    
-    if (!([serialization isKindOfClass:NSDictionary.class]
-          && [serialization[@"name"] isKindOfClass:NSString.class]
-          && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
-        *error = [NSError errorWithDomain:@"Enjoyable"
-                                     code:0
-                              description:@"This isn't a valid mapping file."];
-        return nil;
-    }
-
-    NSDictionary *entries = serialization[@"entries"];
-    NJMapping *mapping = [[NJMapping alloc] initWithName:serialization[@"name"]];
-    for (id key in entries) {
-        NSDictionary *value = entries[key];
-        if ([key isKindOfClass:NSString.class]) {
-            NJOutput *output = [NJOutput outputDeserialize:value
-                                              withMappings:_mappings];
-            if (output)
-                mapping.entries[key] = output;
-        }
-    }
-    return mapping;
-}
-
 - (void)addMappingWithContentsOfURL:(NSURL *)url {
     NSWindow *window = popoverActivate.window;
     NSError *error;
 }
 
 - (IBAction)moveUpPressed:(id)sender {
-    NSUInteger idx = [_mappings indexOfObject:_currentMapping];
-    if (idx > 1 && idx != NSNotFound) {
-        [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
+    if ([_mappings moveFirstwards:_currentMapping upTo:1])
         [self mappingsChanged];
-    }
 }
 
 - (IBAction)moveDownPressed:(id)sender {
-    NSUInteger idx = [_mappings indexOfObject:_currentMapping];
-    if (idx < _mappings.count - 1) {
-        [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
+    if ([_mappings moveLastwards:_currentMapping])
         [self mappingsChanged];
-    }
 }
 
 - (BOOL)tableView:(NSTableView *)tableView_
index cbb48f7e1cab1a145ef98825de82ecd5ec470cbc..5d90bee67a896d14c82a05c80671e1b06a10e859 100644 (file)
             selector:@selector(mappingListDidChange:)
             name:NJEventMappingListChanged
             object:nil];
+        [NSNotificationCenter.defaultCenter
+             addObserver:self
+             selector:@selector(mappingDidChange:)
+             name:NJEventMappingChanged
+             object:nil];
     }
     return self;
 }
     [mappingPopup selectItemWithRepresentedObject:current];
 }
 
+- (void)mappingDidChange:(NSNotification *)note {
+    [self loadCurrent];
+}
+
 @end
index 76b3f48595a99d6b54ac97a317fb982ef168b939..2b4ff4948598fdeb76ca93837048c11fa44a96c9 100644 (file)
@@ -46,7 +46,7 @@
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
-       <string>84</string>
+       <string>90</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.utilities</string>
        <key>NSHumanReadableCopyright</key>
index d6f1307775d9af30327ed861c59e4c85508379ed..c8a38ed1f29943168c1cfb658fb95a46b9c02221 100644 (file)
                                <nil key="NSUserInterfaceItemIdentifier"/>
                                <string key="NSWindowContentMinSize">{664, 323}</string>
                                <object class="NSView" key="NSWindowView" id="177223957">
-                                       <reference key="NSNextResponder"/>
+                                       <nil key="NSNextResponder"/>
                                        <int key="NSvFlags">256</int>
                                        <array class="NSMutableArray" key="NSSubviews">
                                                <object class="NSCustomView" id="734312853">
                                                                        <int key="NSvFlags">274</int>
                                                                        <string key="NSFrame">{{20, 20}, {194, 283}}</string>
                                                                        <reference key="NSSuperview" ref="734312853"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="471332453"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:22</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -523,8 +522,7 @@ aW5nLg</string>
                                                                                                        <array class="NSMutableArray" key="NSSubviews"/>
                                                                                                        <string key="NSFrameSize">{232, 321}</string>
                                                                                                        <reference key="NSSuperview" ref="698362889"/>
-                                                                                                       <reference key="NSWindow"/>
-                                                                                                       <reference key="NSNextKeyView" ref="1036252745"/>
+                                                                                                       <reference key="NSNextKeyView" ref="892486973"/>
                                                                                                        <bool key="NSEnabled">YES</bool>
                                                                                                        <bool key="NSAllowsLogicalLayoutDirection">NO</bool>
                                                                                                        <bool key="NSControlAllowsExpansionToolTips">YES</bool>
@@ -619,7 +617,6 @@ aW5nLg</string>
                                                                                        </array>
                                                                                        <string key="NSFrame">{{1, 1}, {232, 321}}</string>
                                                                                        <reference key="NSSuperview" ref="364857164"/>
-                                                                                       <reference key="NSWindow"/>
                                                                                        <reference key="NSNextKeyView" ref="365506042"/>
                                                                                        <reference key="NSDocView" ref="365506042"/>
                                                                                        <reference key="NSBGColor" ref="834857663"/>
@@ -630,7 +627,6 @@ aW5nLg</string>
                                                                                        <int key="NSvFlags">-2147483392</int>
                                                                                        <string key="NSFrame">{{1, 1}, {8, 298}}</string>
                                                                                        <reference key="NSSuperview" ref="364857164"/>
-                                                                                       <reference key="NSWindow"/>
                                                                                        <reference key="NSNextKeyView" ref="12898323"/>
                                                                                        <bool key="NSAllowsLogicalLayoutDirection">NO</bool>
                                                                                        <reference key="NSTarget" ref="364857164"/>
@@ -642,7 +638,6 @@ aW5nLg</string>
                                                                                        <int key="NSvFlags">-2147483392</int>
                                                                                        <string key="NSFrame">{{-100, -100}, {473, 15}}</string>
                                                                                        <reference key="NSSuperview" ref="364857164"/>
-                                                                                       <reference key="NSWindow"/>
                                                                                        <reference key="NSNextKeyView" ref="698362889"/>
                                                                                        <bool key="NSAllowsLogicalLayoutDirection">NO</bool>
                                                                                        <int key="NSsFlags">1</int>
@@ -653,8 +648,7 @@ aW5nLg</string>
                                                                        </array>
                                                                        <string key="NSFrameSize">{234, 323}</string>
                                                                        <reference key="NSSuperview" ref="734312853"/>
-                                                                       <reference key="NSWindow"/>
-                                                                       <reference key="NSNextKeyView" ref="892486973"/>
+                                                                       <reference key="NSNextKeyView" ref="698362889"/>
                                                                        <int key="NSsFlags">150034</int>
                                                                        <reference key="NSVScroller" ref="1036252745"/>
                                                                        <reference key="NSHScroller" ref="892486973"/>
@@ -667,7 +661,6 @@ aW5nLg</string>
                                                        </array>
                                                        <string key="NSFrameSize">{234, 323}</string>
                                                        <reference key="NSSuperview" ref="177223957"/>
-                                                       <reference key="NSWindow"/>
                                                        <reference key="NSNextKeyView" ref="364857164"/>
                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                        <string key="NSClassName">NSView</string>
@@ -681,7 +674,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{189, 117}, {224, 20}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="385416822"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -723,8 +715,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">268</int>
                                                                        <string key="NSFrame">{{343, 31}, {70, 18}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
-                                                                       <reference key="NSNextKeyView"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
                                                                        <object class="NSButtonCell" key="NSCell" id="868379451">
@@ -755,7 +745,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{189, 33}, {150, 20}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="792189805"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -800,7 +789,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{191, 24}, {146, 16}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="20704797"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -826,7 +814,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{189, 70}, {224, 24}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="921829691"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -880,7 +867,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{191, 108}, {220, 16}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="125828224"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <bool key="NSEnabled">YES</bool>
@@ -906,7 +892,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{191, 196}, {220, 23}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="194275224"/>
                                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                                        <string key="NSClassName">NJKeyInputField</string>
@@ -916,7 +901,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">265</int>
                                                                        <string key="NSFrame">{{188, 153}, {226, 26}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="875916470"/>
                                                                        <bool key="NSEnabled">YES</bool>
                                                                        <object class="NSPopUpButtonCell" key="NSCell" id="74311158">
@@ -949,7 +933,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">268</int>
                                                                        <string key="NSFrame">{{24, 20}, {163, 250}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="57697638"/>
                                                                        <bool key="NSAllowsLogicalLayoutDirection">NO</bool>
                                                                        <int key="NSNumRows">6</int>
@@ -1080,7 +1063,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">266</int>
                                                                        <string key="NSFrame">{{9, 286}, {369, 17}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="497528019"/>
                                                                        <bool key="NSEnabled">YES</bool>
                                                                        <object class="NSTextFieldCell" key="NSCell" id="853503577">
@@ -1104,7 +1086,6 @@ aW5nLg</string>
                                                                        <int key="NSvFlags">10</int>
                                                                        <string key="NSFrame">{{12, 276}, {363, 5}}</string>
                                                                        <reference key="NSSuperview" ref="471332453"/>
-                                                                       <reference key="NSWindow"/>
                                                                        <reference key="NSNextKeyView" ref="120408205"/>
                                                                        <string key="NSOffsets">{0, 0}</string>
                                                                        <object class="NSTextFieldCell" key="NSTitleCell">
@@ -1131,15 +1112,12 @@ aW5nLg</string>
                                                        </array>
                                                        <string key="NSFrame">{{233, 0}, {431, 323}}</string>
                                                        <reference key="NSSuperview" ref="177223957"/>
-                                                       <reference key="NSWindow"/>
                                                        <reference key="NSNextKeyView" ref="1016088174"/>
                                                        <string key="NSReuseIdentifierKey">_NS:9</string>
                                                        <string key="NSClassName">NSView</string>
                                                </object>
                                        </array>
                                        <string key="NSFrameSize">{664, 323}</string>
-                                       <reference key="NSSuperview"/>
-                                       <reference key="NSWindow"/>
                                        <reference key="NSNextKeyView" ref="734312853"/>
                                </object>
                                <string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>
@@ -1743,14 +1721,6 @@ aW5nLg</string>
                                        </object>
                                        <int key="connectionID">816</int>
                                </object>
-                               <object class="IBConnectionRecord">
-                                       <object class="IBOutletConnection" key="connection">
-                                               <string key="label">outputController</string>
-                                               <reference key="source" ref="468285243"/>
-                                               <reference key="destination" ref="801536542"/>
-                                       </object>
-                                       <int key="connectionID">827</int>
-                               </object>
                                <object class="IBConnectionRecord">
                                        <object class="IBActionConnection" key="connection">
                                                <string key="label">mappingPressed:</string>
@@ -3193,7 +3163,6 @@ aW5nLg</string>
                                        <dictionary class="NSMutableDictionary" key="outlets">
                                                <string key="moveDown">NSButton</string>
                                                <string key="moveUp">NSButton</string>
-                                               <string key="outputController">NJOutputController</string>
                                                <string key="popover">NSPopover</string>
                                                <string key="popoverActivate">NSButton</string>
                                                <string key="removeButton">NSButton</string>
@@ -3208,10 +3177,6 @@ aW5nLg</string>
                                                        <string key="name">moveUp</string>
                                                        <string key="candidateClassName">NSButton</string>
                                                </object>
-                                               <object class="IBToOneOutletInfo" key="outputController">
-                                                       <string key="name">outputController</string>
-                                                       <string key="candidateClassName">NJOutputController</string>
-                                               </object>
                                                <object class="IBToOneOutletInfo" key="popover">
                                                        <string key="name">popover</string>
                                                        <string key="candidateClassName">NSPopover</string>