|
@@ -3,7 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
namespace YZWater.Core.Services;
|
|
namespace YZWater.Core.Services;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
-/// 主题切换服务(纯状态管理,不依赖 Avalonia)
|
|
|
|
|
|
|
+/// 主题切换服务
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public partial class ThemeService : ObservableObject
|
|
public partial class ThemeService : ObservableObject
|
|
|
{
|
|
{
|
|
@@ -13,20 +13,39 @@ public partial class ThemeService : ObservableObject
|
|
|
[ObservableProperty]
|
|
[ObservableProperty]
|
|
|
private bool _isDarkTheme = false;
|
|
private bool _isDarkTheme = false;
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 主题变更事件(由 App.axaml.cs 订阅执行实际切换)
|
|
|
|
|
- /// </summary>
|
|
|
|
|
public event Action? ThemeChanged;
|
|
public event Action? ThemeChanged;
|
|
|
|
|
|
|
|
private ThemeService() { }
|
|
private ThemeService() { }
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 切换主题
|
|
|
|
|
|
|
+ /// 从配置加载主题偏好
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ public void LoadFromConfig()
|
|
|
|
|
+ {
|
|
|
|
|
+ var config = ConfigService.GetConfig();
|
|
|
|
|
+ if (config != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ IsDarkTheme = config.IsDarkTheme;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 切换主题并保存
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public void ToggleTheme()
|
|
public void ToggleTheme()
|
|
|
{
|
|
{
|
|
|
IsDarkTheme = !IsDarkTheme;
|
|
IsDarkTheme = !IsDarkTheme;
|
|
|
- Console.WriteLine($"[ThemeService] ToggleTheme: IsDarkTheme={IsDarkTheme}");
|
|
|
|
|
|
|
+ SaveToConfig();
|
|
|
ThemeChanged?.Invoke();
|
|
ThemeChanged?.Invoke();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private void SaveToConfig()
|
|
|
|
|
+ {
|
|
|
|
|
+ var config = ConfigService.GetConfig();
|
|
|
|
|
+ if (config != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ config.IsDarkTheme = IsDarkTheme;
|
|
|
|
|
+ ConfigService.SaveConfig();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|