Skip to content

Commit

Permalink
Revert "Show keyboard on Android entry/editor/searchbar focus (#12890)"
Browse files Browse the repository at this point in the history
This reverts commit 20f54bc, reversing
changes made to a775a2d.
  • Loading branch information
rachelkang committed Mar 13, 2023
1 parent 9e5785a commit 9fb0475
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected override void OnNewIntent(Intent intent)

protected override void OnPause()
{
_layout.HideKeyboard();
_layout.HideKeyboard(true);

// Stop animations or other ongoing actions that could consume CPU
// Commit unsaved changes, build only if users expect such changes to be permanently saved when thy leave such as a draft email
Expand Down
9 changes: 2 additions & 7 deletions src/Controls/src/Core/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;
//using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Graphics;
using Geometry = Microsoft.Maui.Controls.Shapes.Geometry;
using Rect = Microsoft.Maui.Graphics.Rect;
Expand Down Expand Up @@ -866,14 +868,7 @@ public ResourceDictionary Resources
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)
{
Expand Down
19 changes: 9 additions & 10 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Devices;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using Microsoft.Maui.TestUtils.DeviceTests.Runners;
using Xunit;
#if ANDROID || IOS || MACCATALYST
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
Expand Down Expand Up @@ -63,16 +72,6 @@ protected async Task<THandler> CreateHandlerAsync<THandler>(IElement view)
where THandler : IElementHandler, new() =>
await InvokeOnMainThreadAsync(() => CreateHandler<THandler>(view));

protected IElementHandler CreateHandler(IElement view)
{
var handler = view.ToHandler(MauiContext);
InitializeViewHandler(view, handler, MauiContext);
return handler;
}

protected async Task<IElementHandler> CreateHandlerAsync(IElement view) =>
await InvokeOnMainThreadAsync(() => CreateHandler(view));

protected Task<TValue> GetValueAsync<TValue, THandler>(IElement view, Func<THandler, TValue> func)
where THandler : IElementHandler, new()
{
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/Controls/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static class TestCategory
public const string SearchBar = "SearchBar";
public const string Shell = "Shell";
public const string TabbedPage = "TabbedPage";
public const string TextInput = "TextInput";
public const string Toolbar = "Toolbar";
public const string TemplatedView = "TemplatedView";
public const string View = "View";
Expand Down
6 changes: 0 additions & 6 deletions src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ public static void MapCursorPosition(IEditorHandler handler, ITextInput editor)
public static void MapSelectionLength(IEditorHandler handler, ITextInput editor) =>
handler.PlatformView?.UpdateSelectionLength(editor);

static void MapFocus(IEditorHandler handler, IEditor editor, object? args)
{
if (args is FocusRequest request)
handler.PlatformView?.Focus(request);
}

void OnPlatformViewAttachedToWindow(object? sender, ViewAttachedToWindowEventArgs e)
{
if (PlatformView.IsAlive() && PlatformView.Enabled)
Expand Down
5 changes: 1 addition & 4 deletions src/Core/src/Handlers/Editor/EditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public partial class EditorHandler : IEditorHandler

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

public EditorHandler() : this(Mapper)
public EditorHandler() : base(Mapper)
{
}

Expand Down
6 changes: 0 additions & 6 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ public static void MapClearButtonVisibility(IEntryHandler handler, IEntry entry)
handler.PlatformView?.UpdateClearButtonVisibility(entry, platformHandler.GetClearButtonDrawable);
}

static void MapFocus(IEntryHandler handler, IEntry entry, object? args)
{
if (args is FocusRequest request)
handler.PlatformView?.Focus(request);
}

void OnTextChanged(object? sender, TextChangedEventArgs e)
{
if (VirtualView == null)
Expand Down
5 changes: 1 addition & 4 deletions src/Core/src/Handlers/Entry/EntryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ public partial class EntryHandler : IEntryHandler

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

public EntryHandler() : this(Mapper)
public EntryHandler() : base(Mapper)
{
}

Expand Down
6 changes: 0 additions & 6 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ public static void MapCancelButtonColor(ISearchBarHandler handler, ISearchBar se
handler.PlatformView?.UpdateCancelButtonColor(searchBar);
}

static void MapFocus(ISearchBarHandler handler, ISearchBar searchBar, object? args)
{
if (args is FocusRequest request)
handler.QueryEditor?.Focus(request);
}

void OnQueryTextSubmit(object? sender, QueryTextSubmitEventArgs e)
{
VirtualView.SearchButtonPressed();
Expand Down
5 changes: 1 addition & 4 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ public partial class SearchBarHandler : ISearchBarHandler

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

public SearchBarHandler() : this(Mapper)
public SearchBarHandler() : base(Mapper)
{
}

Expand Down
51 changes: 30 additions & 21 deletions src/Core/src/Platform/Android/KeyboardManager.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using AndroidX.Core.View;
using AView = Android.Views.View;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

namespace Microsoft.Maui.Platform
{
internal static class KeyboardManager
{
internal static void HideKeyboard(this AView inputView)
internal static void HideKeyboard(this AView inputView, bool overrideValidation = false)
{
if (inputView?.Context is null)
if (inputView?.Context == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be hidden.");

var focusedView = inputView.Context?.GetActivity()?.Window?.CurrentFocus;
AView tokenView = focusedView ?? inputView;
var context = tokenView.Context;

if (context is null)
return;

using (var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager)
using (var inputMethodManager = (InputMethodManager)inputView.Context.GetSystemService(Context.InputMethodService)!)
{
var windowToken = tokenView.WindowToken;
if (windowToken is not null && inputMethodManager is not null)
if (!overrideValidation && !(inputView is EditText || inputView is TextView || inputView is SearchView))
throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");

var windowToken = inputView.WindowToken;
if (windowToken != null && inputMethodManager != null)
inputMethodManager.HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
}
}

internal static void ShowKeyboard(this TextView inputView)
{
if (inputView?.Context is null)
if (inputView?.Context == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be shown.");

using (var inputMethodManager = inputView.Context.GetSystemService(Context.InputMethodService) as InputMethodManager)
using (var inputMethodManager = (InputMethodManager)inputView.Context.GetSystemService(Context.InputMethodService)!)
{
// The zero value for the second parameter comes from
// https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int)
Expand All @@ -46,22 +44,34 @@ internal static void ShowKeyboard(this TextView inputView)

internal static void ShowKeyboard(this SearchView searchView)
{
if (searchView?.Context is null || searchView?.Resources is null)
if (searchView?.Context == null || searchView?.Resources == null)
{
throw new ArgumentNullException(nameof(searchView));
}

var queryEditor = searchView.GetFirstChildOfType<EditText>();
// Dig into the SearchView and find the actual TextView that we want to show keyboard input for
int searchViewTextViewId = searchView.Resources.GetIdentifier("android:id/search_src_text", null, null);

if (searchViewTextViewId == 0)
{
// Cannot find the resource Id; nothing else to do
return;
}

var textView = searchView.FindViewById(searchViewTextViewId);

if (queryEditor is null)
if (textView == null)
{
// Cannot find the TextView; nothing else to do
return;
}

using (var inputMethodManager = (InputMethodManager)searchView.Context.GetSystemService(Context.InputMethodService)!)
{
// The zero value for the second parameter comes from
// https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int)
// Apparently there's no named value for zero in this case
inputMethodManager?.ShowSoftInput(queryEditor, 0);
inputMethodManager?.ShowSoftInput(textView, 0);
}
}

Expand All @@ -78,7 +88,6 @@ internal static void ShowKeyboard(this AView view)
}
}


internal static void PostShowKeyboard(this AView view)
{
void ShowKeyboard()
Expand All @@ -99,7 +108,7 @@ void ShowKeyboard()
public static bool IsSoftKeyboardVisible(this AView view)
{
var insets = ViewCompat.GetRootWindowInsets(view);
if (insets is null)
if (insets == null)
return false;

var result = insets.IsVisible(WindowInsetsCompat.Type.Ime());
Expand Down
12 changes: 0 additions & 12 deletions src/Core/src/Platform/Android/TextViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,5 @@ public static void UpdateLineHeight(this TextView textView, ILabel label)
if (label.LineHeight >= 0)
textView.SetLineSpacing(0, (float)label.LineHeight);
}

internal static void Focus(this TextView textView, FocusRequest request)
{
if (textView is null)
return;

textView.Focus(request, () =>
{
if (textView.ShowSoftInputOnFocus)
KeyboardManager.PostShowKeyboard(textView);
});
}
}
}
8 changes: 0 additions & 8 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ public static void UpdateIsEnabled(this AView platformView, IView view)
}

public static void Focus(this AView platformView, FocusRequest request)
{
platformView?.Focus(request, null);
}

internal static void Focus(this AView platformView, FocusRequest request, Action? focusRequested)
{
request.IsFocused = true;

Expand All @@ -88,9 +83,6 @@ void RequestFocus()
return;

platformView?.RequestFocus();

if (platformView?.RequestFocus() == true)
focusRequested?.Invoke();
}
}

Expand Down
Loading

0 comments on commit 9fb0475

Please sign in to comment.