Skip to content

Commit

Permalink
Implement Font properties in PickerHandlers (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Apr 7, 2021
1 parent 0736879 commit b83f98a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void RowsCollectionChanged(object sender, EventArgs e)
UpdatePicker();
}

[PortHandler]
void UpdateFont()
{
EditText.Typeface = Element.ToTypeface();
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ protected void UpdateCharacterSpacing()
UpdateAttributedPlaceholder(placeHolder);
}

[PortHandler]
protected internal virtual void UpdateFont()
{
Control.Font = Element.ToUIFont();
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void SetupMauiLayout()
"Japanese Macaque"
};

var picker = new Picker { Title = "Select a monkey" };
var picker = new Picker { Title = "Select a monkey", FontFamily = "Dokdo" };

picker.ItemsSource = monkeyList;
verticalStack.Add(picker);
Expand Down
11 changes: 9 additions & 2 deletions src/Core/src/Handlers/Picker/PickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Specialized;
using System.Linq;
using Android.App;
using Microsoft.Extensions.DependencyInjection;
using AResource = Android.Resource;

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -49,8 +50,14 @@ public static void MapCharacterSpacing(PickerHandler handler, IPicker picker)
handler.NativeView?.UpdateCharacterSpacing(picker);
}

[MissingMapper]
public static void MapFont(PickerHandler handler, IPicker view) { }
public static void MapFont(PickerHandler handler, IPicker picker)
{
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

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

handler.NativeView?.UpdateFont(picker, fontManager);
}

[MissingMapper]
public static void MapTextColor(PickerHandler handler, IPicker view) { }
Expand Down
11 changes: 9 additions & 2 deletions src/Core/src/Handlers/Picker/PickerHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Specialized;
using Microsoft.Extensions.DependencyInjection;
using UIKit;
using RectangleF = CoreGraphics.CGRect;

Expand Down Expand Up @@ -106,8 +107,14 @@ public static void MapCharacterSpacing(PickerHandler handler, IPicker picker)
handler.NativeView?.UpdateCharacterSpacing(picker);
}

[MissingMapper]
public static void MapFont(PickerHandler handler, IPicker view) { }
public static void MapFont(PickerHandler handler, IPicker picker)
{
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

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

handler.NativeView?.UpdateFont(picker, fontManager);
}

[MissingMapper]
public static void MapTextColor(PickerHandler handler, IPicker view) { }
Expand Down
4 changes: 1 addition & 3 deletions src/Core/src/Platform/iOS/PickerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using Foundation;
using Microsoft.Maui.Handlers;
using UIKit;

namespace Microsoft.Maui
{
Expand Down Expand Up @@ -39,7 +37,7 @@ internal static void SetSelectedIndex(this MauiPicker nativePicker, IPicker pick
if (pickerView?.Model is PickerSource source)
{
source.SelectedIndex = selectedIndex;
source.SelectedItem = selectedIndex >= 0 ? picker.Items[selectedIndex] : null;
source.SelectedItem = (selectedIndex >= 0 && picker.Items.Count > selectedIndex) ? picker.Items[selectedIndex] : null;
}

pickerView?.Select(Math.Max(selectedIndex, 0), 0, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,17 @@ double GetNativeCharacterSpacing(PickerHandler pickerHandler)

return -1;
}

double GetNativeUnscaledFontSize(PickerHandler pickerHandler)
{
var mauiPicker = GetNativePicker(pickerHandler);
return mauiPicker.TextSize / mauiPicker.Resources.DisplayMetrics.Density;
}

bool GetNativeIsBold(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Typeface.IsBold;

bool GetNativeIsItalic(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Typeface.IsItalic;
}
}
57 changes: 56 additions & 1 deletion src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
using Microsoft.Maui.DeviceTests.Stubs;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Picker)]
public partial class PickerHandlerTests : HandlerTestBase<PickerHandler, PickerStub>
{
[Theory(DisplayName = "Font Size Initializes Correctly")]
[InlineData(1)]
[InlineData(10)]
[InlineData(20)]
[InlineData(100)]
public async Task FontSizeInitializesCorrectly(int fontSize)
{
var items = new List<string>
{
"Item 1",
"Item 2",
"Item 3"
};

var picker = new PickerStub()
{
Title = "Select an Item",
Font = Font.OfSize("Arial", fontSize)
};

picker.ItemsSource = items;
picker.SelectedIndex = 0;

await ValidatePropertyInitValue(picker, () => picker.Font.FontSize, GetNativeUnscaledFontSize, picker.Font.FontSize);
}

[Theory(DisplayName = "Font Attributes Initialize Correctly")]
[InlineData(FontAttributes.None, false, false)]
[InlineData(FontAttributes.Bold, true, false)]
[InlineData(FontAttributes.Italic, false, true)]
[InlineData(FontAttributes.Bold | FontAttributes.Italic, true, true)]
public async Task FontAttributesInitializeCorrectly(FontAttributes attributes, bool isBold, bool isItalic)
{
var items = new List<string>
{
"Item 1",
"Item 2",
"Item 3"
};

var picker = new PickerStub()
{
Title = "Select an Item",
Font = Font.OfSize("Arial", 10).WithAttributes(attributes)
};

picker.ItemsSource = items;
picker.SelectedIndex = 0;

await ValidatePropertyInitValue(picker, () => picker.Font.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold);
await ValidatePropertyInitValue(picker, () => picker.Font.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using UIKit;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -36,5 +37,14 @@ async Task ValidateNativeSelectedIndex(IPicker slider, int selectedIndex)
});
Assert.Equal(expected, selectedIndex);
}

double GetNativeUnscaledFontSize(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Font.PointSize;

bool GetNativeIsBold(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Font.FontDescriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Bold);

bool GetNativeIsItalic(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).Font.FontDescriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Italic);
}
}

0 comments on commit b83f98a

Please sign in to comment.