| 1 | using Core.Librarys; |
| 2 | using Core.Servicers.Interfaces; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Collections.ObjectModel; |
| 6 | using System.Diagnostics; |
| 7 | using System.Drawing; |
| 8 | using System.Linq; |
| 9 | using System.Text; |
| 10 | using System.Threading.Tasks; |
| 11 | using System.Windows; |
| 12 | using System.Windows.Media; |
| 13 | using UI.ViewModels; |
| 14 | |
| 15 | namespace 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 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 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 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 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 | } |