Skip to content

Commit

Permalink
UI: Attempt to workaround/fix reported crash when generating installe…
Browse files Browse the repository at this point in the history
…d font list
  • Loading branch information
SourMesen committed Mar 27, 2024
1 parent 9e1adfc commit ec9c892
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
36 changes: 34 additions & 2 deletions UI/Config/Configuration.cs
Expand Up @@ -7,6 +7,7 @@
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -125,11 +126,42 @@ public void InitializeDefaults()
}

private static HashSet<string>? _installedFonts = null;
private static List<string>? _sortedFonts = null;

[MemberNotNull(nameof(_installedFonts), nameof(_sortedFonts))]
private static void InitInstalledFonts()
{
_installedFonts = new();
_sortedFonts = new();
try {
int count = FontManager.Current.SystemFonts.Count;
for(int i = 0; i < count; i++) {
try {
string? fontName = FontManager.Current.SystemFonts[i]?.Name;
if(!string.IsNullOrWhiteSpace(fontName)) {
_installedFonts.Add(fontName);
}
} catch { }
}

_sortedFonts.AddRange(_installedFonts);
_sortedFonts.Sort();
} catch {
}
}

public static List<string> GetSortedFontList()
{
if(_sortedFonts == null) {
InitInstalledFonts();
}
return new List<string>(_sortedFonts);
}

private static string FindMatchingFont(string defaultFont, params string[] fontNames)
{
if(_installedFonts == null) {
_installedFonts = new(FontManager.Current.SystemFonts.Select(x => x.Name));
InitInstalledFonts();
}

foreach(string name in fontNames) {
Expand All @@ -144,7 +176,7 @@ private static string FindMatchingFont(string defaultFont, params string[] fontN
public static string GetValidFontFamily(string requestedFont, bool preferMonoFont)
{
if(_installedFonts == null) {
_installedFonts = new(FontManager.Current.SystemFonts.Select(x => x.Name));
InitInstalledFonts();
}

if(_installedFonts.Contains(requestedFont)) {
Expand Down
4 changes: 1 addition & 3 deletions UI/Views/FontOptionsView.axaml.cs
Expand Up @@ -54,9 +54,7 @@ public FontOptionsView()
InitializeComponent();

ComboBox cboFontFamily = this.GetControl<ComboBox>("cboFontFamily");
List<string> fontFamilies = FontManager.Current.SystemFonts.Select(x => x.Name).ToList();
fontFamilies.Sort();
cboFontFamily.ItemsSource = fontFamilies;
cboFontFamily.ItemsSource = Configuration.GetSortedFontList();

ComboBox cboFontSize = this.GetControl<ComboBox>("cboFontSize");
cboFontSize.ItemsSource = new double[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
Expand Down

0 comments on commit ec9c892

Please sign in to comment.