From bdcc33dff21864faf8b054f78749dcb668651f60 Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Tue, 9 Feb 2016 16:55:02 -0500 Subject: [PATCH] Fixes #160 - Adds keyboard shortcuts to open the dev options menu. --- .../DevSupport/DevSupportManager.cs | 25 ++++++---- .../DevSupport/DisabledDevSupportManager.cs | 8 +++ .../DevSupport/IDevSupportManager.cs | 11 +++++ ReactWindows/ReactNative/ReactPage.cs | 49 +++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/ReactWindows/ReactNative/DevSupport/DevSupportManager.cs b/ReactWindows/ReactNative/DevSupport/DevSupportManager.cs index f03d4ef33e6..f2487cb7760 100644 --- a/ReactWindows/ReactNative/DevSupport/DevSupportManager.cs +++ b/ReactWindows/ReactNative/DevSupport/DevSupportManager.cs @@ -14,11 +14,11 @@ class DevSupportManager : IDevSupportManager { private const int NativeErrorCookie = -1; + private readonly ShakeAccelerometer _accelerometer = ShakeAccelerometer.GetDefault(); + private readonly IReactInstanceDevCommandsHandler _reactInstanceCommandsHandler; private readonly string _jsBundleFile; private readonly string _jsAppBundleName; - - private readonly ShakeAccelerometer _accelerometer; private RedBoxDialog _redBoxDialog; private Action _dismissRedBoxDialog; @@ -34,14 +34,7 @@ public DevSupportManager( _jsBundleFile = jsBundleFile; _jsAppBundleName = jsAppBundleName; - _accelerometer = ShakeAccelerometer.GetDefault(); - if (_accelerometer != null) - { - _accelerometer.Shaken += (sender, args) => - { - ShowDevOptionsDialog(); - }; - } + RegisterDevOptionsMenuTriggers(); } public bool IsEnabled { get; set; } = true; @@ -217,6 +210,18 @@ private Task ReloadJavaScriptFromFileAsync(CancellationToken token) return Task.FromResult(true); } + private void RegisterDevOptionsMenuTriggers() + { + if (_accelerometer != null) + { + _accelerometer.Shaken += (sender, args) => + { + ShowDevOptionsDialog(); + }; + } + } + + class DevOptionHandler { private readonly Action _onSelect; diff --git a/ReactWindows/ReactNative/DevSupport/DisabledDevSupportManager.cs b/ReactWindows/ReactNative/DevSupport/DisabledDevSupportManager.cs index 3f4c1a4d276..3086e82d923 100644 --- a/ReactWindows/ReactNative/DevSupport/DisabledDevSupportManager.cs +++ b/ReactWindows/ReactNative/DevSupport/DisabledDevSupportManager.cs @@ -39,6 +39,14 @@ public void HandleException(Exception exception) { } + public void HandleReloadJavaScript() + { + } + + public void ShowDevOptionsDialog() + { + } + public void ShowNewJavaScriptError(string title, JArray details, int exceptionId) { } diff --git a/ReactWindows/ReactNative/DevSupport/IDevSupportManager.cs b/ReactWindows/ReactNative/DevSupport/IDevSupportManager.cs index 6b9d4bdd74a..47e262b9a91 100644 --- a/ReactWindows/ReactNative/DevSupport/IDevSupportManager.cs +++ b/ReactWindows/ReactNative/DevSupport/IDevSupportManager.cs @@ -36,6 +36,17 @@ public interface IDevSupportManager /// The exception. void HandleException(Exception exception); + /// + /// Handles reloading the JavaScript bundle. + /// + void HandleReloadJavaScript(); + + + /// + /// Show the developer options dialog. + /// + void ShowDevOptionsDialog(); + /// /// Display a JavaScript error. /// diff --git a/ReactWindows/ReactNative/ReactPage.cs b/ReactWindows/ReactNative/ReactPage.cs index bdb20fb9d7d..5bd7c2808a3 100644 --- a/ReactWindows/ReactNative/ReactPage.cs +++ b/ReactWindows/ReactNative/ReactPage.cs @@ -4,6 +4,8 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Input; +using Windows.System; namespace ReactNative { @@ -16,6 +18,8 @@ public class ReactPage : Page private readonly string _mainComponentName; private readonly Action _onBackPressed; + private bool _isShiftKeyDown; + /// /// Instantiates the . /// @@ -90,6 +94,51 @@ protected virtual ReactRootView CreateRootView() return new ReactRootView(); } + /// + /// Captures the key down events to + /// + /// + protected override void OnKeyDown(KeyRoutedEventArgs e) + { + if (_reactInstanceManager.DevSupportManager.IsEnabled) + { + if (e.Key == VirtualKey.Shift) + { + _isShiftKeyDown = true; + } + else if (_isShiftKeyDown && e.Key == VirtualKey.F11) + { + _reactInstanceManager.DevSupportManager.ShowDevOptionsDialog(); + e.Handled = true; + } + else if (_isShiftKeyDown && e.Key == VirtualKey.F12) + { + _reactInstanceManager.DevSupportManager.HandleReloadJavaScript(); + e.Handled = true; + } + } + } + + /// + /// Captures the key up event to potentially launch the dev options menu. + /// + /// + protected override void OnKeyUp(KeyRoutedEventArgs e) + { + if (_reactInstanceManager.DevSupportManager.IsEnabled) + { + if (e.Key == VirtualKey.Menu) + { + _reactInstanceManager.DevSupportManager.ShowDevOptionsDialog(); + e.Handled = true; + } + else if (e.Key == VirtualKey.Shift) + { + _isShiftKeyDown = false; + } + } + } + private IReactInstanceManager CreateReactInstanceManager(string jsBundleFile, IReadOnlyList packages) { var builder = new ReactInstanceManager.Builder