Browse Source

Allow only Light and Dark in the ColorPaletteResourcesCollection

pull/11843/head
Max Katz 3 years ago
parent
commit
1381ec28be
  1. 3
      samples/ControlCatalog/App.xaml
  2. 4
      samples/ControlCatalog/App.xaml.cs
  3. 25
      src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs

3
samples/ControlCatalog/App.xaml

@ -28,6 +28,9 @@
</ResourceDictionary.ThemeDictionaries> </ResourceDictionary.ThemeDictionaries>
<!-- Styles attached dynamically depending on current theme (simple or fluent) --> <!-- Styles attached dynamically depending on current theme (simple or fluent) -->
<FluentTheme x:Key="FluentTheme">
</FluentTheme>
<SimpleTheme x:Key="SimpleTheme" />
<StyleInclude x:Key="DataGridFluent" Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" /> <StyleInclude x:Key="DataGridFluent" Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
<StyleInclude x:Key="DataGridSimple" Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" /> <StyleInclude x:Key="DataGridSimple" Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" />
<StyleInclude x:Key="ColorPickerFluent" Source="avares://Avalonia.Controls.ColorPicker/Themes/Fluent/Fluent.xaml" /> <StyleInclude x:Key="ColorPickerFluent" Source="avares://Avalonia.Controls.ColorPicker/Themes/Fluent/Fluent.xaml" />

4
samples/ControlCatalog/App.xaml.cs

@ -30,8 +30,8 @@ namespace ControlCatalog
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
_fluentTheme = new FluentTheme(); _fluentTheme = (FluentTheme)Resources["FluentTheme"]!;
_simpleTheme = new SimpleTheme(); _simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!;
_colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!; _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
_colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!; _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
_dataGridFluent = (IStyle)Resources["DataGridFluent"]!; _dataGridFluent = (IStyle)Resources["DataGridFluent"]!;

25
src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs

@ -10,12 +10,18 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary<ThemeVariant
public ColorPaletteResourcesCollection() : base(2) public ColorPaletteResourcesCollection() : base(2)
{ {
this.ForEachItem( this.ForEachItem(
(_, x) => (key, x) =>
{ {
if (Owner is not null) if (Owner is not null)
{ {
x.PropertyChanged += Palette_PropertyChanged; 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) => (_, x) =>
{ {
@ -30,22 +36,15 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary<ThemeVariant
public bool HasResources => Count > 0; public bool HasResources => Count > 0;
public bool TryGetResource(object key, ThemeVariant? theme, out object? value) 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) theme = ThemeVariant.Light;
&& themePaletteResources.TryGetResource(key, theme, out value))
{
return true;
}
} }
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) return true;
&& defaultPaletteResources.TryGetResource(key, ThemeVariant.Default, out value))
{
return true;
}
} }
value = null; value = null;

Loading…
Cancel
Save