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

Consolidate Maui Core to single Font property #416

Merged
merged 6 commits into from
Mar 10, 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 @@ -22,7 +22,9 @@ internal static Typeface ToTypeface(this IFontElement self)
if (self.IsDefault())
return Forms.FontManager.DefaultTypeface;

return Forms.FontManager.GetTypeface(Font.OfSize(self.FontFamily, self.FontSize).WithAttributes(self.FontAttributes));
var font = Font.OfSize(self.FontFamily, self.FontSize).WithAttributes(self.FontAttributes);

return Forms.FontManager.GetTypeface(font);
}
}
}
11 changes: 8 additions & 3 deletions src/Controls/src/Core/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,24 @@ protected override void OnBindingContextChanged()
}

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (Button)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
HandleFontChanged();

void HandleFontChanged()
{
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}

Aspect IImageElement.Aspect => Aspect.AspectFit;
ImageSource IImageElement.Source => ImageSource;
Expand Down
15 changes: 11 additions & 4 deletions src/Controls/src/Core/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (Entry)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void HandleFontChanged()
{
// Null out the Maui font value so it will be recreated next time it's accessed
_font = null;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}

public event EventHandler Completed;

Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Button.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ void IButton.Released()
{
(this as IButtonController).SendReleased();
}

Font IText.Font => Font;
}
}
13 changes: 13 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Entry.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
{
public partial class Entry : IEntry
{
Font? _font;

Font IText.Font
{
get
{
if (_font == null)
{
_font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
}

return _font.Value;
}
}
}
}
13 changes: 13 additions & 0 deletions src/Controls/src/Core/HandlerImpl/Label.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ namespace Microsoft.Maui.Controls
{
public partial class Label : ILabel
{
Font? _font;

Font IText.Font
{
get
{
if (_font == null)
{
_font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
}

return _font.Value;
}
}
}
}
13 changes: 10 additions & 3 deletions src/Controls/src/Core/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,23 @@ double IFontElement.FontSizeDefaultValueCreator() =>
Device.GetNamedSize(NamedSize.Default, (Label)this);

void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
HandleFontChanged();

void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
HandleFontChanged();

void HandleFontChanged()
{
// Null out the Maui font value so it will be recreated next time it's accessed
_font = null;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}

void ILineHeightElement.OnLineHeightChanged(double oldValue, double newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
Expand Down
23 changes: 0 additions & 23 deletions src/Core/src/Core/IFont.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Core/src/Core/ILabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Maui
/// <summary>
/// Represents a View that displays text.
/// </summary>
public interface ILabel : IView, IText, IFont
public interface ILabel : IView, IText
{
/// <summary>
/// Gets the space between the text of the Label and it's border.
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Core/IText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public interface IText
/// Gets the text color.
/// </summary>
Color TextColor { get; }

/// <summary>
/// Gets the font family, style and size of the font.
/// </summary>
Font Font { get; }
}
}
25 changes: 5 additions & 20 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Android.Widget;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;

namespace Microsoft.Maui.Handlers
{
Expand Down Expand Up @@ -32,30 +31,16 @@ public static void MapTextColor(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdateTextColor(label, DefaultTextColor);
}

public static void MapFontFamily(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapFontSize(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapFontAttributes(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapPadding(LabelHandler handler, ILabel label)

public static void MapPadding(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdatePadding(label);
}

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

handler.TypedNativeView?.UpdateFont(label, fontManager);
Expand Down
6 changes: 2 additions & 4 deletions src/Core/src/Handlers/Label/LabelHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ public partial class LabelHandler : AbstractViewHandler<ILabel, object>

public static void MapText(IViewHandler handler, ILabel label) { }
public static void MapTextColor(IViewHandler handler, ILabel label) { }
public static void MapFontFamily(LabelHandler handler, ILabel label) { }
public static void MapFontSize(LabelHandler handler, ILabel label) { }
public static void MapFontAttributes(LabelHandler handler, ILabel label) { }
public static void MapPadding(LabelHandler handler, ILabel label) { }
public static void MapFont(LabelHandler handler, ILabel label) { }
public static void MapPadding(LabelHandler handler, ILabel label) { }
}
}
4 changes: 1 addition & 3 deletions src/Core/src/Handlers/Label/LabelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ public partial class LabelHandler
{
[nameof(ILabel.TextColor)] = MapTextColor,
[nameof(ILabel.Text)] = MapText,
[nameof(ILabel.FontFamily)] = MapFontFamily,
[nameof(ILabel.FontSize)] = MapFontSize,
[nameof(ILabel.FontAttributes)] = MapFontAttributes,
[nameof(ILabel.Font)] = MapFont,
[nameof(ILabel.Padding)] = MapPadding,
};

Expand Down
20 changes: 3 additions & 17 deletions src/Core/src/Handlers/Label/LabelHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,15 @@ public static void MapTextColor(LabelHandler handler, ILabel label)
handler.TypedNativeView?.UpdateTextColor(label);
}

public static void MapFontFamily(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapFontSize(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapFontAttributes(LabelHandler handler, ILabel label)
{
MapFont(handler, label);
}

public static void MapPadding(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdatePadding(label);
}

static void MapFont(LabelHandler handler, ILabel label)
public static void MapFont(LabelHandler handler, ILabel label)
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
{
var services = App.Current?.Services ?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var services = App.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(label, fontManager);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void UpdateTextColor(this TextView textView, ILabel label, Color d

public static void UpdateFont(this TextView textView, ILabel label, IFontManager fontManager)
{
var font = label.GetFont();
var font = label.Font;

var tf = fontManager.GetTypeface(font);
textView.Typeface = tf;
Expand Down
8 changes: 0 additions & 8 deletions src/Core/src/Platform/FontExtensions.cs

This file was deleted.

4 changes: 1 addition & 3 deletions src/Core/src/Platform/iOS/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public static void UpdateTextColor(this UILabel nativeLabel, ILabel label)

public static void UpdateFont(this UILabel nativeLabel, ILabel label, IFontManager fontManager)
{
var font = label.GetFont();

var uiFont = fontManager.GetFont(font);
var uiFont = fontManager.GetFont(label.Font);
nativeLabel.Font = uiFont;
}

Expand Down
1 change: 0 additions & 1 deletion src/Core/src/Primitives/Font.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;

namespace Microsoft.Maui
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task FontFamilyInitializesCorrectly(string family)
var label = new LabelStub()
{
Text = "Test",
FontFamily = family
Font = Font.OfSize(family, 10)
};

var handler = await CreateHandlerAsync(label);
Expand Down
10 changes: 5 additions & 5 deletions src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public async Task FontSizeInitializesCorrectly(int fontSize)
var label = new LabelStub()
{
Text = "Test",
FontSize = fontSize
Font = Font.OfSize("Arial", fontSize)
};

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

[Theory(DisplayName = "Font Attributes Initialize Correctly")]
Expand All @@ -73,11 +73,11 @@ public async Task AttributesInitializeCorrectly(FontAttributes attributes, bool
var label = new LabelStub()
{
Text = "Test",
FontAttributes = attributes
Font = Font.OfSize("Arial", 10).WithAttributes(attributes)
};

await ValidatePropertyInitValue(label, () => label.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold);
await ValidatePropertyInitValue(label, () => label.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic);
await ValidatePropertyInitValue(label, () => label.Font.FontAttributes.HasFlag(FontAttributes.Bold), GetNativeIsBold, isBold);
await ValidatePropertyInitValue(label, () => label.Font.FontAttributes.HasFlag(FontAttributes.Italic), GetNativeIsItalic, isItalic);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task FontFamilyInitializesCorrectly(string family)
var label = new LabelStub()
{
Text = "Test",
FontFamily = family
Font = Font.OfSize(family, 10)
};

var nativeFont = await GetValueAsync(label, handler => GetNativeLabel(handler).Font);
Expand Down
3 changes: 2 additions & 1 deletion src/Core/tests/DeviceTests/Stubs/ButtonStub.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.Maui;

namespace Microsoft.Maui.DeviceTests.Stubs
{
Expand All @@ -9,6 +8,8 @@ public partial class ButtonStub : StubBase, IButton

public Color TextColor { get; set; }

public Font Font { get; set; }

public event EventHandler Pressed;
public event EventHandler Released;
public event EventHandler Clicked;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/EntryStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public string Text

void OnTextChanged(string oldValue, string newValue) =>
TextChanged?.Invoke(this, new StubPropertyChangedEventArgs<string>(oldValue, newValue));

public Font Font { get; set; }
}
}
Loading