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

52 lines
1.8 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;
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)";
public static ColorThemes Theme
{
get
{
var curStyleRes = Resources.Where(r => r.Source != null &&
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
if (curStyleRes == null) return ColorThemes.Classic;
var match = Regex.Match(curStyleRes.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase);
return (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
}
set
{
var curStyleRes = Resources.Where(r => r.Source != null &&
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
if (curStyleRes != null)
Resources.Remove(curStyleRes);
if (value == ColorThemes.Classic)
return;
Resources.Add(new ResourceDictionary()
{
2017-08-31 13:39:03 +03:00
Source = new Uri($"/brightsharp;component/themes/style.{value}.xaml", UriKind.RelativeOrAbsolute)
2017-01-09 13:25:52 +03:00
});
}
}
private static ICollection<ResourceDictionary> Resources
{
get { return Application.Current.Resources.MergedDictionaries; }
}
}
}