Skip to content

Commit

Permalink
add density style option in Fluent Theme provider
Browse files Browse the repository at this point in the history
  • Loading branch information
emmauss committed Jan 27, 2022
1 parent c0ec9d6 commit 96be812
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 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,7 +54,6 @@ public FluentTheme(IServiceProvider serviceProvider)
InitStyles(_baseUri);
}


public static readonly StyledProperty<FluentThemeMode> ModeProperty =
AvaloniaProperty.Register<FluentTheme, FluentThemeMode>(nameof(Mode));
/// <summary>
Expand All @@ -58,6 +64,18 @@ public FluentThemeMode Mode
get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}

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>
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 +92,25 @@ protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T>
(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;
Expand All @@ -97,6 +134,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 +226,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 96be812

Please sign in to comment.