From df551668048e66fb2e0d84277358ed989f93136e Mon Sep 17 00:00:00 2001 From: ionite34 Date: Sun, 7 Jan 2024 14:44:58 +0800 Subject: [PATCH] Fallback fonts on macos --- StabilityMatrix.Avalonia/App.axaml.cs | 60 ++++++++++++++++++- .../Views/MainWindow.axaml.cs | 51 ++-------------- 2 files changed, 63 insertions(+), 48 deletions(-) diff --git a/StabilityMatrix.Avalonia/App.axaml.cs b/StabilityMatrix.Avalonia/App.axaml.cs index d4bba201..3ea5e003 100644 --- a/StabilityMatrix.Avalonia/App.axaml.cs +++ b/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; + /// /// Called before 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 } } + /// + /// Set the default font family for the application. + /// + private void SetFontFamily(FontFamily fontFamily) + { + Resources["ContentControlThemeFontFamily"] = fontFamily; + } + + /// + /// Get the default font family for the current platform and language. + /// + public FontFamily GetPlatformDefaultFontFamily() + { + try + { + var fonts = new List(); + + 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; + } + } + /// /// Setup tasks to be run shortly before any window is shown /// @@ -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"); diff --git a/StabilityMatrix.Avalonia/Views/MainWindow.axaml.cs b/StabilityMatrix.Avalonia/Views/MainWindow.axaml.cs index f512b502..e13f3841 100644 --- a/StabilityMatrix.Avalonia/Views/MainWindow.axaml.cs +++ b/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(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(); - - 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(); } }