Skip to content
Open
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
21 changes: 1 addition & 20 deletions src/Avalonia.Base/Media/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,7 @@ public FontManager(IFontManagerImpl platformImpl)
/// <summary>
/// Get the current font manager instance.
/// </summary>
public static FontManager Current
{
get
{
var current = AvaloniaLocator.Current.GetService<FontManager>();

if (current != null)
{
return current;
}

var fontManagerImpl = AvaloniaLocator.Current.GetRequiredService<IFontManagerImpl>();

current = new FontManager(fontManagerImpl);

AvaloniaLocator.CurrentMutable.Bind<FontManager>().ToConstant(current);

return current;
}
}
public static FontManager Current => AvaloniaLocator.Current.GetRequiredService<FontManager>();

/// <summary>
/// Gets the system's default font family.
Expand Down
4 changes: 3 additions & 1 deletion src/Avalonia.Controls/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ public virtual void RegisterServices()
.Bind<IGlobalDataTemplates>().ToConstant(this)
.Bind<IGlobalStyles>().ToConstant(this)
.Bind<IThemeVariantHost>().ToConstant(this)
.Bind<FontManager>().ToLazy(() => new FontManager(
AvaloniaLocator.Current.GetRequiredService<IFontManagerImpl>()))
.Bind<IInputManager>().ToConstant(InputManager)
.Bind< IToolTipService>().ToConstant(new ToolTipService(InputManager))
.Bind<IToolTipService>().ToConstant(new ToolTipService(InputManager))
.Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
.Bind<IDragDropDevice>().ToConstant(DragDropDevice.Instance);

Expand Down
16 changes: 10 additions & 6 deletions tests/Avalonia.Base.UnitTests/Media/FontManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public void Should_Create_Single_Instance_Typeface()
[Fact]
public void Should_Throw_When_Default_FamilyName_Is_Null_And_Installed_Font_Family_Names_Is_Empty()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
.With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
installedFontFamilyNames: new string[] { },
defaultFamilyName: null))))
// Wrap whole UnitTestApplication in the Assert.Throws, because disposal will try to access FontManager.Current again.
Assert.Throws<InvalidOperationException>(() =>
{
Assert.Throws<InvalidOperationException>(() => FontManager.Current);
}
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface
.With(fontManagerImpl: new HeadlessFontManagerWithMultipleSystemFontsStub(
installedFontFamilyNames: new string[] { },
defaultFamilyName: null))))
{
Assert.NotNull(FontManager.Current);
}
});
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions tests/Avalonia.RenderTests/TestRenderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ static class TestRenderHelper
static TestRenderHelper()
{
#if AVALONIA_SKIA
SkiaPlatform.Initialize();
SkiaPlatform.Initialize();
#else
Direct2D1Platform.Initialize();
#endif
AvaloniaLocator.CurrentMutable
.Bind<IDispatcherImpl>()
.ToConstant(s_dispatcherImpl);
.ToConstant(s_dispatcherImpl)
.Bind<FontManager>().ToLazy(() => new FontManager(
AvaloniaLocator.Current.GetRequiredService<IFontManagerImpl>()));

AvaloniaLocator.CurrentMutable.Bind<IAssetLoader>().ToConstant(new StandardAssetLoader());
}
Expand Down
17 changes: 10 additions & 7 deletions tests/Avalonia.UnitTests/UnitTestApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,22 @@ public override void RegisterServices()
.Bind<IKeyboardNavigationHandler>().ToFunc(Services.KeyboardNavigation ?? (() => null))
.Bind<IRuntimePlatform>().ToConstant(Services.Platform)
.Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
.Bind<IFontManagerImpl>().ToConstant(Services.FontManagerImpl)
.Bind<ITextShaperImpl>().ToConstant(Services.TextShaperImpl)
.Bind<IDispatcherImpl>().ToConstant(Services.DispatcherImpl)
.Bind<ICursorFactory>().ToConstant(Services.StandardCursorFactory)
.Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
.Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>()
.Bind<IPlatformSettings>().ToSingleton<DefaultPlatformSettings>()
.Bind<IAccessKeyHandler>().ToConstant(Services.AccessKeyHandler)
;

// This is a hack to make tests work, we need to refactor the way font manager is registered
// See https://github.com/AvaloniaUI/Avalonia/issues/10081
AvaloniaLocator.CurrentMutable.Bind<FontManager>().ToConstant((FontManager)null!);
.Bind<IAccessKeyHandler>().ToConstant(Services.AccessKeyHandler);

if (Services.FontManagerImpl is not null)
{
AvaloniaLocator.CurrentMutable
.Bind<IFontManagerImpl>().ToConstant(Services.FontManagerImpl)
.Bind<FontManager>().ToLazy(() => new FontManager(
AvaloniaLocator.Current.GetRequiredService<IFontManagerImpl>()));
}

var theme = Services.Theme?.Invoke();

if (theme is Style styles)
Expand Down
Loading