Skip to content

Commit 56f5f3b

Browse files
committed
enhance: use custom JsonConverter instead of converting string to FontFamily in each control
1 parent 0fadab2 commit 56f5f3b

File tree

10 files changed

+49
-49
lines changed

10 files changed

+49
-49
lines changed

src/SourceGit/App.axaml.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,6 @@ public override void Initialize()
179179

180180
SetLocale(pref.Locale);
181181
SetTheme(pref.Theme);
182-
183-
if (string.IsNullOrEmpty(pref.DefaultFont))
184-
{
185-
pref.DefaultFont = FontManager.Current.DefaultFontFamily.ToString();
186-
}
187182
}
188183

189184
public override void OnFrameworkInitializationCompleted()

src/SourceGit/Converters/StringConverters.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,5 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
7070

7171
public static FuncValueConverter<string, string> ToShortSHA =
7272
new FuncValueConverter<string, string>(v => v.Length > 10 ? v.Substring(0, 10) : v);
73-
74-
public class ToFontFamilyConverter : IValueConverter
75-
{
76-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
77-
{
78-
var name = value as string;
79-
if (string.IsNullOrEmpty(name))
80-
{
81-
return FontManager.Current.DefaultFontFamily;
82-
}
83-
else
84-
{
85-
return new FontFamily(name);
86-
}
87-
}
88-
89-
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
90-
{
91-
var fontFamily = value as FontFamily;
92-
return fontFamily == null ? string.Empty : fontFamily.ToString();
93-
}
94-
}
95-
96-
public static ToFontFamilyConverter ToFontFamily = new ToFontFamilyConverter();
9773
}
9874
}

src/SourceGit/Resources/Styles.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</Style>
1717

1818
<Style Selector="ContentPresenter">
19-
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFont, Converter={x:Static c:StringConverters.ToFontFamily}}"/>
19+
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFont}"/>
2020
</Style>
2121

2222
<Style Selector="Path">
@@ -70,7 +70,7 @@
7070
<Setter Property="FontStyle" Value="Italic"/>
7171
</Style>
7272
<Style Selector="TextBlock.monospace">
73-
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"/>
73+
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"/>
7474
</Style>
7575
<Style Selector="TextBlock.group_header_label">
7676
<Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>

src/SourceGit/ViewModels/Preference.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.Json.Serialization;
66

77
using Avalonia.Collections;
8+
using Avalonia.Media;
89

910
using CommunityToolkit.Mvvm.ComponentModel;
1011

@@ -38,6 +39,16 @@ public static Preference Instance
3839

3940
_instance.Repositories.RemoveAll(x => !Directory.Exists(x.FullPath));
4041

42+
if (_instance.DefaultFont == null)
43+
{
44+
_instance.DefaultFont = FontManager.Current.DefaultFontFamily;
45+
}
46+
47+
if (_instance.MonospaceFont == null)
48+
{
49+
_instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono");
50+
}
51+
4152
if (!_instance.IsGitConfigured)
4253
{
4354
_instance.GitInstallPath = Native.OS.FindGitExecutable();
@@ -71,13 +82,15 @@ public string Theme
7182
}
7283
}
7384

74-
public string DefaultFont
85+
[JsonConverter(typeof(FontFamilyConverter))]
86+
public FontFamily DefaultFont
7587
{
7688
get => _defaultFont;
7789
set => SetProperty(ref _defaultFont, value);
7890
}
7991

80-
public string MonospaceFont
92+
[JsonConverter(typeof(FontFamilyConverter))]
93+
public FontFamily MonospaceFont
8194
{
8295
get => _monospaceFont;
8396
set => SetProperty(ref _monospaceFont, value);
@@ -364,8 +377,8 @@ private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<Reposi
364377

365378
private string _locale = "en_US";
366379
private string _theme = "Default";
367-
private string _defaultFont = string.Empty;
368-
private string _monospaceFont = "fonts:SourceGit#JetBrains Mono";
380+
private FontFamily _defaultFont = null;
381+
private FontFamily _monospaceFont = null;
369382

370383
private int _maxHistoryCommits = 20000;
371384
private bool _restoreTabs = false;
@@ -388,6 +401,20 @@ private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<Reposi
388401
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
389402
}
390403

404+
public class FontFamilyConverter : JsonConverter<FontFamily>
405+
{
406+
public override FontFamily Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
407+
{
408+
var name = reader.GetString();
409+
return new FontFamily(name);
410+
}
411+
412+
public override void Write(Utf8JsonWriter writer, FontFamily value, JsonSerializerOptions options)
413+
{
414+
writer.WriteStringValue(value.ToString());
415+
}
416+
}
417+
391418
[JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)]
392419
[JsonSerializable(typeof(Preference))]
393420
internal partial class JsonSerializationCodeGen : JsonSerializerContext { }

src/SourceGit/Views/Blame.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
BorderThickness="1"
7373
Background="{DynamicResource Brush.Contents}"
7474
Foreground="{DynamicResource Brush.FG1}"
75-
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"
75+
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
7676
FontSize="12"
7777
BlameData="{Binding Data}"/>
7878

src/SourceGit/Views/CommitBaseInfo.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<Grid RowDefinitions="24,Auto,Auto,Auto" ColumnDefinitions="96,*">
4444
<!-- SHA -->
4545
<TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.SHA}" />
46-
<SelectableTextBlock Grid.Row="0" Grid.Column="1" Text="{Binding SHA}" Margin="12,0,0,0" FontSize="12" VerticalAlignment="Center" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"/>
46+
<SelectableTextBlock Grid.Row="0" Grid.Column="1" Text="{Binding SHA}" Margin="12,0,0,0" FontSize="12" VerticalAlignment="Center" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"/>
4747

4848
<!-- PARENTS -->
4949
<TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Parents}" IsVisible="{Binding Parents.Count, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"/>
@@ -95,7 +95,7 @@
9595
<!-- Messages -->
9696
<TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Message}" VerticalAlignment="Top" Margin="0,4,0,0" />
9797
<ScrollViewer Grid.Row="3" Grid.Column="1" Margin="12,5,0,0" MaxHeight="100" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
98-
<SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" TextWrapping="Wrap"/>
98+
<SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" TextWrapping="Wrap"/>
9999
</ScrollViewer>
100100
</Grid>
101101

src/SourceGit/Views/Preference.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
Padding="8,0"
122122
HorizontalAlignment="Stretch"
123123
ItemsSource="{Binding #me.InstalledFonts}"
124-
SelectedItem="{Binding DefaultFont, Converter={x:Static c:StringConverters.ToFontFamily}, Mode=TwoWay}">
124+
SelectedItem="{Binding DefaultFont, Mode=TwoWay}">
125125
<ComboBox.ItemTemplate>
126126
<DataTemplate DataType="FontFamily">
127127
<Border Height="24">
@@ -140,7 +140,7 @@
140140
Padding="8,0"
141141
HorizontalAlignment="Stretch"
142142
ItemsSource="{Binding #me.InstalledMonospaceFonts}"
143-
SelectedItem="{Binding MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}, Mode=TwoWay}">
143+
SelectedItem="{Binding MonospaceFont, Mode=TwoWay}">
144144
<ComboBox.ItemTemplate>
145145
<DataTemplate DataType="FontFamily">
146146
<Border Height="24">

src/SourceGit/Views/Preference.axaml.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,27 @@ public Preference()
6969
var pref = ViewModels.Preference.Instance;
7070
DataContext = pref;
7171

72+
var builtInMono = new FontFamily("fonts:SourceGit#JetBrains Mono");
73+
7274
InstalledFonts = new AvaloniaList<FontFamily>();
73-
InstalledFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono"));
75+
InstalledFonts.Add(builtInMono);
7476
InstalledFonts.AddRange(FontManager.Current.SystemFonts);
7577

7678
InstalledMonospaceFonts = new AvaloniaList<FontFamily>();
77-
InstalledMonospaceFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono"));
79+
InstalledMonospaceFonts.Add(builtInMono);
7880

7981
var curMonoFont = pref.MonospaceFont;
80-
if (!string.IsNullOrEmpty(curMonoFont) && curMonoFont != "fonts:SourceGit#JetBrains Mono")
82+
if (curMonoFont != builtInMono)
8183
{
82-
InstalledMonospaceFonts.Add(new FontFamily(curMonoFont));
84+
InstalledMonospaceFonts.Add(curMonoFont);
8385
}
8486

8587
Task.Run(() =>
8688
{
8789
var sysMonoFonts = new List<FontFamily>();
8890
foreach (var font in FontManager.Current.SystemFonts)
8991
{
90-
if (font.ToString() == curMonoFont) continue;
92+
if (font == curMonoFont) continue;
9193

9294
var typeface = new Typeface(font);
9395
var testI = new FormattedText(

src/SourceGit/Views/RevisionFiles.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
</DataTemplate>
9090

9191
<DataTemplate DataType="m:RevisionTextFile">
92-
<v:RevisionTextFileView FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" FontSize="12" Background="{DynamicResource Brush.Contents}"/>
92+
<v:RevisionTextFileView FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" FontSize="12" Background="{DynamicResource Brush.Contents}"/>
9393
</DataTemplate>
9494

9595
<DataTemplate DataType="m:RevisionLFSObject">

src/SourceGit/Views/TextDiffView.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
BorderThickness="0"
1818
Foreground="{DynamicResource Brush.FG1}"
1919
SecondaryFG="{DynamicResource Brush.FG2}"
20-
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"
20+
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
2121
FontSize="12"
2222
DiffData="{Binding}"
2323
SyncScrollOffset="{Binding $parent[v:DiffView].DataContext.(vm:DiffContext).SyncScrollOffset, Mode=TwoWay}"
@@ -36,7 +36,7 @@
3636
BorderThickness="0"
3737
Foreground="{DynamicResource Brush.FG1}"
3838
SecondaryFG="{DynamicResource Brush.FG2}"
39-
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"
39+
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
4040
FontSize="12"
4141
DiffData="{Binding}"/>
4242

@@ -52,7 +52,7 @@
5252
BorderThickness="0"
5353
Foreground="{DynamicResource Brush.FG1}"
5454
SecondaryFG="{DynamicResource Brush.FG2}"
55-
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"
55+
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
5656
FontSize="12"
5757
DiffData="{Binding}"/>
5858
</Grid>

0 commit comments

Comments
 (0)