From 1381ec28be5202139fca91ac6c7f2539401171bc Mon Sep 17 00:00:00 2001 From: Max Katz Date: Thu, 29 Jun 2023 17:04:24 -0400 Subject: [PATCH] Allow only Light and Dark in the ColorPaletteResourcesCollection --- samples/ControlCatalog/App.xaml | 3 +++ samples/ControlCatalog/App.xaml.cs | 4 +-- .../ColorPaletteResourcesCollection.cs | 25 +++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/samples/ControlCatalog/App.xaml b/samples/ControlCatalog/App.xaml index 64bf3e53b3..02b1242471 100644 --- a/samples/ControlCatalog/App.xaml +++ b/samples/ControlCatalog/App.xaml @@ -28,6 +28,9 @@ + + + diff --git a/samples/ControlCatalog/App.xaml.cs b/samples/ControlCatalog/App.xaml.cs index 246fe4385f..605254c995 100644 --- a/samples/ControlCatalog/App.xaml.cs +++ b/samples/ControlCatalog/App.xaml.cs @@ -30,8 +30,8 @@ namespace ControlCatalog AvaloniaXamlLoader.Load(this); - _fluentTheme = new FluentTheme(); - _simpleTheme = new SimpleTheme(); + _fluentTheme = (FluentTheme)Resources["FluentTheme"]!; + _simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!; _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!; _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!; _dataGridFluent = (IStyle)Resources["DataGridFluent"]!; diff --git a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs index f26f6d8660..6eb465d399 100644 --- a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs +++ b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs @@ -10,12 +10,18 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary + (key, x) => { if (Owner is not null) { x.PropertyChanged += Palette_PropertyChanged; } + + if (key != ThemeVariant.Dark && key != ThemeVariant.Light) + { + throw new InvalidOperationException( + $"{nameof(FluentTheme)}.{nameof(FluentTheme.Palettes)} only supports Light and Dark variants."); + } }, (_, x) => { @@ -30,22 +36,15 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary Count > 0; public bool TryGetResource(object key, ThemeVariant? theme, out object? value) { - if (theme is not null) + if (theme == null || theme == ThemeVariant.Default) { - if (base.TryGetValue(theme, out var themePaletteResources) - && themePaletteResources.TryGetResource(key, theme, out value)) - { - return true; - } + theme = ThemeVariant.Light; } - if (theme != ThemeVariant.Default) + if (base.TryGetValue(theme, out var themePaletteResources) + && themePaletteResources.TryGetResource(key, theme, out value)) { - if (base.TryGetValue(ThemeVariant.Default, out var defaultPaletteResources) - && defaultPaletteResources.TryGetResource(key, ThemeVariant.Default, out value)) - { - return true; - } + return true; } value = null;