跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

ThemeServicer.cs

202 行 · 6.8 KB历史下载
1using Core.Librarys;
2using Core.Servicers.Interfaces;
3using System;
4using System.Collections.Generic;
5using System.Collections.ObjectModel;
6using System.Diagnostics;
7using System.Drawing;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows;
12using System.Windows.Media;
13using UI.ViewModels;
14
15namespace UI.Servicers
16{
17 public class ThemeServicer : IThemeServicer
18 {
19 /// <summary>
20 /// 当前主题名称
21 /// </summary>
22 private string themeName;
23 private MainWindow mainWindow;
24 private Collection<ResourceDictionary> MergedDictionaries;
25 private readonly string[] themeOptions = { "Light", "Dark" };
26 private readonly IAppConfig appConfig;
27
28 public event EventHandler OnThemeChanged;
29
30 public ThemeServicer(IAppConfig appConfig)
31 {
32 this.appConfig = appConfig;
33
34 appConfig.ConfigChanged += AppConfig_ConfigChanged;
35
36 MergedDictionaries = Application.Current.Resources.MergedDictionaries;
37 }
38
39 private void AppConfig_ConfigChanged(Core.Models.Config.ConfigModel oldConfig, Core.Models.Config.ConfigModel newConfig)
40 {
41 if (oldConfig.General.Theme != newConfig.General.Theme)
42 {
43 LoadTheme(themeOptions[newConfig.General.Theme]);
44 OnThemeChanged?.Invoke(this, EventArgs.Empty);
45 }
46
47 if (oldConfig.General.ThemeColor != newConfig.General.ThemeColor)
48 {
49 LoadTheme(themeOptions[newConfig.General.Theme], true);
50 OnThemeChanged?.Invoke(this, EventArgs.Empty);
51 }
52
53 if (oldConfig.General.IsSaveWindowSize != newConfig.General.IsSaveWindowSize)
54 {
55 HandleWindowSizeChangedEvent();
56 }
57
58 }
59 public void Init()
60 {
61 LoadTheme(themeOptions[appConfig.GetConfig().General.Theme]);
62 }
63 public void LoadTheme(string themeName, bool isRefresh = false)
64 {
65 if (string.IsNullOrEmpty(themeName))
66 {
67 // 设置默认主题
68 themeName = themeOptions[0];
69 }
70
71 if (themeName == this.themeName && !isRefresh)
72 {
73 return;
74 }
75
76 var themeDict = GetResourceDictionary($"pack://application:,,,/Tai;component/Resources/Themes/{themeName}.xaml");
77 var controlDict = GetResourceDictionary($"pack://application:,,,/Tai;component/Themes/Generic.xaml");
78 if (themeDict == null || controlDict == null)
79 {
80 return;
81 }
82
83 //Collection<ResourceDictionary> resourceDictionaries = Application.Current.Resources.MergedDictionaries;
84 //for (int i = 0; i < resourceDictionaries.Count; i++)
85 //{
86 // ResourceDictionary resourceDictionary = resourceDictionaries[i];
87 // //if (resourceDictionary.FindName(""))
88 // //{
89 // if (resourceDictionary.Contains("ThemeName"))
90 // {
91 // foreach(var key in resourceDictionary.Keys)
92 // {
93 // resourceDictionary[key] = themeDict[key];
94 // }
95 // //resourceDictionary["ThemeColor"] = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ff0000");
96 // }
97 // //}
98 //}
99 //清除旧主题
100 if (this.themeName != null)
101 {
102 var oldStyles = MergedDictionaries.Where(m => m.Source.OriginalString.Contains(this.themeName) || m.Source.OriginalString.Contains("Generic")).ToList();
103 if (oldStyles != null)
104 {
105 foreach (var oldStyle in oldStyles)
106 {
107 MergedDictionaries.Remove(oldStyle);
108 }
109 }
110 }
111
112 MergedDictionaries.Add(themeDict);
113 MergedDictionaries.Add(controlDict);
114
115 //if (configDict != null)
116 //{
117 // //MergedDictionaries.Add(configDict);
118 //}
119 //if (controlDict != null)
120 //{
121 // MergedDictionaries.Add(controlDict);
122 //}
123 this.themeName = themeName;
124
125 UpdateWindowStyle();
126 UpdateThemeColor();
127
128 Debug.WriteLine("已加载主题:" + themeName);
129 }
130
131 /// <summary>
132 /// 刷新主题颜色
133 /// </summary>
134 private void UpdateThemeColor()
135 {
136
137 var config = appConfig.GetConfig();
138 if (string.IsNullOrEmpty(config.General.ThemeColor))
139 {
140 StateData.ThemeColor = ((System.Windows.Media.Color)Application.Current.Resources["ThemeColor"]).ToString();
141 return;
142 }
143
144 StateData.ThemeColor = config.General.ThemeColor;
145 Application.Current.Resources["ThemeColor"] = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(config.General.ThemeColor);
146 Application.Current.Resources["ThemeBrush"] = UI.Base.Color.Colors.GetFromString(config.General.ThemeColor);
147 }
148
149 public void SetMainWindow(MainWindow mainWindow)
150 {
151 this.mainWindow = mainWindow;
152 HandleWindowSizeChangedEvent();
153 }
154
155 private void HandleWindowSizeChangedEvent()
156 {
157 if (mainWindow == null || mainWindow.IsWindowClosed) return;
158
159 mainWindow.SizeChanged -= MainWindow_SizeChanged;
160
161 var config = appConfig.GetConfig();
162 if (config.General.IsSaveWindowSize)
163 {
164 // 保存窗口大小信息
165 mainWindow.SizeChanged += MainWindow_SizeChanged;
166 }
167 }
168
169 private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
170 {
171 var config = appConfig.GetConfig();
172 config.General.WindowWidth = mainWindow.ActualWidth;
173 config.General.WindowHeight = mainWindow.ActualHeight;
174 appConfig.Save();
175 }
176
177 public void UpdateWindowStyle()
178 {
179 if (mainWindow != null)
180 {
181 var themeStyle = Application.Current.Resources["WindowStyle"] as Style;
182 if (themeStyle != null)
183 {
184 mainWindow.Style = themeStyle;
185 }
186 }
187 }
188
189 private ResourceDictionary GetResourceDictionary(string uri)
190 {
191 try
192 {
193 return new ResourceDictionary { Source = new Uri(uri, UriKind.RelativeOrAbsolute) };
194 }
195 catch (Exception e)
196 {
197 Logger.Error(e.ToString());
198 return null;
199 }
200 }
201 }
202}