Files
BrightSharp.Toolkit/BrightSharp/Themes/ThemeManager.cs

77 lines
3.0 KiB
C#
Raw Permalink Normal View History

2017-01-09 13:25:52 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
2021-04-14 15:28:30 +03:00
using System.Threading.Tasks;
2017-01-09 13:25:52 +03:00
using System.Windows;
using System.Windows.Threading;
2017-01-09 13:25:52 +03:00
2017-08-31 13:39:03 +03:00
namespace BrightSharp.Themes
2017-01-09 13:25:52 +03:00
{
public enum ColorThemes
{
Classic,
DevLab,
Silver,
Blue,
DarkBlue
}
public static class ThemeManager
{
private const string StyleDictionaryPattern = @"(?<=.+style\.)(.*?)(?=\.xaml)";
2021-04-14 15:28:30 +03:00
private const string ThemeDictionaryUri = "/brightsharp;component/themes/theme.xaml";
2021-04-14 15:28:30 +03:00
private static ColorThemes? ThemeField;
2017-08-31 16:27:19 +03:00
public static ColorThemes? Theme
2017-01-09 13:25:52 +03:00
{
get {
2021-04-14 15:28:30 +03:00
if (ThemeField.HasValue) return ThemeField.Value;
2017-01-09 13:25:52 +03:00
var curStyleRes = Resources.Where(r => r.Source != null &&
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
2017-08-31 16:27:19 +03:00
if (curStyleRes == null) return null;
2017-01-09 13:25:52 +03:00
var match = Regex.Match(curStyleRes.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase);
2021-04-14 15:28:30 +03:00
ThemeField = (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
return ThemeField.Value;
}
set {
2021-04-14 15:28:30 +03:00
_ = SetTheme(value);
2017-01-09 13:25:52 +03:00
}
}
2021-04-14 15:28:30 +03:00
public static async Task SetTheme(ColorThemes? value, DispatcherPriority priority = DispatcherPriority.Background)
{
if (ThemeField == value) { return; }
ThemeField = value;
await Application.Current.Dispatcher.InvokeAsync(() =>
2017-01-09 13:25:52 +03:00
{
2021-04-14 15:28:30 +03:00
if (ThemeField != value) return;
Application.Current.Resources.BeginInit();
2017-01-09 13:25:52 +03:00
var curStyleRes = Resources.Where(r => r.Source != null &&
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
2021-04-14 15:28:30 +03:00
var curThemeRes = Resources.Where(r => r.Source != null && r.Source.OriginalString.Equals(ThemeDictionaryUri, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
if (curThemeRes != null)
Resources.Remove(curThemeRes);
2017-01-09 13:25:52 +03:00
if (curStyleRes != null)
Resources.Remove(curStyleRes);
2017-08-31 16:27:19 +03:00
if (value == null) return;
Resources.Add(new ResourceDictionary() { Source = new Uri($"/brightsharp;component/themes/style.{value}.xaml", UriKind.RelativeOrAbsolute) });
2021-04-14 15:28:30 +03:00
Resources.Add(new ResourceDictionary() { Source = new Uri(ThemeDictionaryUri, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.EndInit();
ThemeChanged?.Invoke(null, EventArgs.Empty);
2021-04-14 15:28:30 +03:00
}, priority);
2017-01-09 13:25:52 +03:00
}
public static event EventHandler ThemeChanged;
2017-01-09 13:25:52 +03:00
private static ICollection<ResourceDictionary> Resources
{
get { return Application.Current.Resources.MergedDictionaries; }
}
}
}