39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
namespace FieldLogger.Resources.Styles;
|
|
|
|
public enum UlThemeMode
|
|
{
|
|
Default,
|
|
Sunlight,
|
|
}
|
|
|
|
/// <summary>Applies generated semantic colors. Call on the UI thread after app resources initialize.</summary>
|
|
public static class ThemeManager
|
|
{
|
|
private static readonly string[] SemanticColors =
|
|
[
|
|
"Canvas", "Surface", "SurfaceRaised", "SurfaceStrong", "Text", "TextMuted",
|
|
"Primary", "PrimaryPressed", "Accent", "Border", "Focus", "Map", "Success",
|
|
"Warning", "Error", "Neutral", "Disabled", "OnStrong", "OnPrimary", "OnAccent",
|
|
];
|
|
|
|
public static UlThemeMode Current { get; private set; } = UlThemeMode.Default;
|
|
|
|
public static void Apply(UlThemeMode mode)
|
|
{
|
|
var resources = Application.Current?.Resources
|
|
?? throw new InvalidOperationException("Application resources are not initialized.");
|
|
var prefix = mode == UlThemeMode.Sunlight ? "UlSunlightColor" : "UlDefaultColor";
|
|
|
|
foreach (var name in SemanticColors)
|
|
{
|
|
if (!resources.TryGetValue($"{prefix}{name}", out var value) || value is not Color color)
|
|
{
|
|
throw new InvalidOperationException($"Generated theme token {prefix}{name} is missing.");
|
|
}
|
|
resources[$"UlColor{name}"] = color;
|
|
resources[$"UlBrush{name}"] = new SolidColorBrush(color);
|
|
}
|
|
Current = mode;
|
|
}
|
|
}
|