From 8f3f011d813ae9f39b981b7f2750b938e0db35de Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 21 Mar 2024 20:01:47 +0800 Subject: [PATCH] enhance: calculate monospace fonts in background to avoid delay for preference window (#30) --- src/SourceGit/Views/Preference.axaml.cs | 66 ++++++++++++++++--------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/SourceGit/Views/Preference.axaml.cs b/src/SourceGit/Views/Preference.axaml.cs index 9dcd8c06..a32e9410 100644 --- a/src/SourceGit/Views/Preference.axaml.cs +++ b/src/SourceGit/Views/Preference.axaml.cs @@ -2,24 +2,27 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Threading.Tasks; +using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Platform.Storage; +using Avalonia.Threading; namespace SourceGit.Views { public partial class Preference : Window { - public List InstalledFonts + public AvaloniaList InstalledFonts { get; private set; } - public List InstalledMonospaceFonts + public AvaloniaList InstalledMonospaceFonts { get; private set; @@ -66,37 +69,52 @@ namespace SourceGit.Views var pref = ViewModels.Preference.Instance; DataContext = pref; - InstalledFonts = new List(); + InstalledFonts = new AvaloniaList(); InstalledFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); InstalledFonts.AddRange(FontManager.Current.SystemFonts); InstalledFonts.Remove(FontManager.Current.DefaultFontFamily); InstalledFonts.Add(FontManager.Current.DefaultFontFamily); - InstalledMonospaceFonts = new List(); + InstalledMonospaceFonts = new AvaloniaList(); InstalledMonospaceFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); - foreach (var font in FontManager.Current.SystemFonts) + + var curMonoFont = pref.MonospaceFont; + if (!string.IsNullOrEmpty(curMonoFont) && curMonoFont != "fonts:SourceGit#JetBrains Mono") { - var typeface = new Typeface(font); - var testI = new FormattedText( - "i", - CultureInfo.CurrentCulture, - FlowDirection.LeftToRight, - typeface, - 12, - Brushes.White); - var testW = new FormattedText( - "W", - CultureInfo.CurrentCulture, - FlowDirection.LeftToRight, - typeface, - 12, - Brushes.White); - if (testI.Width == testW.Width) - { - InstalledMonospaceFonts.Add(font); - } + InstalledMonospaceFonts.Add(new FontFamily(curMonoFont)); } + Task.Run(() => + { + var sysMonoFonts = new List(); + foreach (var font in FontManager.Current.SystemFonts) + { + if (font.ToString() == curMonoFont) continue; + + var typeface = new Typeface(font); + var testI = new FormattedText( + "i", + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + typeface, + 12, + Brushes.White); + var testW = new FormattedText( + "W", + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + typeface, + 12, + Brushes.White); + if (testI.Width == testW.Width) + { + sysMonoFonts.Add(font); + } + } + + Dispatcher.UIThread.Post(() => InstalledMonospaceFonts.AddRange(sysMonoFonts)); + }); + var ver = string.Empty; if (pref.IsGitConfigured) {