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);
SetTheme(pref.Theme);
if (string.IsNullOrEmpty(pref.DefaultFont))
{
pref.DefaultFont = FontManager.Current.DefaultFontFamily.ToString();
}
}
public override void OnFrameworkInitializationCompleted()

View file

@ -70,29 +70,5 @@ namespace SourceGit.Converters
public static FuncValueConverter<string, string> ToShortSHA =
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 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 Selector="Path">
@ -70,7 +70,7 @@
<Setter Property="FontStyle" Value="Italic"/>
</Style>
<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 Selector="TextBlock.group_header_label">
<Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>

View file

@ -5,6 +5,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Avalonia.Collections;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
@ -38,6 +39,16 @@ namespace SourceGit.ViewModels
_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)
{
_instance.GitInstallPath = Native.OS.FindGitExecutable();
@ -71,13 +82,15 @@ namespace SourceGit.ViewModels
}
}
public string DefaultFont
[JsonConverter(typeof(FontFamilyConverter))]
public FontFamily DefaultFont
{
get => _defaultFont;
set => SetProperty(ref _defaultFont, value);
}
public string MonospaceFont
[JsonConverter(typeof(FontFamilyConverter))]
public FontFamily MonospaceFont
{
get => _monospaceFont;
set => SetProperty(ref _monospaceFont, value);
@ -364,8 +377,8 @@ namespace SourceGit.ViewModels
private string _locale = "en_US";
private string _theme = "Default";
private string _defaultFont = string.Empty;
private string _monospaceFont = "fonts:SourceGit#JetBrains Mono";
private FontFamily _defaultFont = null;
private FontFamily _monospaceFont = null;
private int _maxHistoryCommits = 20000;
private bool _restoreTabs = false;
@ -388,6 +401,20 @@ namespace SourceGit.ViewModels
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)]
[JsonSerializable(typeof(Preference))]
internal partial class JsonSerializationCodeGen : JsonSerializerContext { }

View file

@ -72,7 +72,7 @@
BorderThickness="1"
Background="{DynamicResource Brush.Contents}"
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"
BlameData="{Binding Data}"/>

View file

@ -43,7 +43,7 @@
<Grid RowDefinitions="24,Auto,Auto,Auto" ColumnDefinitions="96,*">
<!-- 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 -->
<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 -->
<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">
<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>
</Grid>

View file

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

View file

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

View file

@ -89,7 +89,7 @@
</DataTemplate>
<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 DataType="m:RevisionLFSObject">

View file

@ -17,7 +17,7 @@
BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}"
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"
DiffData="{Binding}"
SyncScrollOffset="{Binding $parent[v:DiffView].DataContext.(vm:DiffContext).SyncScrollOffset, Mode=TwoWay}"
@ -36,7 +36,7 @@
BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}"
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"
DiffData="{Binding}"/>
@ -52,7 +52,7 @@
BorderThickness="0"
Foreground="{DynamicResource Brush.FG1}"
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"
DiffData="{Binding}"/>
</Grid>