-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
210 lines (185 loc) · 7.99 KB
/
Copy pathMainViewModel.cs
File metadata and controls
210 lines (185 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
namespace FontsQuickView;
public sealed class MainViewModel : INotifyPropertyChanged
{
private readonly List<FontFamilyItem> _allFonts = new();
private string _sampleText = "字体速览 Font Preview 0123456789";
private double _previewSize = 32;
private string _searchQuery = string.Empty;
private int _batchIndex;
private volatile bool _loading;
private bool _showAllFonts = true;
private bool _showChineseOnly;
private bool _showEnglishOnly;
public string SampleText
{
get => _sampleText;
set
{
if (_sampleText != value)
{
bool oldCJK = HasChineseText;
bool oldEN = HasEnglishText;
_sampleText = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ItemWidth));
if (oldCJK != HasChineseText || oldEN != HasEnglishText)
{
OnPropertyChanged(nameof(HasChineseText));
OnPropertyChanged(nameof(HasEnglishText));
UpdateUnsupportedLabels();
ApplyFilter();
}
}
}
}
public double PreviewSize
{
get => _previewSize;
set { if (Math.Abs(_previewSize - value) > 0.01) { _previewSize = value; OnPropertyChanged(); OnPropertyChanged(nameof(PreviewSizeDisplay)); OnPropertyChanged(nameof(ItemWidth)); OnPropertyChanged(nameof(ItemHeight)); } }
}
public string PreviewSizeDisplay => _previewSize.ToString("0");
public string SearchQuery
{
get => _searchQuery;
set { if (_searchQuery != value) { _searchQuery = value; OnPropertyChanged(); ApplyFilter(); } }
}
public bool ShowAllFonts
{
get => _showAllFonts;
set { if (_showAllFonts != value) { if (value) { _showChineseOnly = false; _showEnglishOnly = false; } _showAllFonts = value; OnPropertyChanged(); OnPropertyChanged(nameof(ShowChineseOnly)); OnPropertyChanged(nameof(ShowEnglishOnly)); ApplyFilter(); } }
}
public bool ShowChineseOnly
{
get => _showChineseOnly;
set { if (_showChineseOnly != value) { if (value) _showAllFonts = false; _showChineseOnly = value; OnPropertyChanged(); OnPropertyChanged(nameof(ShowAllFonts)); ApplyFilter(); } }
}
public bool ShowEnglishOnly
{
get => _showEnglishOnly;
set { if (_showEnglishOnly != value) { if (value) _showAllFonts = false; _showEnglishOnly = value; OnPropertyChanged(); OnPropertyChanged(nameof(ShowAllFonts)); ApplyFilter(); } }
}
// 输入是否包含中文字符
public bool HasChineseText
{
get
{
foreach (char c in _sampleText ?? "")
if ((c >= 0x4E00 && c <= 0x9FFF) || (c >= 0x3400 && c <= 0x4DBF) || (c >= 0xF900 && c <= 0xFAFF) || (c >= 0x3000 && c <= 0x303F))
return true;
return false;
}
}
// 输入是否包含英文字母
public bool HasEnglishText
{
get
{
foreach (char c in _sampleText ?? "")
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return true;
return false;
}
}
public double ItemWidth
{
get
{
string text = _sampleText ?? "";
double w = 0;
foreach (char c in text)
{
if ((c >= 0x4E00 && c <= 0x9FFF) || (c >= 0x3000 && c <= 0x303F) || (c >= 0xFF00 && c <= 0xFFEF) || (c >= 0xAC00 && c <= 0xD7AF) || (c >= 0x3040 && c <= 0x30FF))
w += _previewSize * 0.95;
else if (c >= 0x20 && c <= 0x7E)
w += _previewSize * 0.55;
else
w += _previewSize * 0.7;
}
if (w < 1) w = _previewSize * 8;
return Math.Clamp(w + 48, 220, 2000);
}
}
public double ItemHeight { get { double h = _previewSize * 3.0 + 28; return Math.Clamp(h, 56, 300); } }
public ObservableCollection<FontFamilyItem> Fonts { get; } = new();
private int _totalCount;
public int TotalCount { get => _totalCount; private set { if (_totalCount != value) { _totalCount = value; OnPropertyChanged(); OnPropertyChanged(nameof(CountDisplay)); } } }
private int _shownCount;
public int ShownCount { get => _shownCount; private set { if (_shownCount != value) { _shownCount = value; OnPropertyChanged(); OnPropertyChanged(nameof(CountDisplay)); } } }
public string CountDisplay => "显示 " + ShownCount + " / " + TotalCount + " 种字体";
public void LoadFonts()
{
_allFonts.Clear(); Fonts.Clear(); _batchIndex = 0; _loading = true;
string[] families = FontEnumerator.GetSystemFontFamilies();
_allFonts.AddRange(families.Select(f => new FontFamilyItem(f)));
TotalCount = _allFonts.Count;
UpdateUnsupportedLabels();
var dispatcher = DispatcherQueue.GetForCurrentThread();
if (dispatcher != null)
{
var timer = dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromMilliseconds(15);
timer.IsRepeating = true;
int batchSize = 300;
timer.Tick += (_, _) =>
{
if (!_loading) { timer.Stop(); return; }
int end = Math.Min(_batchIndex + batchSize, _allFonts.Count);
for (int i = _batchIndex; i < end; i++) Fonts.Add(_allFonts[i]);
_batchIndex = end; ShownCount = Fonts.Count;
if (_batchIndex >= _allFonts.Count) { _loading = false; timer.Stop(); ApplyFilter(); }
};
timer.Start();
}
else { foreach (var item in _allFonts) Fonts.Add(item); ShownCount = Fonts.Count; _loading = false; }
}
private void ApplyFilter()
{
if (_loading) return;
Fonts.Clear();
string query = _searchQuery?.Trim() ?? string.Empty;
var filtered = _allFonts.AsEnumerable();
if (!string.IsNullOrEmpty(query))
filtered = filtered.Where(f => f.Name.Contains(query, StringComparison.OrdinalIgnoreCase));
if (_showChineseOnly)
filtered = filtered.Where(f => f.IsCJK);
if (_showEnglishOnly)
filtered = filtered.Where(f => f.IsLatin);
// 排序:中英混合时 CJK+Latin 字体最优先,纯中文时 CJK 优先,纯英文时 Latin 优先
if (HasChineseText && HasEnglishText)
filtered = filtered.OrderByDescending(f => f.IsCJK).ThenByDescending(f => f.IsLatin).ThenBy(f => f.Name, StringComparer.OrdinalIgnoreCase);
else if (HasChineseText)
filtered = filtered.OrderByDescending(f => f.IsCJK).ThenBy(f => f.Name, StringComparer.OrdinalIgnoreCase);
else if (HasEnglishText)
filtered = filtered.OrderByDescending(f => f.IsLatin).ThenBy(f => f.Name, StringComparer.OrdinalIgnoreCase);
foreach (var item in filtered) Fonts.Add(item);
ShownCount = Fonts.Count;
}
private void UpdateUnsupportedLabels()
{
bool hasCJK = HasChineseText;
bool hasEN = HasEnglishText;
foreach (var item in _allFonts)
{
var parts = new List<string>();
if (hasCJK && !item.IsCJK) parts.Add("汉字");
if (hasEN && !item.IsLatin) parts.Add("英文");
if (parts.Count > 0)
{
item.UnsupportedText = " -不支持" + string.Join("和", parts);
item.UnsupportedVis = Visibility.Visible;
}
else
{
item.UnsupportedText = "";
item.UnsupportedVis = Visibility.Collapsed;
}
}
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}