code_review: PR #1092

- Remove `SourceGit.ViewModels.Preference.FixFontFamilyName` (it is not necessary any more)
- Use `string.Join` instead of `StringBuilder` to make the logic more clear

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-03-13 15:17:20 +08:00
parent b9b684a83d
commit 9560496c7b
No known key found for this signature in database
2 changed files with 14 additions and 50 deletions

View file

@ -181,8 +181,8 @@ namespace SourceGit
app._fontsOverrides = null;
}
defaultFont = ProcessFontString(defaultFont);
monospaceFont = ProcessFontString(monospaceFont);
defaultFont = FixFontFamilyName(defaultFont);
monospaceFont = FixFontFamilyName(monospaceFont);
var resDic = new ResourceDictionary();
if (!string.IsNullOrEmpty(defaultFont))
@ -440,26 +440,22 @@ namespace SourceGit
return true;
}
private static string ProcessFontString(string input)
private static string FixFontFamilyName(string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
if (string.IsNullOrEmpty(input))
return string.Empty;
var parts = input.Split(',');
var result = new StringBuilder();
var isFirst = true;
var trimmed = new List<string>();
foreach (var part in parts)
{
var trimmed = part.Trim();
if (!string.IsNullOrEmpty(trimmed))
{
if (!isFirst) result.Append(',');
result.Append(trimmed);
isFirst = false;
}
var t = part.Trim();
if (!string.IsNullOrEmpty(t))
trimmed.Add(t);
}
return result.ToString();
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
}
private bool TryLaunchAsCoreEditor(IClassicDesktopStyleApplicationLifetime desktop)

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Avalonia.Collections;
@ -66,9 +65,8 @@ namespace SourceGit.ViewModels
get => _defaultFontFamily;
set
{
var name = FixFontFamilyName(value);
if (SetProperty(ref _defaultFontFamily, name) && !_isLoading)
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
if (SetProperty(ref _defaultFontFamily, value) && !_isLoading)
App.SetFonts(value, _monospaceFontFamily, _onlyUseMonoFontInEditor);
}
}
@ -77,9 +75,8 @@ namespace SourceGit.ViewModels
get => _monospaceFontFamily;
set
{
var name = FixFontFamilyName(value);
if (SetProperty(ref _monospaceFontFamily, name) && !_isLoading)
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
if (SetProperty(ref _monospaceFontFamily, value) && !_isLoading)
App.SetFonts(_defaultFontFamily, value, _onlyUseMonoFontInEditor);
}
}
@ -620,35 +617,6 @@ namespace SourceGit.ViewModels
return changed;
}
private string FixFontFamilyName(string name)
{
var trimmed = name.Trim();
if (string.IsNullOrEmpty(trimmed))
return string.Empty;
var builder = new StringBuilder();
var lastIsSpace = false;
for (int i = 0; i < trimmed.Length; i++)
{
var c = trimmed[i];
if (char.IsWhiteSpace(c))
{
if (lastIsSpace)
continue;
lastIsSpace = true;
}
else
{
lastIsSpace = false;
}
builder.Append(c);
}
return builder.ToString();
}
private static Preferences _instance = null;
private static bool _isLoading = false;