enhance: calculate monospace fonts in background to avoid delay for preference window (#30)

This commit is contained in:
leo 2024-03-21 20:01:47 +08:00
parent ff2e0d7928
commit 8f3f011d81

View file

@ -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<FontFamily> InstalledFonts
public AvaloniaList<FontFamily> InstalledFonts
{
get;
private set;
}
public List<FontFamily> InstalledMonospaceFonts
public AvaloniaList<FontFamily> InstalledMonospaceFonts
{
get;
private set;
@ -66,16 +69,28 @@ namespace SourceGit.Views
var pref = ViewModels.Preference.Instance;
DataContext = pref;
InstalledFonts = new List<FontFamily>();
InstalledFonts = new AvaloniaList<FontFamily>();
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<FontFamily>();
InstalledMonospaceFonts = new AvaloniaList<FontFamily>();
InstalledMonospaceFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono"));
var curMonoFont = pref.MonospaceFont;
if (!string.IsNullOrEmpty(curMonoFont) && curMonoFont != "fonts:SourceGit#JetBrains Mono")
{
InstalledMonospaceFonts.Add(new FontFamily(curMonoFont));
}
Task.Run(() =>
{
var sysMonoFonts = new List<FontFamily>();
foreach (var font in FontManager.Current.SystemFonts)
{
if (font.ToString() == curMonoFont) continue;
var typeface = new Typeface(font);
var testI = new FormattedText(
"i",
@ -93,10 +108,13 @@ namespace SourceGit.Views
Brushes.White);
if (testI.Width == testW.Width)
{
InstalledMonospaceFonts.Add(font);
sysMonoFonts.Add(font);
}
}
Dispatcher.UIThread.Post(() => InstalledMonospaceFonts.AddRange(sysMonoFonts));
});
var ver = string.Empty;
if (pref.IsGitConfigured)
{