Skip to content

Commit

Permalink
Implement Keyboard property in WinUI Editor (#1553)
Browse files Browse the repository at this point in the history
Co-authored-by: Rachel Kang <[email protected]>
  • Loading branch information
jsuarezruiz and rachelkang authored Jul 13, 2021
1 parent ffab305 commit 961ec34
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/WinUI/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ void UpdateFont()
_fontApplied = true;
}

[PortHandler("Still pending to port IsSpellCheckEnabled")]
void UpdateInputScope()
{
Editor editor = Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<Editor
Text="I am read only"
IsReadOnly="True"/>
<Label
Text="Keyboard"
Style="{StaticResource Headline}"/>
<Editor
Keyboard="Numeric"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
Expand Down
6 changes: 4 additions & 2 deletions src/Core/src/Handlers/Editor/EditorHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public static void MapIsReadOnly(EditorHandler handler, IEditor editor)
public static void MapTextColor(EditorHandler handler, IEditor editor) =>
handler.NativeView?.UpdateTextColor(editor);

[MissingMapper]
public static void MapKeyboard(EditorHandler handler, IEditor editor) { }
public static void MapKeyboard(EditorHandler handler, IEditor editor)
{
handler.NativeView?.UpdateKeyboard(editor);
}

void OnLostFocus(object? sender, RoutedEventArgs e)
{
Expand Down
86 changes: 86 additions & 0 deletions src/Core/src/Platform/Windows/KeyboardExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Diagnostics;
using Microsoft.UI.Xaml.Input;

namespace Microsoft.Maui
{
public static class KeyboardExtensions
{
public static InputScope ToInputScope(this Keyboard self)
{
if (self == null)
throw new ArgumentNullException("self");

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

if (self == Keyboard.Default)
{
name.NameValue = InputScopeNameValue.Default;
}
else if (self == Keyboard.Chat)
{
name.NameValue = InputScopeNameValue.Chat;
}
else if (self == Keyboard.Email)
{
name.NameValue = InputScopeNameValue.EmailSmtpAddress;
}
else if (self == Keyboard.Numeric)
{
name.NameValue = InputScopeNameValue.Number;
}
else if (self == Keyboard.Telephone)
{
name.NameValue = InputScopeNameValue.TelephoneNumber;
}
else if (self == Keyboard.Text)
{
name.NameValue = InputScopeNameValue.Default;
}
else if (self == Keyboard.Url)
{
name.NameValue = InputScopeNameValue.Url;
}
else
{
var custom = (CustomKeyboard)self;
var capitalizedSentenceEnabled = (custom.Flags & KeyboardFlags.CapitalizeSentence) == KeyboardFlags.CapitalizeSentence;
var capitalizedWordsEnabled = (custom.Flags & KeyboardFlags.CapitalizeWord) == KeyboardFlags.CapitalizeWord;
var capitalizedCharacterEnabled = (custom.Flags & KeyboardFlags.CapitalizeCharacter) == KeyboardFlags.CapitalizeCharacter;

var spellcheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) == KeyboardFlags.Spellcheck;
var suggestionsEnabled = (custom.Flags & KeyboardFlags.Suggestions) == KeyboardFlags.Suggestions;

InputScopeNameValue nameValue = InputScopeNameValue.Default;

if (capitalizedSentenceEnabled)
{
if (!spellcheckEnabled)
{
Debug.WriteLine(null, "CapitalizeSentence only works when spell check is enabled");
}
}
else if (capitalizedWordsEnabled)
{
if (!spellcheckEnabled)
{
Debug.WriteLine(null, "CapitalizeWord only works when spell check is enabled");
}

nameValue = InputScopeNameValue.NameOrPhoneNumber;
}

if (capitalizedCharacterEnabled)
{
Debug.WriteLine(null, "WinUI does not support CapitalizeCharacter");
}

name.NameValue = nameValue;
}

result.Names.Add(name);
return result;
}
}
}
22 changes: 22 additions & 0 deletions src/Core/src/Platform/Windows/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ public static void UpdateIsPassword(this MauiTextBox textBox, IEntry entry)
{
textBox.IsPassword = entry.IsPassword;
}

public static void UpdateKeyboard(this MauiTextBox textBox, IEditor editor)
{
textBox.UpdateInputScope(editor);
}

internal static void UpdateInputScope(this MauiTextBox textBox, ITextInput textInput)
{
if (textInput.Keyboard is CustomKeyboard custom)
{
textBox.IsTextPredictionEnabled = (custom.Flags & KeyboardFlags.Suggestions) != 0;
textBox.IsSpellCheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) != 0;
}
else
{
textBox.IsTextPredictionEnabled = textInput.IsTextPredictionEnabled;

// TODO: Update IsSpellCheckEnabled
}

textBox.InputScope = textInput.Keyboard.ToInputScope();
}

public static void UpdateHorizontalTextAlignment(this MauiTextBox textBox, IEntry entry)
{
Expand Down

0 comments on commit 961ec34

Please sign in to comment.