-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFileButton.cs
447 lines (367 loc) · 17.2 KB
/
FileButton.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// Copyright Fela Ameghino 2015-2025
//
// 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.Geometry;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Telegram.Controls.Media;
using Telegram.Navigation;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
namespace Telegram.Controls
{
public enum MessageContentState
{
None,
Download,
Downloading,
Uploading,
Confirm,
Document,
Photo,
Animation,
Ttl,
Unlock,
Play,
Pause,
Theme,
}
public partial class FileButton : GlyphHyperlinkButton
{
private Grid RootGrid;
private ProgressBarRing ProgressBar;
private TextBlock ContentPresenter1;
private TextBlock ContentPresenter2;
private TextBlock _label;
private bool _hasContainer;
private long _fileId;
private MessageContentState _state;
private double _enqueuedProgress;
private bool _shouldEnqueueProgress;
public FileButton()
{
DefaultStyleKey = typeof(FileButton);
Connected += OnConnected;
}
private void OnConnected(object sender, RoutedEventArgs e)
{
// Used to enable FrameworkElementEx
}
public MessageContentState State => _state;
public bool IsSmall { get; set; }
protected override AutomationPeer OnCreateAutomationPeer()
{
return new FileButtonAutomationPeer(this);
}
protected override void OnApplyTemplate()
{
RootGrid = GetTemplateChild(nameof(RootGrid)) as Grid;
ProgressBar = GetTemplateChild(nameof(ProgressBar)) as ProgressBarRing;
ContentPresenter1 = GetTemplateChild(nameof(ContentPresenter1)) as TextBlock;
ContentPresenter2 = GetTemplateChild(nameof(ContentPresenter2)) as TextBlock;
ContentPresenter1.Text = Glyph ?? string.Empty;
ContentPresenter2.Text = string.Empty;
_label = ContentPresenter1;
if (ProgressBar != null)
{
ProgressBar.Value = _progress;
}
}
#region ProgressVisibility
public Visibility ProgressVisibility
{
get => (Visibility)GetValue(ProgressVisibilityProperty);
set => SetValue(ProgressVisibilityProperty, value);
}
public static readonly DependencyProperty ProgressVisibilityProperty =
DependencyProperty.Register("ProgressVisibility", typeof(Visibility), typeof(FileButton), new PropertyMetadata(Visibility.Visible));
#endregion
private double _progress;
public double Progress
{
get => _progress;
set
{
if (_shouldEnqueueProgress || ProgressBar == null || !IsConnected)
{
_enqueuedProgress = value;
}
else if (_state is MessageContentState.Downloading or MessageContentState.Uploading)
{
ProgressBar.Value = Math.Max(0.05, value);
}
else
{
ProgressBar.Value = value;
}
if (AutomationPeer.ListenerExists(AutomationEvents.PropertyChanged))
{
var peer = FrameworkElementAutomationPeer.FromElement(this);
peer?.RaisePropertyChangedEvent(ValuePatternIdentifiers.ValueProperty, _progress, value);
}
_progress = value;
}
}
public void SetGlyph(int fileId, MessageContentState state)
{
if (fileId != _fileId)
{
_state = MessageContentState.None;
}
switch (state)
{
case MessageContentState.Download:
OnGlyphChanged(IsSmall ? Icons.DownloadSmall : Icons.ArrowDownloadFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionDownload);
break;
case MessageContentState.Downloading:
if (IsSmall || (_state != MessageContentState.Download && _state != MessageContentState.Downloading))
{
OnGlyphChanged(IsSmall ? Icons.CancelSmall : Icons.DismissFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionCancelDownload, false);
}
else if (_state != MessageContentState.Downloading)
{
SetDownloadGlyph(_state != MessageContentState.Download);
}
break;
case MessageContentState.Uploading:
OnGlyphChanged(IsSmall ? Icons.CancelSmall : Icons.DismissFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionCancelDownload);
break;
case MessageContentState.Confirm:
OnGlyphChanged(Icons.CheckmarkFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionCancelDownload);
break;
case MessageContentState.Document:
OnGlyphChanged(Icons.DocumentFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionOpenFile);
break;
case MessageContentState.Animation:
OnGlyphChanged(Icons.Animation, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionPlay);
break;
case MessageContentState.Photo:
OnGlyphChanged(string.Empty, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionOpenFile);
break;
case MessageContentState.Play:
OnGlyphChanged(Icons.PlayFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionPlay);
break;
case MessageContentState.Pause:
OnGlyphChanged(Icons.PauseFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionPause);
break;
case MessageContentState.Ttl:
OnGlyphChanged(Icons.TtlFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionOpenFile);
break;
case MessageContentState.Unlock:
OnGlyphChanged(Icons.LockClosedFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionOpenFile);
break;
case MessageContentState.Theme:
OnGlyphChanged(Icons.ColorFilled24, Glyph, _state != state && _state != MessageContentState.None, Strings.AccActionOpenFile);
break;
}
_fileId = fileId;
_state = state;
}
private void OnGlyphChanged(string newValue, string oldValue, bool animate, string automation, bool clearContainer = true)
{
if (string.Equals(newValue, oldValue, StringComparison.OrdinalIgnoreCase))
{
return;
}
Glyph = newValue;
AutomationProperties.SetName(this, automation);
if (_label == null)
{
return;
}
if (animate)
{
var labelShow = _label == ContentPresenter1 ? ContentPresenter2 : ContentPresenter1;
var labelHide = _label == ContentPresenter1 ? ContentPresenter1 : ContentPresenter2;
var visualShow = ElementComposition.GetElementVisual(labelShow);
var visualHide = ElementComposition.GetElementVisual(labelHide);
var compositor = visualShow.Compositor;
visualShow.CenterPoint = new Vector3(10);
visualHide.CenterPoint = new Vector3(10);
var hide1 = compositor.CreateVector3KeyFrameAnimation();
hide1.InsertKeyFrame(0, Vector3.One);
hide1.InsertKeyFrame(1, Vector3.Zero);
var hide2 = compositor.CreateScalarKeyFrameAnimation();
hide2.InsertKeyFrame(0, 1);
hide2.InsertKeyFrame(1, 0);
visualHide.StartAnimation("Scale", hide1);
visualHide.StartAnimation("Opacity", hide2);
var show1 = compositor.CreateVector3KeyFrameAnimation();
show1.InsertKeyFrame(1, Vector3.One);
show1.InsertKeyFrame(0, Vector3.Zero);
var show2 = compositor.CreateScalarKeyFrameAnimation();
show2.InsertKeyFrame(1, 1);
show2.InsertKeyFrame(0, 0);
visualShow.StartAnimation("Scale", show1);
visualShow.StartAnimation("Opacity", show2);
_label = labelShow;
}
_label.Text = newValue;
if (_hasContainer && (clearContainer || !animate) && IsConnected)
{
_hasContainer = false;
ElementComposition.SetElementChildVisual(RootGrid, null);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetDownloadGlyph(bool animate)
{
try
{
SetDownloadGlyphImpl(animate);
}
catch
{
// Compositor.CreateSpriteShape can throw InvalidCastException
OnGlyphChanged(Icons.DismissFilled24, Glyph, animate, Strings.AccActionCancelDownload, false);
}
}
private void SetDownloadGlyphImpl(bool animate)
{
if (RootGrid == null)
{
return;
}
OnGlyphChanged(string.Empty, Glyph, animate, Strings.AccActionCancelDownload, false);
var compositor = BootStrapper.Current.Compositor;
var diameter = 48f; // min(bounds.size.width, bounds.size.height)
var factor = diameter / 48f;
var lineWidth = 2; //MathF.Max(1.6f, 2.25f * factor);
var arrowHeadSize = 15.0f * factor;
var arrowLength = 18.0f * factor;
var arrowHeadOffset = 1.0f * factor;
var leftLine = compositor.CreateLineGeometry();
leftLine.Start = new Vector2(x: diameter / 2.0f, y: diameter / 2.0f + arrowLength / 2.0f + arrowHeadOffset);
leftLine.End = new Vector2(x: diameter / 2.0f - arrowHeadSize / 2.0f, y: diameter / 2.0f + arrowLength / 2.0f - arrowHeadSize / 2.0f + arrowHeadOffset);
var rightLine = compositor.CreateLineGeometry();
rightLine.Start = new Vector2(x: diameter / 2.0f, y: diameter / 2.0f + arrowLength / 2.0f + arrowHeadOffset);
rightLine.End = new Vector2(x: diameter / 2.0f + arrowHeadSize / 2.0f, y: diameter / 2.0f + arrowLength / 2.0f - arrowHeadSize / 2.0f + arrowHeadOffset);
var leftShape = compositor.CreateSpriteShape(leftLine);
leftShape.StrokeThickness = lineWidth;
leftShape.StrokeBrush = compositor.CreateColorBrush(Windows.UI.Colors.White);
leftShape.StrokeStartCap = CompositionStrokeCap.Round;
leftShape.StrokeEndCap = CompositionStrokeCap.Round;
var rightShape = compositor.CreateSpriteShape(rightLine);
rightShape.StrokeThickness = lineWidth;
rightShape.StrokeBrush = compositor.CreateColorBrush(Windows.UI.Colors.White);
rightShape.StrokeStartCap = CompositionStrokeCap.Round;
rightShape.StrokeEndCap = CompositionStrokeCap.Round;
var arrowPath = compositor.CreatePathGeometry();
//arrowPath.Path = svgPath("M4.2,31.1 C4.7,32.5 5.4,33.8 6.2,35 C12.1,44 24,44.7 24,33.3 L24,16 ", Vector2.One, Vector2.Zero);
arrowPath.Path = GetArrowShape();
arrowPath.TrimStart = 0.65f;
var arrowShape = compositor.CreateSpriteShape(arrowPath);
arrowShape.StrokeThickness = lineWidth;
arrowShape.StrokeBrush = compositor.CreateColorBrush(Windows.UI.Colors.White);
arrowShape.StrokeStartCap = CompositionStrokeCap.Round;
arrowShape.StrokeEndCap = CompositionStrokeCap.Round;
arrowShape.StrokeLineJoin = CompositionStrokeLineJoin.Round;
var visual1 = compositor.CreateShapeVisual();
visual1.Shapes.Add(leftShape);
visual1.Shapes.Add(rightShape);
visual1.Size = new Vector2(48, 48);
var visual3 = compositor.CreateShapeVisual();
visual3.Shapes.Add(arrowShape);
visual3.Size = new Vector2(48, 48);
var container = BootStrapper.Current.Compositor.CreateContainerVisual();
container.Children.InsertAtTop(visual1);
container.Children.InsertAtTop(visual3);
container.Size = new Vector2(48, 48);
_hasContainer = true;
ElementCompositionPreview.SetElementChildVisual(RootGrid, container);
_shouldEnqueueProgress = true;
var batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
batch.Completed += OnDownloadingCompleted;
var animLeft = compositor.CreateScalarKeyFrameAnimation();
animLeft.InsertKeyFrame(0, 1);
animLeft.InsertKeyFrame(1, 0);
animLeft.Duration = TimeSpan.FromMilliseconds(200);
leftLine.StartAnimation("TrimEnd", animLeft);
rightLine.StartAnimation("TrimEnd", animLeft);
var opacityLeft = compositor.CreateScalarKeyFrameAnimation();
opacityLeft.InsertKeyFrame(0, 1);
opacityLeft.InsertKeyFrame(1, 0);
opacityLeft.Duration = TimeSpan.FromSeconds(0.10);
opacityLeft.DelayTime = TimeSpan.FromSeconds(0.06);
visual1.StartAnimation("Opacity", opacityLeft);
batch.End();
var animBody = compositor.CreateScalarKeyFrameAnimation();
animBody.InsertKeyFrame(0, 0.65f);
animBody.InsertKeyFrame(1, 0, compositor.CreateLinearEasingFunction());
animBody.Duration = TimeSpan.FromMilliseconds(400);
var animBodyEnd = compositor.CreateScalarKeyFrameAnimation();
animBodyEnd.InsertKeyFrame(0, 1);
animBodyEnd.InsertKeyFrame(1, 0, compositor.CreateLinearEasingFunction());
animBodyEnd.Duration = TimeSpan.FromMilliseconds(400);
arrowPath.StartAnimation("TrimStart", animBody);
arrowPath.StartAnimation("TrimEnd", animBodyEnd);
}
private void OnDownloadingCompleted(object sender, CompositionBatchCompletedEventArgs args)
{
try
{
if (_state == MessageContentState.Downloading && ProgressBar != null && IsConnected)
{
OnGlyphChanged(Icons.Cancel, Icons.ArrowDownload, true, Strings.AccActionCancelDownload, false);
ProgressBar.Value = _enqueuedProgress;
}
_shouldEnqueueProgress = false;
//_container.Children.RemoveAll();
}
catch
{
// May throw MissingInteropDataException, kind of unexplicable as no reflection is involved.
}
}
private CompositionPath GetArrowShape()
{
CanvasGeometry result;
using (var builder = new CanvasPathBuilder(null))
{
builder.BeginFigure(4.2f, 31.1f);
builder.AddCubicBezier(new Vector2(4.7f, 32.5f), new Vector2(5.4f, 33.8f), new Vector2(6.2f, 35f));
builder.AddCubicBezier(new Vector2(12.1f, 44f), new Vector2(24f, 44.7f), new Vector2(24f, 33.3f));
builder.AddLine(24f, 16f);
builder.EndFigure(CanvasFigureLoop.Open);
result = CanvasGeometry.CreatePath(builder);
}
return new CompositionPath(result);
}
}
public partial class FileButtonAutomationPeer : HyperlinkButtonAutomationPeer, IValueProvider
{
private readonly FileButton _owner;
public FileButtonAutomationPeer(FileButton owner)
: base(owner)
{
_owner = owner;
}
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Value)
{
return this;
}
return base.GetPatternCore(patternInterface);
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Button;
}
public void SetValue(string value)
{
// Not implemented
}
public bool IsReadOnly => true;
public string Value => _owner.Progress.ToString("P0");
}
}