Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Remove interfering keyboard focus logic #15873

Closed
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
2 changes: 1 addition & 1 deletion Xamarin.Forms.Core/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ internal void SetValueCore(BindableProperty property, object value, SetValueFlag

if (!converted && !property.TryConvert(ref value))
{
Log.Warning("SetValue", $"Cannot convert {value} to type '{property.ReturnType}'");
Log.Warning("SetValue", $"Cannot convert {value} to type '{property.ReturnType}' for {this.GetType().Name}.{property.PropertyName}");
return;
}

Expand Down
50 changes: 0 additions & 50 deletions Xamarin.Forms.Platform.Android/PlatformRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace Xamarin.Forms.Platform.Android
internal class PlatformRenderer : ViewGroup
{
readonly IPlatformLayout _canvas;
Point _downPosition;

DateTime _downTime;

public PlatformRenderer(Context context, IPlatformLayout canvas) : base(context)
{
Expand All @@ -24,53 +21,6 @@ public PlatformRenderer(Context context, IPlatformLayout canvas) : base(context)
}
}

public override bool DispatchTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
_downTime = DateTime.UtcNow;
_downPosition = new Point(e.RawX, e.RawY);
}

if (e.Action != MotionEventActions.Up)
return base.DispatchTouchEvent(e);

global::Android.Views.View currentView = Context.GetActivity().CurrentFocus;
bool result = base.DispatchTouchEvent(e);

do
{
if (!(currentView is EditText))
break;

global::Android.Views.View newCurrentView = Context.GetActivity().CurrentFocus;

if (currentView != newCurrentView)
break;

double distance = _downPosition.Distance(new Point(e.RawX, e.RawY));

if (distance > Context.ToPixels(20) || DateTime.UtcNow - _downTime > TimeSpan.FromMilliseconds(200))
break;

var location = new int[2];
currentView.GetLocationOnScreen(location);

float x = e.RawX + currentView.Left - location[0];
float y = e.RawY + currentView.Top - location[1];

var rect = new Rectangle(currentView.Left, currentView.Top, currentView.Width, currentView.Height);

if (rect.Contains(x, y))
break;

Context.HideKeyboard(currentView);
Context.GetActivity().Window.DecorView.ClearFocus();
} while (false);

return result;
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
Profile.FrameBegin();
Expand Down
2 changes: 0 additions & 2 deletions Xamarin.Forms.Platform.Android/Renderers/IShellSearchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ public interface IShellSearchView : IDisposable
void LoadView();

event EventHandler SearchConfirmed;

bool ShowKeyboardOnAttached { get; set; }
}
}
2 changes: 0 additions & 2 deletions Xamarin.Forms.Platform.Android/Renderers/SearchBarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ bool SearchView.IOnQueryTextListener.OnQueryTextChange(string newText)
bool SearchView.IOnQueryTextListener.OnQueryTextSubmit(string query)
{
((ISearchBarController)Element).OnSearchButtonPressed();
ClearFocus(Control);
return true;
}

Expand Down Expand Up @@ -225,7 +224,6 @@ void UpdateEnabled()
SearchView control = Control;
if (!model.IsEnabled)
{
ClearFocus(control);
// removes cursor in SearchView
control.SetInputType(InputTypes.Null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView)
_searchHandler = searchView.SearchHandler;
_control = searchView.View;
_searchHandler.PropertyChanged += SearchHandlerPropertyChanged;
_searchHandler.FocusChangeRequested += SearchHandlerFocusChangeRequested;
_editText = (_control as ViewGroup).GetChildrenOfType<EditText>().FirstOrDefault();
_textColorSwitcher = new TextColorSwitcher(_editText.TextColors, false);
_hintColorSwitcher = new TextColorSwitcher(_editText.HintTextColors, false);
Expand All @@ -43,22 +42,6 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView)
UpdateInputType();
}

protected virtual void SearchHandlerFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
{
e.Result = true;

if (e.Focus)
{
_control?.RequestFocus();
_control?.PostShowKeyboard();
}
else
{
_control.ClearFocus();
_control.HideKeyboard();
}
}

protected virtual void SearchHandlerPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.Is(SearchHandler.BackgroundColorProperty))
Expand Down Expand Up @@ -235,7 +218,6 @@ protected virtual void Dispose(bool disposing)
{
if (_searchHandler != null)
{
_searchHandler.FocusChangeRequested -= SearchHandlerFocusChangeRequested;
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
}
_searchHandler = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
elementHolder.Element = item.Element;
}

class LinearLayoutWithFocus : LinearLayout, ITabStop, IVisualElementRenderer
class ShellLinearLayout : LinearLayout, IVisualElementRenderer
{
public LinearLayoutWithFocus(global::Android.Content.Context context) : base(context)
public ShellLinearLayout(global::Android.Content.Context context) : base(context)
{
}

AView ITabStop.TabStop => this;

#region IVisualElementRenderer

VisualElement IVisualElementRenderer.Element => Content?.BindingContext as VisualElement;
Expand Down Expand Up @@ -180,7 +178,7 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int

var content = (View)template.CreateContent();

var linearLayout = new LinearLayoutWithFocus(parent.Context)
var linearLayout = new ShellLinearLayout(parent.Context)
{
Orientation = Orientation.Vertical,
LayoutParameters = new RecyclerView.LayoutParams(LP.MatchParent, LP.WrapContent),
Expand Down
11 changes: 0 additions & 11 deletions Xamarin.Forms.Platform.Android/Renderers/ShellSearchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class ShellSearchView : FrameLayout, IShellSearchView, TextView.IOnEditor

public SearchHandler SearchHandler { get; set; }

public bool ShowKeyboardOnAttached { get; set; }

AView IShellSearchView.View
{
get
Expand Down Expand Up @@ -113,8 +111,6 @@ bool TextView.IOnEditorActionListener.OnEditorAction(TextView v, ImeAction actio
// Fire Completed and dismiss keyboard for hardware / physical keyboards
if (actionId == ImeAction.Done || (actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter && e.Action == KeyEventActions.Up))
{
_textBlock.ClearFocus();
v.HideKeyboard();
SearchConfirmed?.Invoke(this, EventArgs.Empty);
Controller.QueryConfirmed();
}
Expand Down Expand Up @@ -248,9 +244,6 @@ protected override async void OnAttachedToWindow()
{
base.OnAttachedToWindow();

if (!ShowKeyboardOnAttached)
return;

Alpha = 0;
Animate().Alpha(1).SetDuration(200).SetListener(null);

Expand All @@ -259,9 +252,6 @@ protected override async void OnAttachedToWindow()

if (_disposed)
return;

_textBlock.RequestFocus();
Context.ShowKeyboard(_textBlock);
}

protected virtual void OnClearButtonClicked(object sender, EventArgs e)
Expand Down Expand Up @@ -343,7 +333,6 @@ void OnTextBlockItemClicked(object sender, AdapterView.ItemClickEventArgs e)
var item = Controller.ListProxy[index];

_textBlock.Text = "";
_textBlock.HideKeyboard();
SearchConfirmed?.Invoke(this, EventArgs.Empty);
Controller.ItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,11 @@ protected virtual void UpdateToolbarItems(Toolbar toolbar, Page page)
if (_searchView.View.Parent != null)
_searchView.View.RemoveFromParent();

_searchView.ShowKeyboardOnAttached = true;
item.SetActionView(_searchView.View);
item.Dispose();
}
else if (SearchHandler.SearchBoxVisibility == SearchBoxVisibility.Expanded)
{
_searchView.ShowKeyboardOnAttached = false;
if (_searchView.View.Parent != _toolbar)
_toolbar.AddView(_searchView.View);
}
Expand Down