Move simulation toggle out of device controller.
[enjoyable.git] / Classes / EnjoyableApplicationDelegate.m
index ca641f7..41d068a 100644 (file)
@@ -49,7 +49,7 @@
     statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:36];
     statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
     statusItem.highlightMode = YES;
-    statusItem.menu = statusItemMenu;
+    statusItem.menu = self.statusItemMenu;
     statusItem.target = self;
 }
 
@@ -58,7 +58,7 @@
         && NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume)
         [self transformIntoElement:nil];
     else
-        [window makeKeyAndOrderFront:nil];
+        [self.window makeKeyAndOrderFront:nil];
 }
 
 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
@@ -71,7 +71,7 @@
     ProcessSerialNumber psn = { 0, kCurrentProcess };
     TransformProcessType(&psn, kProcessTransformToForegroundApplication);
     [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
-    [window makeKeyAndOrderFront:sender];
+    [self.window makeKeyAndOrderFront:sender];
     [NSObject cancelPreviousPerformRequestsWithTarget:self
                                              selector:@selector(transformIntoElement:)
                                                object:self];
@@ -79,7 +79,7 @@
 }
 
 - (void)applicationWillBecomeActive:(NSNotification *)notification {
-    if (window.isVisible)
+    if (self.window.isVisible)
         [self restoreToForeground:notification];
 }
 
 }
 
 - (void)eventSimulationStarted:(NSNotification *)note {
+    self.simulatingEventsButton.state = NSOnState;
     statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
     [NSWorkspace.sharedWorkspace.notificationCenter
         addObserver:self
 }
 
 - (void)eventSimulationStopped:(NSNotification *)note {
+    self.simulatingEventsButton.state = NSOffState;
     statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
     [NSWorkspace.sharedWorkspace.notificationCenter
         removeObserver:self
     NSUInteger idx = [note.userInfo[NJMappingIndexKey] intValue];
     [self.mvc changedActiveMappingToIndex:idx];
 
-    if (!window.isVisible)
+    if (!self.window.isVisible)
         for (int i = 0; i < 4; ++i)
             [self performSelector:@selector(flashStatusItem)
                        withObject:self
 }
 
 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
-    return dockMenu;
+    return self.dockMenu;
 }
 
 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
         [self.mvc endUpdates];
         [self.mappingsController activateMapping:mapping];
     } else {
-        [window presentError:error
-              modalForWindow:window
-                    delegate:nil
-          didPresentSelector:nil
-                 contextInfo:nil];
+        [self.window presentError:error
+                   modalForWindow:self.window
+                         delegate:nil
+               didPresentSelector:nil
+                      contextInfo:nil];
     }
     return !!mapping;
 }
                        returnCode:(int)returnCode
                       contextInfo:(void *)contextInfo {
     [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"explained login items"];
-    [window performClose:sheet];
+    [self.window performClose:sheet];
 }
 
 - (BOOL)windowShouldClose:(NSWindow *)sender {
-    if (sender != window
+    if (sender != self.window
         || NSRunningApplication.currentApplication.isLoginItem
         || [NSUserDefaults.standardUserDefaults boolForKey:@"explained login items"])
         return YES;
         NSLocalizedString(@"login items prompt", @"alert prompt for adding to login items"),
         NSLocalizedString(@"login items add button", @"button to add to login items"),
         NSLocalizedString(@"login items don't add button", @"button to not add to login items"),
-        nil, window, self,
+        nil, self.window, self,
         @selector(loginItemPromptDidEnd:returnCode:contextInfo:),
         @selector(loginItemPromptDidDismiss:returnCode:contextInfo:),
         NULL,
 - (void)importMappingClicked:(id)sender {
     NSOpenPanel *panel = [NSOpenPanel openPanel];
     panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
-    [panel beginSheetModalForWindow:window
+    [panel beginSheetModalForWindow:self.window
                   completionHandler:^(NSInteger result) {
                       if (result != NSFileHandlingPanelOKButton)
                           return;
                       } else if (mapping) {
                           [self.mappingsController addMapping:mapping];
                       } else {
-                          [window presentError:error
-                                modalForWindow:window
-                                      delegate:nil
-                            didPresentSelector:nil
-                                   contextInfo:nil];
+                          [self.window presentError:error
+                                     modalForWindow:self.window
+                                           delegate:nil
+                                 didPresentSelector:nil
+                                        contextInfo:nil];
                       }
                   }];
     
     panel.allowedFileTypes = @[ @"enjoyable" ];
     NJMapping *mapping = self.mappingsController.currentMapping;
     panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
-    [panel beginSheetModalForWindow:window
+    [panel beginSheetModalForWindow:self.window
                   completionHandler:^(NSInteger result) {
                       if (result != NSFileHandlingPanelOKButton)
                           return;
                       [panel close];
                       NSError *error;
                       if (![mapping writeToURL:panel.URL error:&error]) {
-                          [window presentError:error
-                                modalForWindow:window
-                                      delegate:nil
-                            didPresentSelector:nil
-                                   contextInfo:nil];
+                          [self.window presentError:error
+                                     modalForWindow:self.window
+                                           delegate:nil
+                                 didPresentSelector:nil
+                                        contextInfo:nil];
                       }
                   }];
 }
     [conflictAlert addButtonWithTitle:NSLocalizedString(@"import and merge", @"button to merge imported mappings")];
     [conflictAlert addButtonWithTitle:NSLocalizedString(@"cancel import", @"button to cancel import")];
     [conflictAlert addButtonWithTitle:NSLocalizedString(@"import new mapping", @"button to import as new mapping")];
-    [conflictAlert beginSheetModalForWindow:window
+    [conflictAlert beginSheetModalForWindow:self.window
                               modalDelegate:self
                              didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:)
                                 contextInfo:(void *)CFBridgingRetain(@{ @"index": @(idx),
     // Since the error shows the window, it can trigger another attempt
     // to re-open the HID manager, which will also probably fail and error,
     // so don't bother repeating ourselves.
-    if (!window.attachedSheet) {
+    if (!self.window.attachedSheet) {
         [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
-        [window makeKeyAndOrderFront:nil];
-        [window presentError:error
-              modalForWindow:window
-                    delegate:nil
-          didPresentSelector:nil
-                 contextInfo:nil];
+        [self.window makeKeyAndOrderFront:nil];
+        [self.window presentError:error
+                   modalForWindow:self.window
+                         delegate:nil
+               didPresentSelector:nil
+                      contextInfo:nil];
     }
 }
 
     return self.deviceController[idx];
 }
 
+- (IBAction)simulatingEventsChanged:(NSButton *)sender {
+    self.deviceController.simulatingEvents = sender.state == NSOnState;
+}
+
 @end