From 1e3099f494b25439e473ae74495953ac599d248a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Sun, 11 Jul 2021 12:30:14 +0200 Subject: [PATCH] Set correct cursor position in Entry and Editor on Android --- .../Handlers/Editor/EditorHandler.Android.cs | 7 ++++ .../Handlers/Entry/EntryHandler.Android.cs | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/GraphicsControls/Handlers/Editor/EditorHandler.Android.cs b/src/GraphicsControls/Handlers/Editor/EditorHandler.Android.cs index 572d383..cc69d11 100644 --- a/src/GraphicsControls/Handlers/Editor/EditorHandler.Android.cs +++ b/src/GraphicsControls/Handlers/Editor/EditorHandler.Android.cs @@ -25,6 +25,13 @@ namespace Microsoft.Maui.Graphics.Controls nativeEditor.TextAlignment = ATextAlignment.ViewStart; nativeEditor.SetHorizontallyScrolling(false); + if (Drawable is MaterialEditorDrawable) + nativeEditor.SetPadding(36, 60, 0, 0); + else if (Drawable is FluentEditorDrawable) + nativeEditor.SetPadding(24, 12, 0, 0); + else if (Drawable is CupertinoEditorDrawable) + nativeEditor.SetPadding(24, 12, 0, 0); + return nativeEditor; } diff --git a/src/GraphicsControls/Handlers/Entry/EntryHandler.Android.cs b/src/GraphicsControls/Handlers/Entry/EntryHandler.Android.cs index f4e734a..966f16f 100644 --- a/src/GraphicsControls/Handlers/Entry/EntryHandler.Android.cs +++ b/src/GraphicsControls/Handlers/Entry/EntryHandler.Android.cs @@ -3,6 +3,7 @@ using Android.Runtime; using Android.Views; using Android.Views.InputMethods; using Android.Widget; +using static Android.Views.View; using static Android.Widget.TextView; namespace Microsoft.Maui.Graphics.Controls @@ -12,16 +13,32 @@ namespace Microsoft.Maui.Graphics.Controls static ColorStateList? DefaultTextColors { get; set; } EditorActionListener ActionListener { get; } = new EditorActionListener(); + EntryFocusChangeListener FocusChangeListener { get; } = new EntryFocusChangeListener(); protected override GraphicsEntry CreateNativeView() { - return new GraphicsEntry(Context!) { GraphicsControl = this }; + var graphicsEntry = new GraphicsEntry(Context!) + { + GraphicsControl = this + }; + + if (Drawable is MaterialEntryDrawable) + graphicsEntry.SetPadding(36, 60, 0, 0); + else if (Drawable is FluentEntryDrawable) + graphicsEntry.SetPadding(24, 12, 0, 0); + else if (Drawable is CupertinoEntryDrawable) + graphicsEntry.SetPadding(24, 12, 0, 0); + + return graphicsEntry; } protected override void ConnectHandler(GraphicsEntry nativeView) { ActionListener.Handler = this; + FocusChangeListener.Handler = this; + nativeView.SetOnEditorActionListener(ActionListener); + nativeView.OnFocusChangeListener = FocusChangeListener; base.ConnectHandler(nativeView); } @@ -29,7 +46,10 @@ namespace Microsoft.Maui.Graphics.Controls protected override void DisconnectHandler(GraphicsEntry nativeView) { nativeView.SetOnEditorActionListener(null); + nativeView.OnFocusChangeListener = null; + ActionListener.Handler = null; + FocusChangeListener.Handler = null; base.DisconnectHandler(nativeView); } @@ -121,6 +141,11 @@ namespace Microsoft.Maui.Graphics.Controls handler.NativeView?.UpdateSelectionLength(entry); } + void OnFocusedChange(bool hasFocus) + { + Drawable.HasFocus = hasFocus; + } + class EditorActionListener : Java.Lang.Object, IOnEditorActionListener { public EntryHandler? Handler { get; set; } @@ -141,5 +166,15 @@ namespace Microsoft.Maui.Graphics.Controls return true; } } + + class EntryFocusChangeListener : Java.Lang.Object, IOnFocusChangeListener + { + public EntryHandler? Handler { get; set; } + + public void OnFocusChange(View? v, bool hasFocus) + { + Handler?.OnFocusedChange(hasFocus); + } + } } } \ No newline at end of file