Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor keyboard focus code for .NET 8 #13824

Merged
merged 16 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Controls/src/Core/Entry/Entry.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -30,5 +31,11 @@ public static void MapText(IEntryHandler handler, Entry entry)
{
Platform.EditTextExtensions.UpdateText(handler.PlatformView, entry);
}

static void MapFocus(IViewHandler handler, IView view, object args)
{
handler.ShowKeyboardIfFocused(view);
EntryHandler.CommandMapper.Chained?.Invoke(handler, view, nameof(IView.Focus), args);
}
}
}
8 changes: 8 additions & 0 deletions src/Controls/src/Core/Entry/Entry.Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ public partial class Entry
[nameof(TextTransform)] = MapText,
};

static CommandMapper<IEntry, IEntryHandler> ControlsCommandMapper = new(EntryHandler.CommandMapper)
{
#if ANDROID
[nameof(IEntry.Focus)] = MapFocus
#endif
};

internal static new void RemapForControls()
{
// Adjust the mappings to preserve Controls.Entry legacy behaviors
EntryHandler.Mapper = ControlsEntryMapper;
EntryHandler.CommandMapper = ControlsCommandMapper;
}
}
}
8 changes: 8 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Editor/Editor.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable disable
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls
{
public partial class Editor
Expand All @@ -18,5 +20,11 @@ public static void MapText(IEditorHandler handler, Editor editor)
{
Platform.EditTextExtensions.UpdateText(handler.PlatformView, editor);
}

static void MapFocus(IViewHandler handler, IView view, object args)
{
handler.ShowKeyboardIfFocused(view);
EditorHandler.CommandMapper.Chained?.Invoke(handler, view, nameof(IView.Focus), args);
}
}
}
8 changes: 8 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Editor/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ public partial class Editor
[nameof(TextTransform)] = MapText,
};

static CommandMapper<IEditor, IEditorHandler> ControlsCommandMapper = new(EditorHandler.CommandMapper)
{
#if ANDROID
[nameof(IEditor.Focus)] = MapFocus
#endif
};

internal static new void RemapForControls()
{
// Adjust the mappings to preserve Controls.Editor legacy behaviors
EditorHandler.Mapper = ControlsEditorMapper;
EditorHandler.CommandMapper = ControlsCommandMapper;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable disable
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls
{
public partial class SearchBar
Expand All @@ -10,5 +12,11 @@ public static void MapText(ISearchBarHandler handler, SearchBar searchBar)
{
Platform.SearchViewExtensions.UpdateText(handler.PlatformView, searchBar);
}

static void MapFocus(IViewHandler handler, IView view, object args)
{
handler.ShowKeyboardIfFocused(view);
SearchBarHandler.CommandMapper.Chained?.Invoke(handler, view, nameof(IView.Focus), args);
}
}
}
8 changes: 8 additions & 0 deletions src/Controls/src/Core/HandlerImpl/SearchBar/SearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ public partial class SearchBar
[nameof(TextTransform)] = MapText,
};

static CommandMapper<ISearchBar, ISearchBarHandler> ControlsCommandMapper = new(SearchBarHandler.CommandMapper)
{
#if ANDROID
[nameof(ISearchBar.Focus)] = MapFocus
#endif
};

internal static new void RemapForControls()
{
// Adjust the mappings to preserve Controls.SearchBar legacy behaviors
SearchBarHandler.Mapper = ControlsSearchBarMapper;
SearchBarHandler.CommandMapper = ControlsCommandMapper;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ public partial class VisualElement
[nameof(IViewHandler.ContainerView)] = MapContainerView,
};

static CommandMapper<IView, IViewHandler> ControlsViewCommandMapper = new(ViewHandler.ViewCommandMapper)
{
[nameof(IView.Focus)] = MapFocus,
};

internal static void RemapForControls()
{
ViewHandler.ViewMapper = ControlsVisualElementMapper;
ViewHandler.ViewCommandMapper = ControlsViewCommandMapper;
}

public static void MapBackgroundColor(IViewHandler handler, IView view)
Expand Down Expand Up @@ -63,5 +69,13 @@ static void MapContainerView(IViewHandler arg1, IView arg2)
ve._platformContainerViewChanged?.Invoke(arg2, EventArgs.Empty);
}
}

static void MapFocus(IViewHandler handler, IView view, object args)
{
if (args is not FocusRequest fr)
return;

view.MapFocus(handler, fr);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#nullable disable

namespace Microsoft.Maui.Controls.Platform
{
internal static class InputViewExtensions
{
internal static void ShowKeyboardIfFocused(this IViewHandler handler, IView view)
{
if (view is VisualElement ve && ve.IsFocused && handler is IPlatformViewHandler platformViewHandler)
{
KeyboardManager.ShowKeyboard(platformViewHandler.PlatformView);
}
}
}
}
46 changes: 46 additions & 0 deletions src/Controls/src/Core/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,51 @@ internal static bool TrySetValue(this Element element, string text)

return false;
}

static internal bool Focus(this VisualElement view, FocusRequest focusRequest)
{
var handler = view.Handler;

if (handler is not null)
handler.Invoke(nameof(IView.Focus), focusRequest);
else
MapFocus(view, handler, focusRequest);

return focusRequest.IsFocused;
}

// If the handler isn't attached we still need some focus code to execute
static internal void MapFocus(this IView view, IViewHandler? handler, FocusRequest args)
{
if (args is not FocusRequest fr)
return;

if (view is not VisualElement ve)
{
FocusRequest focusRequest = new FocusRequest(false);
if (handler is not null)
ViewHandler.MapFocus(handler, view, focusRequest);
return;
}

if (ve.IsFocused)
{
fr.IsFocused = true;
return;
}

if (!ve.HasFocusChangeRequestedEvent)
{
FocusRequest focusRequest = new FocusRequest(false);
if (handler is not null)
ViewHandler.MapFocus(handler, view, focusRequest);

return;
}

var arg = new VisualElement.FocusRequestArgs { Focus = true };
ve.InvokeFocusChangeRequested(arg);
fr.IsFocused = arg.Result;
}
}
}
28 changes: 6 additions & 22 deletions src/Controls/src/Core/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,28 +871,8 @@ public ResourceDictionary Resources
/// <include file="../../docs/Microsoft.Maui.Controls/VisualElement.xml" path="//Member[@MemberName='Focus']/Docs/*" />
public bool Focus()
{
if (IsFocused)
{
#if ANDROID
// TODO: Refactor using mappers for .NET 8
if (this is ITextInput && Handler is IPlatformViewHandler platformViewHandler)
KeyboardManager.ShowKeyboard(platformViewHandler.PlatformView);
#endif
return true;
}

if (FocusChangeRequested == null)
{
FocusRequest focusRequest = new FocusRequest(false);

Handler?.Invoke(nameof(IView.Focus), focusRequest);

return focusRequest.IsFocused;
}

var arg = new FocusRequestArgs { Focus = true };
FocusChangeRequested(this, arg);
return arg.Result;
FocusRequest focusRequest = new FocusRequest(false);
return this.Focus(focusRequest);
}

public event EventHandler<FocusEventArgs> Focused;
Expand Down Expand Up @@ -1055,8 +1035,12 @@ internal void ComputeConstrainsForChildren()

internal virtual void ComputeConstraintForView(View view) => view.ComputedConstraint = LayoutConstraint.None;

// TODO: Obsolete in favor of MapFocus https://github.com/dotnet/maui/issues/14299
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<FocusRequestArgs> FocusChangeRequested;
internal void InvokeFocusChangeRequested(FocusRequestArgs args) =>
FocusChangeRequested?.Invoke(this, args);
internal bool HasFocusChangeRequestedEvent => FocusChangeRequested is not null;

/// <include file="../../docs/Microsoft.Maui.Controls/VisualElement.xml" path="//Member[@MemberName='InvalidateMeasureNonVirtual']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static async Task SendKeyboardReturnType(this AView view, ReturnType retu
.PerformEditorAction(returnType.ToPlatform());

// Let the action propagate
await Task.Delay(10);
await Task.Delay(100);
}

public static async Task WaitForFocused(this AView view, int timeout = 1000, string message = "")
Expand Down