Skip to content

Commit 62c3d3a

Browse files
committed
chore: 处理一下字体
1 parent d04f610 commit 62c3d3a

20 files changed

Lines changed: 362 additions & 132 deletions

File tree

.idea/.idea.MFAAvalonia/.idea/avalonia.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ColorDocument.Avalonia/ColorDocument.Avalonia.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<LangVersion>14</LangVersion>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<Nullable>enable</Nullable>
12-
<DebugSymbols>False</DebugSymbols>
13-
<DebugType>None</DebugType>
14-
<IsPackable>true</IsPackable>
15-
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
12+
<DebugSymbols>False</DebugSymbols>
13+
<DebugType>None</DebugType>
14+
<IsPackable>true</IsPackable>
15+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
16+
</PropertyGroup>
17+
<PropertyGroup>
18+
<NoWarn>CS0618;CS8602;CS8618;NETSDK1206;CS8524;CS8604;CS0067;CS0219;CS0612;CS8629;CS8619</NoWarn>
1619
</PropertyGroup>
17-
<PropertyGroup>
18-
<NoWarn>CS0618;CS8602;CS8618;NETSDK1206;CS8524;CS8604;CS0067;CS0219;CS0612;CS8629;CS8619</NoWarn>
19-
</PropertyGroup>
2020
<ItemGroup>
2121
<PackageReference Include="Avalonia" Version="11.3.9" />
2222
</ItemGroup>

ColorTextBlock.Avalonia/ColorTextBlock.Avalonia.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<LangVersion>14</LangVersion>
1212
<Nullable>enable</Nullable>
13-
<DebugSymbols>False</DebugSymbols>
14-
<DebugType>None</DebugType>
15-
<IsPackable>true</IsPackable>
16-
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
13+
<DebugSymbols>False</DebugSymbols>
14+
<DebugType>None</DebugType>
15+
<IsPackable>true</IsPackable>
16+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
17+
</PropertyGroup>
18+
<PropertyGroup>
19+
<NoWarn>CS0618;CS8602;CS8618;NETSDK1206;CS8524;CS8604;CS0067;CS0219;CS0612;CS8629;CS8619</NoWarn>
1720
</PropertyGroup>
18-
<PropertyGroup>
19-
<NoWarn>CS0618;CS8602;CS8618;NETSDK1206;CS8524;CS8604;CS0067;CS0219;CS0612;CS8629;CS8619</NoWarn>
20-
</PropertyGroup>
2121
<ItemGroup>
22-
<ProjectReference Include="../SukiUI/SukiUI.csproj" />
22+
<ProjectReference Include="../SukiUI/SukiUI.csproj" />
2323
<PackageReference Include="Avalonia" Version="11.3.9" />
2424
</ItemGroup>
2525

MFAAvalonia.sln.DotSettings.user

Lines changed: 11 additions & 0 deletions
Large diffs are not rendered by default.

MFAAvalonia/App.axaml.cs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,62 @@ public override void OnFrameworkInitializationCompleted()
8787
base.OnFrameworkInitializationCompleted();
8888
}
8989

90-
private void OnShutdownRequested(object sender, ShutdownRequestedEventArgs e)
91-
{
92-
ConfigurationManager.Current.SetValue(ConfigurationKeys.TaskItems, Instances.TaskQueueViewModel.TaskItemViewModels.ToList().Select(model => model.InterfaceItem));
93-
94-
MaaProcessor.Instance.SetTasker();
95-
GlobalHotkeyService.Shutdown();
96-
97-
// 释放内存优化器
98-
_memoryCracker?.Dispose();
99-
_memoryCracker = null;
90+
private void OnShutdownRequested(object sender, ShutdownRequestedEventArgs e)
91+
{
92+
ConfigurationManager.Current.SetValue(ConfigurationKeys.TaskItems, Instances.TaskQueueViewModel.TaskItemViewModels.ToList().Select(model => model.InterfaceItem));
93+
94+
MaaProcessor.Instance.SetTasker();
95+
GlobalHotkeyService.Shutdown();
96+
97+
// 强制清理所有应用资源(包括字体)
98+
ForceCleanupAllResources();
99+
100+
// 释放内存优化器
101+
_memoryCracker?.Dispose();
102+
_memoryCracker = null;
103+
104+
// 取消全局异常事件订阅,避免内存泄漏
105+
TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
106+
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
107+
Dispatcher.UIThread.UnhandledException -= OnDispatcherUnhandledException;
108+
}
100109

101-
// 取消全局异常事件订阅,避免内存泄漏
102-
TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
103-
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
104-
Dispatcher.UIThread.UnhandledException -= OnDispatcherUnhandledException;
105-
}
110+
/// <summary>
111+
/// 手动清理内存缓存(用于降低内存占用)
112+
/// 此方法会清除字体缓存等非必要的内存占用
113+
/// </summary>
114+
public static void ClearMemoryCaches()
115+
{
116+
try
117+
{
118+
// 清除字体缓存(保留当前使用的字体)
119+
FontService.Instance.ClearFontCache();
120+
121+
LoggerHelper.Info("[内存管理]已清除应用程序缓存");
122+
}
123+
catch (Exception ex)
124+
{
125+
LoggerHelper.Warning($"[内存管理]清除缓存时发生错误: {ex.Message}");
126+
}
127+
}
128+
129+
/// <summary>
130+
/// 强制清理所有资源(用于应用退出)
131+
/// </summary>
132+
private static void ForceCleanupAllResources()
133+
{
134+
try
135+
{
136+
// 强制清理所有字体资源
137+
FontService.Instance.ForceCleanupAllFontResources();
138+
139+
LoggerHelper.Info("[内存管理]已强制清理所有应用资源");
140+
}
141+
catch (Exception ex)
142+
{
143+
LoggerHelper.Warning($"[内存管理]强制清理资源时发生错误: {ex.Message}");
144+
}
145+
}
106146

107147
private static ViewsHelper ConfigureViews(ServiceCollection services)
108148
{

MFAAvalonia/Assets/Localization/MFAResxLangPlugin.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public void AddResource(params Assembly[] assemblies)
130130
return key;
131131
}
132132

133-
134133
private void Sync(CultureInfo cultureInfo)
135134
{
136135
if (_resourceManagers == null || _resourceManagers.Count == 0)
@@ -181,7 +180,11 @@ IEnumerable<DictionaryEntry> GetResources(ResourceManager resourceManager)
181180
{
182181
if (entry is { Key: string key, Value: string value })
183182
{
184-
currentLanResources.Languages[key] = value;
183+
// 使用字符串驻留减少重复字符串的内存占用
184+
// 对于资源键和值都使用驻留,因为很多资源字符串是重复的
185+
var internedKey = string.Intern(key);
186+
var internedValue = string.Intern(value);
187+
currentLanResources.Languages[internedKey] = internedValue;
185188
}
186189
}
187190
}

MFAAvalonia/Extensions/MaaFW/FocusHandler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ namespace MFAAvalonia.Extensions.MaaFW;
1919
public class FocusHandler
2020
{
2121
private AutoInitDictionary autoInitDictionary;
22-
22+
2323
public FocusHandler(AutoInitDictionary autoInitDictionary)
2424
{
2525
this.autoInitDictionary = autoInitDictionary;
2626
}
27-
27+
2828
public void UpdateDictionary(AutoInitDictionary dictionary)
2929
{
3030
autoInitDictionary = dictionary;
@@ -80,7 +80,7 @@ public void DisplayFocus(JObject taskModel, string message, string detail, Actio
8080
{
8181
if (taskModel["focus"] == null)
8282
return;
83-
83+
8484
var focusToken = taskModel["focus"];
8585
var focus = new Focus();
8686
JObject? newProtocolFocus = null;
@@ -119,7 +119,6 @@ public void DisplayFocus(JObject taskModel, string message, string detail, Actio
119119
// 忽略详情解析错误
120120
}
121121
}
122-
123122
// 1. 处理新协议(如果有)
124123
if (newProtocolFocus is { HasValues: true } && newProtocolFocus.TryGetValue(message, out var templateToken))
125124
{

MFAAvalonia/Extensions/MaaFW/MaaProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,6 @@ public void HandleCallBack(object? sender, MaaCallbackEventArgs args)
980980
}
981981
if (jObject.ContainsKey("focus"))
982982
{
983-
Console.WriteLine(jObject);
984983
_focusHandler ??= new FocusHandler(AutoInitDictionary);
985984
_focusHandler.UpdateDictionary(AutoInitDictionary);
986985
_focusHandler.DisplayFocus(jObject, args.Message, args.Details);

0 commit comments

Comments
 (0)