enhance: use custom JsonConverter instead of converting string to FontFamily in each control

This commit is contained in:
leo 2024-03-21 23:19:09 +08:00
parent 0fadab2ca2
commit 56f5f3b4a7
10 changed files with 49 additions and 49 deletions

View file

@ -179,11 +179,6 @@ namespace SourceGit
SetLocale(pref.Locale); SetLocale(pref.Locale);
SetTheme(pref.Theme); SetTheme(pref.Theme);
if (string.IsNullOrEmpty(pref.DefaultFont))
{
pref.DefaultFont = FontManager.Current.DefaultFontFamily.ToString();
}
} }
public override void OnFrameworkInitializationCompleted() public override void OnFrameworkInitializationCompleted()

View file

@ -70,29 +70,5 @@ namespace SourceGit.Converters
public static FuncValueConverter<string, string> ToShortSHA = public static FuncValueConverter<string, string> ToShortSHA =
new FuncValueConverter<string, string>(v => v.Length > 10 ? v.Substring(0, 10) : v); new FuncValueConverter<string, string>(v => v.Length > 10 ? v.Substring(0, 10) : v);
public class ToFontFamilyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var name = value as string;
if (string.IsNullOrEmpty(name))
{
return FontManager.Current.DefaultFontFamily;
}
else
{
return new FontFamily(name);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var fontFamily = value as FontFamily;
return fontFamily == null ? string.Empty : fontFamily.ToString();
}
}
public static ToFontFamilyConverter ToFontFamily = new ToFontFamilyConverter();
} }
} }

View file

@ -16,7 +16,7 @@
</Style> </Style>
<Style Selector="ContentPresenter"> <Style Selector="ContentPresenter">
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFont, Converter={x:Static c:StringConverters.ToFontFamily}}"/> <Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=DefaultFont}"/>
</Style> </Style>
<Style Selector="Path"> <Style Selector="Path">
@ -70,7 +70,7 @@
<Setter Property="FontStyle" Value="Italic"/> <Setter Property="FontStyle" Value="Italic"/>
</Style> </Style>
<Style Selector="TextBlock.monospace"> <Style Selector="TextBlock.monospace">
<Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}"/> <Setter Property="FontFamily" Value="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"/>
</Style> </Style>
<Style Selector="TextBlock.group_header_label"> <Style Selector="TextBlock.group_header_label">
<Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/> <Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>

View file

@ -5,6 +5,7 @@ using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
@ -38,6 +39,16 @@ namespace SourceGit.ViewModels
_instance.Repositories.RemoveAll(x => !Directory.Exists(x.FullPath)); _instance.Repositories.RemoveAll(x => !Directory.Exists(x.FullPath));
if (_instance.DefaultFont == null)
{
_instance.DefaultFont = FontManager.Current.DefaultFontFamily;
}
if (_instance.MonospaceFont == null)
{
_instance.MonospaceFont = new FontFamily("fonts:SourceGit#JetBrains Mono");
}
if (!_instance.IsGitConfigured) if (!_instance.IsGitConfigured)
{ {
_instance.GitInstallPath = Native.OS.FindGitExecutable(); _instance.GitInstallPath = Native.OS.FindGitExecutable();
@ -71,13 +82,15 @@ namespace SourceGit.ViewModels
} }
} }
public string DefaultFont [JsonConverter(typeof(FontFamilyConverter))]
public FontFamily DefaultFont
{ {
get => _defaultFont; get => _defaultFont;
set => SetProperty(ref _defaultFont, value); set => SetProperty(ref _defaultFont, value);
} }
public string MonospaceFont [JsonConverter(typeof(FontFamilyConverter))]
public FontFamily MonospaceFont
{ {
get => _monospaceFont; get => _monospaceFont;
set => SetProperty(ref _monospaceFont, value); set => SetProperty(ref _monospaceFont, value);
@ -364,8 +377,8 @@ namespace SourceGit.ViewModels
private string _locale = "en_US"; private string _locale = "en_US";
private string _theme = "Default"; private string _theme = "Default";
private string _defaultFont = string.Empty; private FontFamily _defaultFont = null;
private string _monospaceFont = "fonts:SourceGit#JetBrains Mono"; private FontFamily _monospaceFont = null;
private int _maxHistoryCommits = 20000; private int _maxHistoryCommits = 20000;
private bool _restoreTabs = false; private bool _restoreTabs = false;
@ -388,6 +401,20 @@ namespace SourceGit.ViewModels
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>(); private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
} }
public class FontFamilyConverter : JsonConverter<FontFamily>
{
public override FontFamily Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var name = reader.GetString();
return new FontFamily(name);
}
public override void Write(Utf8JsonWriter writer, FontFamily value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
[JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)] [JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)]
[JsonSerializable(typeof(Preference))] [JsonSerializable(typeof(Preference))]
internal partial class JsonSerializationCodeGen : JsonSerializerContext { } internal partial class JsonSerializationCodeGen : JsonSerializerContext { }

View file

@ -72,7 +72,7 @@
BorderThickness="1" BorderThickness="1"
Background="{DynamicResource Brush.Contents}" Background="{DynamicResource Brush.Contents}"
Foreground="{DynamicResource Brush.FG1}" Foreground="{DynamicResource Brush.FG1}"
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
FontSize="12" FontSize="12"
BlameData="{Binding Data}"/> BlameData="{Binding Data}"/>

View file

@ -43,7 +43,7 @@
<Grid RowDefinitions="24,Auto,Auto,Auto" ColumnDefinitions="96,*"> <Grid RowDefinitions="24,Auto,Auto,Auto" ColumnDefinitions="96,*">
<!-- SHA --> <!-- SHA -->
<TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.SHA}" /> <TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.SHA}" />
<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}}"/> <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}"/>
<!-- PARENTS --> <!-- PARENTS -->
<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}}"/> <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 @@
<!-- Messages --> <!-- Messages -->
<TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Message}" VerticalAlignment="Top" Margin="0,4,0,0" /> <TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Message}" VerticalAlignment="Top" Margin="0,4,0,0" />
<ScrollViewer Grid.Row="3" Grid.Column="1" Margin="12,5,0,0" MaxHeight="100" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <ScrollViewer Grid.Row="3" Grid.Column="1" Margin="12,5,0,0" MaxHeight="100" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" TextWrapping="Wrap"/> <SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" TextWrapping="Wrap"/>
</ScrollViewer> </ScrollViewer>
</Grid> </Grid>

View file

@ -121,7 +121,7 @@
Padding="8,0" Padding="8,0"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
ItemsSource="{Binding #me.InstalledFonts}" ItemsSource="{Binding #me.InstalledFonts}"
SelectedItem="{Binding DefaultFont, Converter={x:Static c:StringConverters.ToFontFamily}, Mode=TwoWay}"> SelectedItem="{Binding DefaultFont, Mode=TwoWay}">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
<DataTemplate DataType="FontFamily"> <DataTemplate DataType="FontFamily">
<Border Height="24"> <Border Height="24">
@ -140,7 +140,7 @@
Padding="8,0" Padding="8,0"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
ItemsSource="{Binding #me.InstalledMonospaceFonts}" ItemsSource="{Binding #me.InstalledMonospaceFonts}"
SelectedItem="{Binding MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}, Mode=TwoWay}"> SelectedItem="{Binding MonospaceFont, Mode=TwoWay}">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
<DataTemplate DataType="FontFamily"> <DataTemplate DataType="FontFamily">
<Border Height="24"> <Border Height="24">

View file

@ -69,17 +69,19 @@ namespace SourceGit.Views
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
DataContext = pref; DataContext = pref;
var builtInMono = new FontFamily("fonts:SourceGit#JetBrains Mono");
InstalledFonts = new AvaloniaList<FontFamily>(); InstalledFonts = new AvaloniaList<FontFamily>();
InstalledFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); InstalledFonts.Add(builtInMono);
InstalledFonts.AddRange(FontManager.Current.SystemFonts); InstalledFonts.AddRange(FontManager.Current.SystemFonts);
InstalledMonospaceFonts = new AvaloniaList<FontFamily>(); InstalledMonospaceFonts = new AvaloniaList<FontFamily>();
InstalledMonospaceFonts.Add(new FontFamily("fonts:SourceGit#JetBrains Mono")); InstalledMonospaceFonts.Add(builtInMono);
var curMonoFont = pref.MonospaceFont; var curMonoFont = pref.MonospaceFont;
if (!string.IsNullOrEmpty(curMonoFont) && curMonoFont != "fonts:SourceGit#JetBrains Mono") if (curMonoFont != builtInMono)
{ {
InstalledMonospaceFonts.Add(new FontFamily(curMonoFont)); InstalledMonospaceFonts.Add(curMonoFont);
} }
Task.Run(() => Task.Run(() =>
@ -87,7 +89,7 @@ namespace SourceGit.Views
var sysMonoFonts = new List<FontFamily>(); var sysMonoFonts = new List<FontFamily>();
foreach (var font in FontManager.Current.SystemFonts) foreach (var font in FontManager.Current.SystemFonts)
{ {
if (font.ToString() == curMonoFont) continue; if (font == curMonoFont) continue;
var typeface = new Typeface(font); var typeface = new Typeface(font);
var testI = new FormattedText( var testI = new FormattedText(

View file

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

View file

@ -17,7 +17,7 @@
BorderThickness="0" BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}" Foreground="{DynamicResource Brush.FG1}"
SecondaryFG="{DynamicResource Brush.FG2}" SecondaryFG="{DynamicResource Brush.FG2}"
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
FontSize="12" FontSize="12"
DiffData="{Binding}" DiffData="{Binding}"
SyncScrollOffset="{Binding $parent[v:DiffView].DataContext.(vm:DiffContext).SyncScrollOffset, Mode=TwoWay}" SyncScrollOffset="{Binding $parent[v:DiffView].DataContext.(vm:DiffContext).SyncScrollOffset, Mode=TwoWay}"
@ -36,7 +36,7 @@
BorderThickness="0" BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}" Foreground="{DynamicResource Brush.FG1}"
SecondaryFG="{DynamicResource Brush.FG2}" SecondaryFG="{DynamicResource Brush.FG2}"
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
FontSize="12" FontSize="12"
DiffData="{Binding}"/> DiffData="{Binding}"/>
@ -52,7 +52,7 @@
BorderThickness="0" BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}" Foreground="{DynamicResource Brush.FG1}"
SecondaryFG="{DynamicResource Brush.FG2}" SecondaryFG="{DynamicResource Brush.FG2}"
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont, Converter={x:Static c:StringConverters.ToFontFamily}}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
FontSize="12" FontSize="12"
DiffData="{Binding}"/> DiffData="{Binding}"/>
</Grid> </Grid>