public ThemeManager(bool useWinSysAccent, bool useWinDefFont, bool includeWindowsTitleBar) { UseSegoeUIOnWindows = useWinDefFont; UseSystemAccentOnWindows = useWinSysAccent; IncludeWindowsTitleBarInThemeChange = includeWindowsTitleBar; _isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (_isWindows) { Win32Interop.OSVERSIONINFOEX osInfo = new Win32Interop.OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(Win32Interop.OSVERSIONINFOEX)) }; Win32Interop.RtlGetVersion(ref osInfo); bool useDark = Win32Interop.GetSystemTheme(osInfo); PreferredTheme = useDark ? FluentThemeMode.Dark : FluentThemeMode.Light; } else { PreferredTheme = FluentThemeMode.Light; } }
public void Refresh() { var appStyles = Application.Current.Styles; for (int i = 0; i < appStyles.Count; i++) { if (appStyles[i] is FluentTheme ft) { ft.Mode = PreferredTheme; } else if (appStyles[i] is FluentAvaloniaTheme fa) { fa.Mode = PreferredTheme; if (UseSystemAccentOnWindows && _isWindows) { SetSystemAccentColorsFromSystem(fa.ThemeStyles.CommonResources); } else if (CustomAccentColors != null && CustomAccentColors.Length == 7) { SetCustomAccentColor(fa.ThemeStyles.CommonResources); } if (UseSegoeUIOnWindows && _isWindows) { fa.ThemeStyles.CommonResources["ContentControlThemeFontFamily"] = new FontFamily("Segoe UI"); } } else if (appStyles[i] is ThemeStyles thmSty) { thmSty.CurrentTheme = PreferredTheme; if (UseSystemAccentOnWindows && _isWindows) { SetSystemAccentColorsFromSystem(thmSty.CommonResources); } else if (CustomAccentColors != null && CustomAccentColors.Length == 7) { SetCustomAccentColor(thmSty.CommonResources); } if (UseSegoeUIOnWindows && _isWindows) { thmSty.CommonResources["ContentControlThemeFontFamily"] = new FontFamily("Segoe UI"); } } else if (appStyles[i] is StyleInclude si) { if (si.Loaded != null && si.Loaded is ThemeStyles ts) { ts.CurrentTheme = PreferredTheme == FluentThemeMode.Light ? "Light" : "Dark"; if (UseSystemAccentOnWindows && _isWindows) { SetSystemAccentColorsFromSystem(ts.CommonResources); } else if (CustomAccentColors != null && CustomAccentColors.Length == 7) { SetCustomAccentColor(ts.CommonResources); } if (UseSegoeUIOnWindows && _isWindows) { ts.CommonResources["ContentControlThemeFontFamily"] = new FontFamily("Segoe UI"); } } } } if (IncludeWindowsTitleBarInThemeChange && _isWindows) { if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime cd) { Win32Interop.OSVERSIONINFOEX osInfo = new Win32Interop.OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(Win32Interop.OSVERSIONINFOEX)) }; Win32Interop.RtlGetVersion(ref osInfo); for (int i = 0; i < cd.Windows.Count; i++) { Win32Interop.ApplyTheme(cd.Windows[i].PlatformImpl.Handle.Handle, PreferredTheme == FluentThemeMode.Dark, osInfo); } } } }