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

76 lines
3.1 KiB
C#
Raw Normal View History

2017-01-09 13:25:52 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
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)";
private const string StaticThemeDictionaryUri = "/brightsharp;component/themes/theme.static.xaml";
static ColorThemes? _theme;
2017-08-31 16:27:19 +03:00
public static ColorThemes? Theme
2017-01-09 13:25:52 +03:00
{
get {
if (_theme.HasValue) return _theme.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);
_theme = (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
return _theme.Value;
}
set {
SetTheme(value);
2017-01-09 13:25:52 +03:00
}
}
2017-08-31 16:27:19 +03:00
public static DispatcherOperation SetTheme(ColorThemes? value, DispatcherPriority priority = DispatcherPriority.Background) {
if (_theme == value) return Application.Current.Dispatcher.BeginInvoke(new Action(() => { }), priority);
_theme = value;
return Application.Current.Dispatcher.BeginInvoke(new Action(() =>
2017-01-09 13:25:52 +03:00
{
if (_theme != 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();
var curThemeRes = Resources.Where(r => r.Source != null && r.Source.OriginalString.Equals(StaticThemeDictionaryUri, 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) });
if (curThemeRes != null)
Resources.Add(new ResourceDictionary() { Source = new Uri(StaticThemeDictionaryUri, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.EndInit();
ThemeChanged?.Invoke(null, EventArgs.Empty);
}), 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; }
}
}
}