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

[GH-473] Port Font to Editor #503

Merged
merged 10 commits into from
Mar 24, 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 @@ -210,6 +210,7 @@ internal override void OnNativeFocusChanged(bool hasFocus)
ElementController.SendCompleted();
}

[PortHandler]
protected virtual void UpdateFont()
{
EditText.Typeface = Element.ToTypeface();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ void UpdateEditable()
TextView.InputAccessoryView.Hidden = !Element.IsEnabled;
}

[PortHandler]
protected internal virtual void UpdateFont()
{
var font = Element.ToUIFont();
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 @@ -90,6 +90,7 @@ void SetupMauiLayout()
verticalStack.Add(new Editor { Text = "Editor" });
verticalStack.Add(new Editor { Text = "Lorem ipsum dolor sit amet", MaxLength = 10 });
verticalStack.Add(new Editor { Text = "Predictive Text Off", IsTextPredictionEnabled = false });
verticalStack.Add(new Editor { Text = "Lorem ipsum dolor sit amet", FontSize = 10, FontFamily = "dokdo_regular"});

var entry = new Entry();
entry.TextChanged += (sender, e) =>
Expand Down
13 changes: 12 additions & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Android.Views;
using System;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Maui.Handlers
{
Expand Down Expand Up @@ -40,5 +42,14 @@ public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor edi
{
handler.TypedNativeView?.UpdateIsTextPredictionEnabled(editor);
}

public static void MapFont(EditorHandler handler, IEditor editor)
{
var services = handler.Services
?? throw new InvalidOperationException($"Unable to find service provider, the handler.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(editor, fontManager);
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Editor/EditorHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public partial class EditorHandler : AbstractViewHandler<IEditor, object>
public static void MapCharacterSpacing(IViewHandler handler, IEditor editor) { }
public static void MapMaxLength(IViewHandler handler, IEditor editor) { }
public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor editor) { }
public static void MapFont(IViewHandler 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 @@ -7,7 +7,8 @@ public partial class EditorHandler
[nameof(IEditor.Text)] = MapText,
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing,
[nameof(IEditor.MaxLength)] = MapMaxLength,
[nameof(IEditor.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled
[nameof(IEditor.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled,
[nameof(IEditor.Font)] = MapFont
};

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

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -75,5 +77,14 @@ bool OnShouldChangeText(UITextView textView, NSRange range, string replacementSt

return newLength <= VirtualView.MaxLength;
}

public static void MapFont(EditorHandler handler, IEditor editor)
{
var services = handler.Services ??
throw new InvalidOperationException($"Unable to find service provider, the handler.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(editor, fontManager);
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public static void UpdateReturnType(this AppCompatEditText editText, IEntry entr
editText.ImeOptions = entry.ReturnType.ToNative();
}

public static void UpdateFont(this AppCompatEditText editText, IEditor editor, IFontManager fontManager)
{
var font = editor.Font;

var tf = fontManager.GetTypeface(font);
editText.Typeface = tf;

var sp = fontManager.GetScaledPixel(font);
editText.SetTextSize(Android.Util.ComplexUnitType.Sp, sp);
}

internal static void SetInputType(this AppCompatEditText editText, IEntry entry)
{
editText.InputType = InputTypes.ClassText;
Expand Down
6 changes: 6 additions & 0 deletions src/Core/src/Platform/iOS/TextViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ public static void UpdatePredictiveText(this UITextView textView, IEditor editor
textView.AutocorrectionType = editor.IsTextPredictionEnabled
? UITextAutocorrectionType.Yes : UITextAutocorrectionType.No;
}

public static void UpdateFont(this UITextView textView, IEditor editor, IFontManager fontManager)
{
var uiFont = fontManager.GetFont(editor.Font);
textView.Font = uiFont;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Android.Text;
using AndroidX.AppCompat.Widget;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;
Expand Down Expand Up @@ -34,6 +35,33 @@ public async Task CharacterSpacingInitializesCorrectly()
Assert.Equal(xplatCharacterSpacing, values.ViewValue);
Assert.Equal(expectedValue, values.NativeViewValue, EmCoefficientPrecision);
}

[Theory(DisplayName = "Font Family Initializes Correctly")]
[InlineData(null)]
[InlineData("monospace")]
[InlineData("Dokdo")]
public async Task FontFamilyInitializesCorrectly(string family)
{
var editor = new EditorStub()
{
Text = "Test",
Font = Font.OfSize(family, 10)
};

var handler = await CreateHandlerAsync(editor);
var nativeEditor = GetNativeEditor(handler);

var fontManager = handler.Services.GetRequiredService<IFontManager>();

var nativeFont = fontManager.GetTypeface(Font.OfSize(family, 0.0));

Assert.Equal(nativeFont, nativeEditor.Typeface);

if (string.IsNullOrEmpty(family))
Assert.Equal(fontManager.DefaultTypeface, nativeEditor.Typeface);
else
Assert.NotEqual(fontManager.DefaultTypeface, nativeEditor.Typeface);
}

AppCompatEditText GetNativeEditor(EditorHandler editorHandler) =>
(AppCompatEditText)editorHandler.View;
Expand All @@ -55,5 +83,11 @@ public async Task CharacterSpacingInitializesCorrectly()

return -1;
}

double GetNativeUnscaledFontSize(EditorHandler editorHandler)
{
var textView = GetNativeEditor(editorHandler);
return textView.TextSize / textView.Resources.DisplayMetrics.Density;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,21 @@ public async Task IsTextPredictionEnabledUpdatesCorrectly(bool setValue, bool un
setValue,
unsetValue);
}

[Theory(DisplayName = "Font Size Initializes Correctly")]
[InlineData(1)]
[InlineData(10)]
[InlineData(20)]
[InlineData(100)]
public async Task FontSizeInitializesCorrectly(int fontSize)
{
var editor = new EditorStub()
{
Text = "Test",
Font = Font.OfSize("Arial", fontSize)
};

await ValidatePropertyInitValue(editor, () => editor.Font.FontSize, GetNativeUnscaledFontSize, editor.Font.FontSize);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using UIKit;
Expand Down Expand Up @@ -33,6 +34,32 @@ public async Task CharacterSpacingInitializesCorrectly()
Assert.Equal(xplatCharacterSpacing, values.NativeViewValue);
}

[Theory(DisplayName = "Font Family Initializes Correctly")]
[InlineData(null)]
[InlineData("Times New Roman")]
[InlineData("Dokdo")]
public async Task FontFamilyInitializesCorrectly(string family)
{
var editor = new EditorStub()
{
Text = "Test",
Font = Font.OfSize(family, 10)
};

var handler = await CreateHandlerAsync(editor);
var nativeFont = await GetValueAsync(editor, handler => GetNativeEditor(handler).Font);

var fontManager = handler.Services.GetRequiredService<IFontManager>();

var expectedNativeFont = fontManager.GetFont(Font.OfSize(family, 0.0));

Assert.Equal(expectedNativeFont.FamilyName, nativeFont.FamilyName);
if (string.IsNullOrEmpty(family))
Assert.Equal(fontManager.DefaultFont.FamilyName, nativeFont.FamilyName);
else
Assert.NotEqual(fontManager.DefaultFont.FamilyName, nativeFont.FamilyName);
}

UITextView GetNativeEditor(EditorHandler editorHandler) =>
(UITextView)editorHandler.View;

Expand All @@ -47,5 +74,8 @@ public async Task CharacterSpacingInitializesCorrectly()

bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).AutocorrectionType == UITextAutocorrectionType.Yes;

double GetNativeUnscaledFontSize(EditorHandler editorHandler) =>
GetNativeEditor(editorHandler).Font.PointSize;
}
}