refactor: Abstract out modal expand/collapse animation/behavior

From the analytics module view (so it can be used by other windows)
This commit is contained in:
John Colvin committed 2022-05-10 13:31:57 -04:00
1 parent 904ef92ece
commit 9b5aea2458
3 files changed
+86 -60

No files matched your search

@@ -126,7 +126,7 @@
c:RadioButtonGroupManager.Manager="{StaticResource WindowTypeRadioButtonGroupManager}"
Content="Vol Surfaces"
DataContext="vol_surfaces"
IsChecked="{Binding Path='(shell:AnalyticsShellModuleView.ShowWindowTypeContent)', ElementName=VolSurfacesContent, Mode=TwoWay}"
IsChecked="{Binding Path='(u:ExpandCollapseAnimator.IsExpanded)', ElementName=VolSurfacesContent, Mode=TwoWay}"
Style="{StaticResource LargeMenuToggleButton}"
/>
@@ -144,7 +144,7 @@
Margin="0,6,0,0"
DataContext="slide_reports"
Content="Slide Reports"
IsChecked="{Binding Path='(shell:AnalyticsShellModuleView.ShowWindowTypeContent)', ElementName=SlideReportContent, Mode=TwoWay}"
IsChecked="{Binding Path='(u:ExpandCollapseAnimator.IsExpanded)', ElementName=SlideReportContent, Mode=TwoWay}"
Style="{StaticResource LargeMenuToggleButton}"
/>
@@ -162,7 +162,7 @@
Margin="0,6,0,0"
DataContext="combo_reports"
Content="Combo Report"
IsChecked="{Binding Path='(shell:AnalyticsShellModuleView.ShowWindowTypeContent)', ElementName=ComboReportContent, Mode=TwoWay}"
IsChecked="{Binding Path='(u:ExpandCollapseAnimator.IsExpanded)', ElementName=ComboReportContent, Mode=TwoWay}"
Style="{StaticResource LargeMenuToggleButton}"
/>
@@ -1,11 +1,5 @@
namespace mbts.ui.shell {
using bts.utils;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
/// <summary>
/// Interaction logic for VolSurfaceShellModuleContent.xaml
@@ -13,56 +7,5 @@
partial class AnalyticsShellModuleView : UserControl {
public AnalyticsShellModuleView() =>
InitializeComponent();
#region ShowWindowTypeContent
public static readonly DependencyProperty ShowWindowTypeContentProperty =
DependencyProperty.RegisterAttached(
"ShowWindowTypeContent", typeof(bool), typeof(AnalyticsShellModuleView),
new PropertyMetadata(false, _on_show_window_type_content_changed));
static void _on_show_window_type_content_changed(
DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (d is not FrameworkElement content) {
return;
}
var collapsible_container = content.find_ancestor_of_type<ContentControl>();
content.Measure(new(double.PositiveInfinity, double.PositiveInfinity));
bool show = (bool)e.NewValue;
var (from, to) = show ?
(0D, content.DesiredSize.Height) : (content.DesiredSize.Height, 0D);
var anim = new DoubleAnimation {
From = from,
To = to,
EasingFunction = new QuadraticEase() { EasingMode = EasingMode.EaseOut },
Duration = TimeSpan.FromSeconds(0.23)
};
Storyboard.SetTarget(anim, collapsible_container);
Storyboard.SetTargetProperty(anim, new(HeightProperty));
var sb = new Storyboard();
sb.Children.Add(anim);
sb.Begin();
if (show) {
// now that animation is complete, set height to nan so the content automatically
// resizes as items (reports) are added/removed
clear_height();
async void clear_height() {
while (sb.GetCurrentProgress() != 1.0) {
await Task.Delay(100);
}
collapsible_container.SetCurrentValue(HeightProperty, double.NaN);
}
}
}
public static bool GetShowWindowTypeContent(DependencyObject d) =>
(bool)d.GetValue(ShowWindowTypeContentProperty);
public static void SetShowWindowTypeContent(DependencyObject d, bool value) =>
d.SetValue(ShowWindowTypeContentProperty, value);
#endregion
}
}
@@ -0,0 +1,83 @@
#nullable enable
namespace mbts.ui {
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
static class ExpandCollapseAnimator {
#region ExpandHeightAnimation
public static readonly DependencyProperty ExpandHeightAnimationProperty =
DependencyProperty.RegisterAttached(
"ExpandHeightAnimation", typeof(DoubleAnimation), typeof(ExpandCollapseAnimator));
public static DoubleAnimation? GetExpandHeightAnimation(DependencyObject d) =>
d.GetValue(ExpandHeightAnimationProperty) as DoubleAnimation;
public static void SetExpandHeightAnimation(DependencyObject d, DoubleAnimation? value) =>
d.SetValue(ExpandHeightAnimationProperty, value);
#endregion
#region IsExpanded
public static readonly DependencyProperty IsExpandedProperty =
DependencyProperty.RegisterAttached(
"IsExpanded",
typeof(bool),
typeof(ExpandCollapseAnimator),
new(false, _on_is_expanded_changed));
static void _on_is_expanded_changed(
DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (d is not FrameworkElement content) {
return;
}
var anim = GetExpandHeightAnimation(content);
_animate(content, anim, (bool)e.NewValue);
}
static void _animate(FrameworkElement content, DoubleAnimation? anim, bool is_expanded) {
var collapsible_container = content.find_ancestor_of_type<ContentControl>();
content.Measure(new(double.PositiveInfinity, double.PositiveInfinity));
var (from, to) = is_expanded ?
(0D, content.DesiredSize.Height) : (content.DesiredSize.Height, 0D);
anim ??= _default_height_animation();
anim.SetCurrentValue(DoubleAnimation.FromProperty, from);
anim.SetCurrentValue(DoubleAnimation.ToProperty, to);
Storyboard.SetTarget(anim, collapsible_container);
Storyboard.SetTargetProperty(anim, new(FrameworkElement.HeightProperty));
var sb = new Storyboard();
sb.Children.Add(anim);
sb.Begin();
if (is_expanded) {
// now that animation is complete, set height to nan so the content automatically
// resizes as items (reports) are added/removed
clear_height();
async void clear_height() {
while (sb.GetCurrentProgress() != 1.0) {
await Task.Delay(100);
}
collapsible_container.SetCurrentValue(FrameworkElement.HeightProperty, double.NaN);
}
}
}
public static bool GetIsExpanded(DependencyObject d) =>
(bool)d.GetValue(IsExpandedProperty);
public static void SetIsExpanded(DependencyObject d, bool value) =>
d.SetValue(IsExpandedProperty, value);
#endregion
static DoubleAnimation _default_height_animation() =>
new() {
EasingFunction = new QuadraticEase() { EasingMode = EasingMode.EaseOut },
Duration = TimeSpan.FromSeconds(0.23)
};
}
}