Skip to content
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
9 changes: 7 additions & 2 deletions src/Controls/src/Core/Editor/Editor.Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ namespace Microsoft.Maui.Controls
{
public partial class Editor
{
internal static Action<IElementHandler, IElement> DefaultTextMapping { get; private set; }
#if ANDROID
internal static Action<IElementHandler, IElement> Material3TextMapping { get; private set; }
#endif

internal static new void RemapForControls()
{
// Adjust the mappings to preserve Controls.Editor legacy behaviors
#if WINDOWS
EditorHandler.Mapper.ReplaceMapping<Editor, IEditorHandler>(PlatformConfiguration.WindowsSpecific.InputView.DetectReadingOrderFromContentProperty.PropertyName, MapDetectReadingOrderFromContent);
#endif
EditorHandler.Mapper.ReplaceMapping<Editor, IEditorHandler>(nameof(Text), MapText);
DefaultTextMapping = RegisterTextMapping<Editor, IEditorHandler>(EditorHandler.Mapper, MapText);
EditorHandler.Mapper.ReplaceMapping<Editor, IEditorHandler>(nameof(TextTransform), MapText);

#if ANDROID
if (RuntimeFeature.IsMaterial3Enabled)
{
EditorHandler2.Mapper.ReplaceMapping<Editor, EditorHandler2>(nameof(Text), MapText);
Material3TextMapping = RegisterTextMapping<Editor, EditorHandler2>(EditorHandler2.Mapper, MapText);
EditorHandler2.Mapper.ReplaceMapping<Editor, EditorHandler2>(nameof(TextTransform), MapText);
EditorHandler2.Mapper.AppendToMapping<Editor, EditorHandler2>(nameof(VisualElement.IsFocused), InputView.MapIsFocused);
EditorHandler2.CommandMapper.PrependToMapping<Editor, EditorHandler2>(nameof(IEditor.Focus), InputView.MapFocus);
Expand Down
9 changes: 7 additions & 2 deletions src/Controls/src/Core/Entry/Entry.Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Microsoft.Maui.Controls
{
public partial class Entry
{
internal static Action<IElementHandler, IElement> DefaultTextMapping { get; private set; }
#if ANDROID
internal static Action<IElementHandler, IElement> Material3TextMapping { get; private set; }
#endif

internal static new void RemapForControls()
{
// Adjust the mappings to preserve Controls.Entry legacy behaviors
Expand All @@ -17,15 +22,15 @@ public partial class Entry
EntryHandler.Mapper.ReplaceMapping<Entry, IEntryHandler>(PlatformConfiguration.iOSSpecific.Entry.CursorColorProperty.PropertyName, MapCursorColor);
EntryHandler.Mapper.ReplaceMapping<Entry, IEntryHandler>(PlatformConfiguration.iOSSpecific.Entry.AdjustsFontSizeToFitWidthProperty.PropertyName, MapAdjustsFontSizeToFitWidth);
#endif
EntryHandler.Mapper.ReplaceMapping<Entry, IEntryHandler>(nameof(Text), MapText);
DefaultTextMapping = RegisterTextMapping<Entry, IEntryHandler>(EntryHandler.Mapper, MapText);
EntryHandler.Mapper.ReplaceMapping<Entry, IEntryHandler>(nameof(TextTransform), MapText);

// Material3 Entry Handler mappings
#if ANDROID
if (RuntimeFeature.IsMaterial3Enabled)
{
EntryHandler2.Mapper.ReplaceMapping<Entry, EntryHandler2>(PlatformConfiguration.AndroidSpecific.Entry.ImeOptionsProperty.PropertyName, MapImeOptions);
EntryHandler2.Mapper.ReplaceMapping<Entry, EntryHandler2>(nameof(Text), MapText);
Material3TextMapping = RegisterTextMapping<Entry, EntryHandler2>(EntryHandler2.Mapper, MapText);
EntryHandler2.Mapper.ReplaceMapping<Entry, EntryHandler2>(nameof(TextTransform), MapText);
EntryHandler2.Mapper.AppendToMapping(nameof(VisualElement.IsFocused), InputView.MapIsFocused);
EntryHandler2.Mapper.AppendToMapping(nameof(VisualElement.IsVisible), InputView.MapIsVisible);
Expand Down
111 changes: 110 additions & 1 deletion src/Controls/src/Core/InputView/InputView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
using Microsoft.Maui.Controls.Internals;

using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
#if IOS || MACCATALYST || WINDOWS
using Microsoft.Maui.Platform;
#endif
#if IOS || MACCATALYST
using UIKit;
#endif

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -312,7 +319,109 @@ int ITextInput.CursorPosition
string ITextInput.Text
{
get => Text;
set => SetValue(TextProperty, value, SetterSpecificity.FromHandler);
set
{
#if WINDOWS
if (this is SearchBar && Handler is SearchBarHandler)
{
// AutoSuggestBox ignores the mapper's programmatic echo: only the display is transformed.
SetValue(TextProperty, value, SetterSpecificity.FromHandler);
return;
}
#endif
// Normalize before notifying subscribers, without changing the casing of passwords.
var transformedText = value;
if (value is not null && (Handler is null || UsesDefaultTextMapping()) && !IsPasswordInput)
{
var textElement = GetHandlerTextElement();
var transform = textElement?.TextTransform ?? TextTransform;
if (transform is TextTransform.Lowercase or TextTransform.Uppercase)
transformedText = textElement is not null
? textElement.UpdateFormsText(value, transform)
: TextTransformUtilities.GetTransformedText(value, transform);
}

if (value != transformedText && Text == transformedText)
{
// The native edit needs correcting even though the managed value hasn't changed.
Handler?.UpdateValue(nameof(ITextInput.Text));
return;
}

SetValue(TextProperty, transformedText, SetterSpecificity.FromHandler);
}
}

private protected static Action<IElementHandler, IElement> RegisterTextMapping<TView, THandler>(
IPropertyMapper<IElement, IElementHandler> mapper, Action<THandler, TView> mapText)
where TView : InputView where THandler : IElementHandler
{
mapper.ReplaceMapping<TView, THandler>(nameof(Text), mapText);
return mapper.GetProperty(nameof(Text));
}

bool UsesDefaultTextMapping()
{
if (Handler is not ElementHandler handler)
return false;

var mapping = handler._mapper.GetProperty(nameof(Text));
if (mapping is null)
return false;

// Compare the active mapping, not the handler type or mutable global mapper.
// Controls can remap Text, but a stock handler can also be used without that setup.
return (this, handler) switch
{
#if ANDROID
(Entry, EntryHandler2) => mapping == Entry.Material3TextMapping || mapping == EntryHandler2.BuiltInTextMapping,
(Editor, EditorHandler2) => mapping == Editor.Material3TextMapping || mapping == EditorHandler2.BuiltInTextMapping,
(SearchBar, SearchBarHandler2) => mapping == SearchBar.Material3TextMapping || mapping == SearchBarHandler2.BuiltInTextMapping,
#endif
(Entry, IEntryHandler) => mapping == Entry.DefaultTextMapping || mapping == EntryHandler.BuiltInTextMapping,
(Editor, IEditorHandler) => mapping == Editor.DefaultTextMapping || mapping == EditorHandler.BuiltInTextMapping,
(SearchBar, ISearchBarHandler) => mapping == SearchBar.DefaultTextMapping || mapping == SearchBarHandler.BuiltInTextMapping,
Comment on lines +381 to +383
_ => false
};
}

ITextElement GetHandlerTextElement()
{
// Match the native mappers that dispatch through ITextElement.
#if ANDROID
if (this is Entry or Editor || (this is SearchBar && Handler is SearchBarHandler2))
return this;
#elif IOS || MACCATALYST
if (this is Entry or Editor)
return this;
#endif
return null;
}

bool IsPasswordInput
{
get
{
#if ANDROID
if (this is SearchBar && Handler is SearchBarHandler2 searchBarHandler)
return Platform.EditTextExtensions.IsPasswordInput(searchBarHandler.QueryEditor, this);

if (this is Entry or Editor)
{
var editText = Handler is EntryHandler2 entryHandler
? entryHandler.PlatformView?.EditText
: Handler?.PlatformView as global::Android.Widget.EditText;
return Platform.EditTextExtensions.IsPasswordInput(editText, this);
}
#elif IOS || MACCATALYST
if ((this is Entry or Editor) && Handler?.PlatformView is IUITextInput textInput && textInput.GetSecureTextEntry())
return true;
#elif WINDOWS
if (this is Entry && Handler?.PlatformView is MauiPasswordTextBox { IsPassword: true })
return true;
#endif
return this is Entry entry && entry.IsPassword;
}
Comment thread
github-actions[bot] marked this conversation as resolved.
Resolved
}

private protected override void OnBindablePropertySet(BindableProperty property, object original, object value, bool changed, bool willFirePropertyChanged)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ internal static (string oldText, string newText) GetTexts(EditText editText, str
{
var oldText = editText.Text ?? string.Empty;

var inputType = editText.InputType;
var newText = textElement.UpdateFormsText(text,
IsPasswordInput(editText, textElement) ? TextTransform.None : textElement.TextTransform);

return (oldText, newText);
}

internal static bool IsPasswordInput(EditText editText, ITextElement textElement)
{
// A read-only EditText has no InputType flags (TYPE_NULL), so check what sets the password ones too:
// IsPassword on an Entry, the keyboard on other inputs
bool isPasswordEnabled =
(textElement is Entry entry ? entry.IsPassword : textElement is InputView inputView && inputView.Keyboard == Keyboard.Password) ||
(inputType.HasFlag(InputTypes.ClassText) &&
if (textElement is Entry entry ? entry.IsPassword : textElement is InputView inputView && inputView.Keyboard == Keyboard.Password)
return true;

if (editText is null)
return false;

var inputType = editText.InputType;
return (inputType.HasFlag(InputTypes.ClassText) &&
inputType.HasFlag(InputTypes.TextVariationPassword)) ||
(inputType.HasFlag(InputTypes.ClassNumber) &&
inputType.HasFlag(InputTypes.NumberVariationPassword));

var newText = textElement.UpdateFormsText(text,
isPasswordEnabled ? TextTransform.None : textElement.TextTransform);

return (oldText, newText);
}

public static void UpdateText(this EditText editText, InputView inputView) =>
Expand Down
11 changes: 8 additions & 3 deletions src/Controls/src/Core/SearchBar/SearchBar.Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Microsoft.Maui.Controls
{
public partial class SearchBar
{
internal static Action<IElementHandler, IElement> DefaultTextMapping { get; private set; }
#if ANDROID
internal static Action<IElementHandler, IElement> Material3TextMapping { get; private set; }
#endif

static SearchBar()
{
// Register dependency: SearchCommand depends on SearchCommandParameter for CanExecute evaluation
Expand All @@ -26,16 +31,16 @@ static SearchBar()
if (RuntimeFeature.IsMaterial3Enabled)
{
// Material3 SearchBar handler mappings
SearchBarHandler2.Mapper.ReplaceMapping<SearchBar, SearchBarHandler2>(nameof(Text), MapText);
Material3TextMapping = RegisterTextMapping<SearchBar, SearchBarHandler2>(SearchBarHandler2.Mapper, MapText);
SearchBarHandler2.Mapper.ReplaceMapping<SearchBar, SearchBarHandler2>(nameof(TextTransform), MapText);
}
else
{
SearchBarHandler.Mapper.ReplaceMapping<SearchBar, ISearchBarHandler>(nameof(Text), MapText);
DefaultTextMapping = RegisterTextMapping<SearchBar, ISearchBarHandler>(SearchBarHandler.Mapper, MapText);
SearchBarHandler.Mapper.ReplaceMapping<SearchBar, ISearchBarHandler>(nameof(TextTransform), MapText);
}
#else
SearchBarHandler.Mapper.ReplaceMapping<SearchBar, ISearchBarHandler>(nameof(Text), MapText);
DefaultTextMapping = RegisterTextMapping<SearchBar, ISearchBarHandler>(SearchBarHandler.Mapper, MapText);
SearchBarHandler.Mapper.ReplaceMapping<SearchBar, ISearchBarHandler>(nameof(TextTransform), MapText);
#endif

Expand Down
105 changes: 105 additions & 0 deletions src/Controls/tests/Core.UnitTests/InputViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,117 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests
{
public class InputViewTests : BaseTestFixture
{
[Theory]
[InlineData(TextTransform.Uppercase, "d", "D")]
[InlineData(TextTransform.Lowercase, "D", "d")]
[InlineData(TextTransform.None, "d", "d")]
public void HandlerTextChangesRaiseOneEventWithTransformedText(TextTransform transform, string input, string expected)
{
foreach (var view in new InputView[] { new Entry(), new Editor(), new SearchBar() })
{
view.TextTransform = transform;
view.Text = "123";
var changes = new List<TextChangedEventArgs>();
view.TextChanged += (_, args) => changes.Add(args);

var textInput = (ITextInput)view;
textInput.Text = input;
textInput.Text = expected; // Simulate the native echo after mapping.

Assert.Equal(expected, view.Text);
var change = Assert.Single(changes);
Assert.Equal("123", change.OldTextValue);
Assert.Equal(expected, change.NewTextValue);
}
}

[Theory]
[InlineData(TextTransform.Uppercase)]
[InlineData(TextTransform.Lowercase)]
public void HandlerTextChangesPreservePasswordCasing(TextTransform transform)
{
var entry = new Entry { IsPassword = true, TextTransform = transform };
var boundEntry = new Entry();
entry.SetBinding(InputView.TextProperty, new Binding(nameof(InputView.Text), source: boundEntry));
var changes = new List<TextChangedEventArgs>();
entry.TextChanged += (_, args) => changes.Add(args);

((ITextInput)entry).Text = "aB";

Assert.Equal("aB", entry.Text);
Assert.Equal("aB", boundEntry.Text);
Assert.Equal("aB", Assert.Single(changes).NewTextValue);
}

[Theory]
[InlineData(TextTransform.Uppercase, "aB")]
[InlineData(TextTransform.Lowercase, "Ab")]
public void ReplacementTextMappingPreservesRawInput(TextTransform transform, string input)
{
var entry = new Entry { TextTransform = transform };
var source = new Entry { Text = "initial" };
entry.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source));
var handler = new ReplacementTextEntryHandler();
entry.Handler = handler;
var changes = new List<string>();
entry.TextChanged += (_, args) => changes.Add(args.NewTextValue);

handler.PlatformText = input;
((ITextInput)entry).UpdateText(input);

Assert.Equal(input, handler.PlatformText);
Assert.Equal(input, entry.Text);
Assert.Equal(input, source.Text);
Assert.Equal(input, Assert.Single(changes));
}

[Theory]
[InlineData(null)]
[InlineData("x")]
public void DirectTextInputNullAssignmentPreservesNull(string initialText)
{
var source = new Entry { Text = initialText };
var editor = new Editor();
editor.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source));
var changes = new List<TextChangedEventArgs>();
editor.TextChanged += (_, args) => changes.Add(args);

((ITextInput)editor).Text = null;

Assert.Null(editor.Text);
Assert.Null(source.Text);
if (initialText is null)
Assert.Empty(changes);
else
{
var change = Assert.Single(changes);
Assert.Equal(initialText, change.OldTextValue);
Assert.Null(change.NewTextValue);
}
}

class ReplacementTextEntryHandler : EntryHandler
{
static readonly PropertyMapper<IEntry, ReplacementTextEntryHandler> ReplacementMapper = new(EntryHandler.Mapper)
{
[nameof(IEntry.Text)] = (handler, view) => handler.PlatformText = view.Text,
};

public ReplacementTextEntryHandler() : base(ReplacementMapper)
{
}

public string PlatformText { get; set; }

protected override object CreatePlatformView() => new object();
}

[Theory]
[InlineData(typeof(Entry))]
Expand Down
Loading
Loading