Repository navigation
Expand file tree
/
Copy pathThemeManager.cs
More file actions
134 lines (111 loc) · 4.84 KB
/
Copy pathThemeManager.cs
File metadata and controls
134 lines (111 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Microsoft.Win32;
namespace HttpTraceAnalyser
{
public enum AppTheme
{
Light,
Dark,
}
/// <summary>User's theme choice: Auto follows the system theme, others force a specific theme.</summary>
public enum ThemePreference
{
Auto,
Light,
Dark,
}
/// <summary>
/// Detects and tracks the system theme and drives WPF's built-in Fluent
/// theme (Application.ThemeMode), which themes all standard controls for
/// light/dark mode without a custom brush ResourceDictionary.
/// </summary>
public static class ThemeManager
{
public static AppTheme Current { get; private set; } = AppTheme.Light;
public static event EventHandler? ThemeChanged;
public static AppTheme GetSystemTheme()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
if (key?.GetValue("AppsUseLightTheme") is int v && v == 0)
return AppTheme.Dark;
}
catch
{
// Fall through to light default on any error.
}
return AppTheme.Light;
}
public static void Apply(AppTheme theme)
{
var app = Application.Current;
if (app is null)
return;
// WPF's built-in Fluent theme themes all standard controls
// (toolbars, menus, grids, text boxes, etc.) for light/dark mode.
#pragma warning disable WPF0001 // ThemeMode is for evaluation purposes only.
app.ThemeMode = theme == AppTheme.Dark ? ThemeMode.Dark : ThemeMode.Light;
#pragma warning restore WPF0001
Current = theme;
ThemeChanged?.Invoke(null, EventArgs.Empty);
}
internal static Brush GetContrastingForeground(Color backgroundColor)
=> GetContrastingColor(backgroundColor) == Colors.White ? Brushes.White : Brushes.Black;
internal static Color GetContrastingColor(Color backgroundColor)
{
double r = backgroundColor.R / 255.0;
double g = backgroundColor.G / 255.0;
double b = backgroundColor.B / 255.0;
r = r <= 0.03928 ? r / 12.92 : Math.Pow((r + 0.055) / 1.055, 2.4);
g = g <= 0.03928 ? g / 12.92 : Math.Pow((g + 0.055) / 1.055, 2.4);
b = b <= 0.03928 ? b / 12.92 : Math.Pow((b + 0.055) / 1.055, 2.4);
double luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return luminance < 0.5 ? Colors.White : Colors.Black;
}
}
/// <summary>
/// Substitutes the current theme's foreground brush when a row's foreground binding is null.
/// </summary>
public sealed class RowForegroundConverter : IValueConverter
{
public static readonly RowForegroundConverter Instance = new();
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Brush brush)
return brush;
return Application.Current?.TryFindResource("TextFillColorPrimaryBrush") as Brush
?? SystemColors.ControlTextBrush;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
/// <summary>
/// Resolves the effective foreground brush for a highlighted row:
/// * If the row has an explicit foreground, use it.
/// * Otherwise, if the row has a highlight background, automatically choose
/// black or white based on the background brightness to ensure readability.
/// * Otherwise, fall back to the current theme's window foreground.
/// Expects two bound values: [0] RowForeground, [1] RowBackground.
/// </summary>
public sealed class RowForegroundMultiConverter : IMultiValueConverter
{
public static readonly RowForegroundMultiConverter Instance = new();
public object Convert(object[] values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Length > 0 && values[0] is Brush explicitForeground)
return explicitForeground;
if (values.Length > 1 && values[1] is SolidColorBrush backgroundBrush)
return ThemeManager.GetContrastingForeground(backgroundBrush.Color);
return Application.Current?.TryFindResource("TextFillColorPrimaryBrush") as Brush
?? SystemColors.ControlTextBrush;
}
public object[] ConvertBack(object value, Type[] targetTypes, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
}