-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathButtonExtensions.cs
259 lines (220 loc) · 8.15 KB
/
ButtonExtensions.cs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Windows.Foundation;
using Uno.Extensions.Reactive.Commands;
namespace Uno.Extensions.Reactive.UI;
/// <summary>
/// Extensions for <see cref="ButtonBase"/>.
/// </summary>
public static class ButtonExtensions
{
#region IsExecutionTrackingEnabled (Attached DP)
/// <summary>
/// Backing property for the IsExecutionTrackingEnabled flag.
/// </summary>
public static readonly DependencyProperty IsExecutionTrackingEnabledProperty = DependencyProperty.RegisterAttached(
"IsExecutionTrackingEnabled", typeof(bool), typeof(ButtonExtensions), new PropertyMetadata(default(bool), OnIsEnabledChanged));
/// <summary>
/// Gets a bool which indicates if the tracking of execution of AsyncCommand is enabled or not.
/// This will enable the extended visual states on the Button: Idle, Executing, Failed, Succeed.
/// It's also required to enable this to get the <see cref="LastExecutionErrorProperty"/> to be full-filled.
/// </summary>
/// <param name="button">The button to get the flag for.</param>
/// <returns>True if async command execution tracking is enabled, false otherwise.</returns>
public static bool GetIsExecutionTrackingEnabled(ButtonBase button)
=> (bool)button.GetValue(IsExecutionTrackingEnabledProperty);
/// <summary>
/// Sets a bool which indicates if the tracking of execution of AsyncCommand is enabled or not.
/// This will enable the extended visual states on the Button: Idle, Executing, Failed, Succeed.
/// It's also required to enable this to get the <see cref="LastExecutionErrorProperty"/> to be updated.
/// </summary>
/// <param name="button">The button to set the flag for.</param>
/// <param name="isEnabled">True to enable tracking of async command execution, false otherwise.</param>
public static void SetIsExecutionTrackingEnabled(ButtonBase button, bool isEnabled)
=> button.SetValue(IsExecutionTrackingEnabledProperty, isEnabled);
#endregion
#region LastExecutionError (Attached DP)
/// <summary>
/// Backing property for the LastExecutionError.
/// </summary>
public static readonly DependencyProperty LastExecutionErrorProperty = DependencyProperty.RegisterAttached(
"LastExecutionError", typeof(Exception), typeof(ButtonExtensions), new PropertyMetadata(default));
/// <summary>
/// Gets the exception raised by last execution of the command.
/// The <see cref="IsExecutionTrackingEnabledProperty"/> as to be enabled to get this property updated.
/// </summary>
/// <param name="button">The button to get the error for.</param>
/// <returns>The error raised by the command on last execution.</returns>
public static Exception GetLastExecutionError(ButtonBase button)
=> (Exception)button.GetValue(LastExecutionErrorProperty);
#endregion
private static void OnIsEnabledChanged(DependencyObject snd, DependencyPropertyChangedEventArgs args)
{
if (snd is not ButtonBase button)
{
return;
}
if (args.NewValue is true)
{
AsyncCommandExecutionTracker.GetOrCreate(button).Enable();
}
else if (AsyncCommandExecutionTracker.TryGet(button, out var states))
{
states!.Dispose();
}
}
private class AsyncCommandExecutionTracker : IDisposable
{
private const string _idleStateName = "Idle";
private const string _executingStateName = "Executing";
private const string _failedStateName = "Failed";
private const string _succeedStateName = "Succeed";
private static readonly ConditionalWeakTable<ButtonBase, AsyncCommandExecutionTracker> _instances = new();
public static AsyncCommandExecutionTracker GetOrCreate(ButtonBase button)
=> _instances.GetValue(button, b => new AsyncCommandExecutionTracker(b));
public static bool TryGet(ButtonBase button, /*CS0436 [NotNullWhen(true)]*/ out AsyncCommandExecutionTracker? instance)
=> _instances.TryGetValue(button, out instance);
private readonly HashSet<Guid> _activeExecutions = new();
private readonly HashSet<Exception> _activeErrors = new();
private readonly ButtonBase _button;
private long? _commandRegistrationToken;
private long? _isPressedRegistrationToken;
private bool _isButtonPressed;
private IAsyncAction? _toggleIsButtonPressed;
private WeakReference<AsyncCommand>? _command;
private bool _isDisposed;
private AsyncCommandExecutionTracker(ButtonBase button)
{
_button = button;
}
public void Enable()
{
if (_isDisposed || _commandRegistrationToken is not null)
{
return;
}
_commandRegistrationToken = _button.RegisterPropertyChangedCallback(ButtonBase.CommandProperty, OnCommandChanged);
_isPressedRegistrationToken = _button.RegisterPropertyChangedCallback(ButtonBase.IsPressedProperty, OnIsPressedChanged);
Subscribe(_button.Command);
}
private void OnIsPressedChanged(DependencyObject sender, DependencyProperty dp)
{
if (sender is ButtonBase button && TryGet(button, out var that))
{
that!._toggleIsButtonPressed?.Cancel();
if (button.IsPressed)
{
that._isButtonPressed = true;
}
else
{
_toggleIsButtonPressed = button.Dispatcher.RunIdleAsync(_ => that._isButtonPressed = false);
}
}
}
private static void OnCommandChanged(DependencyObject sender, DependencyProperty dp)
{
if (sender is ButtonBase button && TryGet(button, out var that))
{
that!.Subscribe(button.Command);
}
}
private void Subscribe(ICommand? command)
{
AsyncCommand? previous = default;
_command?.TryGetTarget(out previous);
if (_isDisposed || previous == command)
{
return; // Weird case !
}
if (previous is not null)
{
UnSubscribe(previous);
}
ClearState();
if (command is AsyncCommand current)
{
_command = new WeakReference<AsyncCommand>(current);
current.ExecutionStarted += OnExecutionStarted;
current.ExecutionCompleted += OnExecutionCompleted;
}
else
{
_command = null;
}
}
private void UnSubscribe(AsyncCommand command)
{
command.ExecutionStarted -= OnExecutionStarted;
command.ExecutionCompleted -= OnExecutionCompleted;
}
private void OnExecutionStarted(object? sender, ExecutionStartedEventArgs e)
{
if (!_isDisposed && _isButtonPressed && e.Parameter == _button.CommandParameter)
{
if (_activeExecutions is { Count: 0 })
{
// On first active execution we make sure to clear the state of the previous executions
_activeErrors.Clear();
_button.SetValue(LastExecutionErrorProperty, null);
}
_activeExecutions.Add(e.Id);
VisualStateManager.GoToState(_button, _executingStateName, useTransitions: true);
}
}
private void OnExecutionCompleted(object? sender, ExecutionCompletedEventArgs e)
{
if (!_isDisposed && _activeExecutions.Remove(e.Id))
{
if (e.Error is not null)
{
_activeErrors.Add(e.Error);
}
if (_activeExecutions is { Count: 0 })
{
// At the end of the last active execution we leave the Executing state
var (state, error) = _activeErrors switch
{
{ Count: 1 } => (_failedStateName, _activeErrors.First()),
{ Count: > 1 } => (_failedStateName, new AggregateException(_activeErrors)),
_ => (_succeedStateName, default)
};
_activeErrors.Clear();
_button.SetValue(LastExecutionErrorProperty, error);
VisualStateManager.GoToState(_button, state, useTransitions: true);
}
}
}
private void ClearState()
{
_toggleIsButtonPressed?.Cancel();
_isButtonPressed = false;
_activeExecutions.Clear();
_activeErrors.Clear();
_button.SetValue(LastExecutionErrorProperty, null);
VisualStateManager.GoToState(_button, _idleStateName, useTransitions: false);
}
/// <inheritdoc />
public void Dispose()
{
_isDisposed = true;
if (_commandRegistrationToken is not null)
{
_button.UnregisterPropertyChangedCallback(ButtonBase.CommandProperty, _commandRegistrationToken.Value);
}
if (_isPressedRegistrationToken is not null)
{
_button.UnregisterPropertyChangedCallback(ButtonBase.IsPressedProperty, _isPressedRegistrationToken.Value);
}
if (_command?.TryGetTarget(out var command) ?? false)
{
UnSubscribe(command);
}
ClearState();
}
}
}