2 // NJDeviceViewController.m
5 // Created by Joe Wreschnig on 3/16/13.
9 #import "NJDeviceViewController.h"
11 #import "NJInputPathElement.h"
13 @implementation NJDeviceViewController {
14 NSMutableArray *_devices;
15 NSMutableArray *_expanded;
19 if ((self = [super init])) {
20 NSArray *expanded = [NSUserDefaults.standardUserDefaults objectForKey:@"expanded rows"];
21 if (![expanded isKindOfClass:NSArray.class])
23 _expanded = [[NSMutableArray alloc] initWithCapacity:MAX(16, _expanded.count)];
24 [_expanded addObjectsFromArray:expanded];
25 _devices = [[NSMutableArray alloc] initWithCapacity:16];
30 - (void)expandRecursive:(NJInputPathElement *)pathElement {
32 [self expandRecursive:pathElement.parent];
33 [self.inputsTree expandItem:pathElement];
37 - (id)elementForUID:(NSString *)uid {
38 for (NJInputPathElement *dev in _devices) {
39 id item = [dev elementForUID:uid];
46 - (void)expandRecursiveByUID:(NSString *)uid {
47 [self expandRecursive:[self elementForUID:uid]];
51 for (NSString *uid in [_expanded copy])
52 [self expandRecursiveByUID:uid];
53 if (self.inputsTree.selectedRow == -1) {
54 NSString *selectedUid = [NSUserDefaults.standardUserDefaults objectForKey:@"selected input"];
55 id item = [self elementForUID:selectedUid];
56 [self.inputsTree selectItem:item];
60 - (void)setDevices:(NSArray *)devices {
61 _devices = [devices mutableCopy];
62 id item = self.inputsTree.selectedItem;
63 [self.inputsTree selectItem:nil];
64 [self.inputsTree reloadData];
66 [self.inputsTree selectItem:item];
67 self.noDevicesNotice.hidden = self.devices.count || !self.hidStoppedNotice.isHidden;
70 - (void)addedDevice:(NJDevice *)device atIndex:(NSUInteger)idx {
71 [_devices insertObject:device atIndex:idx];
72 [self.inputsTree beginUpdates];
73 [self.inputsTree insertItemsAtIndexes:[[NSIndexSet alloc] initWithIndex:idx]
75 withAnimation:NSTableViewAnimationSlideLeft];
77 [self.inputsTree endUpdates];
78 self.noDevicesNotice.hidden = YES;
81 - (void)removedDevice:(NJDevice *)device atIndex:(NSUInteger)idx {
82 [_devices removeObjectAtIndex:idx];
83 [self.inputsTree beginUpdates];
84 [self.inputsTree removeItemsAtIndexes:[[NSIndexSet alloc] initWithIndex:idx]
86 withAnimation:NSTableViewAnimationSlideLeft];
87 [self.inputsTree endUpdates];
88 self.noDevicesNotice.hidden = self.devices.count || !self.hidStoppedNotice.isHidden;
92 self.noDevicesNotice.hidden = !!self.devices.count;
93 self.hidStoppedNotice.hidden = YES;
97 self.hidStoppedNotice.hidden = NO;
101 - (void)expandAndSelectItem:(NJInputPathElement *)item {
102 [self expandRecursive:item];
103 NSInteger row = [self.inputsTree rowForItem:item];
105 [self.inputsTree selectRowIndexes:[[NSIndexSet alloc] initWithIndex:row]
106 byExtendingSelection:NO];
107 [self.inputsTree scrollRowToVisible:row];
111 - (NSInteger)outlineView:(NSOutlineView *)outlineView
112 numberOfChildrenOfItem:(NJInputPathElement *)item {
113 return item ? item.children.count : _devices.count;
116 - (BOOL)outlineView:(NSOutlineView *)outlineView
117 isItemExpandable:(NJInputPathElement *)item {
118 return item ? [[item children] count] > 0: YES;
121 - (id)outlineView:(NSOutlineView *)outlineView
122 child:(NSInteger)index
123 ofItem:(NJInputPathElement *)item {
124 return item ? item.children[index] : _devices[index];
127 - (id)outlineView:(NSOutlineView *)outlineView
128 objectValueForTableColumn:(NSTableColumn *)tableColumn
129 byItem:(NJInputPathElement *)item {
130 return item ? item.name : @"root";
133 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
134 NSOutlineView *outlineView = notification.object;
135 NJInputPathElement *item = outlineView.selectedItem;
137 [NSUserDefaults.standardUserDefaults setObject:item.uid
138 forKey:@"selected input"];
140 [self.delegate deviceViewController:self
141 didSelectHandler:item];
142 else if (!item.parent)
143 [self.delegate deviceViewController:self
144 didSelectDevice:item];
146 [self.delegate deviceViewController:self
147 didSelectBranch:item];
149 [self.delegate deviceViewControllerDidSelectNothing:self];
153 - (void)outlineViewItemDidExpand:(NSNotification *)notification {
154 NSString *uid = [notification.userInfo[@"NSObject"] uid];
155 if (![_expanded containsObject:uid])
156 [_expanded addObject:uid];
160 - (void)outlineViewItemDidCollapse:(NSNotification *)notification {
161 [_expanded removeObject:[notification.userInfo[@"NSObject"] uid]];
164 - (BOOL)outlineView:(NSOutlineView *)outlineView
165 isGroupItem:(NJInputPathElement *)item {
169 - (BOOL)outlineView:(NSOutlineView *)outlineView
170 shouldSelectItem:(NJInputPathElement *)item {
171 return ![self outlineView:outlineView isGroupItem:item];
174 - (NJInputPathElement *)selectedHandler {
175 NJInputPathElement *element = self.inputsTree.selectedItem;
176 return element.children ? nil : element;