diff --git a/React/CoreModules/RCTDevMenu.h b/React/CoreModules/RCTDevMenu.h index e0324754c8..eedfff1568 100644 --- a/React/CoreModules/RCTDevMenu.h +++ b/React/CoreModules/RCTDevMenu.h @@ -39,6 +39,11 @@ RCT_EXTERN NSString *const RCTShowDevMenuNotification; */ @property (nonatomic, assign) BOOL hotLoadingEnabled DEPRECATED_ATTRIBUTE; +/** + * Whether the hotkeys that toggles the developer menu is enabled. + */ +@property (nonatomic, assign) BOOL hotkeysEnabled; + /** * Presented items in development menu */ diff --git a/React/CoreModules/RCTDevMenu.mm b/React/CoreModules/RCTDevMenu.mm index 6d47652b6a..83ddd83133 100644 --- a/React/CoreModules/RCTDevMenu.mm +++ b/React/CoreModules/RCTDevMenu.mm @@ -123,37 +123,66 @@ RCT_EXPORT_MODULE() object:nil]; _extraMenuItems = [NSMutableArray new]; -#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST - RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; - __weak __typeof(self) weakSelf = self; - - // Toggle debug menu - [commands registerKeyCommandWithInput:@"d" - modifierFlags:UIKeyModifierCommand - action:^(__unused UIKeyCommand *command) { - [weakSelf toggle]; - }]; - - // Toggle element inspector - [commands registerKeyCommandWithInput:@"i" - modifierFlags:UIKeyModifierCommand - action:^(__unused UIKeyCommand *command) { - [(RCTDevSettings *)[weakSelf.moduleRegistry moduleForName:"DevSettings"] - toggleElementInspector]; - }]; - - // Reload in normal mode - [commands registerKeyCommandWithInput:@"n" - modifierFlags:UIKeyModifierCommand - action:^(__unused UIKeyCommand *command) { - [(RCTDevSettings *)[weakSelf.moduleRegistry moduleForName:"DevSettings"] - setIsDebuggingRemotely:NO]; - }]; -#endif + [self registerHotkeys]; } return self; } +- (void)registerHotkeys +{ +#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST + RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; + __weak __typeof(self) weakSelf = self; + + // Toggle debug menu + [commands registerKeyCommandWithInput:@"d" + modifierFlags:UIKeyModifierCommand + action:^(__unused UIKeyCommand *command) { + [weakSelf toggle]; + }]; + + // Toggle element inspector + [commands registerKeyCommandWithInput:@"i" + modifierFlags:UIKeyModifierCommand + action:^(__unused UIKeyCommand *command) { + [(RCTDevSettings *)[weakSelf.moduleRegistry moduleForName:"DevSettings"] + toggleElementInspector]; + }]; + + // Reload in normal mode + [commands registerKeyCommandWithInput:@"n" + modifierFlags:UIKeyModifierCommand + action:^(__unused UIKeyCommand *command) { + [(RCTDevSettings *)[weakSelf.moduleRegistry moduleForName:"DevSettings"] + setIsDebuggingRemotely:NO]; + }]; +#endif +} + +- (void)unregisterHotkeys +{ +#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST + RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; + + [commands unregisterKeyCommandWithInput:@"d" modifierFlags:UIKeyModifierCommand]; + [commands unregisterKeyCommandWithInput:@"i" modifierFlags:UIKeyModifierCommand]; + [commands unregisterKeyCommandWithInput:@"n" modifierFlags:UIKeyModifierCommand]; +#endif +} + +- (BOOL)isHotkeysRegistered +{ +#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST + RCTKeyCommands *commands = [RCTKeyCommands sharedInstance]; + + return [commands isKeyCommandRegisteredForInput:@"d" modifierFlags:UIKeyModifierCommand] && + [commands isKeyCommandRegisteredForInput:@"i" modifierFlags:UIKeyModifierCommand] && + [commands isKeyCommandRegisteredForInput:@"n" modifierFlags:UIKeyModifierCommand]; +#else + return NO; +#endif +} + - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); @@ -481,6 +510,20 @@ RCT_EXPORT_METHOD(setHotLoadingEnabled : (BOOL)enabled) return ((RCTDevSettings *)[_moduleRegistry moduleForName:"DevSettings"]).isHotLoadingEnabled; } +- (void)setHotkeysEnabled:(BOOL)enabled +{ + if (enabled) { + [self registerHotkeys]; + } else { + [self unregisterHotkeys]; + } +} + +- (BOOL)hotkeysEnabled +{ + return [self isHotkeysRegistered]; +} + - (std::shared_ptr)getTurboModule: (const facebook::react::ObjCTurboModule::InitParams &)params {