-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ExpressionNode.cs
253 lines (227 loc) · 8.26 KB
/
ExpressionNode.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace Avalonia.Data.Core.ExpressionNodes;
/// <summary>
/// A node in the binding path of an <see cref="BindingExpression"/>.
/// </summary>
internal abstract class ExpressionNode
{
private WeakReference<object?>? _source;
private object? _value = AvaloniaProperty.UnsetValue;
/// <summary>
/// Gets the index of the node in the binding path.
/// </summary>
public int Index { get; private set; }
/// <summary>
/// Gets the owning <see cref="BindingExpression"/>.
/// </summary>
public BindingExpression? Owner { get; private set; }
/// <summary>
/// Gets the source object from which the node will read its value.
/// </summary>
public object? Source
{
get
{
if (_source?.TryGetTarget(out var source) == true)
return source;
return null;
}
}
/// <summary>
/// Gets the current value of the node.
/// </summary>
public object? Value => _value;
/// <summary>
/// Appends a string representation of the expression node to a string builder.
/// </summary>
/// <param name="builder">The string builder.</param>
public virtual void BuildString(StringBuilder builder) { }
/// <summary>
/// Builds a string representation of a binding expression.
/// </summary>
/// <param name="builder">The string builder.</param>
/// <param name="nodes">The nodes in the binding expression.</param>
public virtual void BuildString(StringBuilder builder, IReadOnlyList<ExpressionNode> nodes)
{
if (Index > 0)
nodes[Index - 1].BuildString(builder, nodes);
BuildString(builder);
}
/// <summary>
/// Resets the node to its uninitialized state when the <see cref="Owner"/> is unsubscribed.
/// </summary>
public void Reset()
{
SetSource(null, null);
_source = null;
_value = AvaloniaProperty.UnsetValue;
}
/// <summary>
/// Sets the owner binding.
/// </summary>
/// <param name="owner">The owner binding.</param>
/// <param name="index">The index of the node in the binding path.</param>
/// <exception cref="InvalidOperationException">
/// The node already has an owner.
/// </exception>
public void SetOwner(BindingExpression owner, int index)
{
if (Owner is not null)
throw new InvalidOperationException($"{this} already has an owner.");
Owner = owner;
Index = index;
}
/// <summary>
/// Sets the <see cref="Source"/> from which the node will read its value and updates
/// the current <see cref="Value"/>, notifying the <see cref="Owner"/> if the value
/// changes.
/// </summary>
/// <param name="source">
/// The new source from which the node will read its value. May be
/// <see cref="AvaloniaProperty.UnsetValue"/> in which case the source will be considered
/// to be null.
/// </param>
/// <param name="dataValidationError">
/// Any data validation error reported by the previous expression node.
/// </param>
public void SetSource(object? source, Exception? dataValidationError)
{
var oldSource = Source;
if (source == AvaloniaProperty.UnsetValue)
source = null;
if (source == oldSource)
return;
if (oldSource is not null)
Unsubscribe(oldSource);
_source = new(source);
if (source is null)
{
// If the source is null then the value is null. We explicitly do not want to call
// OnSourceChanged as we don't want to raise errors for subsequent nodes in the
// binding change.
_value = AvaloniaProperty.UnsetValue;
}
else
{
try { OnSourceChanged(source, dataValidationError); }
catch (Exception e) { SetError(e); }
}
}
/// <summary>
/// Sets the current value to <see cref="AvaloniaProperty.UnsetValue"/>.
/// </summary>
protected void ClearValue() => SetValue(AvaloniaProperty.UnsetValue);
/// <summary>
/// Notifies the <see cref="Owner"/> of a data validation error.
/// </summary>
/// <param name="error">The error.</param>
protected void SetDataValidationError(Exception error)
{
if (error is TargetInvocationException tie)
error = tie.InnerException!;
Owner?.OnDataValidationError(error);
}
/// <summary>
/// Sets the current value to <see cref="AvaloniaProperty.UnsetValue"/> and notifies the
/// <see cref="Owner"/> of the error.
/// </summary>
/// <param name="message">The error message.</param>
protected void SetError(string message)
{
_value = AvaloniaProperty.UnsetValue;
Owner?.OnNodeError(Index, message);
}
/// <summary>
/// Sets the current value to <see cref="AvaloniaProperty.UnsetValue"/> and notifies the
/// <see cref="Owner"/> of the error.
/// </summary>
/// <param name="e">The error.</param>
protected void SetError(Exception e)
{
if (e is TargetInvocationException tie)
e = tie.InnerException!;
if (e is AggregateException ae && ae.InnerExceptions.Count == 1)
e = e.InnerException!;
SetError(e.Message);
}
/// <summary>
/// Sets the current <see cref="Value"/>, notifying the <see cref="Owner"/> if the value
/// has changed.
/// </summary>
/// <param name="valueOrNotification">
/// The new value. May be a <see cref="BindingNotification"/>.
/// </param>
protected void SetValue(object? valueOrNotification)
{
if (valueOrNotification is BindingNotification notification)
{
if (notification.ErrorType == BindingErrorType.Error)
{
SetError(notification.Error!);
}
else if (notification.ErrorType == BindingErrorType.DataValidationError)
{
if (notification.HasValue)
SetValue(notification.Value, notification.Error);
else
SetDataValidationError(notification.Error!);
}
else
{
SetValue(notification.Value, null);
}
}
else
{
SetValue(valueOrNotification, null);
}
}
/// <summary>
/// Sets the current <see cref="Value"/>, notifying the <see cref="Owner"/> if the value
/// has changed.
/// </summary>
/// <param name="value">
/// The new value. May not be a <see cref="BindingNotification"/>.
/// </param>
/// <param name="dataValidationError">
/// The data validation error associated with the new value, if any.
/// </param>
protected void SetValue(object? value, Exception? dataValidationError = null)
{
Debug.Assert(value is not BindingNotification);
if (Owner is null)
return;
// We raise a change notification if:
//
// - This is the initial value (_value is null)
// - There is a data validation error
// - There is no data validation error, but the owner has one
// - The new value is different to the old value
if (_value is null ||
dataValidationError is not null ||
(dataValidationError is null && Owner.ErrorType == BindingErrorType.DataValidationError) ||
!Equals(value, _value))
{
_value = value;
Owner.OnNodeValueChanged(Index, value, dataValidationError);
}
}
/// <summary>
/// When implemented in a derived class, subscribes to the new source, and updates the current
/// <see cref="Value"/>.
/// </summary>
/// <param name="source">The new source.</param>
/// <param name="dataValidationError">
/// Any data validation error reported by the previous expression node.
/// </param>
protected abstract void OnSourceChanged(object source, Exception? dataValidationError);
/// <summary>
/// When implemented in a derived class, unsubscribes from the previous source.
/// </summary>
/// <param name="oldSource">The old source.</param>
protected virtual void Unsubscribe(object oldSource) { }
}