Просмотр исходного кода

Skip keyboard shortcut override when setting defaults

Maxime Ollivier лет назад: 6
Родитель
Сommit
7377399673

+ 25 - 15
Classes/Manager/FLEXManager+Extensibility.m

@@ -63,7 +63,7 @@
 
 
 - (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description {
 - (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description {
 #if TARGET_OS_SIMULATOR
 #if TARGET_OS_SIMULATOR
-    [FLEXKeyboardShortcutManager.sharedManager registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description];
+    [FLEXKeyboardShortcutManager.sharedManager registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description allowOverride:YES];
 #endif
 #endif
 }
 }
 
 
@@ -81,37 +81,47 @@
 #endif
 #endif
 }
 }
 
 
+
+#pragma mark - Shortcuts Defaults
+
+- (void)registerDefaultSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description {
+#if TARGET_OS_SIMULATOR
+    // Don't allow override to avoid changing keys registered by the app
+    [FLEXKeyboardShortcutManager.sharedManager registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description allowOverride:NO];
+#endif
+}
+
 - (void)registerDefaultSimulatorShortcuts {
 - (void)registerDefaultSimulatorShortcuts {
-    [self registerSimulatorShortcutWithKey:@"f" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"f" modifiers:0 action:^{
         [self toggleExplorer];
         [self toggleExplorer];
     } description:@"Toggle FLEX toolbar"];
     } description:@"Toggle FLEX toolbar"];
     
     
-    [self registerSimulatorShortcutWithKey:@"g" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"g" modifiers:0 action:^{
         [self showExplorerIfNeeded];
         [self showExplorerIfNeeded];
         [self.explorerViewController toggleMenuTool];
         [self.explorerViewController toggleMenuTool];
     } description:@"Toggle FLEX globals menu"];
     } description:@"Toggle FLEX globals menu"];
     
     
-    [self registerSimulatorShortcutWithKey:@"v" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"v" modifiers:0 action:^{
         [self showExplorerIfNeeded];
         [self showExplorerIfNeeded];
         [self.explorerViewController toggleViewsTool];
         [self.explorerViewController toggleViewsTool];
     } description:@"Toggle view hierarchy menu"];
     } description:@"Toggle view hierarchy menu"];
     
     
-    [self registerSimulatorShortcutWithKey:@"s" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"s" modifiers:0 action:^{
         [self showExplorerIfNeeded];
         [self showExplorerIfNeeded];
         [self.explorerViewController toggleSelectTool];
         [self.explorerViewController toggleSelectTool];
     } description:@"Toggle select tool"];
     } description:@"Toggle select tool"];
     
     
-    [self registerSimulatorShortcutWithKey:@"m" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"m" modifiers:0 action:^{
         [self showExplorerIfNeeded];
         [self showExplorerIfNeeded];
         [self.explorerViewController toggleMoveTool];
         [self.explorerViewController toggleMoveTool];
     } description:@"Toggle move tool"];
     } description:@"Toggle move tool"];
     
     
-    [self registerSimulatorShortcutWithKey:@"n" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"n" modifiers:0 action:^{
         [self toggleTopViewControllerOfClass:[FLEXNetworkMITMViewController class]];
         [self toggleTopViewControllerOfClass:[FLEXNetworkMITMViewController class]];
     } description:@"Toggle network history view"];
     } description:@"Toggle network history view"];
     
     
     // 't' is for testing: quickly present an object explorer for debugging
     // 't' is for testing: quickly present an object explorer for debugging
-    [self registerSimulatorShortcutWithKey:@"t" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"t" modifiers:0 action:^{
         [self showExplorerIfNeeded];
         [self showExplorerIfNeeded];
         
         
         [self.explorerViewController toggleToolWithViewControllerProvider:^UINavigationController *{
         [self.explorerViewController toggleToolWithViewControllerProvider:^UINavigationController *{
@@ -121,25 +131,25 @@
         } completion:nil];
         } completion:nil];
     } description:@"Present an object explorer for debugging"];
     } description:@"Present an object explorer for debugging"];
     
     
-    [self registerSimulatorShortcutWithKey:UIKeyInputDownArrow modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:UIKeyInputDownArrow modifiers:0 action:^{
         if (self.isHidden || ![self.explorerViewController handleDownArrowKeyPressed]) {
         if (self.isHidden || ![self.explorerViewController handleDownArrowKeyPressed]) {
             [self tryScrollDown];
             [self tryScrollDown];
         }
         }
     } description:@"Cycle view selection\n\t\tMove view down\n\t\tScroll down"];
     } description:@"Cycle view selection\n\t\tMove view down\n\t\tScroll down"];
     
     
-    [self registerSimulatorShortcutWithKey:UIKeyInputUpArrow modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:UIKeyInputUpArrow modifiers:0 action:^{
         if (self.isHidden || ![self.explorerViewController handleUpArrowKeyPressed]) {
         if (self.isHidden || ![self.explorerViewController handleUpArrowKeyPressed]) {
             [self tryScrollUp];
             [self tryScrollUp];
         }
         }
     } description:@"Cycle view selection\n\t\tMove view up\n\t\tScroll up"];
     } description:@"Cycle view selection\n\t\tMove view up\n\t\tScroll up"];
     
     
-    [self registerSimulatorShortcutWithKey:UIKeyInputRightArrow modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:UIKeyInputRightArrow modifiers:0 action:^{
         if (!self.isHidden) {
         if (!self.isHidden) {
             [self.explorerViewController handleRightArrowKeyPressed];
             [self.explorerViewController handleRightArrowKeyPressed];
         }
         }
     } description:@"Move selected view right"];
     } description:@"Move selected view right"];
     
     
-    [self registerSimulatorShortcutWithKey:UIKeyInputLeftArrow modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:UIKeyInputLeftArrow modifiers:0 action:^{
         if (self.isHidden) {
         if (self.isHidden) {
             [self tryGoBack];
             [self tryGoBack];
         } else {
         } else {
@@ -147,15 +157,15 @@
         }
         }
     } description:@"Move selected view left"];
     } description:@"Move selected view left"];
     
     
-    [self registerSimulatorShortcutWithKey:@"?" modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"?" modifiers:0 action:^{
         [self toggleTopViewControllerOfClass:[FLEXKeyboardHelpViewController class]];
         [self toggleTopViewControllerOfClass:[FLEXKeyboardHelpViewController class]];
     } description:@"Toggle (this) help menu"];
     } description:@"Toggle (this) help menu"];
     
     
-    [self registerSimulatorShortcutWithKey:UIKeyInputEscape modifiers:0 action:^{
+    [self registerDefaultSimulatorShortcutWithKey:UIKeyInputEscape modifiers:0 action:^{
         [[self.topViewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
         [[self.topViewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
     } description:@"End editing text\n\t\tDismiss top view controller"];
     } description:@"End editing text\n\t\tDismiss top view controller"];
     
     
-    [self registerSimulatorShortcutWithKey:@"o" modifiers:UIKeyModifierCommand|UIKeyModifierShift action:^{
+    [self registerDefaultSimulatorShortcutWithKey:@"o" modifiers:UIKeyModifierCommand|UIKeyModifierShift action:^{
         [self toggleTopViewControllerOfClass:[FLEXFileBrowserController class]];
         [self toggleTopViewControllerOfClass:[FLEXFileBrowserController class]];
     } description:@"Toggle file browser menu"];
     } description:@"Toggle file browser menu"];
 }
 }

+ 7 - 1
Classes/Utility/Keyboard/FLEXKeyboardShortcutManager.h

@@ -12,10 +12,16 @@
 
 
 @property (nonatomic, readonly, class) FLEXKeyboardShortcutManager *sharedManager;
 @property (nonatomic, readonly, class) FLEXKeyboardShortcutManager *sharedManager;
 
 
+/// @param key A single character string matching a key on the keyboard
+/// @param modifiers Modifier keys such as shift, command, or alt/option
+/// @param action The block to run on the main thread when the key & modifier combination is recognized.
+/// @param description Shown the the keyboard shortcut help menu, which is accessed via the '?' key.
+/// @param allowOverride Allow registering even if there's an existing action associated with that key/modifier.
 - (void)registerSimulatorShortcutWithKey:(NSString *)key
 - (void)registerSimulatorShortcutWithKey:(NSString *)key
                                modifiers:(UIKeyModifierFlags)modifiers
                                modifiers:(UIKeyModifierFlags)modifiers
                                   action:(dispatch_block_t)action
                                   action:(dispatch_block_t)action
-                             description:(NSString *)description;
+                             description:(NSString *)description
+                           allowOverride:(BOOL)allowOverride;
 
 
 @property (nonatomic, getter=isEnabled) BOOL enabled;
 @property (nonatomic, getter=isEnabled) BOOL enabled;
 @property (nonatomic, readonly) NSString *keyboardShortcutsDescription;
 @property (nonatomic, readonly) NSString *keyboardShortcutsDescription;

+ 7 - 2
Classes/Utility/Keyboard/FLEXKeyboardShortcutManager.m

@@ -212,9 +212,14 @@
 - (void)registerSimulatorShortcutWithKey:(NSString *)key
 - (void)registerSimulatorShortcutWithKey:(NSString *)key
                                modifiers:(UIKeyModifierFlags)modifiers
                                modifiers:(UIKeyModifierFlags)modifiers
                                   action:(dispatch_block_t)action
                                   action:(dispatch_block_t)action
-                             description:(NSString *)description {
+                             description:(NSString *)description
+                           allowOverride:(BOOL)allowOverride {
     FLEXKeyInput *keyInput = [FLEXKeyInput keyInputForKey:key flags:modifiers helpDescription:description];
     FLEXKeyInput *keyInput = [FLEXKeyInput keyInputForKey:key flags:modifiers helpDescription:description];
-    [self.actionsForKeyInputs setObject:action forKey:keyInput];
+    if (!allowOverride && self.actionsForKeyInputs[keyInput] != nil) {
+        return;
+    } else {
+        [self.actionsForKeyInputs setObject:action forKey:keyInput];
+    }
 }
 }
 
 
 static const long kFLEXControlKeyCode = 0xe0;
 static const long kFLEXControlKeyCode = 0xe0;