-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThemeSelectorService.cs
168 lines (152 loc) · 5.67 KB
/
ThemeSelectorService.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
using SoftdocMusicPlayer.Helpers;
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Foundation.Metadata;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
namespace SoftdocMusicPlayer.Services
{
public static class ThemeSelectorService
{
private const string SettingsKey = "AppBackgroundRequestedTheme";
public static ElementTheme Theme { get; set; } = ElementTheme.Default;
/// <summary>
/// InitializeTheme()
/// </summary>
/// <returns></returns>
public static async Task InitializeAsync()
{
Theme = await LoadThemeFromSettingsAsync();
}
/// <summary>
/// set <paramref name="theme"/>
/// </summary>
/// <param name="theme"></param>
public static async Task SetThemeAsync(ElementTheme theme)
{
Theme = theme;
await SetRequestedThemeAsync();
await SaveThemeInSettingsAsync(Theme);
}
/// <summary>
/// set requested theme
/// </summary>
/// <returns></returns>
public static async Task SetRequestedThemeAsync()
{
foreach (var view in CoreApplication.Views)
{
// set requested theme for all pages/views
await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (Window.Current.Content is FrameworkElement frameworkElement)
{
frameworkElement.RequestedTheme = Theme;
}
});
}
// Update title bar
await SetupTitlebarAsync();
}
public static ElementTheme TrueTheme()
{
// get framework element
// and return its actual theme.
var frameworkElement = Window.Current.Content as FrameworkElement;
return frameworkElement.ActualTheme;
}
public static string GetSystemControlForegroundColorForThemeHex()
{
return (TrueTheme() == ElementTheme.Dark ? "#FFFFFF" : "#000000");
}
/// <summary>
/// update title bar
/// </summary>
private static async Task SetupTitlebarAsync()
{
if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.ApplicationView"))
{
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
if (titleBar != null)
{
// set title bar button's background color.
titleBar.ButtonBackgroundColor = Windows.UI.Colors.Transparent;
if (Theme == ElementTheme.Default)
{
// Windows default theme
if (TrueTheme() == ElementTheme.Dark)
{
// Dark theme
await SetLightColor(titleBar);
}
else
{
// Light theme
await SetDarkColor(titleBar);
}
}
else if (TrueTheme() == ElementTheme.Dark)
{
// App Dark theme
await SetLightColor(titleBar);
}
else
{
// App Light theme
await SetDarkColor(titleBar);
}
// extend application view into title bar.
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
}
}
}
/// <summary>
/// update title bar for light theme
/// </summary>
private static async Task SetLightColor(ApplicationViewTitleBar titleBar)
{
await Task.Run(() =>
{
titleBar.ButtonForegroundColor = Windows.UI.Colors.White;
titleBar.ButtonHoverForegroundColor = Windows.UI.Colors.White;
titleBar.ButtonHoverBackgroundColor = Windows.UI.Color.FromArgb(155, 25, 25, 25);
});
}
/// <summary>
/// update title bar for dark theme
/// </summary>
private static async Task SetDarkColor(ApplicationViewTitleBar titleBar)
{
await Task.Run(() =>
{
titleBar.ButtonForegroundColor = Windows.UI.Colors.Black;
titleBar.ButtonHoverForegroundColor = Windows.UI.Colors.Black;
titleBar.ButtonHoverBackgroundColor = Windows.UI.Color.FromArgb(155, 230, 230, 230);
});
}
/// <summary>
/// Return theme from settings. By default returns system default theme.
/// </summary>
public static async Task<ElementTheme> LoadThemeFromSettingsAsync()
{
ElementTheme cacheTheme = ElementTheme.Default;
string themeName = await ApplicationData.Current.LocalSettings.ReadAsync<string>(SettingsKey);
if (!string.IsNullOrEmpty(themeName))
{
Enum.TryParse(themeName, out cacheTheme);
}
return cacheTheme;
}
/// <summary>
/// Save <paramref name="theme"/> in settings
/// </summary>
/// <param name="theme"></param>
private static async Task SaveThemeInSettingsAsync(ElementTheme theme)
{
await ApplicationData.Current.LocalSettings.SaveAsync(SettingsKey, theme.ToString());
}
}
}