From ad4005ef05ba4b4a6fa0679ea0e4132119c12216 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 6 Jan 2023 02:02:12 -0500 Subject: [PATCH] Use PlatformColorValues in the control catalog --- samples/ControlCatalog/MainView.xaml.cs | 73 ++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/samples/ControlCatalog/MainView.xaml.cs b/samples/ControlCatalog/MainView.xaml.cs index f3c1a68e72..ecd9d0bf52 100644 --- a/samples/ControlCatalog/MainView.xaml.cs +++ b/samples/ControlCatalog/MainView.xaml.cs @@ -3,9 +3,12 @@ using System.Collections; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.LogicalTree; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Media.Immutable; +using Avalonia.Platform; +using Avalonia.Styling; using Avalonia.VisualTree; using ControlCatalog.Models; using ControlCatalog.Pages; @@ -14,10 +17,14 @@ namespace ControlCatalog { public class MainView : UserControl { + private readonly IPlatformSettings _platformSettings; + public MainView() { AvaloniaXamlLoader.Load(this); - + _platformSettings = AvaloniaLocator.Current.GetRequiredService(); + PlatformSettingsOnColorValuesChanged(_platformSettings, _platformSettings.GetColorValues()); + var sideBar = this.Get("Sidebar"); if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime) @@ -37,6 +44,15 @@ namespace ControlCatalog if (themes.SelectedItem is CatalogTheme theme) { App.SetThemeVariant(theme); + + ((TopLevel?)this.GetVisualRoot())?.PlatformImpl?.SetFrameThemeVariant(theme switch + { + CatalogTheme.FluentLight => PlatformThemeVariant.Light, + CatalogTheme.FluentDark => PlatformThemeVariant.Dark, + CatalogTheme.SimpleLight => PlatformThemeVariant.Light, + CatalogTheme.SimpleDark => PlatformThemeVariant.Dark, + _ => throw new ArgumentOutOfRangeException() + }); } }; @@ -89,6 +105,61 @@ namespace ControlCatalog var decorations = this.Get("Decorations"); if (VisualRoot is Window window) decorations.SelectedIndex = (int)window.SystemDecorations; + + _platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged; + } + + protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) + { + base.OnDetachedFromLogicalTree(e); + + _platformSettings.ColorValuesChanged -= PlatformSettingsOnColorValuesChanged; + } + + private void PlatformSettingsOnColorValuesChanged(object? sender, PlatformColorValues e) + { + var themes = this.Get("Themes"); + var currentTheme = (CatalogTheme?)themes.SelectedItem ?? CatalogTheme.FluentLight; + var newTheme = (currentTheme, e.ThemeVariant) switch + { + (CatalogTheme.FluentDark, PlatformThemeVariant.Light) => CatalogTheme.FluentLight, + (CatalogTheme.FluentLight, PlatformThemeVariant.Dark) => CatalogTheme.FluentDark, + (CatalogTheme.SimpleDark, PlatformThemeVariant.Light) => CatalogTheme.SimpleLight, + (CatalogTheme.SimpleLight, PlatformThemeVariant.Dark) => CatalogTheme.SimpleDark, + _ => currentTheme + }; + themes.SelectedItem = newTheme; + + Application.Current!.Resources["SystemAccentColor"] = e.AccentColor1; + Application.Current.Resources["SystemAccentColorDark1"] = ChangeColorLuminosity(e.AccentColor1, -0.3); + Application.Current.Resources["SystemAccentColorDark2"] = ChangeColorLuminosity(e.AccentColor1, -0.5); + Application.Current.Resources["SystemAccentColorDark3"] = ChangeColorLuminosity(e.AccentColor1, -0.7); + Application.Current.Resources["SystemAccentColorLight1"] = ChangeColorLuminosity(e.AccentColor1, -0.3); + Application.Current.Resources["SystemAccentColorLight2"] = ChangeColorLuminosity(e.AccentColor1, -0.5); + Application.Current.Resources["SystemAccentColorLight3"] = ChangeColorLuminosity(e.AccentColor1, -0.7); + + static Color ChangeColorLuminosity(Color color, double luminosityFactor) + { + var red = (double)color.R; + var green = (double)color.G; + var blue = (double)color.B; + + if (luminosityFactor < 0) + { + luminosityFactor = 1 + luminosityFactor; + red *= luminosityFactor; + green *= luminosityFactor; + blue *= luminosityFactor; + } + else if (luminosityFactor >= 0) + { + red = (255 - red) * luminosityFactor + red; + green = (255 - green) * luminosityFactor + green; + blue = (255 - blue) * luminosityFactor + blue; + } + + return new Color(color.A, (byte)red, (byte)green, (byte)blue); + } } } }