Browse Source

Fallback fonts on macos

pull/438/head
ionite34 11 months ago
parent
commit
df55166804
No known key found for this signature in database
GPG Key ID: B3404C5F3827849B
  1. 60
      StabilityMatrix.Avalonia/App.axaml.cs
  2. 51
      StabilityMatrix.Avalonia/Views/MainWindow.axaml.cs

60
StabilityMatrix.Avalonia/App.axaml.cs

@ -17,11 +17,13 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Input.Platform;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Avalonia.Styling;
using Avalonia.Threading;
using FluentAvalonia.Interop;
using FluentAvalonia.UI.Controls;
using MessagePipe;
using Microsoft.Extensions.Configuration;
@ -97,6 +99,8 @@ public sealed class App : Application
public IClassicDesktopStyleApplicationLifetime? DesktopLifetime =>
ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
public static new App? Current => (App?)Application.Current;
/// <summary>
/// Called before <see cref="Services"/> is built.
/// Can be used by UI tests to override services.
@ -107,6 +111,8 @@ public sealed class App : Application
{
AvaloniaXamlLoader.Load(this);
SetFontFamily(GetPlatformDefaultFontFamily());
// Set design theme
if (Design.IsDesignMode)
{
@ -182,6 +188,58 @@ public sealed class App : Application
}
}
/// <summary>
/// Set the default font family for the application.
/// </summary>
private void SetFontFamily(FontFamily fontFamily)
{
Resources["ContentControlThemeFontFamily"] = fontFamily;
}
/// <summary>
/// Get the default font family for the current platform and language.
/// </summary>
public FontFamily GetPlatformDefaultFontFamily()
{
try
{
var fonts = new List<string>();
if (Cultures.Current?.Name == "ja-JP")
{
return Resources["NotoSansJP"] as FontFamily
?? throw new ApplicationException("Font NotoSansJP not found");
}
if (Compat.IsWindows)
{
fonts.Add(OSVersionHelper.IsWindows11() ? "Segoe UI Variable Text" : "Segoe UI");
}
else if (Compat.IsMacOS)
{
// Use Segoe fonts if installed, but we can't distribute them
fonts.Add("Segoe UI Variable");
fonts.Add("Segoe UI");
fonts.Add("San Francisco");
fonts.Add("Helvetica Neue");
fonts.Add("Helvetica");
}
else
{
return FontFamily.Default;
}
return new FontFamily(string.Join(",", fonts));
}
catch (Exception e)
{
LogManager.GetCurrentClassLogger().Error(e);
return FontFamily.Default;
}
}
/// <summary>
/// Setup tasks to be run shortly before any window is shown
/// </summary>
@ -220,8 +278,6 @@ public sealed class App : Application
mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
mainWindow.SetDefaultFonts();
VisualRoot = mainWindow;
StorageProvider = mainWindow.StorageProvider;
Clipboard = mainWindow.Clipboard ?? throw new NullReferenceException("Clipboard is null");

51
StabilityMatrix.Avalonia/Views/MainWindow.axaml.cs

@ -91,6 +91,8 @@ public partial class MainWindow : AppWindowBase
EventManager.Instance.CultureChanged += (_, _) => SetDefaultFonts();
EventManager.Instance.UpdateAvailable += OnUpdateAvailable;
SetDefaultFonts();
Observable
.FromEventPattern<SizeChangedEventArgs>(this, nameof(SizeChanged))
.Where(x => x.EventArgs.PreviousSize != x.EventArgs.NewSize)
@ -243,54 +245,11 @@ public partial class MainWindow : AppWindowBase
});
}
public void SetDefaultFonts()
private void SetDefaultFonts()
{
var fonts = new List<string>();
try
{
if (Cultures.Current?.Name == "ja-JP")
{
var customFont = (Application.Current!.Resources["NotoSansJP"] as FontFamily)!;
Resources["ContentControlThemeFontFamily"] = customFont;
FontFamily = customFont;
return;
}
if (Compat.IsWindows)
{
if (OSVersionHelper.IsWindows11())
{
fonts.Add("Segoe UI Variable Text");
}
else
{
fonts.Add("Segoe UI");
}
}
else if (Compat.IsMacOS)
{
fonts.Add("San Francisco");
fonts.Add("Helvetica Neue");
fonts.Add("Helvetica");
}
else
{
Resources["ContentControlThemeFontFamily"] = FontFamily.Default;
FontFamily = FontFamily.Default;
return;
}
var fontString = new FontFamily(string.Join(",", fonts));
Resources["ContentControlThemeFontFamily"] = fontString;
FontFamily = fontString;
}
catch (Exception e)
if (App.Current is not null)
{
LogManager.GetCurrentClassLogger().Error(e);
Resources["ContentControlThemeFontFamily"] = FontFamily.Default;
FontFamily = FontFamily.Default;
FontFamily = App.Current.GetPlatformDefaultFontFamily();
}
}

Loading…
Cancel
Save