code_style: move App.FixFontFamilyNames to StringExtensions.FormatFontNames

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-04-16 14:37:43 +08:00
parent 3c747494a8
commit 5114c43dcd
2 changed files with 46 additions and 45 deletions

View File

@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Avalonia.Media;
namespace SourceGit
{
@@ -14,6 +17,47 @@ namespace SourceGit
{
return value.Replace("\"", "\\\"", StringComparison.Ordinal);
}
public static string FormatFontNames(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
var parts = input.Split(',');
var trimmed = new List<string>();
foreach (var part in parts)
{
var t = part.Trim();
if (string.IsNullOrEmpty(t))
continue;
var sb = new StringBuilder();
var prevChar = '\0';
foreach (var c in t)
{
if (c == ' ' && prevChar == ' ')
continue;
sb.Append(c);
prevChar = c;
}
var name = sb.ToString();
try
{
var fontFamily = FontFamily.Parse(name);
if (fontFamily.FamilyTypefaces.Count > 0)
trimmed.Add(name);
}
catch
{
// Ignore exceptions.
}
}
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
}
}
public static class CommandExtensions

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
@@ -180,8 +178,8 @@ namespace SourceGit
app._fontsOverrides = null;
}
defaultFont = app.FixFontFamilyName(defaultFont);
monospaceFont = app.FixFontFamilyName(monospaceFont);
defaultFont = StringExtensions.FormatFontNames(defaultFont);
monospaceFont = StringExtensions.FormatFontNames(monospaceFont);
var resDic = new ResourceDictionary();
if (!string.IsNullOrEmpty(defaultFont))
@@ -570,47 +568,6 @@ namespace SourceGit
}
#endregion
private string FixFontFamilyName(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
var parts = input.Split(',');
var trimmed = new List<string>();
foreach (var part in parts)
{
var t = part.Trim();
if (string.IsNullOrEmpty(t))
continue;
var sb = new StringBuilder();
var prevChar = '\0';
foreach (var c in t)
{
if (c == ' ' && prevChar == ' ')
continue;
sb.Append(c);
prevChar = c;
}
var name = sb.ToString();
try
{
var fontFamily = FontFamily.Parse(name);
if (fontFamily.FamilyTypefaces.Count > 0)
trimmed.Add(name);
}
catch
{
// Ignore exceptions.
}
}
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
}
private Models.IpcChannel _ipcChannel = null;
private ViewModels.Launcher _launcher = null;
private ResourceDictionary _activeLocale = null;