-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathAttachedShadowElementContext.cs
More file actions
325 lines (271 loc) · 10.7 KB
/
AttachedShadowElementContext.cs
File metadata and controls
325 lines (271 loc) · 10.7 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Numerics;
#if WINAPPSDK
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml.Hosting;
#else
using Windows.UI.Composition;
using Windows.UI.Xaml.Hosting;
#endif
namespace CommunityToolkit.WinUI;
/// <summary>
/// Class which maintains the context of a <see cref="DropShadow"/> for a particular <see cref="UIElement"/> linked to the definition of that shadow provided by the <see cref="AttachedShadowBase"/> implementation being used.
/// </summary>
public sealed class AttachedShadowElementContext
{
private bool _isConnected;
private Dictionary<string, object> _resources = new();
internal long? VisibilityToken { get; set; }
/// <summary>
/// Gets a value indicating whether or not this <see cref="AttachedShadowElementContext"/> has been initialized.
/// </summary>
public bool IsInitialized { get; private set; }
/// <summary>
/// Gets the <see cref="AttachedShadowBase"/> that contains this <see cref="AttachedShadowElementContext"/>.
/// </summary>
public AttachedShadowBase Parent { get; private set; }
/// <summary>
/// Gets the <see cref="FrameworkElement"/> this instance is attached to
/// </summary>
public FrameworkElement Element { get; private set; }
/// <summary>
/// Gets the <see cref="Visual"/> for the <see cref="FrameworkElement"/> this instance is attached to.
/// </summary>
public Visual? ElementVisual { get; private set; }
/// <summary>
/// Gets the <see cref="Windows.UI.Composition.Compositor"/> for this instance.
/// </summary>
public Compositor? Compositor { get; private set; }
/// <summary>
/// Gets the <see cref="SpriteVisual"/> that contains the <see cref="DropShadow">shadow</see> for this instance
/// </summary>
public SpriteVisual? SpriteVisual { get; private set; }
/// <summary>
/// Gets the <see cref="DropShadow"/> that is rendered on this instance's <see cref="Element"/>
/// </summary>
public DropShadow? Shadow { get; private set; }
/// <summary>
/// Connects a <see cref="FrameworkElement"/> to its parent <see cref="AttachedShadowBase"/> definition.
/// </summary>
/// <param name="parent">The <see cref="AttachedShadowBase"/> that is using this context.</param>
/// <param name="element">The <see cref="FrameworkElement"/> that a shadow is being attached to.</param>
internal AttachedShadowElementContext(AttachedShadowBase parent, FrameworkElement element)
{
if (_isConnected)
{
throw new InvalidOperationException("This AttachedShadowElementContext has already been connected to an element");
}
_isConnected = true;
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
Element = element ?? throw new ArgumentNullException(nameof(element));
Element.Loaded += OnElementLoaded;
Element.Unloaded += OnElementUnloaded;
Initialize();
}
internal void DisconnectFromElement()
{
if (!_isConnected)
{
return;
}
Uninitialize();
Element.Loaded -= OnElementLoaded;
Element.Unloaded -= OnElementUnloaded;
_isConnected = false;
}
/// <summary>
/// Force early creation of this instance's resources, otherwise they will be created automatically when <see cref="Element"/> is loaded.
/// </summary>
public void CreateResources() => Initialize(true);
private void Initialize(bool forceIfNotLoaded = false)
{
if (IsInitialized || !_isConnected || (!Element.IsLoaded && !forceIfNotLoaded))
{
return;
}
IsInitialized = true;
ElementVisual = ElementCompositionPreview.GetElementVisual(Element);
Compositor = ElementVisual.Compositor;
Shadow = Compositor.CreateDropShadow();
SpriteVisual = Compositor.CreateSpriteVisual();
SpriteVisual.RelativeSizeAdjustment = Vector2.One;
SpriteVisual.Shadow = Shadow;
if (Parent.SupportsOnSizeChangedEvent == true)
{
Element.SizeChanged += OnElementSizeChanged;
}
Parent.OnElementContextInitialized(this);
}
private void Uninitialize()
{
if (!IsInitialized)
{
return;
}
IsInitialized = false;
Parent.OnElementContextUninitialized(this);
if (SpriteVisual != null)
{
SpriteVisual.Shadow = null;
SpriteVisual.Dispose();
}
if (Shadow != null)
{
Shadow.Dispose();
}
if (Element != null)
{
ElementCompositionPreview.SetElementChildVisual(Element, null!);
Element.SizeChanged -= OnElementSizeChanged;
}
SpriteVisual = null;
Shadow = null;
ElementVisual = null;
}
private void OnElementUnloaded(object sender, RoutedEventArgs e)
{
Uninitialize();
}
private void OnElementLoaded(object sender, RoutedEventArgs e)
{
Initialize();
}
private void OnElementSizeChanged(object sender, SizeChangedEventArgs e)
{
Parent.OnSizeChanged(this, e.NewSize, e.PreviousSize);
}
/// <summary>
/// Adds a resource to this instance's resource dictionary with the specified key
/// </summary>
/// <typeparam name="T">The type of the resource being added.</typeparam>
/// <param name="key">Key to use to lookup the resource later.</param>
/// <param name="resource">Object to store within the resource dictionary.</param>
/// <returns>The added resource</returns>
public T AddResource<T>(string key, T resource)
where T : notnull
{
if (_resources.ContainsKey(key))
{
_resources[key] = resource;
}
else
{
_resources.Add(key, resource);
}
return resource;
}
/// <summary>
/// Retrieves a resource with the specified key and type if it exists
/// </summary>
/// <typeparam name="T">The type of the resource being retrieved.</typeparam>
/// <param name="key">Key to use to lookup the resource.</param>
/// <param name="resource">Object to retrieved from the resource dictionary or default value.</param>
/// <returns>True if the resource exists, false otherwise</returns>
public bool TryGetResource<T>(string key, out T? resource)
{
if (_resources.TryGetValue(key, out var objResource) && objResource is T tResource)
{
resource = tResource;
return true;
}
resource = default;
return false;
}
/// <summary>
/// Retries a resource with the specified key and type
/// </summary>
/// <typeparam name="T">The type of the resource being retrieved.</typeparam>
/// <param name="key">Key to use to lookup the resource.</param>
/// <returns>The resource if available, otherwise default value.</returns>
public T? GetResource<T>(string key)
{
if (TryGetResource(key, out T? resource))
{
return resource;
}
return default;
}
/// <summary>
/// Removes an existing resource with the specified key and type
/// </summary>
/// <typeparam name="T">The type of the resource being removed.</typeparam>
/// <param name="key">Key to use to lookup the resource.</param>
/// <returns>The resource that was removed, if any</returns>
public T? RemoveResource<T>(string key)
{
if (_resources.TryGetValue(key, out var objResource))
{
_resources.Remove(key);
if (objResource is T resource)
{
return resource;
}
}
return default;
}
/// <summary>
/// Removes an existing resource with the specified key and type, and <see cref="IDisposable.Dispose">disposes</see> it
/// </summary>
/// <typeparam name="T">The type of the resource being removed.</typeparam>
/// <param name="key">Key to use to lookup the resource.</param>
/// <returns>The resource that was removed, if any</returns>
public T? RemoveAndDisposeResource<T>(string key)
where T : IDisposable
{
if (_resources.TryGetValue(key, out var objResource))
{
_resources.Remove(key);
if (objResource is T resource)
{
resource.Dispose();
return resource;
}
}
return default;
}
/// <summary>
/// Adds a resource to this instance's collection with the specified key
/// </summary>
/// <typeparam name="T">The type of the resource being added.</typeparam>
/// <returns>The resource that was added</returns>
public T AddResource<T>(TypedResourceKey<T> key, T resource)
where T : notnull => AddResource(key.Key, resource);
/// <summary>
/// Retrieves a resource with the specified key and type if it exists
/// </summary>
/// <typeparam name="T">The type of the resource being retrieved.</typeparam>
/// <returns>True if the resource exists, false otherwise</returns>
public bool TryGetResource<T>(TypedResourceKey<T> key, out T? resource) => TryGetResource(key.Key, out resource);
/// <summary>
/// Retries a resource with the specified key and type
/// </summary>
/// <typeparam name="T">The type of the resource being retrieved.</typeparam>
/// <returns>The resource if it exists or a default value.</returns>
public T? GetResource<T>(TypedResourceKey<T> key) => GetResource<T>(key.Key);
/// <summary>
/// Removes an existing resource with the specified key and type
/// </summary>
/// <typeparam name="T">The type of the resource being removed.</typeparam>
/// <returns>The resource that was removed, if any</returns>
public T? RemoveResource<T>(TypedResourceKey<T> key) => RemoveResource<T>(key.Key);
/// <summary>
/// Removes an existing resource with the specified key and type, and <see cref="IDisposable.Dispose">disposes</see> it
/// </summary>
/// <typeparam name="T">The type of the resource being removed.</typeparam>
/// <returns>The resource that was removed, if any</returns>
public T? RemoveAndDisposeResource<T>(TypedResourceKey<T> key)
where T : IDisposable => RemoveAndDisposeResource<T>(key.Key);
/// <summary>
/// Disposes of any resources that implement <see cref="IDisposable"/> and then clears all resources
/// </summary>
public void ClearAndDisposeResources()
{
foreach (var kvp in _resources)
{
(kvp.Value as IDisposable)?.Dispose();
}
_resources.Clear();
}
}