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.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
using Avalonia.Threading;
namespace SourceGit.Views namespace SourceGit.Views
{ {
public partial class Preference : Window public partial class Preference : Window
{ {
public List<FontFamily> InstalledFonts public AvaloniaList<FontFamily> InstalledFonts
{ {
get; get;
private set; private set;
} }
public List<FontFamily> InstalledMonospaceFonts public AvaloniaList<FontFamily> InstalledMonospaceFonts
{ {
get; get;
private set; private set;
@ -66,37 +69,52 @@ namespace SourceGit.Views
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
DataContext = pref; DataContext = pref;
InstalledFonts = new List<FontFamily>(); InstalledFonts = new AvaloniaList<FontFamily>();
InstalledFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); InstalledFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono"));
InstalledFonts.AddRange(FontManager.Current.SystemFonts); InstalledFonts.AddRange(FontManager.Current.SystemFonts);
InstalledFonts.Remove(FontManager.Current.DefaultFontFamily); InstalledFonts.Remove(FontManager.Current.DefaultFontFamily);
InstalledFonts.Add(FontManager.Current.DefaultFontFamily); InstalledFonts.Add(FontManager.Current.DefaultFontFamily);
InstalledMonospaceFonts = new List<FontFamily>(); InstalledMonospaceFonts = new AvaloniaList<FontFamily>();
InstalledMonospaceFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); 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); InstalledMonospaceFonts.Add(new FontFamily(curMonoFont));
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);
}
} }
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",
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; var ver = string.Empty;
if (pref.IsGitConfigured) if (pref.IsGitConfigured)
{ {