-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExpandContent.xaml.cs
More file actions
91 lines (76 loc) · 2.38 KB
/
Copy pathExpandContent.xaml.cs
File metadata and controls
91 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using DependencyPropertyGenerator;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Coder.Desktop.App.Controls;
[ContentProperty(Name = nameof(Children))]
[DependencyProperty<bool>("IsOpen", DefaultValue = false)]
public sealed partial class ExpandContent : UserControl
{
public UIElementCollection Children => CollapsiblePanel.Children;
private bool? _pendingIsOpen;
public ExpandContent()
{
InitializeComponent();
Loaded += (_, __) =>
{
if (_pendingIsOpen is bool v)
{
_ = AnimateAsync(v);
_pendingIsOpen = null;
}
};
}
partial void OnIsOpenChanged(bool oldValue, bool newValue)
{
if (!this.IsLoaded)
{
_pendingIsOpen = newValue;
return;
}
_ = AnimateAsync(newValue);
}
private async Task AnimateAsync(bool open)
{
if (open)
{
if (_currentlyOpen is not null && _currentlyOpen != this)
await _currentlyOpen.StartCollapseAsync();
_currentlyOpen = this;
CollapsiblePanel.Visibility = Visibility.Visible;
VisualStateManager.GoToState(this, "ExpandedState", true);
await ExpandAsync();
}
else
{
if (_currentlyOpen == this) _currentlyOpen = null;
await StartCollapseAsync();
}
}
private static ExpandContent? _currentlyOpen;
private TaskCompletionSource? _collapseTcs;
private async Task ExpandAsync()
{
CollapsiblePanel.Visibility = Visibility.Visible;
VisualStateManager.GoToState(this, "ExpandedState", true);
var tcs = new TaskCompletionSource();
void done(object? s, object e) { ExpandSb.Completed -= done; tcs.SetResult(); }
ExpandSb.Completed += done;
await tcs.Task;
}
private Task StartCollapseAsync()
{
_collapseTcs = new TaskCompletionSource();
VisualStateManager.GoToState(this, "CollapsedState", true);
return _collapseTcs.Task;
}
private void CollapseStoryboard_Completed(object sender, object e)
{
CollapsiblePanel.Visibility = Visibility.Collapsed;
_collapseTcs?.TrySetResult();
_collapseTcs = null;
}
}