forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UITheme.cs
170 lines (149 loc) · 7.14 KB
/
UITheme.cs
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
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Utility.Logging;
using MahApps.Metro;
using Point = System.Windows.Point;
using Application = System.Windows.Application;
namespace Hearthstone_Deck_Tracker
{
// ReSharper disable once InconsistentNaming
public static class UITheme
{
private const string WindowAccentName = "Windows Accent";
private const string DefaultAccentName = "Blue";
private static Color _currentWindowsAccent = SystemParameters.WindowGlassColor;
public static AppTheme CurrentTheme => ThemeManager.AppThemes.FirstOrDefault(t => t.Name == Config.Instance.AppTheme.ToString()) ?? ThemeManager.DetectAppStyle().Item1;
public static Accent CurrentAccent => ThemeManager.Accents.FirstOrDefault(a => a.Name == Config.Instance.AccentName) ?? ThemeManager.GetAccent(DefaultAccentName);
public static async Task InitializeTheme()
{
UpdateIconColors();
if(Helper.IsWindows8() || Helper.IsWindows10())
await CreateWindowsAccentStyle();
else if(Config.Instance.AccentName == WindowAccentName)
{
// In case if somehow user will get "Windows Accent" on Windows which not support this.
// (For example move whole HDT on diffrent machine instead of fresh install)
Config.Instance.AccentName = DefaultAccentName;
Config.Save();
}
ThemeManager.ChangeAppStyle(Application.Current, CurrentAccent, CurrentTheme);
}
public static async Task UpdateTheme()
{
if(Config.Instance.AccentName == WindowAccentName)
await CreateWindowsAccentStyle();
ThemeManager.ChangeAppStyle(Application.Current, CurrentAccent, CurrentTheme);
UpdateIconColors();
}
public static async Task UpdateAccent()
{
if(Config.Instance.AccentName == WindowAccentName)
await CreateWindowsAccentStyle();
ThemeManager.ChangeAppStyle(Application.Current, CurrentAccent, CurrentTheme);
Core.MainWindow.DeckPickerList.UpdateDeckModeToggleButton();
}
private static async Task CreateWindowsAccentStyle(bool changeImmediately = false)
{
var resourceDictionary = new ResourceDictionary();
var color = SystemParameters.WindowGlassColor;
resourceDictionary.Add("HighlightColor", color);
resourceDictionary.Add("AccentColor", Color.FromArgb(204, color.R, color.G, color.B));
resourceDictionary.Add("AccentColor2", Color.FromArgb(153, color.R, color.G, color.B));
resourceDictionary.Add("AccentColor3", Color.FromArgb(102, color.R, color.G, color.B));
resourceDictionary.Add("AccentColor4", Color.FromArgb(51, color.R, color.G, color.B));
resourceDictionary.Add("HighlightBrush", new SolidColorBrush((Color)resourceDictionary["HighlightColor"]));
resourceDictionary.Add("AccentColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("AccentColorBrush2", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
resourceDictionary.Add("AccentColorBrush3", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
resourceDictionary.Add("AccentColorBrush4", new SolidColorBrush((Color)resourceDictionary["AccentColor4"]));
resourceDictionary.Add("WindowTitleColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("ProgressBrush", new LinearGradientBrush(new GradientStopCollection(new[]
{
new GradientStop((Color)resourceDictionary["HighlightColor"], 0),
new GradientStop((Color)resourceDictionary["AccentColor3"], 1)
}),
new Point(0.001, 0.5), new Point(1.002, 0.5)));
resourceDictionary.Add("CheckmarkFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("RightArrowFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("IdealForegroundColor", Colors.White);
resourceDictionary.Add("IdealForegroundColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
resourceDictionary.Add("AccentSelectedColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
resourceDictionary.Add("MetroDataGrid.HighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("MetroDataGrid.HighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
resourceDictionary.Add("MetroDataGrid.MouseOverHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
resourceDictionary.Add("MetroDataGrid.FocusBorderBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
var fileName = Path.Combine(Config.Instance.ConfigDir, "WindowsAccent.xaml");
try
{
using(var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using(var writer = XmlWriter.Create(stream, new XmlWriterSettings { Indent = true }))
XamlWriter.Save(resourceDictionary, writer);
}
catch(Exception e)
{
Log.Error("Error creating WindowsAccent: " + e);
return;
}
resourceDictionary = new ResourceDictionary { Source = new Uri(Path.GetFullPath(fileName), UriKind.Absolute) };
try
{
ThemeManager.AddAccent(WindowAccentName, resourceDictionary.Source);
}
catch (IOException e)
{
await Task.Delay(500);
try
{
ThemeManager.AddAccent(WindowAccentName, resourceDictionary.Source);
}
catch (Exception e2)
{
Log.Error("Error adding WindowsAccent: " + e2);
return;
}
}
catch(Exception e)
{
Log.Error("Error adding WindowsAccent: " + e);
return;
}
var oldWindowsAccent = ThemeManager.GetAccent(WindowAccentName);
oldWindowsAccent.Resources.Source = resourceDictionary.Source;
if(changeImmediately)
ThemeManager.ChangeAppStyle(Application.Current, CurrentAccent, CurrentTheme);
}
public static void UpdateIconColors()
{
if(CurrentTheme.Name == MetroTheme.BaseLight.ToString())
{
Application.Current.Resources["GrayTextColorBrush"] = new SolidColorBrush((Color)Application.Current.Resources["GrayTextColor1"]);
Application.Current.Resources["HsReplayIcon"] = Application.Current.Resources["HsReplayIconBlue"];
}
else
{
Application.Current.Resources["GrayTextColorBrush"] = new SolidColorBrush((Color)Application.Current.Resources["GrayTextColor2"]);
Application.Current.Resources["HsReplayIcon"] = Application.Current.Resources["HsReplayIconWhite"];
}
}
public static async Task RefreshWindowsAccent()
{
if(Config.Instance.AccentName == WindowAccentName && _currentWindowsAccent != SystemParameters.WindowGlassColor)
{
Config.Instance.AccentName = DefaultAccentName;
await UpdateAccent();
Config.Instance.AccentName = WindowAccentName;
await UpdateAccent();
_currentWindowsAccent = SystemParameters.WindowGlassColor;
}
}
}
}