UI: Attempt to workaround/fix reported crash when generating installed font list

This commit is contained in:
Sour 2024-03-27 22:14:31 +09:00
parent 9e1adfc106
commit ec9c892797
2 changed files with 35 additions and 5 deletions

View file

@ -7,6 +7,7 @@ using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
@ -125,11 +126,42 @@ namespace Mesen.Config
}
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) {
@ -144,7 +176,7 @@ namespace Mesen.Config
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)) {

View file

@ -54,9 +54,7 @@ namespace Mesen.Views
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 };