From 4bcd5f5dd8cb8f22ef906f8fa8582dc63902e83b Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Thu, 27 Jan 2022 14:22:57 +0000 Subject: [PATCH 1/2] add density style option in Fluent Theme provider --- src/Avalonia.Themes.Fluent/FluentTheme.cs | 53 ++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.cs b/src/Avalonia.Themes.Fluent/FluentTheme.cs index 53be41e4d1..8625eef102 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.cs +++ b/src/Avalonia.Themes.Fluent/FluentTheme.cs @@ -15,6 +15,12 @@ namespace Avalonia.Themes.Fluent Dark, } + public enum DensityStyle + { + Normal, + Compact + } + /// /// Includes the fluent theme in an application. /// @@ -24,6 +30,7 @@ namespace Avalonia.Themes.Fluent private Styles _fluentDark = new(); private Styles _fluentLight = new(); private Styles _sharedStyles = new(); + private Styles _densityStyles = new(); private bool _isLoading; private IStyle? _loaded; @@ -47,7 +54,6 @@ namespace Avalonia.Themes.Fluent InitStyles(_baseUri); } - public static readonly StyledProperty ModeProperty = AvaloniaProperty.Register(nameof(Mode)); /// @@ -58,6 +64,18 @@ namespace Avalonia.Themes.Fluent get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } + + public static readonly StyledProperty DensityStyleProperty = + AvaloniaProperty.Register(nameof(DensityStyle)); + /// + /// Gets or sets the density style of the fluent theme (normal, compact). + /// + public DensityStyle DensityStyle + { + get => GetValue(DensityStyleProperty); + set => SetValue(DensityStyleProperty, value); + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); @@ -74,6 +92,25 @@ namespace Avalonia.Themes.Fluent (Loaded as Styles)![2] = _fluentLight[1]; } } + + if (change.Property == DensityStyleProperty) + { + if (DensityStyle == DensityStyle.Compact) + { + if ((Loaded as Styles)!.Count > 3) + { + (Loaded as Styles)![3] = _densityStyles[0]; + } + else + { + (Loaded as Styles)!.Add( _densityStyles[0]); + } + } + else if(DensityStyle == DensityStyle.Normal) + { + (Loaded as Styles)!.Remove(_densityStyles[0]); + } + } } public IResourceHost? Owner => (Loaded as IResourceProvider)?.Owner; @@ -97,6 +134,12 @@ namespace Avalonia.Themes.Fluent { _loaded = new Styles() { _sharedStyles, _fluentDark[0], _fluentDark[1] }; } + + if (DensityStyle == DensityStyle.Compact) + { + (_loaded as Styles)!.Add(_densityStyles[0]); + } + _isLoading = false; } @@ -183,6 +226,14 @@ namespace Avalonia.Themes.Fluent Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml") } }; + + _densityStyles = new Styles + { + new StyleInclude(baseUri) + { + Source = new Uri("avares://Avalonia.Themes.Fluent/DensityStyles/Compact.xaml") + } + }; } } } From 9a5fe6cbdffff1c09a2cd25caed02ebe83cb6391 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Sat, 29 Jan 2022 11:13:35 +0000 Subject: [PATCH 2/2] fixup --- src/Avalonia.Themes.Fluent/FluentTheme.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.cs b/src/Avalonia.Themes.Fluent/FluentTheme.cs index 8625eef102..d1136d44a4 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.cs +++ b/src/Avalonia.Themes.Fluent/FluentTheme.cs @@ -56,6 +56,10 @@ namespace Avalonia.Themes.Fluent public static readonly StyledProperty ModeProperty = AvaloniaProperty.Register(nameof(Mode)); + + public static readonly StyledProperty DensityStyleProperty = + AvaloniaProperty.Register(nameof(DensityStyle)); + /// /// Gets or sets the mode of the fluent theme (light, dark). /// @@ -64,9 +68,7 @@ namespace Avalonia.Themes.Fluent get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } - - public static readonly StyledProperty DensityStyleProperty = - AvaloniaProperty.Register(nameof(DensityStyle)); + /// /// Gets or sets the density style of the fluent theme (normal, compact). /// @@ -97,16 +99,9 @@ namespace Avalonia.Themes.Fluent { if (DensityStyle == DensityStyle.Compact) { - if ((Loaded as Styles)!.Count > 3) - { - (Loaded as Styles)![3] = _densityStyles[0]; - } - else - { - (Loaded as Styles)!.Add( _densityStyles[0]); - } + (Loaded as Styles)!.Add(_densityStyles[0]); } - else if(DensityStyle == DensityStyle.Normal) + else if (DensityStyle == DensityStyle.Normal) { (Loaded as Styles)!.Remove(_densityStyles[0]); }