mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 02:21:15 +00:00
Theme.Static.xaml (without compile, no perfomance difference with dynamic). +ThemeManager improvements.
This commit is contained in:
@@ -3,11 +3,13 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace BrightSharp.Themes
|
||||
{
|
||||
public enum ColorThemes
|
||||
{
|
||||
None,
|
||||
Classic,
|
||||
DevLab,
|
||||
Silver,
|
||||
@@ -18,30 +20,53 @@ namespace BrightSharp.Themes
|
||||
public static class ThemeManager
|
||||
{
|
||||
private const string StyleDictionaryPattern = @"(?<=.+style\.)(.*?)(?=\.xaml)";
|
||||
private const string StaticThemeDictionaryUri = "/brightsharp;component/themes/theme.static.xaml";
|
||||
|
||||
static ColorThemes? _theme;
|
||||
|
||||
public static ColorThemes Theme
|
||||
{
|
||||
get
|
||||
{
|
||||
get {
|
||||
if (_theme.HasValue) return _theme.Value;
|
||||
var curStyleRes = Resources.Where(r => r.Source != null &&
|
||||
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
|
||||
if (curStyleRes == null) return ColorThemes.Classic;
|
||||
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
|
||||
if (curStyleRes == null) return ColorThemes.None;
|
||||
var match = Regex.Match(curStyleRes.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase);
|
||||
return (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
|
||||
_theme = (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
|
||||
return _theme.Value;
|
||||
}
|
||||
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/themes/style.{value}.xaml", UriKind.RelativeOrAbsolute)
|
||||
});
|
||||
set {
|
||||
SetTheme(value);
|
||||
}
|
||||
}
|
||||
|
||||
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(() =>
|
||||
{
|
||||
if (_theme != value) return;
|
||||
Application.Current.Resources.BeginInit();
|
||||
|
||||
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);
|
||||
if (curStyleRes != null)
|
||||
Resources.Remove(curStyleRes);
|
||||
if (value == ColorThemes.None) 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);
|
||||
}
|
||||
|
||||
public static event EventHandler ThemeChanged;
|
||||
|
||||
private static ICollection<ResourceDictionary> Resources
|
||||
{
|
||||
get { return Application.Current.Resources.MergedDictionaries; }
|
||||
|
||||
Reference in New Issue
Block a user