Skip to content

Commit

Permalink
Font size and font family Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyIX committed Jun 2, 2024
1 parent fcabff1 commit 452c69e
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/Core/src/Fonts/FontManager.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#nullable enable
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Storage;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;

namespace Microsoft.Maui
Expand All @@ -27,6 +28,9 @@ public class FontManager : IFontManager
readonly IFontRegistrar _fontRegistrar;
readonly IServiceProvider? _serviceProvider;

FontFamily? _defaultFontFamily;
double? _defaultFontSize;

/// <summary>
/// Creates a new <see cref="EmbeddedFontLoader"/> instance.
/// </summary>
Expand All @@ -37,15 +41,42 @@ public FontManager(IFontRegistrar fontRegistrar, IServiceProvider? serviceProvid
{
_fontRegistrar = fontRegistrar;
_serviceProvider = serviceProvider;

Application.Current.Resources.RegisterPropertyChangedCallback(Control.FontFamilyProperty, OnPropertyChanged);
Application.Current.Resources.RegisterPropertyChangedCallback(Control.FontSizeProperty, OnPropertyChanged);
}

private void OnPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
if (dp == Control.FontFamilyProperty)
{
_defaultFontFamily = (FontFamily)Application.Current.Resources[SystemFontFamily];
}
else if (dp == Control.FontSizeProperty)
{
_defaultFontSize = (double)Application.Current.Resources[SystemFontSize];
}
}

/// <inheritdoc/>
public FontFamily DefaultFontFamily =>
(FontFamily)UI.Xaml.Application.Current.Resources[SystemFontFamily];
public FontFamily DefaultFontFamily
{
get
{
_defaultFontFamily ??= (FontFamily)Application.Current.Resources[SystemFontFamily];
return _defaultFontFamily;
}
}

/// <inheritdoc/>
public double DefaultFontSize =>
(double)UI.Xaml.Application.Current.Resources[SystemFontSize];
public double DefaultFontSize
{
get
{
_defaultFontSize ??= (double)Application.Current.Resources[SystemFontSize];
return _defaultFontSize.Value;
}
}

/// <inheritdoc/>
public FontFamily GetFontFamily(Font font)
Expand Down

0 comments on commit 452c69e

Please sign in to comment.