theme improvements

This commit is contained in:
2021-04-14 15:28:30 +03:00
parent 4faf7fecc1
commit 55e354b980
12 changed files with 221 additions and 318 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
@@ -19,49 +20,49 @@ namespace BrightSharp.Themes
public static class ThemeManager
{
private const string StyleDictionaryPattern = @"(?<=.+style\.)(.*?)(?=\.xaml)";
private const string StaticThemeDictionaryUri = "/brightsharp;component/themes/theme.static.xaml";
private const string ThemeDictionaryUri = "/brightsharp;component/themes/theme.xaml";
static ColorThemes? _theme;
private static ColorThemes? ThemeField;
public static ColorThemes? Theme
{
get {
if (_theme.HasValue) return _theme.Value;
if (ThemeField.HasValue) return ThemeField.Value;
var curStyleRes = Resources.Where(r => r.Source != null &&
Regex.IsMatch(r.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase)).FirstOrDefault();
if (curStyleRes == null) return null;
var match = Regex.Match(curStyleRes.Source.OriginalString, StyleDictionaryPattern, RegexOptions.IgnoreCase);
_theme = (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
return _theme.Value;
ThemeField = (ColorThemes)Enum.Parse(typeof(ColorThemes), match.Value, true);
return ThemeField.Value;
}
set {
SetTheme(value);
_ = 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(() =>
public static async Task SetTheme(ColorThemes? value, DispatcherPriority priority = DispatcherPriority.Background)
{
if (ThemeField == value) { return; }
ThemeField = value;
await Application.Current.Dispatcher.InvokeAsync(() =>
{
if (_theme != value) return;
if (ThemeField != 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();
var curThemeRes = Resources.Where(r => r.Source != null && r.Source.OriginalString.Equals(ThemeDictionaryUri, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
if (curThemeRes != null)
Resources.Remove(curThemeRes);
if (curStyleRes != null)
Resources.Remove(curStyleRes);
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) });
Resources.Add(new ResourceDictionary() { Source = new Uri(ThemeDictionaryUri, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.EndInit();
ThemeChanged?.Invoke(null, EventArgs.Empty);
}), priority);
}, priority);
}
public static event EventHandler ThemeChanged;