fix: avoid crash if fontfamily contains consecutive whitespace (#799)

This commit is contained in:
leo 2024-12-10 12:06:36 +08:00
parent 0a486a90d9
commit 33ef9a612d
No known key found for this signature in database

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Avalonia.Collections; using Avalonia.Collections;
@ -65,8 +66,8 @@ namespace SourceGit.ViewModels
get => _defaultFontFamily; get => _defaultFontFamily;
set set
{ {
var trimmed = value.Trim(); var name = FixFontFamilyName(value);
if (SetProperty(ref _defaultFontFamily, trimmed) && !_isLoading) if (SetProperty(ref _defaultFontFamily, name) && !_isLoading)
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor); App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
} }
} }
@ -76,8 +77,8 @@ namespace SourceGit.ViewModels
get => _monospaceFontFamily; get => _monospaceFontFamily;
set set
{ {
var trimmed = value.Trim(); var name = FixFontFamilyName(value);
if (SetProperty(ref _monospaceFontFamily, trimmed) && !_isLoading) if (SetProperty(ref _monospaceFontFamily, name) && !_isLoading)
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor); App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
} }
} }
@ -588,6 +589,35 @@ namespace SourceGit.ViewModels
return changed; 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 Preference _instance = null; private static Preference _instance = null;
private static bool _isLoading = false; private static bool _isLoading = false;