Skip to content

Implement IsTextPredictionEnabled property on Editor #515

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

Merged
merged 5 commits into from
Mar 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ protected virtual void UpdateFont()
EditText.SetTextSize(ComplexUnitType.Sp, (float)Element.FontSize);
}

[PortHandler("Partially Ported")]
void UpdateInputType()
{
Editor model = Element;
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ protected internal virtual void UpdateFont()
TextView.Font = font;
}

[PortHandler("Partially Ported")]
void UpdateKeyboard()
{
var keyboard = Element.Keyboard;
Expand Down
1 change: 1 addition & 0 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void SetupMauiLayout()

verticalStack.Add(new Editor());
verticalStack.Add(new Editor { Text = "Editor" });
verticalStack.Add(new Editor { Text = "Predictive Text Off", IsTextPredictionEnabled = false });

var entry = new Entry();
entry.TextChanged += (sender, e) =>
Expand Down
5 changes: 4 additions & 1 deletion src/Core/src/Core/IEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/// </summary>
public interface IEditor : IView, IText
{

/// <summary>
/// Gets a value that controls whether text prediction and automatic text correction is on or off.
/// </summary>
bool IsTextPredictionEnabled { get; }
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
}

public static void MapPredictiveText(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePredictiveText(editor);
}
}
}
3 changes: 3 additions & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public partial class EditorHandler : AbstractViewHandler<IEditor, object>
protected override object CreateNativeView() => throw new NotImplementedException();

public static void MapText(IViewHandler handler, IEditor editor) { }

public static void MapCharacterSpacing(IViewHandler handler, IEditor editor) { }

public static void MapPredictiveText(EditorHandler handler, IEditor editor) { }
}
}
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public partial class EditorHandler
public static PropertyMapper<IEditor, EditorHandler> EditorMapper = new PropertyMapper<IEditor, EditorHandler>(ViewHandler.ViewMapper)
{
[nameof(IEditor.Text)] = MapText,
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing,
[nameof(IEditor.IsTextPredictionEnabled)] = MapPredictiveText
};

public EditorHandler() : base(EditorMapper)
Expand Down
8 changes: 7 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CoreGraphics;
using System;
using CoreGraphics;
using UIKit;

namespace Microsoft.Maui.Handlers
Expand All @@ -24,5 +25,10 @@ public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
}

public static void MapPredictiveText(EditorHandler handler, IEditor editor)
{
handler.TypedNativeView?.UpdatePredictiveText(editor);
}
}
}
11 changes: 10 additions & 1 deletion src/Core/src/Platform/Android/EditorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AndroidX.AppCompat.Widget;
using Android.Text;
using AndroidX.AppCompat.Widget;

namespace Microsoft.Maui
{
Expand All @@ -23,5 +24,13 @@ public static void UpdateCharacterSpacing(this AppCompatEditText editText, IEdit
{
editText.LetterSpacing = editor.CharacterSpacing.ToEm();
}

public static void UpdatePredictiveText(this AppCompatEditText editText, IEditor editor)
{
if(editor.IsTextPredictionEnabled)
return;

editText.InputType |= InputTypes.TextFlagNoSuggestions;
}
}
}
6 changes: 6 additions & 0 deletions src/Core/src/Platform/iOS/EditorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ public static void UpdateCharacterSpacing(this UITextView textView, IEditor edit

// TODO: Include AttributedText to Label Placeholder
}

public static void UpdatePredictiveText(this UITextView textView, IEditor editor)
{
textView.AutocorrectionType = editor.IsTextPredictionEnabled
? UITextAutocorrectionType.Yes : UITextAutocorrectionType.No;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Threading.Tasks;
using Android.Text;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -39,6 +40,9 @@ AppCompatEditText GetNativeEditor(EditorHandler editorHandler) =>

string GetNativeText(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Text;

bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
!GetNativeEditor(editorHandler).InputType.HasFlag(InputTypes.TextFlagNoSuggestions);

double GetNativeCharacterSpacing(EditorHandler editorHandler)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ await ValidatePropertyUpdatesValue(
setValue,
unsetValue);
}

[Theory(DisplayName = "Is Text Prediction Enabled")]
[InlineData(true)]
[InlineData(false)]
public async Task IsTextPredictionEnabledCorrectly(bool isEnabled)
{
var editor = new EditorStub()
{
IsTextPredictionEnabled = isEnabled
};

await ValidatePropertyInitValue(editor, () => editor.IsTextPredictionEnabled, GetNativeIsTextPredictionEnabled, isEnabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ double GetNativeCharacterSpacing(EditorHandler editorHandler)
var editor = GetNativeEditor(editorHandler);
return editor.AttributedText.GetCharacterSpacing();
}
}

bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).AutocorrectionType == UITextAutocorrectionType.Yes;
}
}
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/EditorStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public partial class EditorStub : StubBase, IEditor
public Font Font { get; set; }

public double CharacterSpacing { get; set; }

public bool IsTextPredictionEnabled { get; set; }
}
}