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="ControlContentThemeFontSize">14</x:Double>
<x:Double x:Key="ContentControlFontSize">14</x:Double> <x:Double x:Key="ContentControlFontSize">14</x:Double>
<x:Double x:Key="TextControlThemeMinHeight">24</x:Double> <x:Double x:Key="TextControlThemeMinHeight">24</x:Double>
@ -18,5 +11,7 @@
<x:Double x:Key="ComboBoxMinHeight">24</x:Double> <x:Double x:Key="ComboBoxMinHeight">24</x:Double>
<Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness> <Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness>
<x:Double x:Key="NavigationViewItemOnLeftMinHeight">32</x:Double> <x:Double x:Key="NavigationViewItemOnLeftMinHeight">32</x:Double>
</Styles.Resources> <ResourceDictionary xmlns="https://github.com/avaloniaui"
</Styles> 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> </ResourceDictionary.MergedDictionaries>
<!-- These are not part of MergedDictionaries so we can add or remove them easier --> <!-- 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> </ResourceDictionary>
</Styles.Resources> </Styles.Resources>

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

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

Loading…
Cancel
Save