Skip to content

Commit d91908c

Browse files
committed
add: 等宽字体过滤,这样写才是对的
1 parent b033143 commit d91908c

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

llcomNext/LLCOM/ViewModels/Pages/SettingPageViewModel.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,6 @@ public SettingPageViewModel(Func<Type, ViewModelBase> getService)
2626
//初始化系统信息
2727
Task.Run(async () =>
2828
{
29-
//用skia接口获取系统字体列表
30-
var fontMgr = SkiaSharp.SKFontManager.Default;
31-
var fonts = new List<string>();
32-
var monoFonts = new List<string>();
33-
foreach (var f in fontMgr.FontFamilies)
34-
{
35-
fonts.Add(f);
36-
using var typeface = fontMgr.MatchFamily(f) ;
37-
if (typeface.IsFixedPitch)
38-
monoFonts.Add(f);
39-
}
40-
//把列表内容按字母排序
41-
fonts.Sort();
42-
monoFonts.Sort();
43-
//添加到系统字体列表
44-
foreach (var f in fonts)
45-
SystemFontList.Add(f);
46-
foreach (var f in monoFonts)
47-
MonoFontList.Add(f);
4829
//找找看当前设置的是什么字体,对应上
4930
RefreshFontIndex();
5031

@@ -84,9 +65,7 @@ public SettingPageViewModel(Func<Type, ViewModelBase> getService)
8465
#region FontSettings
8566

8667
[ObservableProperty]
87-
private ObservableCollection<FontFamily> _systemFontList = new();
88-
[ObservableProperty]
89-
private ObservableCollection<FontFamily> _monoFontList = new();
68+
private ObservableCollection<FontFamily> _fontList = new();
9069
[ObservableProperty]
9170
private int _packetFontIndex;
9271
[ObservableProperty]
@@ -95,16 +74,40 @@ public SettingPageViewModel(Func<Type, ViewModelBase> getService)
9574
private int _packetHeaderFontIndex;
9675
[ObservableProperty]
9776
private int _packetExtraFontIndex;
77+
[ObservableProperty]
78+
private bool _isMonoFont = false;
9879

9980
[RelayCommand]
10081
private void RefreshFontIndex()
10182
{
102-
var list = SystemFontList;
83+
//用skia接口获取系统字体列表
84+
var fontMgr = SkiaSharp.SKFontManager.Default;
85+
var fonts = new List<string>();
86+
foreach (var f in fontMgr.FontFamilies)
87+
{
88+
if (IsMonoFont)
89+
{
90+
using var typeface = fontMgr.MatchFamily(f) ;
91+
if (typeface.IsFixedPitch)
92+
fonts.Add(f);
93+
}
94+
else
95+
{
96+
fonts.Add(f);
97+
}
98+
}
99+
//把列表内容按字母排序
100+
fonts.Sort();
101+
//添加到系统字体列表
102+
FontList.Clear();
103+
foreach (var f in fonts)
104+
FontList.Add(f);
105+
103106
//刷新字体索引
104-
PacketFontIndex = list.IndexOf(Services.Utils.Setting.PacketFontFamily ?? "");
105-
PacketHexFontIndex = list.IndexOf(Services.Utils.Setting.PacketHexFontFamily?? "");
106-
PacketHeaderFontIndex = list.IndexOf(Services.Utils.Setting.PacketHeaderFontFamily?? "");
107-
PacketExtraFontIndex = list.IndexOf(Services.Utils.Setting.PacketExtraFontFamily?? "");
107+
PacketFontIndex = FontList.IndexOf(Services.Utils.Setting.PacketFontFamily ?? "");
108+
PacketHexFontIndex = FontList.IndexOf(Services.Utils.Setting.PacketHexFontFamily?? "");
109+
PacketHeaderFontIndex = FontList.IndexOf(Services.Utils.Setting.PacketHeaderFontFamily?? "");
110+
PacketExtraFontIndex = FontList.IndexOf(Services.Utils.Setting.PacketExtraFontFamily?? "");
108111
}
109112

110113
#endregion

llcomNext/LLCOM/Views/Pages/SettingPageView.axaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<TabItem Header="外观与字体" IsSelected="True">
2626
<ScrollViewer Background="{DynamicResource SemiGrey0}">
2727
<StackPanel Margin="10" Spacing="5">
28+
<CheckBox
29+
Margin="5,5,5,0"
30+
Command="{Binding RefreshFontIndexCommand}"
31+
Content="限制使用等宽字体"
32+
IsChecked="{Binding IsMonoFont}" />
33+
<Separator Margin="0,10" />
2834
<Grid
2935
Margin="5,0"
3036
ColumnDefinitions="*, 250"
@@ -38,7 +44,7 @@
3844
Grid.Row="0"
3945
Grid.Column="1"
4046
Width="250"
41-
ItemsSource="{Binding SystemFontList}"
47+
ItemsSource="{Binding FontList}"
4248
SelectedIndex="{Binding PacketFontIndex}"
4349
SelectedItem="{Binding PacketFontFamily, Source={x:Static services:Utils.Setting}}">
4450
<ComboBox.ItemTemplate>
@@ -56,7 +62,7 @@
5662
Grid.Row="2"
5763
Grid.Column="1"
5864
Width="250"
59-
ItemsSource="{Binding SystemFontList}"
65+
ItemsSource="{Binding FontList}"
6066
SelectedIndex="{Binding PacketHexFontIndex}"
6167
SelectedItem="{Binding PacketHexFontFamily, Source={x:Static services:Utils.Setting}}">
6268
<ComboBox.ItemTemplate>
@@ -74,7 +80,7 @@
7480
Grid.Row="4"
7581
Grid.Column="1"
7682
Width="250"
77-
ItemsSource="{Binding SystemFontList}"
83+
ItemsSource="{Binding FontList}"
7884
SelectedIndex="{Binding PacketHeaderFontIndex}"
7985
SelectedItem="{Binding PacketHeaderFontFamily, Source={x:Static services:Utils.Setting}}">
8086
<ComboBox.ItemTemplate>
@@ -92,7 +98,7 @@
9298
Grid.Row="6"
9399
Grid.Column="1"
94100
Width="250"
95-
ItemsSource="{Binding SystemFontList}"
101+
ItemsSource="{Binding FontList}"
96102
SelectedIndex="{Binding PacketExtraFontIndex}"
97103
SelectedItem="{Binding PacketExtraFontFamily, Source={x:Static services:Utils.Setting}}">
98104
<ComboBox.ItemTemplate>

0 commit comments

Comments
 (0)