public void TryEnableMicaEffect(FluentAvaloniaTheme thm) { // The background colors for the Mica brush are still based around SolidBackgroundFillColorBase resource // BUT since we can't control the actual Mica brush color, we have to use the window background to create // the same effect. However, we can't use SolidBackgroundFillColorBase directly since its opaque, and if // we set the opacity the color become lighter than we want. So we take the normal color, darken it and // apply the opacity until we get the roughly the correct color // NOTE that the effect still doesn't look right, but it suffices. Ideally we need access to the Mica // CompositionBrush to properly change the color but I don't know if we can do that or not if (thm.RequestedTheme == FluentAvaloniaTheme.DarkModeString) { var color = this.TryFindResource("SolidBackgroundFillColorBase", out var value) ? (Color2)(Color)value : new Color2(32, 32, 32); color = color.LightenPercent(-0.2f); Background = new ImmutableSolidColorBrush(color, 0.85f); } else if (thm.RequestedTheme == FluentAvaloniaTheme.LightModeString) { // Similar effect here var color = this.TryFindResource("SolidBackgroundFillColorBase", out var value) ? (Color2)(Color)value : new Color2(243, 243, 243); color = color.LightenPercent(0.5f); Background = new ImmutableSolidColorBrush(color, 0.9); } }
private void OnRequestedThemeChanged(FluentAvaloniaTheme sender, RequestedThemeChangedEventArgs args) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // TODO: add Windows version to CoreWindow if (IsWindows11 && args.NewTheme != FluentAvaloniaTheme.HighContrastModeString) { TryEnableMicaEffect(sender); } else if (args.NewTheme == FluentAvaloniaTheme.HighContrastModeString) { // Clear the local value here, and let the normal styles take over for HighContrast theme SetValue(BackgroundProperty, AvaloniaProperty.UnsetValue); } } }