Skip to content

Commit

Permalink
Improves ShadowAssist.Darken, replace animation with transition for B…
Browse files Browse the repository at this point in the history
…order.BoxShadow. Fixes #320
  • Loading branch information
SKProCH committed Dec 11, 2023
1 parent c65e481 commit d9a26ec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
64 changes: 17 additions & 47 deletions Material.Styles/Assists/ShadowAssist.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Globalization;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Styling;

namespace Material.Styles.Assists {
public static class ShadowProvider {
Expand Down Expand Up @@ -68,62 +65,35 @@ static ShadowAssist() {
DarkenProperty.Changed.Subscribe(DarkenPropertyChangedCallback);
}

private static void ShadowDepthChangedCallback(AvaloniaPropertyChangedEventArgs args) {
if (args.Sender is Border border)
border.BoxShadow =
(args.NewValue as ShadowDepth? ?? ShadowDepth.Depth0)
.ToBoxShadows();
}

public static void SetShadowDepth(AvaloniaObject element, ShadowDepth value)
=> element.SetValue(ShadowDepthProperty, value);

public static ShadowDepth GetShadowDepth(AvaloniaObject element)
=> element.GetValue<ShadowDepth>(ShadowDepthProperty);

public static void SetDarken(AvaloniaObject element, bool value)
=> element.SetValue(DarkenProperty, value);

public static bool GetDarken(AvaloniaObject element)
=> element.GetValue<bool>(DarkenProperty);

private static void ShadowDepthChangedCallback(AvaloniaPropertyChangedEventArgs args) {
if (args.Sender is Border border) {
border.BoxShadow =
(args.NewValue as ShadowDepth? ?? ShadowDepth.Depth0)
.ToBoxShadows();
}
}

private static void DarkenPropertyChangedCallback(AvaloniaPropertyChangedEventArgs obj) {
if (obj.Sender is not Border border)
return;

var boxShadow = border.BoxShadow;

var targetBoxShadows = (bool?)obj.NewValue == true
? GetShadowDepth(border).ToBoxShadows(Color.FromArgb(255, 0, 0, 0))
? GetShadowDepth(border).ToBoxShadows(Avalonia.Media.Colors.Black)
: GetShadowDepth(border).ToBoxShadows();

if (!border.Classes.Contains("no-transitions")) {
var animation = new Animation { Duration = TimeSpan.FromMilliseconds(350), FillMode = FillMode.Both };
animation.Children.Add(
new KeyFrame {
Cue = Cue.Parse("0%", CultureInfo.CurrentCulture),
Setters = {
new Setter {
Property = Border.BoxShadowProperty,
Value = boxShadow
}
}
});
animation.Children.Add(
new KeyFrame {
Cue = Cue.Parse("100%", CultureInfo.CurrentCulture),
Setters = {
new Setter {
Property = Border.BoxShadowProperty,
Value = targetBoxShadows
}
}
});
animation.RunAsync(border);
}
else {
border.SetValue(Border.BoxShadowProperty, targetBoxShadows);
}
border.SetValue(Border.BoxShadowProperty, targetBoxShadows);
}

public static void SetDarken(AvaloniaObject element, bool value)
=> element.SetValue(DarkenProperty, value);

public static bool GetDarken(AvaloniaObject element)
=> element.GetValue<bool>(DarkenProperty);
}
}
}
10 changes: 10 additions & 0 deletions Material.Styles/Border.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="Border:not(.no-transitions)">
<Setter Property="Transitions">
<Transitions>
<BoxShadowsTransition Property="BoxShadow" Duration="0:0:0.35" />
</Transitions>
</Setter>
</Style>
</Styles>
3 changes: 2 additions & 1 deletion Material.Styles/MaterialToolKit.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>


<StyleInclude Source="avares://Material.Styles/Border.axaml" />
<StyleInclude Source="avares://Material.Styles/UserControl.xaml" />
</Styles>

0 comments on commit d9a26ec

Please sign in to comment.