Browse Source

Fix compat fluent theme not being applied

pull/11882/head
Max Katz 3 years ago
parent
commit
349d7e9e67
  1. 13
      src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml
  2. 2
      src/Avalonia.Themes.Fluent/FluentTheme.xaml
  3. 35
      src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs

13
src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml

@ -1,10 +1,3 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:ClassModifier="internal">
<Style Selector="TextBlock" >
<Setter Property="FontSize" Value="14" />
</Style>
<Styles.Resources>
<x:Double x:Key="ControlContentThemeFontSize">14</x:Double>
<x:Double x:Key="ContentControlFontSize">14</x:Double>
<x:Double x:Key="TextControlThemeMinHeight">24</x:Double>
@ -18,5 +11,7 @@
<x:Double x:Key="ComboBoxMinHeight">24</x:Double>
<Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness>
<x:Double x:Key="NavigationViewItemOnLeftMinHeight">32</x:Double>
</Styles.Resources>
</Styles>
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:ClassModifier="internal">
</ResourceDictionary>

2
src/Avalonia.Themes.Fluent/FluentTheme.xaml

@ -18,7 +18,7 @@
</ResourceDictionary.MergedDictionaries>
<!-- These are not part of MergedDictionaries so we can add or remove them easier -->
<StyleInclude x:Key="CompactStyles" Source="/DensityStyles/Compact.xaml" />
<ResourceInclude x:Key="CompactStyles" Source="/DensityStyles/Compact.xaml" />
</ResourceDictionary>
</Styles.Resources>

35
src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs

@ -16,9 +16,10 @@ namespace Avalonia.Themes.Fluent
/// <summary>
/// Includes the fluent theme in an application.
/// </summary>
public class FluentTheme : Styles
public class FluentTheme : Styles, IResourceNode
{
private readonly Styles _compactStyles;
private readonly ResourceDictionary _compactStyles;
private DensityStyle _densityStyle;
/// <summary>
/// Initializes a new instance of the <see cref="FluentTheme"/> class.
@ -28,9 +29,7 @@ namespace Avalonia.Themes.Fluent
{
AvaloniaXamlLoader.Load(sp, this);
_compactStyles = (Styles)GetAndRemove("CompactStyles");
EnsureCompactStyles();
_compactStyles = (ResourceDictionary)GetAndRemove("CompactStyles");
Palettes = Resources.MergedDictionaries.OfType<ColorPaletteResourcesCollection>().FirstOrDefault()
?? throw new InvalidOperationException("FluentTheme was initialized with missing ColorPaletteResourcesCollection.");
@ -43,17 +42,17 @@ namespace Avalonia.Themes.Fluent
return val;
}
}
public static readonly StyledProperty<DensityStyle> DensityStyleProperty =
AvaloniaProperty.Register<FluentTheme, DensityStyle>(nameof(DensityStyle));
public static readonly DirectProperty<FluentTheme, DensityStyle> DensityStyleProperty = AvaloniaProperty.RegisterDirect<FluentTheme, DensityStyle>(
nameof(DensityStyle), o => o.DensityStyle, (o, v) => o.DensityStyle = v);
/// <summary>
/// Gets or sets the density style of the fluent theme (normal, compact).
/// </summary>
public DensityStyle DensityStyle
{
get => GetValue(DensityStyleProperty);
set => SetValue(DensityStyleProperty, value);
get => _densityStyle;
set => SetAndRaise(DensityStyleProperty, ref _densityStyle, value);
}
public IDictionary<ThemeVariant, ColorPaletteResources> Palettes { get; }
@ -64,20 +63,20 @@ namespace Avalonia.Themes.Fluent
if (change.Property == DensityStyleProperty)
{
EnsureCompactStyles();
Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
}
}
private void EnsureCompactStyles()
bool IResourceNode.TryGetResource(object key, ThemeVariant? theme, out object? value)
{
if (DensityStyle == DensityStyle.Compact)
// DensityStyle dictionary should be checked first
if (_densityStyle == DensityStyle.Compact
&& _compactStyles.TryGetResource(key, theme, out value))
{
Add(_compactStyles);
}
else
{
Remove(_compactStyles);
return true;
}
return base.TryGetResource(key, theme, out value);
}
}
}

Loading…
Cancel
Save