Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#7449 from emmauss/fluent-compact
Browse files Browse the repository at this point in the history
Add DensityStyle property in Fluent Theme provider
  • Loading branch information
Takoooooo authored and danwalmsley committed Feb 21, 2022
1 parent 7d771c8 commit 870f62f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/Avalonia.Themes.Fluent/FluentTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public enum FluentThemeMode
Dark,
}

public enum DensityStyle
{
Normal,
Compact
}

/// <summary>
/// Includes the fluent theme in an application.
/// </summary>
Expand All @@ -24,6 +30,7 @@ public class FluentTheme : AvaloniaObject, IStyle, IResourceProvider
private Styles _fluentDark = new();
private Styles _fluentLight = new();
private Styles _sharedStyles = new();
private Styles _densityStyles = new();
private bool _isLoading;
private IStyle? _loaded;

Expand All @@ -47,9 +54,12 @@ public FluentTheme(IServiceProvider serviceProvider)
InitStyles(_baseUri);
}


public static readonly StyledProperty<FluentThemeMode> ModeProperty =
AvaloniaProperty.Register<FluentTheme, FluentThemeMode>(nameof(Mode));

public static readonly StyledProperty<DensityStyle> DensityStyleProperty =
AvaloniaProperty.Register<FluentTheme, DensityStyle>(nameof(DensityStyle));

/// <summary>
/// Gets or sets the mode of the fluent theme (light, dark).
/// </summary>
Expand All @@ -58,6 +68,16 @@ public FluentThemeMode Mode
get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}

/// <summary>
/// Gets or sets the density style of the fluent theme (normal, compact).
/// </summary>
public DensityStyle DensityStyle
{
get => GetValue(DensityStyleProperty);
set => SetValue(DensityStyleProperty, value);
}

protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
Expand All @@ -74,6 +94,18 @@ protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T>
(Loaded as Styles)![2] = _fluentLight[1];
}
}

if (change.Property == DensityStyleProperty)
{
if (DensityStyle == DensityStyle.Compact)
{
(Loaded as Styles)!.Add(_densityStyles[0]);
}
else if (DensityStyle == DensityStyle.Normal)
{
(Loaded as Styles)!.Remove(_densityStyles[0]);
}
}
}

public IResourceHost? Owner => (Loaded as IResourceProvider)?.Owner;
Expand All @@ -97,6 +129,12 @@ public IStyle Loaded
{
_loaded = new Styles() { _sharedStyles, _fluentDark[0], _fluentDark[1] };
}

if (DensityStyle == DensityStyle.Compact)
{
(_loaded as Styles)!.Add(_densityStyles[0]);
}

_isLoading = false;
}

Expand Down Expand Up @@ -183,6 +221,14 @@ private void InitStyles(Uri baseUri)
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")
}
};
}
}
}

0 comments on commit 870f62f

Please sign in to comment.