Skip to content

Commit

Permalink
Fixes #160 - Adds keyboard shortcuts to open the dev options menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
rozele committed May 16, 2016
1 parent f8f2317 commit bdcc33d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
25 changes: 15 additions & 10 deletions ReactWindows/ReactNative/DevSupport/DevSupportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void HandleException(Exception exception)
{
}

public void HandleReloadJavaScript()
{
}

public void ShowDevOptionsDialog()
{
}

public void ShowNewJavaScriptError(string title, JArray details, int exceptionId)
{
}
Expand Down
11 changes: 11 additions & 0 deletions ReactWindows/ReactNative/DevSupport/IDevSupportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public interface IDevSupportManager
/// <param name="exception">The exception.</param>
void HandleException(Exception exception);

/// <summary>
/// Handles reloading the JavaScript bundle.
/// </summary>
void HandleReloadJavaScript();


/// <summary>
/// Show the developer options dialog.
/// </summary>
void ShowDevOptionsDialog();

/// <summary>
/// Display a JavaScript error.
/// </summary>
Expand Down
49 changes: 49 additions & 0 deletions ReactWindows/ReactNative/ReactPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -16,6 +18,8 @@ public class ReactPage : Page
private readonly string _mainComponentName;
private readonly Action _onBackPressed;

private bool _isShiftKeyDown;

/// <summary>
/// Instantiates the <see cref="ReactPage"/>.
/// </summary>
Expand Down Expand Up @@ -90,6 +94,51 @@ protected virtual ReactRootView CreateRootView()
return new ReactRootView();
}

/// <summary>
/// Captures the key down events to
/// </summary>
/// <param name="e"></param>
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;
}
}
}

/// <summary>
/// Captures the key up event to potentially launch the dev options menu.
/// </summary>
/// <param name="e"></param>
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<IReactPackage> packages)
{
var builder = new ReactInstanceManager.Builder
Expand Down

0 comments on commit bdcc33d

Please sign in to comment.