|
2 | 2 | using Microsoft.UI.Xaml; |
3 | 3 | using Microsoft.UI.Xaml.Controls; |
4 | 4 | using Microsoft.UI.Xaml.Markup; |
| 5 | +using System; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
5 | 8 |
|
6 | 9 | namespace Coder.Desktop.App.Controls; |
7 | 10 |
|
| 11 | + |
8 | 12 | [ContentProperty(Name = nameof(Children))] |
9 | 13 | [DependencyProperty<bool>("IsOpen", DefaultValue = false)] |
10 | 14 | public sealed partial class ExpandContent : UserControl |
11 | 15 | { |
12 | 16 | public UIElementCollection Children => CollapsiblePanel.Children; |
13 | 17 |
|
| 18 | + private bool? _pendingIsOpen; |
| 19 | + |
| 20 | + private static readonly SemaphoreSlim _sem = new(1, 1); |
| 21 | + |
| 22 | + |
14 | 23 | public ExpandContent() |
15 | 24 | { |
16 | 25 | InitializeComponent(); |
| 26 | + Loaded += (_, __) => |
| 27 | + { |
| 28 | + if (_pendingIsOpen is bool v) |
| 29 | + { |
| 30 | + _ = AnimateAsync(v); |
| 31 | + _pendingIsOpen = null; |
| 32 | + } |
| 33 | + }; |
17 | 34 | } |
18 | 35 |
|
19 | | - public void CollapseAnimation_Completed(object? sender, object args) |
| 36 | + partial void OnIsOpenChanged(bool oldValue, bool newValue) |
20 | 37 | { |
21 | | - // Hide the panel completely when the collapse animation is done. This |
22 | | - // cannot be done with keyframes for some reason. |
23 | | - // |
24 | | - // Without this, the space will still be reserved for the panel. |
25 | | - CollapsiblePanel.Visibility = Visibility.Collapsed; |
| 38 | + if (!this.IsLoaded) |
| 39 | + { |
| 40 | + _pendingIsOpen = newValue; |
| 41 | + return; |
| 42 | + } |
| 43 | + _ = AnimateAsync(newValue); |
26 | 44 | } |
27 | 45 |
|
28 | | - partial void OnIsOpenChanged(bool oldValue, bool newValue) |
| 46 | + private async Task AnimateAsync(bool open) |
| 47 | + { |
| 48 | + await _sem.WaitAsync(); |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + if (open) |
| 53 | + { |
| 54 | + if (_currentlyOpen is not null && _currentlyOpen != this) |
| 55 | + await _currentlyOpen.StartCollapseAsync(); |
| 56 | + |
| 57 | + _currentlyOpen = this; |
| 58 | + CollapsiblePanel.Visibility = Visibility.Visible; |
| 59 | + |
| 60 | + VisualStateManager.GoToState(this, "ExpandedState", true); |
| 61 | + await ExpandAsync(); // wait for your own expand |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + if (_currentlyOpen == this) _currentlyOpen = null; |
| 66 | + await StartCollapseAsync(); |
| 67 | + } |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + _sem.Release(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static ExpandContent? _currentlyOpen; |
| 76 | + private TaskCompletionSource? _collapseTcs; |
| 77 | + |
| 78 | + private async Task ExpandAsync() |
29 | 79 | { |
30 | | - var newState = newValue ? "ExpandedState" : "CollapsedState"; |
| 80 | + CollapsiblePanel.Visibility = Visibility.Visible; |
| 81 | + VisualStateManager.GoToState(this, "ExpandedState", true); |
| 82 | + |
| 83 | + var tcs = new TaskCompletionSource(); |
| 84 | + void done(object? s, object e) { ExpandSb.Completed -= done; tcs.SetResult(); } |
| 85 | + ExpandSb.Completed += done; |
| 86 | + await tcs.Task; |
| 87 | + } |
31 | 88 |
|
32 | | - // The animation can't set visibility when starting or ending the |
33 | | - // animation. |
34 | | - if (newValue) |
35 | | - CollapsiblePanel.Visibility = Visibility.Visible; |
| 89 | + private Task StartCollapseAsync() |
| 90 | + { |
| 91 | + _collapseTcs = new TaskCompletionSource(); |
| 92 | + VisualStateManager.GoToState(this, "CollapsedState", true); |
| 93 | + return _collapseTcs.Task; |
| 94 | + } |
36 | 95 |
|
37 | | - VisualStateManager.GoToState(this, newState, true); |
| 96 | + private void CollapseStoryboard_Completed(object sender, object e) |
| 97 | + { |
| 98 | + CollapsiblePanel.Visibility = Visibility.Collapsed; |
| 99 | + _collapseTcs?.TrySetResult(); |
| 100 | + _collapseTcs = null; |
38 | 101 | } |
39 | 102 | } |
0 commit comments