Skip to content

Commit b83f98a

Browse files
authored
Implement Font properties in PickerHandlers (#589)
1 parent 0736879 commit b83f98a

File tree

9 files changed

+100
-9
lines changed

9 files changed

+100
-9
lines changed

Diff for: src/Compatibility/Core/src/Android/AppCompat/PickerRenderer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ void RowsCollectionChanged(object sender, EventArgs e)
155155
UpdatePicker();
156156
}
157157

158+
[PortHandler]
158159
void UpdateFont()
159160
{
160161
EditText.Typeface = Element.ToTypeface();

Diff for: src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ protected void UpdateCharacterSpacing()
203203
UpdateAttributedPlaceholder(placeHolder);
204204
}
205205

206+
[PortHandler]
206207
protected internal virtual void UpdateFont()
207208
{
208209
Control.Font = Element.ToUIFont();

Diff for: src/Controls/samples/Controls.Sample/Pages/MainPage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void SetupMauiLayout()
171171
"Japanese Macaque"
172172
};
173173

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

176176
picker.ItemsSource = monkeyList;
177177
verticalStack.Add(picker);

Diff for: src/Core/src/Handlers/Picker/PickerHandler.Android.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Specialized;
33
using System.Linq;
44
using Android.App;
5+
using Microsoft.Extensions.DependencyInjection;
56
using AResource = Android.Resource;
67

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

52-
[MissingMapper]
53-
public static void MapFont(PickerHandler handler, IPicker view) { }
53+
public static void MapFont(PickerHandler handler, IPicker picker)
54+
{
55+
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
56+
57+
var fontManager = handler.Services.GetRequiredService<IFontManager>();
58+
59+
handler.NativeView?.UpdateFont(picker, fontManager);
60+
}
5461

5562
[MissingMapper]
5663
public static void MapTextColor(PickerHandler handler, IPicker view) { }

Diff for: src/Core/src/Handlers/Picker/PickerHandler.iOS.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Specialized;
3+
using Microsoft.Extensions.DependencyInjection;
34
using UIKit;
45
using RectangleF = CoreGraphics.CGRect;
56

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

109-
[MissingMapper]
110-
public static void MapFont(PickerHandler handler, IPicker view) { }
110+
public static void MapFont(PickerHandler handler, IPicker picker)
111+
{
112+
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
113+
114+
var fontManager = handler.Services.GetRequiredService<IFontManager>();
115+
116+
handler.NativeView?.UpdateFont(picker, fontManager);
117+
}
111118

112119
[MissingMapper]
113120
public static void MapTextColor(PickerHandler handler, IPicker view) { }

Diff for: src/Core/src/Platform/iOS/PickerExtensions.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using Foundation;
32
using Microsoft.Maui.Handlers;
4-
using UIKit;
53

64
namespace Microsoft.Maui
75
{
@@ -39,7 +37,7 @@ internal static void SetSelectedIndex(this MauiPicker nativePicker, IPicker pick
3937
if (pickerView?.Model is PickerSource source)
4038
{
4139
source.SelectedIndex = selectedIndex;
42-
source.SelectedItem = selectedIndex >= 0 ? picker.Items[selectedIndex] : null;
40+
source.SelectedItem = (selectedIndex >= 0 && picker.Items.Count > selectedIndex) ? picker.Items[selectedIndex] : null;
4341
}
4442

4543
pickerView?.Select(Math.Max(selectedIndex, 0), 0, true);

Diff for: src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs

+12
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,17 @@ double GetNativeCharacterSpacing(PickerHandler pickerHandler)
7272

7373
return -1;
7474
}
75+
76+
double GetNativeUnscaledFontSize(PickerHandler pickerHandler)
77+
{
78+
var mauiPicker = GetNativePicker(pickerHandler);
79+
return mauiPicker.TextSize / mauiPicker.Resources.DisplayMetrics.Density;
80+
}
81+
82+
bool GetNativeIsBold(PickerHandler pickerHandler) =>
83+
GetNativePicker(pickerHandler).Typeface.IsBold;
84+
85+
bool GetNativeIsItalic(PickerHandler pickerHandler) =>
86+
GetNativePicker(pickerHandler).Typeface.IsItalic;
7587
}
7688
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1-
using Microsoft.Maui.DeviceTests.Stubs;
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.Maui.DeviceTests.Stubs;
24
using Microsoft.Maui.Handlers;
5+
using Xunit;
36

47
namespace Microsoft.Maui.DeviceTests
58
{
69
[Category(TestCategory.Picker)]
710
public partial class PickerHandlerTests : HandlerTestBase<PickerHandler, PickerStub>
811
{
12+
[Theory(DisplayName = "Font Size Initializes Correctly")]
13+
[InlineData(1)]
14+
[InlineData(10)]
15+
[InlineData(20)]
16+
[InlineData(100)]
17+
public async Task FontSizeInitializesCorrectly(int fontSize)
18+
{
19+
var items = new List<string>
20+
{
21+
"Item 1",
22+
"Item 2",
23+
"Item 3"
24+
};
25+
26+
var picker = new PickerStub()
27+
{
28+
Title = "Select an Item",
29+
Font = Font.OfSize("Arial", fontSize)
30+
};
31+
32+
picker.ItemsSource = items;
33+
picker.SelectedIndex = 0;
34+
35+
await ValidatePropertyInitValue(picker, () => picker.Font.FontSize, GetNativeUnscaledFontSize, picker.Font.FontSize);
36+
}
37+
38+
[Theory(DisplayName = "Font Attributes Initialize Correctly")]
39+
[InlineData(FontAttributes.None, false, false)]
40+
[InlineData(FontAttributes.Bold, true, false)]
41+
[InlineData(FontAttributes.Italic, false, true)]
42+
[InlineData(FontAttributes.Bold | FontAttributes.Italic, true, true)]
43+
public async Task FontAttributesInitializeCorrectly(FontAttributes attributes, bool isBold, bool isItalic)
44+
{
45+
var items = new List<string>
46+
{
47+
"Item 1",
48+
"Item 2",
49+
"Item 3"
50+
};
51+
52+
var picker = new PickerStub()
53+
{
54+
Title = "Select an Item",
55+
Font = Font.OfSize("Arial", 10).WithAttributes(attributes)
56+
};
57+
58+
picker.ItemsSource = items;
59+
picker.SelectedIndex = 0;
60+
61+
await ValidatePropertyInitValue(picker, () => picker.Font.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold);
62+
await ValidatePropertyInitValue(picker, () => picker.Font.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic);
63+
}
964
}
1065
}

Diff for: src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.iOS.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using Microsoft.Maui.Handlers;
3+
using UIKit;
34
using Xunit;
45

56
namespace Microsoft.Maui.DeviceTests
@@ -36,5 +37,14 @@ async Task ValidateNativeSelectedIndex(IPicker slider, int selectedIndex)
3637
});
3738
Assert.Equal(expected, selectedIndex);
3839
}
40+
41+
double GetNativeUnscaledFontSize(PickerHandler pickerHandler) =>
42+
GetNativePicker(pickerHandler).Font.PointSize;
43+
44+
bool GetNativeIsBold(PickerHandler pickerHandler) =>
45+
GetNativePicker(pickerHandler).Font.FontDescriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Bold);
46+
47+
bool GetNativeIsItalic(PickerHandler pickerHandler) =>
48+
GetNativePicker(pickerHandler).Font.FontDescriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Italic);
3949
}
4050
}

0 commit comments

Comments
 (0)