Skip to content
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

Implement Keyboard property in WinUI Entry #1554

Merged
merged 7 commits into from
Sep 23, 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
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/Windows/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ void UpdateClearButtonVisibility()
Control.ClearButtonVisible = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing;
}

[PortHandler("Pending to port IsSpellCheckEnabled")]
void UpdateInputScope()
{
Entry entry = Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<Entry
Text="Text"
Background="Orange"/>
<Entry
Text="Keyboard"
Style="{StaticResource Headline}"
Keyboard="Numeric"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
Expand Down
6 changes: 4 additions & 2 deletions src/Core/src/Handlers/Entry/EntryHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ public static void MapCharacterSpacing(EntryHandler handler, IEntry entry)
handler.NativeView?.UpdateCharacterSpacing(entry);
}

[MissingMapper]
public static void MapKeyboard(IViewHandler handler, IEntry entry) { }
public static void MapKeyboard(EntryHandler handler, IEntry entry)
{
handler.NativeView?.UpdateKeyboard(entry);
}

void OnNativeKeyUp(object? sender, KeyRoutedEventArgs args)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Core/src/Platform/Windows/KeyboardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ namespace Microsoft.Maui
{
public static class KeyboardExtensions
{
public static InputScope ToInputScope(this Keyboard self)
public static InputScopeName ToInputScopeName(this Keyboard self)
{
if (self == null)
throw new ArgumentNullException("self");
throw new ArgumentNullException(nameof(self));

var result = new InputScope();
var name = new InputScopeName();

if (self == Keyboard.Default)
Expand Down Expand Up @@ -79,8 +78,7 @@ public static InputScope ToInputScope(this Keyboard self)
name.NameValue = nameValue;
}

result.Names.Add(name);
return result;
return name;
}
}
}
13 changes: 9 additions & 4 deletions src/Core/src/Platform/Windows/MauiTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,26 @@ await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>

static string DetermineTextFromPassword(string realText, int start, string passwordText)
{
var lengthDifference = passwordText.Length - realText.Length;
var rt = realText ?? string.Empty;

var lengthDifference = passwordText.Length - (rt?.Length ?? 0);
if (lengthDifference > 0)
realText = realText.Insert(start - lengthDifference, new string(ObfuscationCharacter, lengthDifference));
rt = rt.Insert(start - lengthDifference, new string(ObfuscationCharacter, lengthDifference));
else if (lengthDifference < 0)
realText = realText.Remove(start, -lengthDifference);
rt = rt.Remove(start, -lengthDifference);

var sb = new StringBuilder(passwordText.Length);
for (int i = 0; i < passwordText.Length; i++)
sb.Append(passwordText[i] == ObfuscationCharacter ? realText[i] : passwordText[i]);
sb.Append(passwordText[i] == ObfuscationCharacter ? rt[i] : passwordText[i]);

return sb.ToString();
}

string Obfuscate(string text, bool leaveLastVisible = false)
{
if (string.IsNullOrEmpty(text))
return string.Empty;

if (!leaveLastVisible)
return new string(ObfuscationCharacter, text.Length);

Expand Down
40 changes: 0 additions & 40 deletions src/Core/src/Platform/Windows/ReturnTypeExtensions.cs

This file was deleted.

26 changes: 19 additions & 7 deletions src/Core/src/Platform/Windows/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml.Media;

Expand Down Expand Up @@ -46,8 +47,8 @@ public static void UpdateCharacterSpacing(this MauiTextBox textBox, IEntry entry

public static void UpdateReturnType(this MauiTextBox textBox, IEntry entry)
{
textBox.InputScope = entry.ReturnType.ToNative();
}
textBox.UpdateInputScope(entry);
}

public static void UpdateClearButtonVisibility(this MauiTextBox textBox, IEntry entry)
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public static void UpdateMaxLength(this MauiTextBox textBox, IEntry entry)
if (currentControlText.Length > maxLength)
textBox.Text = currentControlText.Substring(0, maxLength);
}

public static void UpdateIsPassword(this MauiTextBox textBox, IEntry entry)
{
textBox.IsPassword = entry.IsPassword;
Expand All @@ -138,11 +139,17 @@ internal static void UpdateInputScope(this MauiTextBox textBox, ITextInput textI
else
{
textBox.IsTextPredictionEnabled = textInput.IsTextPredictionEnabled;

// TODO: Update IsSpellCheckEnabled
textBox.IsSpellCheckEnabled = textInput.IsTextPredictionEnabled;
}

textBox.InputScope = textInput.Keyboard.ToInputScope();
var inputScope = new UI.Xaml.Input.InputScope();

if (textInput is IEntry entry && entry.ReturnType == ReturnType.Search)
inputScope.Names.Add(new UI.Xaml.Input.InputScopeName(UI.Xaml.Input.InputScopeNameValue.Search));

inputScope.Names.Add(textInput.Keyboard.ToInputScopeName());

textBox.InputScope = inputScope;
}

public static void UpdateHorizontalTextAlignment(this MauiTextBox textBox, IEntry entry)
Expand All @@ -156,6 +163,11 @@ public static void UpdateHorizontalTextAlignment(this MauiTextBox textBox, IEntr
public static void UpdateVerticalTextAlignment(this MauiTextBox textBox, IEntry entry)
{
textBox.VerticalAlignment = entry.VerticalTextAlignment.ToNativeVerticalAlignment();
}
}

public static void UpdateKeyboard(this MauiTextBox textBox, IEntry entry)
{
textBox.UpdateInputScope(entry);
}
}
}