Skip to content

Commit 9560496

Browse files
committed
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 <[email protected]>
1 parent b9b684a commit 9560496

File tree

2 files changed

+14
-50
lines changed

2 files changed

+14
-50
lines changed

Diff for: src/App.axaml.cs

+10-14
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public static void SetFonts(string defaultFont, string monospaceFont, bool onlyU
181181
app._fontsOverrides = null;
182182
}
183183

184-
defaultFont = ProcessFontString(defaultFont);
185-
monospaceFont = ProcessFontString(monospaceFont);
184+
defaultFont = FixFontFamilyName(defaultFont);
185+
monospaceFont = FixFontFamilyName(monospaceFont);
186186

187187
var resDic = new ResourceDictionary();
188188
if (!string.IsNullOrEmpty(defaultFont))
@@ -440,26 +440,22 @@ private static bool TryLaunchAsRebaseMessageEditor(string[] args, out int exitCo
440440
return true;
441441
}
442442

443-
private static string ProcessFontString(string input)
443+
private static string FixFontFamilyName(string input)
444444
{
445-
if (string.IsNullOrEmpty(input)) return string.Empty;
445+
if (string.IsNullOrEmpty(input))
446+
return string.Empty;
446447

447448
var parts = input.Split(',');
448-
var result = new StringBuilder();
449-
var isFirst = true;
449+
var trimmed = new List<string>();
450450

451451
foreach (var part in parts)
452452
{
453-
var trimmed = part.Trim();
454-
if (!string.IsNullOrEmpty(trimmed))
455-
{
456-
if (!isFirst) result.Append(',');
457-
result.Append(trimmed);
458-
isFirst = false;
459-
}
453+
var t = part.Trim();
454+
if (!string.IsNullOrEmpty(t))
455+
trimmed.Add(t);
460456
}
461457

462-
return result.ToString();
458+
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
463459
}
464460

465461
private bool TryLaunchAsCoreEditor(IClassicDesktopStyleApplicationLifetime desktop)

Diff for: src/ViewModels/Preferences.cs

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Text;
54
using System.Text.Json;
65
using System.Text.Json.Serialization;
76
using Avalonia.Collections;
@@ -66,9 +65,8 @@ public string DefaultFontFamily
6665
get => _defaultFontFamily;
6766
set
6867
{
69-
var name = FixFontFamilyName(value);
70-
if (SetProperty(ref _defaultFontFamily, name) && !_isLoading)
71-
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
68+
if (SetProperty(ref _defaultFontFamily, value) && !_isLoading)
69+
App.SetFonts(value, _monospaceFontFamily, _onlyUseMonoFontInEditor);
7270
}
7371
}
7472

@@ -77,9 +75,8 @@ public string MonospaceFontFamily
7775
get => _monospaceFontFamily;
7876
set
7977
{
80-
var name = FixFontFamilyName(value);
81-
if (SetProperty(ref _monospaceFontFamily, name) && !_isLoading)
82-
App.SetFonts(_defaultFontFamily, _monospaceFontFamily, _onlyUseMonoFontInEditor);
78+
if (SetProperty(ref _monospaceFontFamily, value) && !_isLoading)
79+
App.SetFonts(_defaultFontFamily, value, _onlyUseMonoFontInEditor);
8380
}
8481
}
8582

@@ -620,35 +617,6 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
620617
return changed;
621618
}
622619

623-
private string FixFontFamilyName(string name)
624-
{
625-
var trimmed = name.Trim();
626-
if (string.IsNullOrEmpty(trimmed))
627-
return string.Empty;
628-
629-
var builder = new StringBuilder();
630-
var lastIsSpace = false;
631-
for (int i = 0; i < trimmed.Length; i++)
632-
{
633-
var c = trimmed[i];
634-
if (char.IsWhiteSpace(c))
635-
{
636-
if (lastIsSpace)
637-
continue;
638-
639-
lastIsSpace = true;
640-
}
641-
else
642-
{
643-
lastIsSpace = false;
644-
}
645-
646-
builder.Append(c);
647-
}
648-
649-
return builder.ToString();
650-
}
651-
652620
private static Preferences _instance = null;
653621
private static bool _isLoading = false;
654622

0 commit comments

Comments
 (0)