-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathAspectView.cs
488 lines (434 loc) · 16 KB
/
AspectView.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// 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 Telegram.Common;
using Telegram.Td.Api;
using Telegram.ViewModels;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace Telegram.Controls
{
public enum RotationAngle
{
Angle0,
Angle90,
Angle180,
Angle270
}
public struct FixedSize
{
public FixedSize(double width, double height)
{
Width = width;
Height = height;
}
public double Width { get; set; }
public double Height { get; set; }
}
public struct MaximumSize
{
public MaximumSize(double width, double height)
{
Width = width;
Height = height;
}
public double Width { get; set; }
public double Height { get; set; }
}
public partial class AspectView : Grid
{
#region Constraint
public object Constraint
{
get => GetValue(ConstraintProperty);
set => SetValue(ConstraintProperty, value);
}
public static readonly DependencyProperty ConstraintProperty =
DependencyProperty.Register("Constraint", typeof(object), typeof(AspectView), new PropertyMetadata(null, OnConstraintChanged));
private static void OnConstraintChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AspectView)d).InvalidateMeasure();
}
#endregion
#region Rotate
public RotationAngle RotationAngle
{
get => (RotationAngle)GetValue(RotationAngleProperty);
set => SetValue(RotationAngleProperty, value);
}
public static readonly DependencyProperty RotationAngleProperty =
DependencyProperty.Register("RotationAngle", typeof(RotationAngle), typeof(AspectView), new PropertyMetadata(RotationAngle.Angle0, OnRotationAngleChanged));
private static void OnRotationAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AspectView)d).InvalidateMeasure();
}
#endregion
#region Stretch
public Stretch Stretch
{
get => (Stretch)GetValue(StretchProperty);
set => SetValue(StretchProperty, value);
}
public static readonly DependencyProperty StretchProperty =
DependencyProperty.Register("Stretch", typeof(Stretch), typeof(AspectView), new PropertyMetadata(Stretch.Uniform, OnStretchChanged));
private static void OnStretchChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AspectView)d).InvalidateMeasure();
}
#endregion
protected override Size MeasureOverride(Size availableSize)
{
if (Constraint == null)
{
return base.MeasureOverride(availableSize);
}
else if (Constraint is FixedSize fixedSize)
{
base.MeasureOverride(new Size(fixedSize.Width, fixedSize.Height));
return new Size(fixedSize.Width, fixedSize.Height);
}
var availableWidth = Math.Min(availableSize.Width, Math.Min(double.IsNaN(Width) ? double.PositiveInfinity : Width, MaxWidth));
var availableHeight = Math.Min(availableSize.Height, Math.Min(double.IsNaN(Height) ? double.PositiveInfinity : Height, MaxHeight));
var width = 0.0;
var height = 0.0;
var constraint = Constraint;
if (constraint is MessageViewModel viewModel)
{
//ttl = viewModel.SelfDestructTime > 0;
constraint = viewModel.Content;
if (viewModel.MediaAlbumId != 0 && Tag is true)
{
return base.MeasureOverride(availableSize);
}
}
else if (constraint is Message message)
{
//ttl = message.SelfDestructTime > 0;
constraint = message.Content;
if (message.MediaAlbumId != 0 && Tag is true)
{
return base.MeasureOverride(availableSize);
}
}
else if (Constraint is ViewModels.Chats.ChartViewData)
{
width = 640;
height = 420;
}
else if (Constraint is Size size)
{
width = size.Width;
height = size.Height;
}
else if (Constraint is MaximumSize maximumSize)
{
width = maximumSize.Width;
height = maximumSize.Height;
}
#region MessageContent
if (constraint is MessageAnimation animationMessage)
{
constraint = animationMessage.Animation;
}
else if (constraint is MessageInvoice invoiceMessage)
{
if (invoiceMessage.PaidMedia is PaidMediaPhoto paidMediaPhoto)
{
constraint = paidMediaPhoto.Photo;
}
else if (invoiceMessage.PaidMedia is PaidMediaVideo paidMediaVideo)
{
constraint = paidMediaVideo.Video;
}
else if (invoiceMessage.PaidMedia is PaidMediaPreview paidMediaPreview)
{
width = paidMediaPreview.Width;
height = paidMediaPreview.Height;
}
else
{
constraint = invoiceMessage.ProductInfo.Photo;
}
}
else if (constraint is MessageGame gameMessage)
{
if (gameMessage.Game.Animation != null)
{
constraint = gameMessage.Game.Animation;
}
else if (gameMessage.Game.Photo != null)
{
constraint = gameMessage.Game.Photo;
}
}
else if (constraint is MessageLocation locationMessage)
{
constraint = locationMessage.Location;
}
else if (constraint is MessagePhoto photoMessage)
{
constraint = photoMessage.Photo;
}
else if (constraint is MessageSticker stickerMessage)
{
constraint = stickerMessage.Sticker;
}
else if (constraint is MessageText textMessage)
{
if (textMessage?.LinkPreview?.Type is LinkPreviewTypeBackground)
{
width = 900;
height = 1600;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeAnimation previewAnimation)
{
constraint = previewAnimation.Animation;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeDocument previewDocument)
{
constraint = previewDocument.Document;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeEmbeddedAnimationPlayer previewEmbeddedAnimationPlayer)
{
width = previewEmbeddedAnimationPlayer.Width;
height = previewEmbeddedAnimationPlayer.Height;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeEmbeddedVideoPlayer previewEmbeddedVideoPlayer)
{
width = previewEmbeddedVideoPlayer.Width;
height = previewEmbeddedVideoPlayer.Height;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypePhoto previewPhoto)
{
constraint = previewPhoto.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeSticker previewSticker)
{
constraint = previewSticker.Sticker;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideo previewVideo)
{
constraint = previewVideo.Video;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideoNote videoNote)
{
constraint = videoNote.VideoNote;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeApp app)
{
constraint = app.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeArticle article)
{
constraint = article.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeChannelBoost channelBoost)
{
constraint = channelBoost.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeChat chat)
{
constraint = chat.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeSupergroupBoost supergroupBoost)
{
constraint = supergroupBoost.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeUser user)
{
constraint = user.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideoChat videoChat)
{
constraint = videoChat.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeWebApp webApp)
{
constraint = webApp.Photo;
}
}
else if (constraint is MessageVenue venueMessage)
{
constraint = venueMessage.Venue;
}
else if (constraint is MessageVideo videoMessage)
{
constraint = videoMessage.Video;
}
else if (constraint is MessageVideoNote videoNoteMessage)
{
constraint = videoNoteMessage.VideoNote;
}
else if (constraint is MessageChatChangePhoto chatChangePhoto)
{
constraint = chatChangePhoto.Photo;
}
else if (constraint is PaidMediaPhoto paidMediaPhoto)
{
constraint = paidMediaPhoto.Photo;
}
else if (constraint is PaidMediaVideo paidMediaVideo)
{
constraint = paidMediaVideo.Video;
}
else if (constraint is PaidMediaPreview paidMediaPreview)
{
width = paidMediaPreview.Width;
height = paidMediaPreview.Height;
}
else if (constraint is MessageAsyncStory asyncStory)
{
width = 720;
height = 1280;
}
#endregion
#region InlineQueryResult
if (constraint is InlineQueryResultAnimation animationResult)
{
constraint = animationResult.Animation;
}
else if (constraint is InlineQueryResultLocation locationResult)
{
constraint = locationResult.Location;
}
else if (constraint is InlineQueryResultPhoto photoResult)
{
constraint = photoResult.Photo;
}
else if (constraint is InlineQueryResultSticker stickerResult)
{
constraint = stickerResult.Sticker;
}
else if (constraint is InlineQueryResultVideo videoResult)
{
constraint = videoResult.Video;
}
#endregion
if (constraint is Animation animation)
{
width = animation.Width;
height = animation.Height;
}
else if (constraint is Document document)
{
width = document.Thumbnail?.Width ?? width;
height = document.Thumbnail?.Height ?? height;
}
else if (constraint is Location location)
{
width = 320;
height = 200;
}
else if (constraint is Photo photo)
{
var size = photo.Sizes.Count > 0 ? photo.Sizes[^1] : null;
if (size != null)
{
width = size.Width;
height = size.Height;
}
}
else if (constraint is ChatPhoto chatPhoto)
{
var size = chatPhoto.Sizes.Count > 0 ? chatPhoto.Sizes[^1] : null;
if (size != null)
{
width = size.Width;
height = size.Height;
}
}
else if (constraint is Sticker sticker)
{
width = sticker.Width;
height = sticker.Height;
}
else if (constraint is Venue venue)
{
width = 320;
height = 200;
}
else if (constraint is Video video)
{
width = video.Width;
height = video.Height;
}
else if (constraint is VideoNote videoNote)
{
width = 224;
height = 224;
}
if (constraint is PhotoSize photoSize)
{
width = photoSize.Width;
height = photoSize.Height;
}
if (constraint is PageBlockMap map)
{
width = map.Width;
height = map.Height;
}
if (constraint is Background wallpaper)
{
width = 900;
height = 1600;
}
if (width == 0 && height == 0)
{
width = int.MaxValue;
height = int.MaxValue;
}
var rotate = RotationAngle
is RotationAngle.Angle90
or RotationAngle.Angle270;
var cw = rotate ? height : width;
var ch = rotate ? width : height;
if (cw > availableWidth || ch > availableHeight || Constraint is Size || Stretch == Stretch.UniformToFill)
{
var ratioX = availableWidth / cw;
var ratioY = availableHeight / ch;
var ratio = Math.Min(ratioX, ratioY);
cw *= ratio;
ch *= ratio;
}
width = rotate ? ch : cw;
height = rotate ? cw : ch;
width = Math.Max(width, MinWidth);
height = Math.Max(height, MinHeight);
base.MeasureOverride(new Size(width, height));
return new Size(width, height);
}
private bool _applyingRotation;
private RotationAngle _appliedRotation;
protected override Size ArrangeOverride(Size finalSize)
{
ApplyRotation();
return base.ArrangeOverride(finalSize);
}
private void ApplyRotation()
{
if (_applyingRotation || _appliedRotation == RotationAngle)
{
return;
}
_applyingRotation = true;
VisualUtilities.QueueCallbackForCompositionRendering(ApplyRotationImpl);
}
public event RoutedEventHandler RotationAngleChanged;
private void ApplyRotationImpl()
{
_applyingRotation = false;
if (_appliedRotation == RotationAngle)
{
return;
}
_appliedRotation = RotationAngle;
RotationAngleChanged?.Invoke(this, new RoutedEventArgs());
}
}
}