Skip to content

Commit

Permalink
Fixed back button problems in UWP (MonoGame#5810)
Browse files Browse the repository at this point in the history
* Fixed back button race condition

* Moved back button logic to UAPGameWindow.ProcessWindowEvents
  • Loading branch information
timgott authored and nkast committed Jun 26, 2018
1 parent 670fb07 commit 2be8212
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
14 changes: 0 additions & 14 deletions MonoGame.Framework/WindowsUniversal/UAPGamePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public UAPGamePlatform(Game game)
}
}

SystemNavigationManager.GetForCurrentView().BackRequested += BackRequested;

CoreApplication.Suspending += this.CoreApplication_Suspending;

Game.PreviousExecutionState = PreviousExecutionState;
Expand All @@ -115,17 +113,6 @@ public override GameRunBehavior DefaultRunBehavior
get { return GameRunBehavior.Synchronous; }
}

private static void BackRequested(object sender, BackRequestedEventArgs e)
{
// We need to manually hide the keyboard input UI when the back button is pressed
if (KeyboardInput.IsVisible)
KeyboardInput.Cancel(null);
else
GamePad.Back = true;

e.Handled = true;
}

public override void RunLoop()
{
UAPGameWindow.Instance.RunLoop();
Expand All @@ -138,7 +125,6 @@ public override void StartRunLoop()
while (true)
{
UAPGameWindow.Instance.Tick();
GamePad.Back = false;
}
});
var tickWorker = ThreadPool.RunAsync(workItemHandler, WorkItemPriority.High, WorkItemOptions.TimeSliced);
Expand Down
68 changes: 46 additions & 22 deletions MonoGame.Framework/WindowsUniversal/UAPGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ partial class UAPGameWindow : GameWindow
private DisplayOrientation _newOrientation;
private bool _isFocusChanged = false;
private CoreWindowActivationState _newActivationState;
private bool _backPressed = false;

#region Internal Properties

internal Game Game { get; set; }

public ApplicationView AppView { get { return _appView; } }

internal bool IsExiting { get; set; }
Expand All @@ -61,7 +62,7 @@ partial class UAPGameWindow : GameWindow
public override bool AllowUserResizing
{
get { return false; }
set
set
{
// You cannot resize a Metro window!
}
Expand All @@ -80,9 +81,9 @@ protected internal override void SetSupportedOrientations(DisplayOrientation ori
// when no preference is being changed.
if (_supportedOrientations == orientations)
return;

_supportedOrientations = orientations;

DisplayOrientations supported;
if (orientations == DisplayOrientation.Default)
{
Expand Down Expand Up @@ -113,7 +114,7 @@ public void Initialize(CoreWindow coreWindow, UIElement inputElement, TouchQueue
_coreWindow = coreWindow;
_inputEvents = new InputEvents(_coreWindow, inputElement, touchQueue);

_dinfo = DisplayInformation.GetForCurrentView();
_dinfo = DisplayInformation.GetForCurrentView();
_appView = ApplicationView.GetForCurrentView();

// Set a min size that is reasonable knowing someone might try
Expand All @@ -129,7 +130,9 @@ public void Initialize(CoreWindow coreWindow, UIElement inputElement, TouchQueue

_coreWindow.Closed += Window_Closed;
_coreWindow.Activated += Window_FocusChanged;
_coreWindow.CharacterReceived += Window_CharacterReceived;
_coreWindow.CharacterReceived += Window_CharacterReceived;

SystemNavigationManager.GetForCurrentView().BackRequested += BackRequested;

SetViewBounds(_appView.VisibleBounds.Width, _appView.VisibleBounds.Height);

Expand All @@ -146,11 +149,11 @@ private void Window_FocusChanged(CoreWindow sender, WindowActivatedEventArgs arg
}

private void UpdateFocus()
{
{
lock (_eventLocker)
{
{
_isFocusChanged = false;

if (_newActivationState == CoreWindowActivationState.Deactivated)
Platform.IsActive = false;
else
Expand Down Expand Up @@ -208,16 +211,16 @@ private void UpdateSize()
}
}

private void Window_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
private void Window_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
_textQueue.Enqueue((char)args.KeyCode);
}
}

private void UpdateTextInput()
{
char ch;
while (_textQueue.TryDequeue(out ch))
OnTextInput(_coreWindow, new TextInputEventArgs(ch));
OnTextInput(_coreWindow, new TextInputEventArgs(ch));
}

private static DisplayOrientation ToOrientation(DisplayOrientations orientations)
Expand Down Expand Up @@ -273,7 +276,7 @@ private void DisplayProperties_OrientationChanged(DisplayInformation dinfo, obje
lock(_eventLocker)
{
_isOrientationChanged = true;
_newOrientation = ToOrientation(dinfo.CurrentOrientation);
_newOrientation = ToOrientation(dinfo.CurrentOrientation);
}
}

Expand All @@ -282,7 +285,7 @@ private void UpdateOrientation()
lock (_eventLocker)
{
_isOrientationChanged = false;

// Set the new orientation.
_orientation = _newOrientation;

Expand All @@ -295,6 +298,23 @@ private void UpdateOrientation()
}
}

private void BackRequested(object sender, BackRequestedEventArgs e)
{
// We need to manually hide the keyboard input UI when the back button is pressed
if (KeyboardInput.IsVisible)
KeyboardInput.Cancel(null);
else
_backPressed = true;

e.Handled = true;
}

private void UpdateBackButton()
{
GamePad.Back = _backPressed;
_backPressed = false;
}

protected override void SetTitle(string title)
{
Debug.WriteLine("WARNING: GameWindow.Title has no effect under UWP.");
Expand All @@ -306,12 +326,12 @@ internal void SetCursor(bool visible)
return;

var asyncResult = _coreWindow.Dispatcher.RunIdleAsync( (e) =>
{
if (visible)
_coreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
else
_coreWindow.PointerCursor = null;
});
{
if (visible)
_coreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
else
_coreWindow.PointerCursor = null;
});
}

internal void RunLoop()
Expand All @@ -336,7 +356,7 @@ internal void RunLoop()
break;
}
}

void ProcessWindowEvents()
{
// Update input
Expand All @@ -354,8 +374,12 @@ void ProcessWindowEvents()
if (_isOrientationChanged)
UpdateOrientation();

// Update focus
if (_isFocusChanged)
UpdateFocus();

// Update back button
UpdateBackButton();
}

internal void Tick()
Expand Down

0 comments on commit 2be8212

Please sign in to comment.