From 9057b71f2d018e0e33eaeca9a300f15ca4bcc80f Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 19 Aug 2024 17:14:41 +0800 Subject: [PATCH] refactor: rewrite the font configuration (#366) * input font name directly instead of a font picker because localized font family name is not supported by Avalonia * fallback monospace font to default font * remove unused code --- src/App.JsonCodeGen.cs | 15 --------- src/App.axaml.cs | 47 +++++++++++++++++++++++++- src/Native/Linux.cs | 6 ---- src/Native/MacOS.cs | 6 ---- src/Native/Windows.cs | 7 ---- src/Resources/Styles.axaml | 6 ++-- src/Resources/Themes.axaml | 4 +++ src/ViewModels/Preference.cs | 49 +++++++++++---------------- src/Views/Avatar.cs | 3 +- src/Views/Blame.axaml | 2 +- src/Views/BranchTree.axaml | 2 +- src/Views/ChangeStatusIcon.cs | 3 +- src/Views/CommitBaseInfo.axaml | 2 +- src/Views/Histories.axaml | 2 +- src/Views/Preference.axaml | 36 +++++--------------- src/Views/Preference.axaml.cs | 60 ---------------------------------- src/Views/Repository.axaml | 4 +-- src/Views/RevisionFiles.axaml | 2 +- src/Views/Statistics.axaml.cs | 3 +- src/Views/TextDiffView.axaml | 6 ++-- 20 files changed, 96 insertions(+), 169 deletions(-) diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index 5d7b114e..8daea4f9 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -20,20 +20,6 @@ namespace SourceGit } } - public class FontFamilyConverter : JsonConverter - { - public override FontFamily Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var name = reader.GetString(); - return new FontFamily(name); - } - - public override void Write(Utf8JsonWriter writer, FontFamily value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } - } - public class GridLengthConverter : JsonConverter { public override GridLength Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) @@ -54,7 +40,6 @@ namespace SourceGit IgnoreReadOnlyProperties = true, Converters = [ typeof(ColorConverter), - typeof(FontFamilyConverter), typeof(GridLengthConverter), ] )] diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 1015ee34..ce6b5ced 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -104,6 +104,7 @@ namespace SourceGit var pref = ViewModels.Preference.Instance; SetLocale(pref.Locale); SetTheme(pref.Theme, pref.ThemeOverrides); + SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor); } public override void OnFrameworkInitializationCompleted() @@ -143,7 +144,10 @@ namespace SourceGit public static void SetLocale(string localeKey) { var app = Current as App; - var targetLocale = app?.Resources[localeKey] as ResourceDictionary; + if (app == null) + return; + + var targetLocale = app.Resources[localeKey] as ResourceDictionary; if (targetLocale == null || targetLocale == app._activeLocale) return; @@ -208,6 +212,46 @@ namespace SourceGit } } + public static void SetFonts(string defaultFont, string monospaceFont, bool onlyUseMonospaceFontInEditor) + { + var app = Current as App; + if (app == null) + return; + + if (app._fontsOverrides != null) + { + app.Resources.MergedDictionaries.Remove(app._fontsOverrides); + app._fontsOverrides = null; + } + + var resDic = new ResourceDictionary(); + if (!string.IsNullOrEmpty(defaultFont)) + resDic.Add("Fonts.Default", new FontFamily(defaultFont)); + + if (string.IsNullOrEmpty(monospaceFont)) + { + if (!string.IsNullOrEmpty(defaultFont)) + monospaceFont = $"fonts:SourceGit#JetBrains Mono,{defaultFont}"; + } + else + { + if (!string.IsNullOrEmpty(defaultFont) && !monospaceFont.Contains(defaultFont, StringComparison.Ordinal)) + monospaceFont = $"{monospaceFont},{defaultFont}"; + + resDic.Add("Fonts.Monospace", new FontFamily(monospaceFont)); + } + + var primary = onlyUseMonospaceFontInEditor ? defaultFont : monospaceFont; + if (!string.IsNullOrEmpty(primary)) + resDic.Add("Fonts.Primary", new FontFamily(primary)); + + if (resDic.Count > 0) + { + app.Resources.MergedDictionaries.Add(resDic); + app._fontsOverrides = resDic; + } + } + public static async void CopyText(string data) { if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) @@ -510,5 +554,6 @@ namespace SourceGit private ViewModels.Launcher _launcher = null; private ResourceDictionary _activeLocale = null; private ResourceDictionary _themeOverrides = null; + private ResourceDictionary _fontsOverrides = null; } } diff --git a/src/Native/Linux.cs b/src/Native/Linux.cs index 9d444dae..bd973f5f 100644 --- a/src/Native/Linux.cs +++ b/src/Native/Linux.cs @@ -5,7 +5,6 @@ using System.IO; using System.Runtime.Versioning; using Avalonia; -using Avalonia.Media; namespace SourceGit.Native { @@ -37,11 +36,6 @@ namespace SourceGit.Native public void SetupApp(AppBuilder builder) { - builder.With(new FontManagerOptions() - { - DefaultFamilyName = "fonts:SourceGit#JetBrains Mono", - }); - builder.With(new X11PlatformOptions() { EnableIme = true, diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index e034c674..8a401c38 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -6,7 +6,6 @@ using System.Runtime.Versioning; using System.Text; using Avalonia; -using Avalonia.Media; namespace SourceGit.Native { @@ -15,11 +14,6 @@ namespace SourceGit.Native { public void SetupApp(AppBuilder builder) { - builder.With(new FontManagerOptions() - { - DefaultFamilyName = "PingFang SC", - }); - builder.With(new MacOSPlatformOptions() { DisableDefaultApplicationMenuItems = true, diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index d8203d13..7609fe7b 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -8,7 +8,6 @@ using System.Text; using Avalonia; using Avalonia.Controls; -using Avalonia.Media; namespace SourceGit.Native { @@ -62,12 +61,6 @@ namespace SourceGit.Native public void SetupApp(AppBuilder builder) { - builder.With(new FontManagerOptions() - { - DefaultFamilyName = "Microsoft YaHei UI", - FontFallbacks = [new FontFallback { FontFamily = "Microsoft YaHei" }], - }); - // Fix drop shadow issue on Windows 10 RTL_OSVERSIONINFOEX v = new RTL_OSVERSIONINFOEX(); v.dwOSVersionInfoSize = (uint)Marshal.SizeOf(); diff --git a/src/Resources/Styles.axaml b/src/Resources/Styles.axaml index e9477212..3874060a 100644 --- a/src/Resources/Styles.axaml +++ b/src/Resources/Styles.axaml @@ -156,7 +156,7 @@ @@ -251,7 +251,7 @@ - +