-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathExtensions.cs
More file actions
346 lines (302 loc) · 12.8 KB
/
Copy pathExtensions.cs
File metadata and controls
346 lines (302 loc) · 12.8 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Geometry;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using Telegram.Common;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
#if NET9_0_OR_GREATER
using WinRT;
#endif
namespace Windows.UI.Xaml.Hosting
{
public static class ElementComposition
{
public static Visual GetElementVisual(UIElement element)
{
if (element == null)
{
Telegram.Logger.Exception(new ArgumentNullException(nameof(element), Environment.StackTrace));
}
return ElementCompositionPreview.GetElementVisual(element);
}
public static void SetElementChildVisual(UIElement element, Visual visual)
{
try
{
ElementCompositionPreview.SetElementChildVisual(element, visual);
}
catch
{
// TODO
}
}
public static CanvasDevice GetSharedDevice()
{
try
{
// This often throws the following exception:
// The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action. (Exception from HRESULT: 0x887A0005)
// TODO: it's unclear when and why this happens, nor if this is a good solution to the problem.
return CanvasDevice.GetSharedDevice();
}
catch
{
return CanvasDevice.GetSharedDevice(true);
}
}
}
}
namespace Telegram.Composition
{
public static class CompositionExtensions
{
/// <summary>
/// Resolves the private <see cref="ICompositionVisualSurfacePartner"/> on a visual surface,
/// or returns false where the interface does not exist.
/// </summary>
/// <remarks>
/// Older Windows builds do not have this interface, and on CsWinRT the obvious spellings
/// both go wrong: <c>As<T></c> throws on a failed QI rather than returning null, so a
/// null check after it is dead code, and <c>is</c> never matches at all because the
/// interface is a GeneratedComInterface rather than a projected one. Catching the throw is
/// not enough either - WatchDog reports InvalidCastException at first chance, so a handled
/// miss still files a crash report. Probe with a raw QueryInterface instead; once that has
/// succeeded <c>As<T></c> cannot throw.
/// </remarks>
public static bool TryGetPartner(this CompositionVisualSurface surface, out ICompositionVisualSurfacePartner partner)
{
#if NET9_0_OR_GREATER
// NativeObject is the RCW's own reference and is not ours to dispose. The pointer TryAs
// hands back on success is, and it is only wanted as proof that the QI would work.
if (surface is IWinRTObject native
&& native.NativeObject.TryAs(typeof(ICompositionVisualSurfacePartner).GUID, out var abi) >= 0)
{
Marshal.Release(abi);
partner = surface.As<ICompositionVisualSurfacePartner>();
return true;
}
partner = null;
return false;
#else
var temp = surface as object;
partner = temp as ICompositionVisualSurfacePartner;
return partner != null;
#endif
}
// The interface is declared as properties on .NET Native and as accessor methods under
// CsWinRT, where a GeneratedComInterface cannot carry properties. These keep that split out
// of the call sites.
public static void SetRealizationSize(this ICompositionVisualSurfacePartner partner, Vector2 value)
{
#if NET9_0_OR_GREATER
partner.set_RealizationSize(value);
#else
partner.RealizationSize = value;
#endif
}
public static void SetStretch(this ICompositionVisualSurfacePartner partner, CompositionStretch value)
{
#if NET9_0_OR_GREATER
partner.set_Stretch(value);
#else
partner.Stretch = value;
#endif
}
public static CompositionBrush CreateRedirectBrush(this Compositor compositor, UIElement source, Vector2 sourceOffset, Vector2 sourceSize, bool freeze = false)
{
// Create a VisualSurface positioned at the same location as this control and feed that
// through the color effect.
var surfaceBrush = compositor.CreateSurfaceBrush();
var surface = compositor.CreateVisualSurface();
// Select the source visual and the offset/size of this control in that element's space.
surface.SourceVisual = ElementComposition.GetElementVisual(source);
surface.SourceOffset = sourceOffset;
surface.SourceSize = sourceSize;
surfaceBrush.Surface = surface;
surfaceBrush.Stretch = CompositionStretch.Fill;
if (freeze && surface.TryGetPartner(out var partner))
{
partner.SetStretch(CompositionStretch.Fill);
partner.SetRealizationSize(sourceSize * (float)source.XamlRoot.RasterizationScale);
partner.Freeze();
}
return surfaceBrush;
}
public static CompositionBrush CreateRedirectBrush(this Compositor compositor, Visual source, Vector2 sourceOffset, Vector2 sourceSize)
{
// Create a VisualSurface positioned at the same location as this control and feed that
// through the color effect.
var surfaceBrush = compositor.CreateSurfaceBrush();
var surface = compositor.CreateVisualSurface();
// Select the source visual and the offset/size of this control in that element's space.
surface.SourceVisual = source;
surface.SourceOffset = sourceOffset;
surface.SourceSize = sourceSize;
surfaceBrush.Surface = surface;
surfaceBrush.Stretch = CompositionStretch.Fill;
return surfaceBrush;
}
public static SpriteVisual CreateRedirectVisual(this Compositor compositor, UIElement source, Vector2 sourceOffset, Vector2 sourceSize, bool freeze = false)
{
var redirect = compositor.CreateSpriteVisual();
redirect.Brush = compositor.CreateRedirectBrush(source, sourceOffset, sourceSize, freeze);
redirect.Size = sourceSize;
return redirect;
}
public static CompositionPath CreateSmoothCurve(this Compositor compositor, Vector2[] points, float smoothness)
{
var smoothPoints = new List<SmoothPoint>();
for (int index = 0; index < points.Length; index++)
{
var prevIndex = index - 1;
var prev = points[prevIndex >= 0 ? prevIndex : points.Length + prevIndex];
var curr = points[index];
var next = points[(index + 1) % points.Length];
var dx = next.X - prev.X;
var dy = -next.Y + prev.Y;
var angle = MathF.Atan2(dy, dx);
if (angle < 0)
{
angle = MathF.Abs(angle);
}
else
{
angle = 2 * MathF.PI - angle;
}
smoothPoints.Add(
new SmoothPoint(
point: curr,
inAngle: angle + MathF.PI,
inLength: smoothness * Distance(curr, prev),
outAngle: angle,
outLength: smoothness * Distance(curr, next)
)
);
}
CanvasGeometry result;
using (var builder = new CanvasPathBuilder(null))
{
builder.BeginFigure(smoothPoints[0].Point);
for (int i = 0; i < smoothPoints.Count; i++)
{
var prev = smoothPoints[i >= 0 ? i : smoothPoints.Count + i];
var curr = smoothPoints[i];
var next = smoothPoints[(i + 1) % points.Length];
var currSmoothOut = curr.SmoothOut(0.5f);
var nextSmoothIn = next.SmoothIn(2.0f);
builder.AddCubicBezier(currSmoothOut, nextSmoothIn, next.Point);
}
builder.EndFigure(CanvasFigureLoop.Closed);
result = CanvasGeometry.CreatePath(builder);
}
return new CompositionPath(result);
}
public static CompositionPath CreateSmoothCurve(this Compositor compositor, Vector2[] points, float length, float smoothness, bool curve = false)
{
var smoothPoints = new List<SmoothPoint>();
for (int index = 0; index < points.Length; index++)
{
var prevIndex = index - 1;
var prev = points[prevIndex >= 0 ? prevIndex : points.Length + prevIndex];
var curr = points[index];
var next = points[(index + 1) % points.Length];
var dx = next.X - prev.X;
var dy = -next.Y + prev.Y;
var angle = MathF.Atan2(dy, dx);
if (angle < 0)
{
angle = MathF.Abs(angle);
}
else
{
angle = 2 * MathF.PI - angle;
}
smoothPoints.Add(
new SmoothPoint(
point: curr,
inAngle: angle + MathF.PI,
inLength: smoothness * Distance(curr, prev),
outAngle: angle,
outLength: smoothness * Distance(curr, next)
)
);
}
CanvasGeometry result;
using (var builder = new CanvasPathBuilder(null))
{
if (curve)
{
builder.BeginFigure(0, 0);
builder.AddLine(smoothPoints[0].Point);
}
else
{
builder.BeginFigure(smoothPoints[0].Point);
}
var smoothCount = curve ? smoothPoints.Count - 1 : smoothPoints.Count;
for (int index = 0; index < smoothCount; index++)
{
var curr = smoothPoints[index];
var next = smoothPoints[(index + 1) % points.Length];
var currSmoothOut = curr.SmoothOut();
var nextSmoothIn = next.SmoothIn();
builder.AddCubicBezier(currSmoothOut, nextSmoothIn, next.Point);
}
if (curve)
{
builder.AddLine(length, 0);
}
builder.EndFigure(CanvasFigureLoop.Closed);
result = CanvasGeometry.CreatePath(builder);
}
return new CompositionPath(result);
}
private static float Distance(Vector2 fromPoint, Vector2 toPoint)
{
return MathF.Sqrt((fromPoint.X - toPoint.X) * (fromPoint.X - toPoint.X) + (fromPoint.Y - toPoint.Y) * (fromPoint.Y - toPoint.Y));
}
private readonly struct SmoothPoint
{
public readonly Vector2 Point;
private readonly float _inAngle;
private readonly float _inLength;
private readonly float _outAngle;
private readonly float _outLength;
public SmoothPoint(Vector2 point, float inAngle, float inLength, float outAngle, float outLength)
{
Point = point;
_inAngle = inAngle;
_inLength = inLength;
_outAngle = outAngle;
_outLength = outLength;
}
public readonly Vector2 SmoothIn(float multiplier = 1)
{
// TODO: * 2.0f is arbitrary
return Smooth(_inAngle, _inLength * multiplier);
}
public readonly Vector2 SmoothOut(float multiplier = 1)
{
// TODO: * 0.5f is arbistrary
return Smooth(_outAngle, _outLength * multiplier);
}
private readonly Vector2 Smooth(float angle, float length)
{
return new Vector2(
Point.X + length * MathF.Cos(angle),
Point.Y + length * MathF.Sin(angle)
);
}
}
}
}