-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLoadingTextBlock.cs
296 lines (228 loc) · 11.2 KB
/
LoadingTextBlock.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
//
// 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.Collections.Generic;
using System.Numerics;
using Telegram.Common;
using Telegram.Native;
using Telegram.Navigation;
using Telegram.Td.Api;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media;
namespace Telegram.Controls
{
public partial class LoadingTextBlock : Control
{
private ContainerVisual _skeleton;
private SpriteVisual _foreground;
private TextBlock _placeholder;
private TextBlock _presenter;
public LoadingTextBlock()
{
DefaultStyleKey = typeof(LoadingTextBlock);
}
protected override void OnApplyTemplate()
{
var ease = BootStrapper.Current.Compositor.CreateLinearEasingFunction();
var animation = BootStrapper.Current.Compositor.CreateVector3KeyFrameAnimation();
animation.InsertKeyFrame(0, new Vector3(-1, 0, 0), ease);
animation.InsertKeyFrame(1, new Vector3(0, 0, 0), ease);
animation.IterationBehavior = AnimationIterationBehavior.Forever;
animation.Duration = TimeSpan.FromSeconds(1);
var backgroundColor = GetColor(BorderBrushProperty);
var foregroundColor = GetColor(BackgroundProperty);
var gradient = BootStrapper.Current.Compositor.CreateLinearGradientBrush();
gradient.ColorStops.Add(BootStrapper.Current.Compositor.CreateColorGradientStop(0, Color.FromArgb(0x00, backgroundColor.R, backgroundColor.G, backgroundColor.B)));
gradient.ColorStops.Add(BootStrapper.Current.Compositor.CreateColorGradientStop(0.67f, Color.FromArgb(0x67, backgroundColor.R, backgroundColor.G, backgroundColor.B)));
gradient.ColorStops.Add(BootStrapper.Current.Compositor.CreateColorGradientStop(1, Color.FromArgb(0x00, backgroundColor.R, backgroundColor.G, backgroundColor.B)));
gradient.StartPoint = new Vector2(0, 0);
gradient.EndPoint = new Vector2(0.5f, 0);
gradient.ExtendMode = CompositionGradientExtendMode.Wrap;
var background = BootStrapper.Current.Compositor.CreateSpriteVisual();
background.RelativeSizeAdjustment = Vector2.One;
background.Brush = BootStrapper.Current.Compositor.CreateColorBrush(foregroundColor);
_foreground = BootStrapper.Current.Compositor.CreateSpriteVisual();
_foreground.RelativeSizeAdjustment = new Vector2(2, 1);
_foreground.Brush = gradient;
_foreground.StartAnimation("RelativeOffsetAdjustment", animation);
_placeholder = GetTemplateChild("Placeholder") as TextBlock;
_presenter = GetTemplateChild("Presenter") as TextBlock;
_skeleton = BootStrapper.Current.Compositor.CreateContainerVisual();
_skeleton.Children.InsertAtTop(background);
_skeleton.Children.InsertAtTop(_foreground);
_skeleton.Opacity = 0.67f;
_skeleton.AnchorPoint = new Vector2(IsPlaceholderRightToLeft ? 1 : 0, 0);
_skeleton.RelativeOffsetAdjustment = new Vector3(IsPlaceholderRightToLeft ? 1 : 0, 0, 0);
ElementCompositionPreview.SetElementChildVisual(_placeholder, _skeleton);
base.OnApplyTemplate();
}
private Color GetColor(DependencyProperty dp)
{
var value = GetValue(dp);
if (value is SolidColorBrush solid)
{
return solid.Color;
}
return Colors.Black;
}
private CompositionBrush GetBrush(DependencyProperty dp)
{
return BootStrapper.Current.Compositor.CreateColorBrush(GetColor(dp));
}
#region PlaceholderText
public string PlaceholderText
{
get { return (string)GetValue(PlaceholderTextProperty); }
set { SetValue(PlaceholderTextProperty, value); }
}
public static readonly DependencyProperty PlaceholderTextProperty =
DependencyProperty.Register("PlaceholderText", typeof(string), typeof(LoadingTextBlock), new PropertyMetadata(null));
#endregion
#region PlaceholderBrush
public Brush PlaceholderBrush
{
get { return (Brush)GetValue(PlaceholderBrushProperty); }
set { SetValue(PlaceholderBrushProperty, value); }
}
public static readonly DependencyProperty PlaceholderBrushProperty =
DependencyProperty.Register("PlaceholderBrush", typeof(Brush), typeof(LoadingTextBlock), new PropertyMetadata(null));
#endregion
#region Text
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(LoadingTextBlock), new PropertyMetadata(null, OnTextChanged));
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((LoadingTextBlock)d).OnTextChanged((string)e.NewValue, ((LoadingTextBlock)d).PlaceholderText);
}
private async void OnTextChanged(string text, string placeholder)
{
if (string.IsNullOrEmpty(text))
{
return;
}
InvalidateMeasure();
await this.UpdateLayoutAsync();
var batch = BootStrapper.Current.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
batch.Completed += (s, args) =>
{
_placeholder.Visibility = Visibility.Collapsed;
};
var fadeIn = BootStrapper.Current.Compositor.CreateScalarKeyFrameAnimation();
fadeIn.InsertKeyFrame(0, 0);
fadeIn.InsertKeyFrame(1, 1);
var visual2 = ElementComposition.GetElementVisual(_placeholder);
var visual1 = ElementComposition.GetElementVisual(_presenter);
visual1.StartAnimation("Opacity", fadeIn);
var size1 = _presenter.ActualSize;
var size2 = _placeholder.ActualSize;
var final = new Vector2(MathF.Max(size1.X, size2.X), MathF.Max(size1.Y, size2.Y));
StartClip(visual1, true, final);
StartClip(visual2, false, final);
batch.End();
}
private void StartClip(Visual visual, bool show, Vector2 desiredSize)
{
var actualWidth = desiredSize.X;
var actualHeight = desiredSize.Y;
var left = (float)Padding.Left;
var top = (float)Padding.Top;
var width = MathF.Max(actualWidth - left, actualHeight - top);
var diaginal = MathF.Sqrt((width * width) + (width * width));
var device = ElementComposition.GetSharedDevice();
var rect1 = CanvasGeometry.CreateRectangle(device, 0, 0, show ? 0 : actualWidth, show ? 0 : actualHeight);
var elli1 = CanvasGeometry.CreateCircle(device, left, top, 0);
var group1 = CanvasGeometry.CreateGroup(device, new[] { elli1, rect1 }, CanvasFilledRegionDetermination.Alternate);
var elli2 = CanvasGeometry.CreateCircle(device, left, top, diaginal);
var group2 = CanvasGeometry.CreateGroup(device, new[] { elli2, rect1 }, CanvasFilledRegionDetermination.Alternate);
var ellipse = BootStrapper.Current.Compositor.CreatePathGeometry(new CompositionPath(group2));
var clip = BootStrapper.Current.Compositor.CreateGeometricClip(ellipse);
var ease = BootStrapper.Current.Compositor.CreateCubicBezierEasingFunction(new Vector2(.42f, 0), new Vector2(1, 1));
var anim = BootStrapper.Current.Compositor.CreatePathKeyFrameAnimation();
anim.InsertKeyFrame(0, new CompositionPath(group1), ease);
anim.InsertKeyFrame(1, new CompositionPath(group2), ease);
anim.Duration = TimeSpan.FromMilliseconds(500);
ellipse.StartAnimation("Path", anim);
visual.Clip = clip;
}
#endregion
#region IsTextSelectionEnabled
public bool IsTextSelectionEnabled
{
get { return (bool)GetValue(IsTextSelectionEnabledProperty); }
set { SetValue(IsTextSelectionEnabledProperty, value); }
}
public static readonly DependencyProperty IsTextSelectionEnabledProperty =
DependencyProperty.Register("IsTextSelectionEnabled", typeof(bool), typeof(LoadingTextBlock), new PropertyMetadata(false));
#endregion
private bool _isPlaceholderRightToLeft;
public bool IsPlaceholderRightToLeft
{
get => _isPlaceholderRightToLeft;
set
{
if (_skeleton != null)
{
_skeleton.AnchorPoint = new Vector2(value ? 1 : 0, 0);
_skeleton.RelativeOffsetAdjustment = new Vector3(value ? 1 : 0, 0, 0);
}
_isPlaceholderRightToLeft = value;
}
}
protected override Size MeasureOverride(Size availableSize)
{
availableSize = base.MeasureOverride(availableSize);
if (HorizontalAlignment != HorizontalAlignment.Stretch)
{
if (string.IsNullOrEmpty(Text))
{
return _placeholder.DesiredSize;
}
return _presenter.DesiredSize;
}
if (string.IsNullOrEmpty(Text))
{
return availableSize;
}
return new Size(availableSize.Width, _presenter.DesiredSize.Height);
}
protected override Size ArrangeOverride(Size finalSize)
{
finalSize = base.ArrangeOverride(finalSize);
if (_placeholder.DesiredSize.Width == 0)
{
return finalSize;
}
var device = ElementComposition.GetSharedDevice();
var list = new List<CanvasGeometry>();
var left = (float)Padding.Left;
var top = (float)Padding.Top;
var rects = PlaceholderImageHelper.Current.LineMetrics(PlaceholderText ?? string.Empty, Array.Empty<TextEntity>(), _placeholder.FontSize, _placeholder.DesiredSize.Width - Padding.Left - Padding.Right, IsPlaceholderRightToLeft);
foreach (var rect in rects)
{
if (rect.Width < 1 || rect.Height < 1)
{
continue;
}
list.Add(CanvasGeometry.CreateRoundedRectangle(device, new Rect(left + rect.X - 4, top + rect.Y - 2, rect.Width + 6, rect.Height + 6), 4, 4));
}
_skeleton.Clip = BootStrapper.Current.Compositor.CreateGeometricClip(BootStrapper.Current.Compositor.CreatePathGeometry(new CompositionPath(CanvasGeometry.CreateGroup(device, list.ToArray(), CanvasFilledRegionDetermination.Winding))));
_skeleton.Size = _placeholder.DesiredSize.ToVector2();
return finalSize;
}
}
}