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 Font properties in PickerHandlers #589

Merged
merged 25 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6c382a2
Implement Font property in PickerHandlers
jsuarezruiz Mar 24, 2021
e7b0260
Fix test issue
jsuarezruiz Mar 25, 2021
00a4282
Updated sample
jsuarezruiz Mar 25, 2021
924092b
Seems CheckBox is also done on Windows
mattleibow Apr 6, 2021
ccb341a
Added a enw attribute for incomplete handlers
mattleibow Apr 6, 2021
a6890b1
Clean up the handler code a tiny bit
mattleibow Apr 6, 2021
6e1c6b0
Use the new ITextStyle
mattleibow Apr 7, 2021
06f8e4f
Fix tests
mattleibow Apr 7, 2021
dff6f23
Do not fire text changed if nothing changed
mattleibow Apr 7, 2021
01ba004
MaxLength == -1 crashes
mattleibow Apr 7, 2021
103d029
MaxLines < 0 == everything
mattleibow Apr 7, 2021
c8c6e72
Added a test
mattleibow Apr 7, 2021
6915a2a
Attach that event!
mattleibow Apr 7, 2021
ebfda45
That is correct, 2 events
mattleibow Apr 7, 2021
e367844
Fix null ref
mattleibow Apr 7, 2021
7c85853
Don't crash on -1 max length
mattleibow Apr 7, 2021
eb28089
nulls are uncool here
mattleibow Apr 7, 2021
aadd73c
Re-apply the formatting
mattleibow Apr 7, 2021
4ad7689
Merge remote-tracking branch 'origin/main' into dev/handlers
mattleibow Apr 7, 2021
c09e85a
Test the crashes
mattleibow Apr 7, 2021
330f36c
iOS is like Superman
mattleibow Apr 7, 2021
2aaecfb
No need to reset everything
mattleibow Apr 7, 2021
74bbecb
Merge branch 'main' into dev/handlers
mattleibow Apr 7, 2021
b952034
Merge branch 'dev/handlers' into picker-font
mattleibow Apr 7, 2021
59593ba
Merge remote-tracking branch 'origin/main' into picker-font
mattleibow Apr 7, 2021
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 @@ -155,6 +155,7 @@ void RowsCollectionChanged(object sender, EventArgs e)
UpdatePicker();
}

[PortHandler]
void UpdateFont()
{
EditText.Typeface = Element.ToTypeface();
Expand Down
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;
}
}
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);
}
}