Skip to content

Commit

Permalink
[Android] Fix clear button not working on Entry (#7992)
Browse files Browse the repository at this point in the history
* Fix issue Entry clear button not working

* Updated EditTextExtensions

* Update clear button visibility when text changes

Co-authored-by: E.Z. Hart <hartez@gmail.com>
  • Loading branch information
jsuarezruiz and hartez authored Jul 22, 2022
1 parent fa9ce65 commit 8ed80a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
58 changes: 51 additions & 7 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.Maui.Handlers
public partial class EntryHandler : ViewHandler<IEntry, AppCompatEditText>
{
Drawable? _clearButtonDrawable;
bool _clearButtonVisible;
bool _set;

protected override AppCompatEditText CreatePlatformView()
Expand Down Expand Up @@ -127,21 +128,32 @@ public static void MapClearButtonVisibility(IEntryHandler handler, IEntry entry)
handler.PlatformView?.UpdateClearButtonVisibility(entry, platformHandler.GetClearButtonDrawable);
}

void OnTextChanged(object? sender, TextChangedEventArgs e) =>
VirtualView?.UpdateText(e);
void OnTextChanged(object? sender, TextChangedEventArgs e)
{
if (VirtualView == null)
{
return;
}

VirtualView.UpdateText(e);
MapClearButtonVisibility(this, VirtualView);
}

// This will eliminate additional native property setting if not required.
void OnFocusedChange(object? sender, FocusChangeEventArgs e)
{
if (VirtualView?.ClearButtonVisibility == ClearButtonVisibility.WhileEditing)
UpdateValue(nameof(IEntry.ClearButtonVisibility));
if (VirtualView == null)
{
return;
}

MapClearButtonVisibility(this, VirtualView);
}

// Check whether the touched position inbounds with clear button.
void OnTouch(object? sender, TouchEventArgs e) =>
e.Handled =
VirtualView?.ClearButtonVisibility == ClearButtonVisibility.WhileEditing &&
PlatformView.HandleClearButtonTouched(VirtualView.FlowDirection, e, GetClearButtonDrawable);
_clearButtonVisible && VirtualView != null &&
PlatformView.HandleClearButtonTouched(VirtualView.GetEffectiveFlowDirection(), e, GetClearButtonDrawable);

void OnEditorAction(object? sender, EditorActionEventArgs e)
{
Expand All @@ -166,5 +178,37 @@ private void OnSelectionChanged(object? sender, EventArgs e)
if (VirtualView.SelectionLength != selectedTextLength)
VirtualView.SelectionLength = selectedTextLength;
}

internal void ShowClearButton()
{
if (_clearButtonVisible)
{
return;
}

var drawable = GetClearButtonDrawable();

if (VirtualView.GetEffectiveFlowDirection() == FlowDirection.RightToLeft)
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
else
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
}

_clearButtonVisible = true;
}

internal void HideClearButton()
{
if (!_clearButtonVisible)
{
return;
}

PlatformView.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
_clearButtonVisible = false;
}
}
}
30 changes: 9 additions & 21 deletions src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static class EditTextExtensions
{
public static void UpdateText(this EditText editText, IEntry entry)
{
var previousTextLength = editText.Length();

// Setting the text causes the cursor to reset to position zero
// Therefore if:
// User Types => VirtualView Updated => Triggers Native Update
Expand Down Expand Up @@ -171,30 +173,16 @@ public static void UpdateIsReadOnly(this EditText editText, IEditor editor)
editText.SetCursorVisible(isReadOnly);
}

// TODO: NET7 hartez - Remove this, nothing uses it
public static void UpdateClearButtonVisibility(this EditText editText, IEntry entry, Drawable? clearButtonDrawable) =>
UpdateClearButtonVisibility(editText, entry, () => clearButtonDrawable);

// TODO: NET7 hartez - Remove the getClearButtonDrawable parameter, nothing uses it
public static void UpdateClearButtonVisibility(this EditText editText, IEntry entry, Func<Drawable?>? getClearButtonDrawable)
{
// Places clear button drawable at the end or start of the EditText based on FlowDirection.
void ShowClearButton()
{
var drawable = getClearButtonDrawable?.Invoke();

if (entry.GetEffectiveFlowDirection() == FlowDirection.RightToLeft)
{
editText.SetCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
else
{
editText.SetCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
}
}

// Hides clear button drawable from the control.
void HideClearButton()
if (entry?.Handler is not EntryHandler entryHandler)
{
editText.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
return;
}

bool isFocused = editText.IsFocused;
Expand All @@ -206,11 +194,11 @@ void HideClearButton()

if (shouldDisplayClearButton)
{
ShowClearButton();
entryHandler.ShowClearButton();
}
else
{
HideClearButton();
entryHandler.HideClearButton();
}
}

Expand Down Expand Up @@ -364,7 +352,7 @@ internal static bool HandleClearButtonTouched(this EditText? platformView, FlowD
if (motionEvent.Action != MotionEventActions.Up)
return false;

var x = motionEvent.GetX();
var x = motionEvent.RawX;
var y = motionEvent.GetY();

if ((flowDirection != FlowDirection.LeftToRight
Expand Down

0 comments on commit 8ed80a2

Please sign in to comment.