Skip to content

Commit ef69579

Browse files
committed
added comments, simplified logic
1 parent 2efcdd4 commit ef69579

File tree

2 files changed

+18
-49
lines changed

2 files changed

+18
-49
lines changed

App/Controls/ExpandContent.xaml.cs

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public sealed partial class ExpandContent : UserControl
1414
{
1515
public UIElementCollection Children => CollapsiblePanel.Children;
1616

17-
private bool? _pendingIsOpen;
18-
17+
private readonly string _expandedState = "ExpandedState";
18+
private readonly string _collapsedState = "CollapsedState";
1919

2020
public ExpandContent()
2121
{
@@ -26,8 +26,8 @@ public ExpandContent()
2626
// we need to set the initial state based on IsOpen.
2727
VisualStateManager.GoToState(
2828
this,
29-
IsOpen ? "ExpandedState" : "CollapsedState",
30-
useTransitions: false); // NO animation yet
29+
IsOpen ? _expandedState : _collapsedState,
30+
useTransitions: false); // NO animation yet
3131

3232
// If IsOpen was already true we must also show the panel
3333
if (IsOpen)
@@ -41,59 +41,21 @@ public ExpandContent()
4141

4242
partial void OnIsOpenChanged(bool oldValue, bool newValue)
4343
{
44-
if (!IsLoaded)
44+
var newState = newValue ? _expandedState : _collapsedState;
45+
if (newValue)
4546
{
46-
_pendingIsOpen = newValue;
47-
return;
48-
}
49-
_ = AnimateAsync(newValue);
50-
}
51-
52-
private async Task AnimateAsync(bool open)
53-
{
54-
if (open)
55-
{
56-
if (_currentlyOpen is not null && _currentlyOpen != this)
57-
await _currentlyOpen.StartCollapseAsync();
58-
59-
_currentlyOpen = this;
6047
CollapsiblePanel.Visibility = Visibility.Visible;
61-
62-
VisualStateManager.GoToState(this, "ExpandedState", true);
63-
await ExpandAsync();
48+
// We use BeginTime to ensure other panels are collapsed first.
49+
// If the user clicks the expand button quickly, we want to avoid
50+
// the panel expanding to its full height before the collapse animation completes.
51+
CollapseSb.SkipToFill();
6452
}
65-
else
66-
{
67-
if (_currentlyOpen == this) _currentlyOpen = null;
68-
await StartCollapseAsync();
69-
}
70-
}
71-
72-
private static ExpandContent? _currentlyOpen;
73-
private TaskCompletionSource? _collapseTcs;
74-
75-
private async Task ExpandAsync()
76-
{
77-
CollapsiblePanel.Visibility = Visibility.Visible;
78-
VisualStateManager.GoToState(this, "ExpandedState", true);
79-
80-
var tcs = new TaskCompletionSource();
81-
void done(object? s, object e) { ExpandSb.Completed -= done; tcs.SetResult(); }
82-
ExpandSb.Completed += done;
83-
await tcs.Task;
84-
}
8553

86-
private Task StartCollapseAsync()
87-
{
88-
_collapseTcs = new TaskCompletionSource();
89-
VisualStateManager.GoToState(this, "CollapsedState", true);
90-
return _collapseTcs.Task;
54+
VisualStateManager.GoToState(this, newState, true);
9155
}
9256

9357
private void CollapseStoryboard_Completed(object sender, object e)
9458
{
9559
CollapsiblePanel.Visibility = Visibility.Collapsed;
96-
_collapseTcs?.TrySetResult();
97-
_collapseTcs = null;
9860
}
9961
}

App/Views/TrayWindow.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ private void RootFrame_SizeChanged(object sender, SizedFrameEventArgs e)
178178
AnimateWindowHeight(e.NewSize.Height);
179179
}
180180

181+
// We need to animate the height change in code-behind, because XAML
182+
// storyboard animation timeline is immutable - it cannot be changed
183+
// mid-run to accomodate a new height.
181184
private void AnimateWindowHeight(double targetHeight)
182185
{
183186
// If another animation is already running we need to fast forward it.
@@ -211,9 +214,13 @@ private void AnimateWindowHeight(double targetHeight)
211214

212215
private void OnStoryboardCompleted(object? sender, object e)
213216
{
217+
// We need to remove the event handler after the storyboard completes,
218+
// to avoid memory leaks and multiple calls.
214219
if (sender is Storyboard sb)
215220
sb.Completed -= OnStoryboardCompleted;
216221

222+
// SizeChanged handler will stop forwarding resize ticks
223+
// until we start the next storyboard.
217224
_currentSb = null;
218225
}
219226

0 commit comments

Comments
 (0)