Skip to content

Commit

Permalink
Merge pull request #6651 from MartinZikmund/dev/mazi/initial-focus
Browse files Browse the repository at this point in the history
fix: Initial focus on Page
  • Loading branch information
mergify[bot] authored Sep 17, 2021
2 parents 235b2d7 + ae05fa3 commit 24c16cc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 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 @@ -10,6 +10,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 @@ -19,13 +20,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,
animateIfBringIntoView: false);
return;
}

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

Expand All @@ -36,6 +49,8 @@ private protected override void OnLoaded()
focusManager.InitialFocus = true;

TrySetFocusedElement(spFirstFocusableElementDO);

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?.AcceptsFirstResponder() == true)
{
Window.Current?.NativeWindow?.MakeFirstResponder(control);
}
}
}
}
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 @@ -82,6 +82,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
14 changes: 8 additions & 6 deletions src/Uno.UI/UI/Xaml/Window.macOS.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using CoreGraphics;
using Foundation;
using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using AppKit;
using CoreGraphics;
using Foundation;
using Uno.UI;
using Uno.UI.Controls;
using Uno.UI.Xaml.Core;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.UI.Core;
using Uno.UI.Controls;
using System.Drawing;
using Windows.UI.ViewManagement;
using Uno.UI;
using Windows.UI.Xaml.Controls;
using Uno.UI.Xaml.Core;

namespace Windows.UI.Xaml
{
Expand Down Expand Up @@ -62,6 +62,8 @@ public Window()
InitializeCommon();
}

internal NSWindow NativeWindow => _window;

private void ObserveOrientationAndSize()
{
_windowResizeNotificationObject = NSNotificationCenter.DefaultCenter.AddObserver(
Expand Down

0 comments on commit 24c16cc

Please sign in to comment.