-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathVisualUtilities.cs
260 lines (213 loc) · 8 KB
/
VisualUtilities.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
//
// 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 System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Telegram.Navigation;
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;
using Windows.UI.Xaml.Shapes;
namespace Telegram.Common
{
public partial class VisualUtilities
{
public static SpriteVisual DropShadow(UIElement element, float radius = 20, float opacity = 0.25f, UIElement target = null)
{
var compositor = BootStrapper.Current.Compositor;
var shadow = compositor.CreateDropShadow();
shadow.BlurRadius = radius;
shadow.Opacity = opacity;
shadow.Color = Colors.Black;
var visual = compositor.CreateSpriteVisual();
visual.Shadow = shadow;
visual.Size = new Vector2(0, 0);
visual.Offset = new Vector3(0, 0, 0);
visual.RelativeSizeAdjustment = Vector2.One;
switch (element)
{
case Image image:
shadow.Mask = image.GetAlphaMask();
break;
case Shape shape:
shadow.Mask = shape.GetAlphaMask();
break;
case TextBlock textBlock:
shadow.Mask = textBlock.GetAlphaMask();
break;
}
ElementCompositionPreview.SetElementChildVisual(target ?? element, visual);
return visual;
}
public static void ShakeView(FrameworkElement view, float x = 2)
{
// We use first child inside the control (usually a Grid)
// so we don't have to worry about absolute offset
var inner = VisualTreeHelper.GetChild(view, 0) as FrameworkElement;
if (inner == null)
{
return;
}
var visual = ElementComposition.GetElementVisual(inner);
var animation = visual.Compositor.CreateScalarKeyFrameAnimation();
animation.Duration = TimeSpan.FromMilliseconds(50 * 6);
for (int i = 1; i < 6; i++)
{
x = -x;
animation.InsertKeyFrame(i * (1f / 5f), i == 5 ? 0 : x);
}
animation.InsertKeyFrame(0, 0);
animation.InsertKeyFrame(1, 0);
visual.StartAnimation("Offset.X", animation);
}
#region IsVisible
public static bool GetIsVisible(DependencyObject obj)
{
return (bool)obj.GetValue(IsVisibleProperty);
}
public static void SetIsVisible(DependencyObject obj, bool value)
{
obj.SetValue(IsVisibleProperty, value);
}
public static readonly DependencyProperty IsVisibleProperty =
DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(UIElement), new PropertyMetadata(true, OnVisibleChanged));
private static void OnVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as UIElement;
var newValue = (bool)e.NewValue;
var oldValue = (bool)e.OldValue;
if (newValue == oldValue || (sender.Visibility == Visibility.Collapsed && !newValue))
{
return;
}
var scale = GetIsScaleEnabled(d);
var visual = ElementComposition.GetElementVisual(sender);
sender.Visibility = Visibility.Visible;
var compositor = visual.Compositor;
var batch = compositor.CreateScopedBatch(Windows.UI.Composition.CompositionBatchTypes.Animation);
batch.Completed += (s, args) =>
{
visual.Opacity = newValue ? 1 : 0;
visual.Scale = new Vector3(scale ? newValue ? 1 : 0 : 1);
sender.Visibility = newValue ? Visibility.Visible : Visibility.Collapsed;
};
var anim1 = compositor.CreateScalarKeyFrameAnimation();
anim1.InsertKeyFrame(0, newValue ? 0 : 1);
anim1.InsertKeyFrame(1, newValue ? 1 : 0);
visual.StartAnimation("Opacity", anim1);
if (scale)
{
var anim2 = compositor.CreateVector3KeyFrameAnimation();
anim2.InsertKeyFrame(0, new Vector3(newValue ? 0 : 1));
anim2.InsertKeyFrame(1, new Vector3(newValue ? 1 : 0));
visual.StartAnimation("Scale", anim2);
}
batch.End();
}
#endregion
#region IsScaleEnabled
public static bool GetIsScaleEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsScaleEnabledProperty);
}
public static void SetIsScaleEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsScaleEnabledProperty, value);
}
public static readonly DependencyProperty IsScaleEnabledProperty =
DependencyProperty.RegisterAttached("IsScaleEnabled", typeof(bool), typeof(UIElement), new PropertyMetadata(true));
#endregion
static class DelegateKeeper
{
private static ConditionalWeakTable<object, List<Delegate>> cwt = new();
public static void KeepAlive(Delegate d) => cwt.GetOrCreateValue(d?.Target ?? throw new ArgumentNullException(nameof(d))).Add(d);
}
public static void QueueCallbackForCompositionRendering(Action callback)
{
DelegateKeeper.KeepAlive(callback);
var weak = new WeakReference(callback);
void handler(object sender, object e)
{
Windows.UI.Xaml.Media.CompositionTarget.Rendering -= handler;
if (weak.Target is Action callback)
{
callback();
}
}
try
{
Windows.UI.Xaml.Media.CompositionTarget.Rendering += handler;
}
catch
{
// Bla bla
}
}
public static Task WaitForCompositionRenderingAsync()
{
var tsc = new TaskCompletionSource<bool>();
void handler(object sender, object e)
{
Windows.UI.Xaml.Media.CompositionTarget.Rendering -= handler;
tsc.SetResult(true);
}
try
{
Windows.UI.Xaml.Media.CompositionTarget.Rendering += handler;
}
catch
{
// Bla bla
}
return tsc.Task;
}
public static void QueueCallbackForCompositionRendered(Action callback)
{
DelegateKeeper.KeepAlive(callback);
var weak = new WeakReference(callback);
void handler(object sender, object e)
{
Windows.UI.Xaml.Media.CompositionTarget.Rendered -= handler;
if (weak.Target is Action callback)
{
callback();
}
}
try
{
Windows.UI.Xaml.Media.CompositionTarget.Rendered += handler;
}
catch
{
// Bla bla
}
}
public static Task WaitForCompositionRenderedAsync()
{
var tsc = new TaskCompletionSource<bool>();
void handler(object sender, object e)
{
Windows.UI.Xaml.Media.CompositionTarget.Rendered -= handler;
tsc.SetResult(true);
}
try
{
Windows.UI.Xaml.Media.CompositionTarget.Rendered += handler;
}
catch
{
// Bla bla
}
return tsc.Task;
}
}
}