Skip to content

Commit

Permalink
fix: Initial focus on Page
Browse files Browse the repository at this point in the history
Previously, the first focusable child was focused on Page, even if the Page itself was focusable. Now the Page can be focused itself, which matches UWP. In addition, on initial focus we avoid opening the InputPane in case the initial focused element is a TextBox.
  • Loading branch information
MartinZikmund committed Aug 3, 2021
1 parent 152b1c9 commit 39790b8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Page/Page.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Uno.Extensions;
using Uno.UI.Extensions;
using Uno.UI.Xaml.Core;
using Uno.UI.Xaml.Input;
using Windows.UI.Xaml.Automation.Peers;

namespace Windows.UI.Xaml.Controls
Expand All @@ -18,13 +19,25 @@ public partial class Page
private protected override void OnLoaded()
{
base.OnLoaded();

var spCurrentFocusedElement = this.GetFocusedElement();

var focusManager = VisualTree.GetFocusManagerForElement(this);
bool setDefaultFocus = focusManager?.IsPluginFocused() == true;

if (setDefaultFocus && spCurrentFocusedElement == null)
{
// Uno specific: If the page is focusable itself, we want to
// give it focus instead of the first element.
if (FocusProperties.IsFocusable(this))
{
this.SetFocusedElement(
this,
FocusState.Programmatic,
false /*animateIfBringIntoView*/);
return;
}

// Set the focus on the first focusable control
var spFirstFocusableElementCDO = focusManager?.GetFirstFocusableElement(this);

Expand All @@ -48,6 +61,8 @@ private protected override void OnLoaded()
this.Log().LogError($"Setting initial page focus failed: {ex}");
}
}

focusManager.InitialFocus = false;
}

if (spFirstFocusableElementCDO == null)
Expand Down
9 changes: 8 additions & 1 deletion src/Uno.UI/UI/Xaml/Input/FocusManager.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Windows.UI.Xaml.Controls;
using Android.Graphics;
using Windows.UI.ViewManagement;
using Uno.UI.Xaml.Core;

namespace Windows.UI.Xaml.Input
{
Expand All @@ -18,7 +19,13 @@ private static void FocusNative(UIElement element)
// TODO Uno: Handle Hyperlink focus
if (element is Control control)
{
control.RequestFocus();
var focusManager = VisualTree.GetFocusManagerForElement(control);
if (element is Android.Views.View androidView &&
androidView.Focusable &&
focusManager?.InitialFocus == false) // Do not focus natively on initial focus so the soft keyboard is not opened
{
control.RequestFocus();
}

// Forcefully try to bring the control into view when keyboard is open to accommodate adjust nothing mode
if (InputPane.GetForCurrentView().Visible)
Expand Down
14 changes: 12 additions & 2 deletions src/Uno.UI/UI/Xaml/Input/FocusManager.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
using UIKit;
using Uno.UI.Extensions;
using Windows.UI.Xaml.Controls;
using Windows.UI.ViewManagement;
using Uno.UI.Xaml.Core;

namespace Windows.UI.Xaml.Input
{
public partial class FocusManager
{
private static void FocusNative(UIElement control) => control?.BecomeFirstResponder();
{
private static void FocusNative(UIElement control)
{
var focusManager = VisualTree.GetFocusManagerForElement(control);
if (control?.CanBecomeFirstResponder == true &&
focusManager?.InitialFocus == false) // Do not focus natively on initial focus so the soft keyboard is not opened
{
control.BecomeFirstResponder();
}
}
}
}
8 changes: 7 additions & 1 deletion src/Uno.UI/UI/Xaml/Input/FocusManager.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace Windows.UI.Xaml.Input
{
public partial class FocusManager
{
private static void FocusNative(UIElement control) => control?.BecomeFirstResponder();
private static void FocusNative(UIElement control)
{
if (control?.CanBecomeFirstResponder == true)
{
control.BecomeFirstResponder();
}
}
}
}
8 changes: 8 additions & 0 deletions src/Uno.UI/UI/Xaml/Input/FocusManager.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ internal static bool FocusNative(UIElement element)
return false;
}

var focusManager = VisualTree.GetFocusManagerForElement(element);

if (focusManager?.InitialFocus == true)
{
// Do not focus natively on initial focus so the soft keyboard is not opened
return false;
}

if (element is TextBox textBox)
{
return textBox.FocusTextView();
Expand Down

0 comments on commit 39790b8

Please sign in to comment.