mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 02:21:15 +00:00
Initial
This commit is contained in:
53
BrightSharp/ThemeManager.cs
Normal file
53
BrightSharp/ThemeManager.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace BrightSharp
|
||||
{
|
||||
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()
|
||||
{
|
||||
Source = new Uri($"/brightsharp;component/style.{value}.xaml", UriKind.RelativeOrAbsolute)
|
||||
});
|
||||
}
|
||||
}
|
||||
private static ICollection<ResourceDictionary> Resources
|
||||
{
|
||||
get { return Application.Current.Resources.MergedDictionaries; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user